Update app.py
Browse files
app.py
CHANGED
|
@@ -1,673 +1,757 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import os
|
| 3 |
-
import json
|
| 4 |
-
import pandas as pd
|
| 5 |
-
import random
|
| 6 |
-
from os.path import join
|
| 7 |
-
from datetime import datetime
|
| 8 |
-
from src import (
|
| 9 |
-
preprocess_and_load_df,
|
| 10 |
-
load_agent,
|
| 11 |
-
ask_agent,
|
| 12 |
-
decorate_with_code,
|
| 13 |
-
show_response,
|
| 14 |
-
get_from_user,
|
| 15 |
-
load_smart_df,
|
| 16 |
-
ask_question,
|
| 17 |
-
)
|
| 18 |
-
from dotenv import load_dotenv
|
| 19 |
-
from langchain_groq import ChatGroq
|
| 20 |
-
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 21 |
-
from streamlit_feedback import streamlit_feedback
|
| 22 |
-
from huggingface_hub import HfApi
|
| 23 |
-
from datasets import load_dataset, get_dataset_config_info, Dataset
|
| 24 |
-
from PIL import Image
|
| 25 |
-
import time
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
color: #
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
font-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
border:
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
color:
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
margin
|
| 117 |
-
margin-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
margin
|
| 138 |
-
margin-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
margin
|
| 158 |
-
margin-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
}
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
color: #
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
border:
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
color: #
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
border:
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
border:
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
color:
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
border
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
}
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
"
|
| 267 |
-
"
|
| 268 |
-
"
|
| 269 |
-
"
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
st.
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
""
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
""
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
"
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
"
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
|
| 395 |
-
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
|
| 440 |
-
|
| 441 |
-
|
| 442 |
-
|
| 443 |
-
|
| 444 |
-
|
| 445 |
-
|
| 446 |
-
|
| 447 |
-
|
| 448 |
-
|
| 449 |
-
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
|
| 453 |
-
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
|
| 457 |
-
|
| 458 |
-
|
| 459 |
-
|
| 460 |
-
|
| 461 |
-
|
| 462 |
-
|
| 463 |
-
|
| 464 |
-
|
| 465 |
-
|
| 466 |
-
|
| 467 |
-
|
| 468 |
-
|
| 469 |
-
|
| 470 |
-
|
| 471 |
-
|
| 472 |
-
|
| 473 |
-
|
| 474 |
-
|
| 475 |
-
|
| 476 |
-
|
| 477 |
-
st.
|
| 478 |
-
|
| 479 |
-
|
| 480 |
-
|
| 481 |
-
|
| 482 |
-
|
| 483 |
-
|
| 484 |
-
|
| 485 |
-
|
| 486 |
-
|
| 487 |
-
|
| 488 |
-
|
| 489 |
-
|
| 490 |
-
|
| 491 |
-
|
| 492 |
-
|
| 493 |
-
|
| 494 |
-
|
| 495 |
-
|
| 496 |
-
|
| 497 |
-
|
| 498 |
-
|
| 499 |
-
|
| 500 |
-
|
| 501 |
-
|
| 502 |
-
|
| 503 |
-
|
| 504 |
-
|
| 505 |
-
|
| 506 |
-
|
| 507 |
-
|
| 508 |
-
|
| 509 |
-
|
| 510 |
-
|
| 511 |
-
|
| 512 |
-
|
| 513 |
-
|
| 514 |
-
|
| 515 |
-
|
| 516 |
-
|
| 517 |
-
|
| 518 |
-
|
| 519 |
-
|
| 520 |
-
|
| 521 |
-
|
| 522 |
-
|
| 523 |
-
|
| 524 |
-
|
| 525 |
-
|
| 526 |
-
|
| 527 |
-
|
| 528 |
-
|
| 529 |
-
|
| 530 |
-
|
| 531 |
-
|
| 532 |
-
|
| 533 |
-
|
| 534 |
-
|
| 535 |
-
|
| 536 |
-
|
| 537 |
-
|
| 538 |
-
|
| 539 |
-
|
| 540 |
-
|
| 541 |
-
|
| 542 |
-
|
| 543 |
-
|
| 544 |
-
|
| 545 |
-
|
| 546 |
-
|
| 547 |
-
|
| 548 |
-
|
| 549 |
-
|
| 550 |
-
|
| 551 |
-
|
| 552 |
-
|
| 553 |
-
|
| 554 |
-
|
| 555 |
-
|
| 556 |
-
|
| 557 |
-
|
| 558 |
-
|
| 559 |
-
|
| 560 |
-
|
| 561 |
-
|
| 562 |
-
|
| 563 |
-
|
| 564 |
-
|
| 565 |
-
|
| 566 |
-
|
| 567 |
-
|
| 568 |
-
|
| 569 |
-
|
| 570 |
-
|
| 571 |
-
|
| 572 |
-
|
| 573 |
-
|
| 574 |
-
|
| 575 |
-
|
| 576 |
-
|
| 577 |
-
|
| 578 |
-
|
| 579 |
-
|
| 580 |
-
|
| 581 |
-
|
| 582 |
-
|
| 583 |
-
|
| 584 |
-
|
| 585 |
-
|
| 586 |
-
|
| 587 |
-
|
| 588 |
-
|
| 589 |
-
|
| 590 |
-
|
| 591 |
-
|
| 592 |
-
|
| 593 |
-
|
| 594 |
-
|
| 595 |
-
|
| 596 |
-
#
|
| 597 |
-
|
| 598 |
-
|
| 599 |
-
|
| 600 |
-
|
| 601 |
-
|
| 602 |
-
|
| 603 |
-
|
| 604 |
-
|
| 605 |
-
|
| 606 |
-
|
| 607 |
-
|
| 608 |
-
|
| 609 |
-
|
| 610 |
-
|
| 611 |
-
|
| 612 |
-
|
| 613 |
-
|
| 614 |
-
"
|
| 615 |
-
"
|
| 616 |
-
|
| 617 |
-
|
| 618 |
-
|
| 619 |
-
|
| 620 |
-
|
| 621 |
-
|
| 622 |
-
|
| 623 |
-
|
| 624 |
-
|
| 625 |
-
|
| 626 |
-
|
| 627 |
-
|
| 628 |
-
|
| 629 |
-
|
| 630 |
-
|
| 631 |
-
|
| 632 |
-
|
| 633 |
-
|
| 634 |
-
|
| 635 |
-
|
| 636 |
-
|
| 637 |
-
|
| 638 |
-
|
| 639 |
-
|
| 640 |
-
|
| 641 |
-
|
| 642 |
-
|
| 643 |
-
|
| 644 |
-
|
| 645 |
-
|
| 646 |
-
|
| 647 |
-
|
| 648 |
-
|
| 649 |
-
|
| 650 |
-
|
| 651 |
-
|
| 652 |
-
|
| 653 |
-
|
| 654 |
-
|
| 655 |
-
|
| 656 |
-
|
| 657 |
-
|
| 658 |
-
|
| 659 |
-
|
| 660 |
-
|
| 661 |
-
|
| 662 |
-
|
| 663 |
-
|
| 664 |
-
|
| 665 |
-
|
| 666 |
-
|
| 667 |
-
|
| 668 |
-
|
| 669 |
-
|
| 670 |
-
|
| 671 |
-
|
| 672 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 673 |
""", unsafe_allow_html=True)
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import os
|
| 3 |
+
import json
|
| 4 |
+
import pandas as pd
|
| 5 |
+
import random
|
| 6 |
+
from os.path import join
|
| 7 |
+
from datetime import datetime
|
| 8 |
+
from src import (
|
| 9 |
+
preprocess_and_load_df,
|
| 10 |
+
load_agent,
|
| 11 |
+
ask_agent,
|
| 12 |
+
decorate_with_code,
|
| 13 |
+
show_response,
|
| 14 |
+
get_from_user,
|
| 15 |
+
load_smart_df,
|
| 16 |
+
ask_question,
|
| 17 |
+
)
|
| 18 |
+
from dotenv import load_dotenv
|
| 19 |
+
from langchain_groq import ChatGroq
|
| 20 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 21 |
+
from streamlit_feedback import streamlit_feedback
|
| 22 |
+
from huggingface_hub import HfApi
|
| 23 |
+
from datasets import load_dataset, get_dataset_config_info, Dataset
|
| 24 |
+
from PIL import Image
|
| 25 |
+
import time
|
| 26 |
+
import uuid
|
| 27 |
+
|
| 28 |
+
# Page config with beautiful theme
|
| 29 |
+
st.set_page_config(
|
| 30 |
+
page_title="VayuBuddy - AI Air Quality Assistant",
|
| 31 |
+
page_icon="π¬οΈ",
|
| 32 |
+
layout="wide",
|
| 33 |
+
initial_sidebar_state="expanded"
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
# Custom CSS for beautiful styling
|
| 37 |
+
st.markdown("""
|
| 38 |
+
<style>
|
| 39 |
+
/* Clean app background */
|
| 40 |
+
.stApp {
|
| 41 |
+
background-color: #ffffff;
|
| 42 |
+
color: #212529;
|
| 43 |
+
font-family: 'Segoe UI', sans-serif;
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
/* Sidebar */
|
| 47 |
+
[data-testid="stSidebar"] {
|
| 48 |
+
background-color: #f8f9fa;
|
| 49 |
+
border-right: 1px solid #dee2e6;
|
| 50 |
+
padding: 1rem;
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
/* Main title */
|
| 54 |
+
.main-title {
|
| 55 |
+
text-align: center;
|
| 56 |
+
color: #343a40;
|
| 57 |
+
font-size: 2.5rem;
|
| 58 |
+
font-weight: 700;
|
| 59 |
+
margin-bottom: 0.5rem;
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
/* Subtitle */
|
| 63 |
+
.subtitle {
|
| 64 |
+
text-align: center;
|
| 65 |
+
color: #6c757d;
|
| 66 |
+
font-size: 1.1rem;
|
| 67 |
+
margin-bottom: 1.5rem;
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
/* Instructions */
|
| 71 |
+
.instructions {
|
| 72 |
+
background-color: #f1f3f5;
|
| 73 |
+
border-left: 4px solid #0d6efd;
|
| 74 |
+
padding: 1rem;
|
| 75 |
+
margin-bottom: 1.5rem;
|
| 76 |
+
border-radius: 6px;
|
| 77 |
+
color: #495057;
|
| 78 |
+
text-align: left;
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
/* Quick prompt buttons */
|
| 82 |
+
.quick-prompt-container {
|
| 83 |
+
display: flex;
|
| 84 |
+
flex-wrap: wrap;
|
| 85 |
+
gap: 8px;
|
| 86 |
+
margin-bottom: 1.5rem;
|
| 87 |
+
padding: 1rem;
|
| 88 |
+
background-color: #f8f9fa;
|
| 89 |
+
border-radius: 10px;
|
| 90 |
+
border: 1px solid #dee2e6;
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
.quick-prompt-btn {
|
| 94 |
+
background-color: #0d6efd;
|
| 95 |
+
color: white;
|
| 96 |
+
border: none;
|
| 97 |
+
padding: 8px 16px;
|
| 98 |
+
border-radius: 20px;
|
| 99 |
+
font-size: 0.9rem;
|
| 100 |
+
cursor: pointer;
|
| 101 |
+
transition: all 0.2s ease;
|
| 102 |
+
white-space: nowrap;
|
| 103 |
+
}
|
| 104 |
+
|
| 105 |
+
.quick-prompt-btn:hover {
|
| 106 |
+
background-color: #0b5ed7;
|
| 107 |
+
transform: translateY(-2px);
|
| 108 |
+
}
|
| 109 |
+
|
| 110 |
+
/* User message styling */
|
| 111 |
+
.user-message {
|
| 112 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 113 |
+
color: white;
|
| 114 |
+
padding: 15px 20px;
|
| 115 |
+
border-radius: 20px 20px 5px 20px;
|
| 116 |
+
margin: 10px 0;
|
| 117 |
+
margin-left: auto;
|
| 118 |
+
margin-right: 0;
|
| 119 |
+
max-width: 80%;
|
| 120 |
+
position: relative;
|
| 121 |
+
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
|
| 122 |
+
}
|
| 123 |
+
|
| 124 |
+
.user-info {
|
| 125 |
+
font-size: 0.8rem;
|
| 126 |
+
opacity: 0.8;
|
| 127 |
+
margin-bottom: 5px;
|
| 128 |
+
text-align: right;
|
| 129 |
+
}
|
| 130 |
+
|
| 131 |
+
/* Assistant message styling */
|
| 132 |
+
.assistant-message {
|
| 133 |
+
background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
|
| 134 |
+
color: white;
|
| 135 |
+
padding: 15px 20px;
|
| 136 |
+
border-radius: 20px 20px 20px 5px;
|
| 137 |
+
margin: 10px 0;
|
| 138 |
+
margin-left: 0;
|
| 139 |
+
margin-right: auto;
|
| 140 |
+
max-width: 80%;
|
| 141 |
+
position: relative;
|
| 142 |
+
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
|
| 143 |
+
}
|
| 144 |
+
|
| 145 |
+
.assistant-info {
|
| 146 |
+
font-size: 0.8rem;
|
| 147 |
+
opacity: 0.8;
|
| 148 |
+
margin-bottom: 5px;
|
| 149 |
+
}
|
| 150 |
+
|
| 151 |
+
/* Processing indicator */
|
| 152 |
+
.processing-indicator {
|
| 153 |
+
background: linear-gradient(135deg, #a8edea 0%, #fed6e3 100%);
|
| 154 |
+
color: #333;
|
| 155 |
+
padding: 15px 20px;
|
| 156 |
+
border-radius: 20px 20px 20px 5px;
|
| 157 |
+
margin: 10px 0;
|
| 158 |
+
margin-left: 0;
|
| 159 |
+
margin-right: auto;
|
| 160 |
+
max-width: 80%;
|
| 161 |
+
position: relative;
|
| 162 |
+
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
|
| 163 |
+
animation: pulse 2s infinite;
|
| 164 |
+
}
|
| 165 |
+
|
| 166 |
+
@keyframes pulse {
|
| 167 |
+
0% { opacity: 1; }
|
| 168 |
+
50% { opacity: 0.7; }
|
| 169 |
+
100% { opacity: 1; }
|
| 170 |
+
}
|
| 171 |
+
|
| 172 |
+
/* Feedback box */
|
| 173 |
+
.feedback-section {
|
| 174 |
+
background-color: #f8f9fa;
|
| 175 |
+
border: 1px solid #dee2e6;
|
| 176 |
+
padding: 1rem;
|
| 177 |
+
border-radius: 8px;
|
| 178 |
+
margin: 1rem 0;
|
| 179 |
+
}
|
| 180 |
+
|
| 181 |
+
/* Success and error messages */
|
| 182 |
+
.success-message {
|
| 183 |
+
background-color: #d1e7dd;
|
| 184 |
+
color: #0f5132;
|
| 185 |
+
padding: 1rem;
|
| 186 |
+
border-radius: 6px;
|
| 187 |
+
border: 1px solid #badbcc;
|
| 188 |
+
}
|
| 189 |
+
|
| 190 |
+
.error-message {
|
| 191 |
+
background-color: #f8d7da;
|
| 192 |
+
color: #842029;
|
| 193 |
+
padding: 1rem;
|
| 194 |
+
border-radius: 6px;
|
| 195 |
+
border: 1px solid #f5c2c7;
|
| 196 |
+
}
|
| 197 |
+
|
| 198 |
+
/* Chat input */
|
| 199 |
+
.stChatInput {
|
| 200 |
+
border-radius: 6px;
|
| 201 |
+
border: 1px solid #ced4da;
|
| 202 |
+
background: #ffffff;
|
| 203 |
+
}
|
| 204 |
+
|
| 205 |
+
/* Button */
|
| 206 |
+
.stButton > button {
|
| 207 |
+
background-color: #0d6efd;
|
| 208 |
+
color: white;
|
| 209 |
+
border-radius: 6px;
|
| 210 |
+
padding: 0.5rem 1.25rem;
|
| 211 |
+
border: none;
|
| 212 |
+
font-weight: 600;
|
| 213 |
+
transition: background-color 0.2s ease;
|
| 214 |
+
}
|
| 215 |
+
|
| 216 |
+
.stButton > button:hover {
|
| 217 |
+
background-color: #0b5ed7;
|
| 218 |
+
}
|
| 219 |
+
|
| 220 |
+
/* Code details styling */
|
| 221 |
+
.code-details {
|
| 222 |
+
background-color: #f8f9fa;
|
| 223 |
+
border: 1px solid #dee2e6;
|
| 224 |
+
border-radius: 8px;
|
| 225 |
+
padding: 10px;
|
| 226 |
+
margin-top: 10px;
|
| 227 |
+
}
|
| 228 |
+
|
| 229 |
+
/* Hide default menu and footer */
|
| 230 |
+
#MainMenu {visibility: hidden;}
|
| 231 |
+
footer {visibility: hidden;}
|
| 232 |
+
header {visibility: hidden;}
|
| 233 |
+
|
| 234 |
+
/* Auto scroll */
|
| 235 |
+
.main-container {
|
| 236 |
+
height: 70vh;
|
| 237 |
+
overflow-y: auto;
|
| 238 |
+
}
|
| 239 |
+
</style>
|
| 240 |
+
""", unsafe_allow_html=True)
|
| 241 |
+
|
| 242 |
+
# Auto-scroll JavaScript
|
| 243 |
+
st.markdown("""
|
| 244 |
+
<script>
|
| 245 |
+
function scrollToBottom() {
|
| 246 |
+
setTimeout(function() {
|
| 247 |
+
const mainContainer = document.querySelector('.main-container');
|
| 248 |
+
if (mainContainer) {
|
| 249 |
+
mainContainer.scrollTop = mainContainer.scrollHeight;
|
| 250 |
+
}
|
| 251 |
+
window.scrollTo(0, document.body.scrollHeight);
|
| 252 |
+
}, 100);
|
| 253 |
+
}
|
| 254 |
+
</script>
|
| 255 |
+
""", unsafe_allow_html=True)
|
| 256 |
+
|
| 257 |
+
# FORCE reload environment variables
|
| 258 |
+
load_dotenv(override=True)
|
| 259 |
+
|
| 260 |
+
# Get API keys
|
| 261 |
+
Groq_Token = os.getenv("GROQ_API_KEY")
|
| 262 |
+
hf_token = os.getenv("HF_TOKEN")
|
| 263 |
+
gemini_token = os.getenv("GEMINI_TOKEN")
|
| 264 |
+
|
| 265 |
+
models = {
|
| 266 |
+
"llama3.1": "llama-3.1-8b-instant",
|
| 267 |
+
"mistral": "mistral-saba-24b",
|
| 268 |
+
"llama3.3": "llama-3.3-70b-versatile",
|
| 269 |
+
"gemma": "gemma2-9b-it",
|
| 270 |
+
"gemini-pro": "gemini-1.5-pro",
|
| 271 |
+
}
|
| 272 |
+
|
| 273 |
+
self_path = os.path.dirname(os.path.abspath(__file__))
|
| 274 |
+
|
| 275 |
+
# Initialize session ID for this session
|
| 276 |
+
if "session_id" not in st.session_state:
|
| 277 |
+
st.session_state.session_id = str(uuid.uuid4())
|
| 278 |
+
|
| 279 |
+
def upload_feedback(feedback, error, output, last_prompt, code, status):
|
| 280 |
+
"""Enhanced feedback upload function with better logging and error handling"""
|
| 281 |
+
try:
|
| 282 |
+
if not hf_token or hf_token.strip() == "":
|
| 283 |
+
st.warning("β οΈ Cannot upload feedback - HF_TOKEN not available")
|
| 284 |
+
return False
|
| 285 |
+
|
| 286 |
+
# Create comprehensive feedback data
|
| 287 |
+
feedback_data = {
|
| 288 |
+
"timestamp": datetime.now().isoformat(),
|
| 289 |
+
"session_id": st.session_state.session_id,
|
| 290 |
+
"feedback_score": feedback.get("score", ""),
|
| 291 |
+
"feedback_comment": feedback.get("text", ""),
|
| 292 |
+
"user_prompt": last_prompt,
|
| 293 |
+
"ai_output": str(output),
|
| 294 |
+
"generated_code": code or "",
|
| 295 |
+
"error_message": error or "",
|
| 296 |
+
"is_image_output": status.get("is_image", False),
|
| 297 |
+
"success": not bool(error)
|
| 298 |
+
}
|
| 299 |
+
|
| 300 |
+
# Create unique folder name with timestamp
|
| 301 |
+
timestamp_str = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 302 |
+
random_id = str(uuid.uuid4())[:8]
|
| 303 |
+
folder_name = f"feedback_{timestamp_str}_{random_id}"
|
| 304 |
+
|
| 305 |
+
# Create markdown feedback file
|
| 306 |
+
markdown_content = f"""# VayuBuddy Feedback Report
|
| 307 |
+
|
| 308 |
+
## Session Information
|
| 309 |
+
- **Timestamp**: {feedback_data['timestamp']}
|
| 310 |
+
- **Session ID**: {feedback_data['session_id']}
|
| 311 |
+
|
| 312 |
+
## User Interaction
|
| 313 |
+
**Prompt**: {feedback_data['user_prompt']}
|
| 314 |
+
|
| 315 |
+
## AI Response
|
| 316 |
+
**Output**: {feedback_data['ai_output']}
|
| 317 |
+
|
| 318 |
+
## Generated Code
|
| 319 |
+
```python
|
| 320 |
+
{feedback_data['generated_code']}
|
| 321 |
+
```
|
| 322 |
+
|
| 323 |
+
## Technical Details
|
| 324 |
+
- **Error Message**: {feedback_data['error_message']}
|
| 325 |
+
- **Is Image Output**: {feedback_data['is_image_output']}
|
| 326 |
+
- **Success**: {feedback_data['success']}
|
| 327 |
+
|
| 328 |
+
## User Feedback
|
| 329 |
+
- **Score**: {feedback_data['feedback_score']}
|
| 330 |
+
- **Comments**: {feedback_data['feedback_comment']}
|
| 331 |
+
"""
|
| 332 |
+
|
| 333 |
+
# Save markdown file locally
|
| 334 |
+
markdown_filename = f"{folder_name}.md"
|
| 335 |
+
markdown_local_path = f"/tmp/{markdown_filename}"
|
| 336 |
+
|
| 337 |
+
with open(markdown_local_path, "w", encoding="utf-8") as f:
|
| 338 |
+
f.write(markdown_content)
|
| 339 |
+
|
| 340 |
+
# Upload to Hugging Face
|
| 341 |
+
api = HfApi(token=hf_token)
|
| 342 |
+
|
| 343 |
+
# Upload markdown feedback
|
| 344 |
+
api.upload_file(
|
| 345 |
+
path_or_fileobj=markdown_local_path,
|
| 346 |
+
path_in_repo=f"data/{markdown_filename}",
|
| 347 |
+
repo_id="SustainabilityLabIITGN/VayuBuddy_Feedback",
|
| 348 |
+
repo_type="dataset",
|
| 349 |
+
)
|
| 350 |
+
|
| 351 |
+
# Upload image if it exists and is an image output
|
| 352 |
+
if status.get("is_image", False) and isinstance(output, str) and os.path.exists(output):
|
| 353 |
+
try:
|
| 354 |
+
image_filename = f"{folder_name}_plot.png"
|
| 355 |
+
api.upload_file(
|
| 356 |
+
path_or_fileobj=output,
|
| 357 |
+
path_in_repo=f"data/{image_filename}",
|
| 358 |
+
repo_id="SustainabilityLabIITGN/VayuBuddy_Feedback",
|
| 359 |
+
repo_type="dataset",
|
| 360 |
+
)
|
| 361 |
+
except Exception as img_error:
|
| 362 |
+
print(f"Error uploading image: {img_error}")
|
| 363 |
+
|
| 364 |
+
# Clean up local files
|
| 365 |
+
if os.path.exists(markdown_local_path):
|
| 366 |
+
os.remove(markdown_local_path)
|
| 367 |
+
|
| 368 |
+
st.success("π Feedback uploaded successfully!")
|
| 369 |
+
return True
|
| 370 |
+
|
| 371 |
+
except Exception as e:
|
| 372 |
+
st.error(f"β Error uploading feedback: {e}")
|
| 373 |
+
print(f"Feedback upload error: {e}")
|
| 374 |
+
return False
|
| 375 |
+
|
| 376 |
+
# Beautiful header
|
| 377 |
+
st.markdown("<h1 class='main-title'>π¬οΈ VayuBuddy</h1>", unsafe_allow_html=True)
|
| 378 |
+
|
| 379 |
+
st.markdown("""
|
| 380 |
+
<div class='subtitle'>
|
| 381 |
+
<strong>AI-Powered Air Quality Insights</strong><br>
|
| 382 |
+
Simplifying pollution analysis using conversational AI.
|
| 383 |
+
</div>
|
| 384 |
+
""", unsafe_allow_html=True)
|
| 385 |
+
|
| 386 |
+
st.markdown("""
|
| 387 |
+
<div class='instructions'>
|
| 388 |
+
<strong>How to Use:</strong><br>
|
| 389 |
+
Select a model from the sidebar and ask questions directly in the chat. Use quick prompts below for common queries.
|
| 390 |
+
</div>
|
| 391 |
+
""", unsafe_allow_html=True)
|
| 392 |
+
|
| 393 |
+
os.environ["PANDASAI_API_KEY"] = "$2a$10$gbmqKotzJOnqa7iYOun8eO50TxMD/6Zw1pLI2JEoqncwsNx4XeBS2"
|
| 394 |
+
|
| 395 |
+
# Load data with error handling
|
| 396 |
+
try:
|
| 397 |
+
df = preprocess_and_load_df(join(self_path, "Data.csv"))
|
| 398 |
+
st.success("β
Data loaded successfully!")
|
| 399 |
+
except Exception as e:
|
| 400 |
+
st.error(f"β Error loading data: {e}")
|
| 401 |
+
st.stop()
|
| 402 |
+
|
| 403 |
+
inference_server = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.2"
|
| 404 |
+
image_path = "IITGN_Logo.png"
|
| 405 |
+
|
| 406 |
+
# Beautiful sidebar
|
| 407 |
+
with st.sidebar:
|
| 408 |
+
# Logo and title
|
| 409 |
+
col1, col2, col3 = st.columns([1, 2, 1])
|
| 410 |
+
with col2:
|
| 411 |
+
if os.path.exists(image_path):
|
| 412 |
+
st.image(image_path, use_column_width=True)
|
| 413 |
+
|
| 414 |
+
# Session info
|
| 415 |
+
st.markdown(f"**Session ID**: `{st.session_state.session_id[:8]}...`")
|
| 416 |
+
|
| 417 |
+
# Model selection
|
| 418 |
+
st.markdown("### π€ AI Model Selection")
|
| 419 |
+
|
| 420 |
+
# Filter available models
|
| 421 |
+
available_models = []
|
| 422 |
+
if Groq_Token and Groq_Token.strip():
|
| 423 |
+
available_models.extend(["llama3.1", "llama3.3", "mistral", "gemma"])
|
| 424 |
+
if gemini_token and gemini_token.strip():
|
| 425 |
+
available_models.append("gemini-pro")
|
| 426 |
+
|
| 427 |
+
if not available_models:
|
| 428 |
+
st.error("β No API keys available! Please set up your API keys in the .env file")
|
| 429 |
+
st.stop()
|
| 430 |
+
|
| 431 |
+
model_name = st.selectbox(
|
| 432 |
+
"Choose your AI assistant:",
|
| 433 |
+
available_models,
|
| 434 |
+
help="Different models have different strengths. Try them all!"
|
| 435 |
+
)
|
| 436 |
+
|
| 437 |
+
# Model descriptions
|
| 438 |
+
model_descriptions = {
|
| 439 |
+
"llama3.1": "π¦ Fast and efficient for general queries",
|
| 440 |
+
"llama3.3": "π¦ Most advanced Llama model",
|
| 441 |
+
"mistral": "β‘ Balanced performance and speed",
|
| 442 |
+
"gemma": "π Google's lightweight model",
|
| 443 |
+
"gemini-pro": "π§ Google's most powerful model"
|
| 444 |
+
}
|
| 445 |
+
|
| 446 |
+
if model_name in model_descriptions:
|
| 447 |
+
st.info(model_descriptions[model_name])
|
| 448 |
+
|
| 449 |
+
st.markdown("---")
|
| 450 |
+
|
| 451 |
+
# Logging status
|
| 452 |
+
st.markdown("### π Logging Status")
|
| 453 |
+
if hf_token and hf_token.strip():
|
| 454 |
+
st.success("β
Logging enabled")
|
| 455 |
+
st.caption("Interactions are being logged to HuggingFace")
|
| 456 |
+
else:
|
| 457 |
+
st.warning("β οΈ Logging disabled")
|
| 458 |
+
st.caption("HF_TOKEN not available")
|
| 459 |
+
|
| 460 |
+
st.markdown("---")
|
| 461 |
+
|
| 462 |
+
# Clear Chat Button
|
| 463 |
+
if st.button("π§Ή Clear Chat"):
|
| 464 |
+
st.session_state.responses = []
|
| 465 |
+
st.session_state.processing = False
|
| 466 |
+
# Generate new session ID for new chat
|
| 467 |
+
st.session_state.session_id = str(uuid.uuid4())
|
| 468 |
+
try:
|
| 469 |
+
st.rerun()
|
| 470 |
+
except AttributeError:
|
| 471 |
+
st.experimental_rerun()
|
| 472 |
+
|
| 473 |
+
st.markdown("---")
|
| 474 |
+
|
| 475 |
+
# Chat History in Sidebar
|
| 476 |
+
with st.expander("π Chat History"):
|
| 477 |
+
for i, response in enumerate(st.session_state.get("responses", [])):
|
| 478 |
+
if response.get("role") == "user":
|
| 479 |
+
st.markdown(f"**You:** {response.get('content', '')[:50]}...")
|
| 480 |
+
elif response.get("role") == "assistant":
|
| 481 |
+
content = response.get('content', '')
|
| 482 |
+
if isinstance(content, str) and len(content) > 50:
|
| 483 |
+
st.markdown(f"**VayuBuddy:** {content[:50]}...")
|
| 484 |
+
else:
|
| 485 |
+
st.markdown(f"**VayuBuddy:** {str(content)[:50]}...")
|
| 486 |
+
st.markdown("---")
|
| 487 |
+
|
| 488 |
+
# Load quick prompts
|
| 489 |
+
questions = []
|
| 490 |
+
questions_file = join(self_path, "questions.txt")
|
| 491 |
+
if os.path.exists(questions_file):
|
| 492 |
+
try:
|
| 493 |
+
with open(questions_file, 'r', encoding='utf-8') as f:
|
| 494 |
+
content = f.read()
|
| 495 |
+
questions = [q.strip() for q in content.split("\n") if q.strip()]
|
| 496 |
+
print(f"Loaded {len(questions)} quick prompts") # Debug
|
| 497 |
+
except Exception as e:
|
| 498 |
+
st.error(f"Error loading questions: {e}")
|
| 499 |
+
questions = []
|
| 500 |
+
|
| 501 |
+
# Add some default prompts if file doesn't exist or is empty
|
| 502 |
+
if not questions:
|
| 503 |
+
questions = [
|
| 504 |
+
"What is the average PM2.5 level in the dataset?",
|
| 505 |
+
"Show me the air quality trend over time",
|
| 506 |
+
"Which pollutant has the highest concentration?",
|
| 507 |
+
"Create a correlation plot between different pollutants",
|
| 508 |
+
"What are the peak pollution hours?",
|
| 509 |
+
"Compare weekday vs weekend pollution levels"
|
| 510 |
+
]
|
| 511 |
+
|
| 512 |
+
# Quick prompts section (horizontal)
|
| 513 |
+
st.markdown("### π Quick Prompts")
|
| 514 |
+
|
| 515 |
+
# Create columns for horizontal layout
|
| 516 |
+
cols_per_row = 2 # Reduced to 2 for better fit
|
| 517 |
+
rows = [questions[i:i + cols_per_row] for i in range(0, len(questions), cols_per_row)]
|
| 518 |
+
|
| 519 |
+
selected_prompt = None
|
| 520 |
+
for row_idx, row in enumerate(rows):
|
| 521 |
+
cols = st.columns(len(row))
|
| 522 |
+
for col_idx, question in enumerate(row):
|
| 523 |
+
with cols[col_idx]:
|
| 524 |
+
# Create unique key using row and column indices
|
| 525 |
+
unique_key = f"prompt_btn_{row_idx}_{col_idx}"
|
| 526 |
+
button_text = f"π {question[:35]}{'...' if len(question) > 35 else ''}"
|
| 527 |
+
|
| 528 |
+
if st.button(button_text,
|
| 529 |
+
key=unique_key,
|
| 530 |
+
help=question,
|
| 531 |
+
use_container_width=True):
|
| 532 |
+
selected_prompt = question
|
| 533 |
+
|
| 534 |
+
st.markdown("---")
|
| 535 |
+
|
| 536 |
+
# Initialize chat history and processing state
|
| 537 |
+
if "responses" not in st.session_state:
|
| 538 |
+
st.session_state.responses = []
|
| 539 |
+
if "processing" not in st.session_state:
|
| 540 |
+
st.session_state.processing = False
|
| 541 |
+
|
| 542 |
+
def show_custom_response(response):
|
| 543 |
+
"""Custom response display function"""
|
| 544 |
+
role = response.get("role", "assistant")
|
| 545 |
+
content = response.get("content", "")
|
| 546 |
+
|
| 547 |
+
if role == "user":
|
| 548 |
+
st.markdown(f"""
|
| 549 |
+
<div class='user-message'>
|
| 550 |
+
<div class='user-info'>You</div>
|
| 551 |
+
{content}
|
| 552 |
+
</div>
|
| 553 |
+
""", unsafe_allow_html=True)
|
| 554 |
+
elif role == "assistant":
|
| 555 |
+
st.markdown(f"""
|
| 556 |
+
<div class='assistant-message'>
|
| 557 |
+
<div class='assistant-info'>π€ VayuBuddy</div>
|
| 558 |
+
{content if isinstance(content, str) else str(content)}
|
| 559 |
+
</div>
|
| 560 |
+
""", unsafe_allow_html=True)
|
| 561 |
+
|
| 562 |
+
# Show generated code if available
|
| 563 |
+
if response.get("gen_code"):
|
| 564 |
+
with st.expander("π View Generated Code"):
|
| 565 |
+
st.code(response["gen_code"], language="python")
|
| 566 |
+
|
| 567 |
+
# Try to display image if content is a file path
|
| 568 |
+
try:
|
| 569 |
+
if isinstance(content, str) and (content.endswith('.png') or content.endswith('.jpg')):
|
| 570 |
+
if os.path.exists(content):
|
| 571 |
+
st.image(content)
|
| 572 |
+
return {"is_image": True}
|
| 573 |
+
except:
|
| 574 |
+
pass
|
| 575 |
+
|
| 576 |
+
return {"is_image": False}
|
| 577 |
+
|
| 578 |
+
def show_processing_indicator(model_name, question):
|
| 579 |
+
"""Show processing indicator"""
|
| 580 |
+
st.markdown(f"""
|
| 581 |
+
<div class='processing-indicator'>
|
| 582 |
+
<div class='assistant-info'>π€ VayuBuddy β’ Processing with {model_name}</div>
|
| 583 |
+
<strong>Question:</strong> {question}<br>
|
| 584 |
+
<em>π Generating response...</em>
|
| 585 |
+
</div>
|
| 586 |
+
""", unsafe_allow_html=True)
|
| 587 |
+
|
| 588 |
+
# Main chat container
|
| 589 |
+
chat_container = st.container()
|
| 590 |
+
|
| 591 |
+
with chat_container:
|
| 592 |
+
# Display chat history
|
| 593 |
+
for response_id, response in enumerate(st.session_state.responses):
|
| 594 |
+
status = show_custom_response(response)
|
| 595 |
+
|
| 596 |
+
# Show feedback section for assistant responses
|
| 597 |
+
if response["role"] == "assistant":
|
| 598 |
+
feedback_key = f"feedback_{int(response_id/2)}"
|
| 599 |
+
error = response.get("error", "")
|
| 600 |
+
output = response.get("content", "")
|
| 601 |
+
last_prompt = response.get("last_prompt", "")
|
| 602 |
+
code = response.get("gen_code", "")
|
| 603 |
+
|
| 604 |
+
if "feedback" in st.session_state.responses[response_id]:
|
| 605 |
+
feedback_data = st.session_state.responses[response_id]["feedback"]
|
| 606 |
+
st.markdown(f"""
|
| 607 |
+
<div class='feedback-section'>
|
| 608 |
+
<strong>π Your Feedback:</strong> {feedback_data.get('score', '')}
|
| 609 |
+
{f"- {feedback_data.get('text', '')}" if feedback_data.get('text') else ""}
|
| 610 |
+
</div>
|
| 611 |
+
""", unsafe_allow_html=True)
|
| 612 |
+
else:
|
| 613 |
+
# Beautiful feedback section
|
| 614 |
+
st.markdown("---")
|
| 615 |
+
st.markdown("**How was this response?**")
|
| 616 |
+
|
| 617 |
+
col1, col2 = st.columns(2)
|
| 618 |
+
with col1:
|
| 619 |
+
thumbs_up = st.button("π Helpful", key=f"{feedback_key}_up", use_container_width=True)
|
| 620 |
+
with col2:
|
| 621 |
+
thumbs_down = st.button("π Not Helpful", key=f"{feedback_key}_down", use_container_width=True)
|
| 622 |
+
|
| 623 |
+
if thumbs_up or thumbs_down:
|
| 624 |
+
thumbs = "π Helpful" if thumbs_up else "π Not Helpful"
|
| 625 |
+
comments = st.text_area(
|
| 626 |
+
"π¬ Tell us more (optional):",
|
| 627 |
+
key=f"{feedback_key}_comments",
|
| 628 |
+
placeholder="What could be improved? Any suggestions?",
|
| 629 |
+
max_chars=500
|
| 630 |
+
)
|
| 631 |
+
|
| 632 |
+
if st.button("π Submit Feedback", key=f"{feedback_key}_submit"):
|
| 633 |
+
feedback = {"score": thumbs, "text": comments}
|
| 634 |
+
|
| 635 |
+
# Upload feedback with enhanced error handling
|
| 636 |
+
if upload_feedback(feedback, error, output, last_prompt, code, status or {}):
|
| 637 |
+
st.session_state.responses[response_id]["feedback"] = feedback
|
| 638 |
+
time.sleep(1) # Give user time to see success message
|
| 639 |
+
st.rerun()
|
| 640 |
+
else:
|
| 641 |
+
st.error("Failed to submit feedback. Please try again.")
|
| 642 |
+
|
| 643 |
+
# Show processing indicator if processing
|
| 644 |
+
if st.session_state.get("processing"):
|
| 645 |
+
show_processing_indicator(
|
| 646 |
+
st.session_state.get("current_model", "Unknown"),
|
| 647 |
+
st.session_state.get("current_question", "Processing...")
|
| 648 |
+
)
|
| 649 |
+
|
| 650 |
+
# Chat input (always visible at bottom)
|
| 651 |
+
prompt = st.chat_input("π¬ Ask me anything about air quality!", key="main_chat")
|
| 652 |
+
|
| 653 |
+
# Handle selected prompt from quick prompts
|
| 654 |
+
if selected_prompt:
|
| 655 |
+
prompt = selected_prompt
|
| 656 |
+
|
| 657 |
+
# Handle new queries
|
| 658 |
+
if prompt and not st.session_state.get("processing"):
|
| 659 |
+
# Prevent duplicate processing
|
| 660 |
+
if "last_prompt" in st.session_state:
|
| 661 |
+
last_prompt = st.session_state["last_prompt"]
|
| 662 |
+
last_model_name = st.session_state.get("last_model_name", "")
|
| 663 |
+
if (prompt == last_prompt) and (model_name == last_model_name):
|
| 664 |
+
prompt = None
|
| 665 |
+
|
| 666 |
+
if prompt:
|
| 667 |
+
# Add user input to chat history
|
| 668 |
+
user_response = get_from_user(prompt)
|
| 669 |
+
st.session_state.responses.append(user_response)
|
| 670 |
+
|
| 671 |
+
# Set processing state
|
| 672 |
+
st.session_state.processing = True
|
| 673 |
+
st.session_state.current_model = model_name
|
| 674 |
+
st.session_state.current_question = prompt
|
| 675 |
+
|
| 676 |
+
# Rerun to show processing indicator
|
| 677 |
+
st.rerun()
|
| 678 |
+
|
| 679 |
+
# Process the question if we're in processing state
|
| 680 |
+
if st.session_state.get("processing"):
|
| 681 |
+
prompt = st.session_state.get("current_question")
|
| 682 |
+
model_name = st.session_state.get("current_model")
|
| 683 |
+
|
| 684 |
+
try:
|
| 685 |
+
response = ask_question(model_name=model_name, question=prompt)
|
| 686 |
+
|
| 687 |
+
if not isinstance(response, dict):
|
| 688 |
+
response = {
|
| 689 |
+
"role": "assistant",
|
| 690 |
+
"content": "β Error: Invalid response format",
|
| 691 |
+
"gen_code": "",
|
| 692 |
+
"ex_code": "",
|
| 693 |
+
"last_prompt": prompt,
|
| 694 |
+
"error": "Invalid response format"
|
| 695 |
+
}
|
| 696 |
+
|
| 697 |
+
response.setdefault("role", "assistant")
|
| 698 |
+
response.setdefault("content", "No content generated")
|
| 699 |
+
response.setdefault("gen_code", "")
|
| 700 |
+
response.setdefault("ex_code", "")
|
| 701 |
+
response.setdefault("last_prompt", prompt)
|
| 702 |
+
response.setdefault("error", None)
|
| 703 |
+
|
| 704 |
+
except Exception as e:
|
| 705 |
+
response = {
|
| 706 |
+
"role": "assistant",
|
| 707 |
+
"content": f"Sorry, I encountered an error: {str(e)}",
|
| 708 |
+
"gen_code": "",
|
| 709 |
+
"ex_code": "",
|
| 710 |
+
"last_prompt": prompt,
|
| 711 |
+
"error": str(e)
|
| 712 |
+
}
|
| 713 |
+
|
| 714 |
+
st.session_state.responses.append(response)
|
| 715 |
+
st.session_state["last_prompt"] = prompt
|
| 716 |
+
st.session_state["last_model_name"] = model_name
|
| 717 |
+
st.session_state.processing = False
|
| 718 |
+
|
| 719 |
+
# Clear processing state
|
| 720 |
+
if "current_model" in st.session_state:
|
| 721 |
+
del st.session_state.current_model
|
| 722 |
+
if "current_question" in st.session_state:
|
| 723 |
+
del st.session_state.current_question
|
| 724 |
+
|
| 725 |
+
st.rerun()
|
| 726 |
+
|
| 727 |
+
# Auto-scroll to bottom
|
| 728 |
+
if st.session_state.responses:
|
| 729 |
+
st.markdown("<script>scrollToBottom();</script>", unsafe_allow_html=True)
|
| 730 |
+
|
| 731 |
+
# Beautiful sidebar footer
|
| 732 |
+
with st.sidebar:
|
| 733 |
+
st.markdown("---")
|
| 734 |
+
st.markdown("""
|
| 735 |
+
<div class='contact-section'>
|
| 736 |
+
<h4>π Paper on VayuBuddy</h4>
|
| 737 |
+
<p>Learn more about VayuBuddy in our <a href='https://arxiv.org/abs/2411.12760' target='_blank'>Research Paper</a>.</p>
|
| 738 |
+
</div>
|
| 739 |
+
""", unsafe_allow_html=True)
|
| 740 |
+
|
| 741 |
+
# Statistics (if logging is enabled)
|
| 742 |
+
if hf_token and hf_token.strip():
|
| 743 |
+
st.markdown("### π Session Stats")
|
| 744 |
+
total_interactions = len([r for r in st.session_state.get("responses", []) if r.get("role") == "assistant"])
|
| 745 |
+
st.metric("Interactions", total_interactions)
|
| 746 |
+
|
| 747 |
+
feedbacks_given = len([r for r in st.session_state.get("responses", []) if r.get("role") == "assistant" and "feedback" in r])
|
| 748 |
+
st.metric("Feedbacks Given", feedbacks_given)
|
| 749 |
+
|
| 750 |
+
# Footer
|
| 751 |
+
st.markdown("""
|
| 752 |
+
<div style='text-align: center; margin-top: 3rem; padding: 2rem; background: rgba(255,255,255,0.1); border-radius: 15px;'>
|
| 753 |
+
<h3>π Together for Cleaner Air</h3>
|
| 754 |
+
<p>VayuBuddy - Empowering environmental awareness through AI</p>
|
| 755 |
+
<small>Β© 2024 IIT Gandhinagar Sustainability Lab</small>
|
| 756 |
+
</div>
|
| 757 |
""", unsafe_allow_html=True)
|