Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,1034 +1,1220 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import json
|
| 3 |
-
import sqlite3
|
| 4 |
-
import random
|
| 5 |
-
import requests
|
| 6 |
-
import asyncio
|
| 7 |
-
from typing import TypedDict, Annotated, List, Dict, Any
|
| 8 |
-
from datetime import datetime
|
| 9 |
-
from pathlib import Path
|
| 10 |
-
from enum import Enum
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
from
|
| 17 |
-
from
|
| 18 |
-
from langchain_core.
|
| 19 |
-
from
|
| 20 |
-
from
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
#
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 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 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 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 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
for
|
| 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 |
-
|
| 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 |
-
if
|
| 413 |
-
|
| 414 |
-
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
if
|
| 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 |
-
|
| 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 |
-
state
|
| 503 |
-
|
| 504 |
-
|
| 505 |
-
|
| 506 |
-
|
| 507 |
-
|
| 508 |
-
|
| 509 |
-
|
| 510 |
-
|
| 511 |
-
|
| 512 |
-
|
| 513 |
-
|
| 514 |
-
|
| 515 |
-
|
| 516 |
-
|
| 517 |
-
|
| 518 |
-
|
| 519 |
-
|
| 520 |
-
|
| 521 |
-
response
|
| 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 |
-
state
|
| 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 |
-
|
| 674 |
-
|
| 675 |
-
|
| 676 |
-
|
| 677 |
-
|
| 678 |
-
|
| 679 |
-
|
| 680 |
-
|
| 681 |
-
|
| 682 |
-
|
| 683 |
-
|
| 684 |
-
|
| 685 |
-
|
| 686 |
-
|
| 687 |
-
|
| 688 |
-
|
| 689 |
-
|
| 690 |
-
|
| 691 |
-
|
| 692 |
-
|
| 693 |
-
|
| 694 |
-
|
| 695 |
-
|
| 696 |
-
|
| 697 |
-
state
|
| 698 |
-
|
| 699 |
-
|
| 700 |
-
|
| 701 |
-
|
| 702 |
-
|
| 703 |
-
|
| 704 |
-
|
| 705 |
-
|
| 706 |
-
|
| 707 |
-
|
| 708 |
-
|
| 709 |
-
|
| 710 |
-
|
| 711 |
-
|
| 712 |
-
|
| 713 |
-
|
| 714 |
-
|
| 715 |
-
|
| 716 |
-
|
| 717 |
-
|
| 718 |
-
|
| 719 |
-
|
| 720 |
-
|
| 721 |
-
|
| 722 |
-
""
|
| 723 |
-
|
| 724 |
-
|
| 725 |
-
|
| 726 |
-
|
| 727 |
-
|
| 728 |
-
|
| 729 |
-
|
| 730 |
-
|
| 731 |
-
|
| 732 |
-
|
| 733 |
-
|
| 734 |
-
|
| 735 |
-
|
| 736 |
-
|
| 737 |
-
|
| 738 |
-
|
| 739 |
-
|
| 740 |
-
|
| 741 |
-
|
| 742 |
-
|
| 743 |
-
|
| 744 |
-
|
| 745 |
-
|
| 746 |
-
|
| 747 |
-
|
| 748 |
-
|
| 749 |
-
|
| 750 |
-
|
| 751 |
-
|
| 752 |
-
|
| 753 |
-
|
| 754 |
-
|
| 755 |
-
|
| 756 |
-
|
| 757 |
-
|
| 758 |
-
|
| 759 |
-
|
| 760 |
-
|
| 761 |
-
|
| 762 |
-
|
| 763 |
-
|
| 764 |
-
|
| 765 |
-
|
| 766 |
-
|
| 767 |
-
|
| 768 |
-
|
| 769 |
-
|
| 770 |
-
|
| 771 |
-
|
| 772 |
-
|
| 773 |
-
|
| 774 |
-
|
| 775 |
-
|
| 776 |
-
|
| 777 |
-
|
| 778 |
-
|
| 779 |
-
|
| 780 |
-
|
| 781 |
-
|
| 782 |
-
|
| 783 |
-
|
| 784 |
-
|
| 785 |
-
|
| 786 |
-
|
| 787 |
-
|
| 788 |
-
|
| 789 |
-
|
| 790 |
-
gr.update(visible=
|
| 791 |
-
gr.update(visible=True), #
|
| 792 |
-
|
| 793 |
-
gr.update(visible=False)
|
| 794 |
-
|
| 795 |
-
|
| 796 |
-
|
| 797 |
-
|
| 798 |
-
|
| 799 |
-
|
| 800 |
-
|
| 801 |
-
|
| 802 |
-
|
| 803 |
-
|
| 804 |
-
|
| 805 |
-
|
| 806 |
-
|
| 807 |
-
|
| 808 |
-
|
| 809 |
-
|
| 810 |
-
|
| 811 |
-
|
| 812 |
-
|
| 813 |
-
|
| 814 |
-
|
| 815 |
-
|
| 816 |
-
|
| 817 |
-
|
| 818 |
-
|
| 819 |
-
|
| 820 |
-
|
| 821 |
-
|
| 822 |
-
|
| 823 |
-
|
| 824 |
-
|
| 825 |
-
|
| 826 |
-
|
| 827 |
-
|
| 828 |
-
|
| 829 |
-
|
| 830 |
-
|
| 831 |
-
|
| 832 |
-
|
| 833 |
-
|
| 834 |
-
|
| 835 |
-
|
| 836 |
-
|
| 837 |
-
|
| 838 |
-
|
| 839 |
-
|
| 840 |
-
|
| 841 |
-
|
| 842 |
-
|
| 843 |
-
|
| 844 |
-
|
| 845 |
-
|
| 846 |
-
|
| 847 |
-
|
| 848 |
-
|
| 849 |
-
|
| 850 |
-
|
| 851 |
-
return (
|
| 852 |
-
|
| 853 |
-
"
|
| 854 |
-
gr.update(visible=
|
| 855 |
-
gr.update(visible=
|
| 856 |
-
gr.update(visible=
|
| 857 |
-
gr.update(visible=False),
|
| 858 |
-
"",
|
| 859 |
-
gr.update(visible=False)
|
| 860 |
-
)
|
| 861 |
-
|
| 862 |
-
|
| 863 |
-
|
| 864 |
-
|
| 865 |
-
.
|
| 866 |
-
|
| 867 |
-
|
| 868 |
-
|
| 869 |
-
|
| 870 |
-
|
| 871 |
-
|
| 872 |
-
}
|
| 873 |
-
.
|
| 874 |
-
|
| 875 |
-
|
| 876 |
-
|
| 877 |
-
|
| 878 |
-
.
|
| 879 |
-
|
| 880 |
-
|
| 881 |
-
|
| 882 |
-
|
| 883 |
-
|
| 884 |
-
|
| 885 |
-
""
|
| 886 |
-
|
| 887 |
-
|
| 888 |
-
|
| 889 |
-
|
| 890 |
-
|
| 891 |
-
|
| 892 |
-
|
| 893 |
-
|
| 894 |
-
|
| 895 |
-
|
| 896 |
-
|
| 897 |
-
|
| 898 |
-
|
| 899 |
-
|
| 900 |
-
|
| 901 |
-
|
| 902 |
-
|
| 903 |
-
|
| 904 |
-
|
| 905 |
-
|
| 906 |
-
|
| 907 |
-
|
| 908 |
-
|
| 909 |
-
|
| 910 |
-
)
|
| 911 |
-
|
| 912 |
-
|
| 913 |
-
|
| 914 |
-
|
| 915 |
-
|
| 916 |
-
|
| 917 |
-
|
| 918 |
-
|
| 919 |
-
|
| 920 |
-
|
| 921 |
-
|
| 922 |
-
|
| 923 |
-
|
| 924 |
-
|
| 925 |
-
|
| 926 |
-
|
| 927 |
-
|
| 928 |
-
|
| 929 |
-
|
| 930 |
-
|
| 931 |
-
|
| 932 |
-
|
| 933 |
-
|
| 934 |
-
|
| 935 |
-
|
| 936 |
-
|
| 937 |
-
|
| 938 |
-
|
| 939 |
-
|
| 940 |
-
|
| 941 |
-
|
| 942 |
-
|
| 943 |
-
|
| 944 |
-
|
| 945 |
-
|
| 946 |
-
|
| 947 |
-
|
| 948 |
-
|
| 949 |
-
|
| 950 |
-
|
| 951 |
-
|
| 952 |
-
|
| 953 |
-
|
| 954 |
-
|
| 955 |
-
|
| 956 |
-
|
| 957 |
-
|
| 958 |
-
|
| 959 |
-
|
| 960 |
-
|
| 961 |
-
|
| 962 |
-
|
| 963 |
-
|
| 964 |
-
|
| 965 |
-
#
|
| 966 |
-
|
| 967 |
-
|
| 968 |
-
|
| 969 |
-
|
| 970 |
-
|
| 971 |
-
|
| 972 |
-
|
| 973 |
-
|
| 974 |
-
|
| 975 |
-
|
| 976 |
-
|
| 977 |
-
|
| 978 |
-
|
| 979 |
-
|
| 980 |
-
|
| 981 |
-
|
| 982 |
-
|
| 983 |
-
|
| 984 |
-
|
| 985 |
-
|
| 986 |
-
|
| 987 |
-
|
| 988 |
-
|
| 989 |
-
|
| 990 |
-
|
| 991 |
-
|
| 992 |
-
|
| 993 |
-
|
| 994 |
-
|
| 995 |
-
|
| 996 |
-
|
| 997 |
-
|
| 998 |
-
|
| 999 |
-
|
| 1000 |
-
|
| 1001 |
-
|
| 1002 |
-
|
| 1003 |
-
|
| 1004 |
-
|
| 1005 |
-
|
| 1006 |
-
|
| 1007 |
-
|
| 1008 |
-
|
| 1009 |
-
|
| 1010 |
-
|
| 1011 |
-
|
| 1012 |
-
|
| 1013 |
-
|
| 1014 |
-
|
| 1015 |
-
|
| 1016 |
-
|
| 1017 |
-
|
| 1018 |
-
|
| 1019 |
-
|
| 1020 |
-
|
| 1021 |
-
|
| 1022 |
-
|
| 1023 |
-
|
| 1024 |
-
|
| 1025 |
-
|
| 1026 |
-
|
| 1027 |
-
|
| 1028 |
-
|
| 1029 |
-
|
| 1030 |
-
|
| 1031 |
-
|
| 1032 |
-
|
| 1033 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1034 |
demo.launch()
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
import sqlite3
|
| 4 |
+
import random
|
| 5 |
+
import requests
|
| 6 |
+
import asyncio
|
| 7 |
+
from typing import TypedDict, Annotated, List, Dict, Any, Optional
|
| 8 |
+
from datetime import datetime
|
| 9 |
+
from pathlib import Path
|
| 10 |
+
from enum import Enum
|
| 11 |
+
import tempfile
|
| 12 |
+
import shutil
|
| 13 |
+
|
| 14 |
+
# Third-party imports
|
| 15 |
+
import gradio as gr
|
| 16 |
+
from dotenv import load_dotenv
|
| 17 |
+
from bs4 import BeautifulSoup
|
| 18 |
+
from langchain_core.messages import BaseMessage, HumanMessage, AIMessage, SystemMessage, ToolMessage
|
| 19 |
+
from langchain_mistralai.chat_models import ChatMistralAI
|
| 20 |
+
from langchain_core.tools import tool
|
| 21 |
+
from langchain_community.tools.tavily_search import TavilySearchResults
|
| 22 |
+
from langgraph.graph import StateGraph, END
|
| 23 |
+
from huggingface_hub import HfApi, hf_hub_download, upload_file
|
| 24 |
+
from datasets import Dataset, load_dataset
|
| 25 |
+
|
| 26 |
+
# --- 1. SETUP & CONFIGURATION ---
|
| 27 |
+
|
| 28 |
+
load_dotenv()
|
| 29 |
+
|
| 30 |
+
# Hugging Face Configuration
|
| 31 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 32 |
+
HF_DATASET_REPO = os.getenv("HF_DATASET_REPO", "tuesday-trivia-posts") # e.g., "username/trivia-posts-db"
|
| 33 |
+
HF_USERNAME = os.getenv("HF_USERNAME", None) # Your HF username
|
| 34 |
+
|
| 35 |
+
# Auto-construct repo name if not provided
|
| 36 |
+
if "/" not in HF_DATASET_REPO and HF_USERNAME:
|
| 37 |
+
HF_DATASET_REPO = f"{HF_USERNAME}/{HF_DATASET_REPO}"
|
| 38 |
+
|
| 39 |
+
# Local temporary database
|
| 40 |
+
DB_DIR = Path(tempfile.gettempdir()) / "trivia_data"
|
| 41 |
+
DB_DIR.mkdir(exist_ok=True, parents=True)
|
| 42 |
+
DB_PATH = DB_DIR / "trivia_posts.db"
|
| 43 |
+
|
| 44 |
+
print(f"๐ Local database location: {DB_PATH.absolute()}")
|
| 45 |
+
print(f"โ๏ธ HF Dataset repository: {HF_DATASET_REPO}")
|
| 46 |
+
|
| 47 |
+
# Initialize HF API
|
| 48 |
+
hf_api = HfApi(token=HF_TOKEN) if HF_TOKEN else None
|
| 49 |
+
|
| 50 |
+
# --- 2. API KEY ROTATION ---
|
| 51 |
+
|
| 52 |
+
class APIKeyRotator:
|
| 53 |
+
"""Handles API key rotation for fault tolerance"""
|
| 54 |
+
|
| 55 |
+
def __init__(self, service_name: str):
|
| 56 |
+
self.service_name = service_name
|
| 57 |
+
self.keys = self._load_keys()
|
| 58 |
+
self.current_index = 0
|
| 59 |
+
|
| 60 |
+
def _load_keys(self) -> List[str]:
|
| 61 |
+
keys = []
|
| 62 |
+
i = 1
|
| 63 |
+
while True:
|
| 64 |
+
key = os.getenv(f"{self.service_name}_API_KEY_{i}")
|
| 65 |
+
if not key:
|
| 66 |
+
if i == 1:
|
| 67 |
+
key = os.getenv(f"{self.service_name}_API_KEY")
|
| 68 |
+
if key:
|
| 69 |
+
keys.append(key)
|
| 70 |
+
break
|
| 71 |
+
keys.append(key)
|
| 72 |
+
i += 1
|
| 73 |
+
|
| 74 |
+
if not keys:
|
| 75 |
+
print(f"โ ๏ธ No API keys found for {self.service_name}. Ensure secrets are set.")
|
| 76 |
+
return []
|
| 77 |
+
|
| 78 |
+
random.shuffle(keys)
|
| 79 |
+
return keys
|
| 80 |
+
|
| 81 |
+
def get_key(self) -> str:
|
| 82 |
+
if not self.keys: return ""
|
| 83 |
+
return self.keys[self.current_index]
|
| 84 |
+
|
| 85 |
+
def rotate(self) -> str:
|
| 86 |
+
if not self.keys: return ""
|
| 87 |
+
self.current_index = (self.current_index + 1) % len(self.keys)
|
| 88 |
+
print(f"๐ Rotated {self.service_name} API key to index {self.current_index + 1}/{len(self.keys)}")
|
| 89 |
+
return self.get_key()
|
| 90 |
+
|
| 91 |
+
mistral_rotator = APIKeyRotator("MISTRAL")
|
| 92 |
+
tavily_rotator = APIKeyRotator("TAVILY")
|
| 93 |
+
|
| 94 |
+
os.environ["MISTRAL_API_KEY"] = mistral_rotator.get_key()
|
| 95 |
+
os.environ["TAVILY_API_KEY"] = tavily_rotator.get_key()
|
| 96 |
+
|
| 97 |
+
# --- 3. RESILIENT LLM ---
|
| 98 |
+
|
| 99 |
+
def create_llm_with_rotation(model: str, temperature: float = 0.2, max_retries: int = 3):
|
| 100 |
+
class ResilientLLM:
|
| 101 |
+
def __init__(self, model, temperature):
|
| 102 |
+
self.model = model
|
| 103 |
+
self.temperature = temperature
|
| 104 |
+
|
| 105 |
+
def invoke(self, *args, **kwargs):
|
| 106 |
+
for attempt in range(max_retries):
|
| 107 |
+
try:
|
| 108 |
+
llm = ChatMistralAI(
|
| 109 |
+
model=self.model,
|
| 110 |
+
temperature=self.temperature,
|
| 111 |
+
mistral_api_key=mistral_rotator.get_key()
|
| 112 |
+
)
|
| 113 |
+
return llm.invoke(*args, **kwargs)
|
| 114 |
+
except Exception as e:
|
| 115 |
+
print(f"โ API call failed (attempt {attempt + 1}/{max_retries}): {str(e)}")
|
| 116 |
+
if attempt < max_retries - 1:
|
| 117 |
+
mistral_rotator.rotate()
|
| 118 |
+
else:
|
| 119 |
+
raise Exception(f"All API keys exhausted after {max_retries} attempts")
|
| 120 |
+
|
| 121 |
+
def bind_tools(self, tools):
|
| 122 |
+
class ToolBoundLLM:
|
| 123 |
+
def __init__(self, parent, tools):
|
| 124 |
+
self.parent = parent
|
| 125 |
+
self.tools = tools
|
| 126 |
+
|
| 127 |
+
def invoke(self, *args, **kwargs):
|
| 128 |
+
for attempt in range(max_retries):
|
| 129 |
+
try:
|
| 130 |
+
llm = ChatMistralAI(
|
| 131 |
+
model=self.parent.model,
|
| 132 |
+
temperature=self.parent.temperature,
|
| 133 |
+
mistral_api_key=mistral_rotator.get_key()
|
| 134 |
+
)
|
| 135 |
+
bound_llm = llm.bind_tools(self.tools)
|
| 136 |
+
return bound_llm.invoke(*args, **kwargs)
|
| 137 |
+
except Exception as e:
|
| 138 |
+
print(f"โ API call failed (attempt {attempt + 1}/{max_retries}): {str(e)}")
|
| 139 |
+
if attempt < max_retries - 1:
|
| 140 |
+
mistral_rotator.rotate()
|
| 141 |
+
else:
|
| 142 |
+
raise Exception(f"All API keys exhausted after {max_retries} attempts")
|
| 143 |
+
return ToolBoundLLM(self, tools)
|
| 144 |
+
|
| 145 |
+
return ResilientLLM(model, temperature)
|
| 146 |
+
|
| 147 |
+
llm_small = create_llm_with_rotation("mistral-small-latest", temperature=0.2)
|
| 148 |
+
llm_medium = create_llm_with_rotation("mistral-medium-latest", temperature=0.2)
|
| 149 |
+
llm_large = create_llm_with_rotation("mistral-large-latest", temperature=0.2)
|
| 150 |
+
|
| 151 |
+
# --- 4. DATABASE FUNCTIONS WITH HF SYNC ---
|
| 152 |
+
|
| 153 |
+
def get_db_connection():
|
| 154 |
+
return sqlite3.connect(str(DB_PATH))
|
| 155 |
+
|
| 156 |
+
def sync_from_hf():
|
| 157 |
+
"""Download database from HF Dataset if it exists"""
|
| 158 |
+
if not HF_TOKEN or not hf_api:
|
| 159 |
+
print("โ ๏ธ No HF_TOKEN found, skipping sync from HF")
|
| 160 |
+
return False
|
| 161 |
+
|
| 162 |
+
try:
|
| 163 |
+
print("๐ฅ Syncing database from Hugging Face...")
|
| 164 |
+
|
| 165 |
+
# Try to load the dataset
|
| 166 |
+
try:
|
| 167 |
+
dataset = load_dataset(HF_DATASET_REPO, split="train", token=HF_TOKEN)
|
| 168 |
+
|
| 169 |
+
# Convert dataset to SQLite
|
| 170 |
+
conn = get_db_connection()
|
| 171 |
+
cursor = conn.cursor()
|
| 172 |
+
|
| 173 |
+
# Clear existing data
|
| 174 |
+
cursor.execute('DELETE FROM posts')
|
| 175 |
+
|
| 176 |
+
# Insert data from HF
|
| 177 |
+
for row in dataset:
|
| 178 |
+
cursor.execute('''
|
| 179 |
+
INSERT INTO posts (id, date, topic, summary, source_url, quality_score,
|
| 180 |
+
engagement_score, hashtags, created_at, updated_at)
|
| 181 |
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
| 182 |
+
''', (
|
| 183 |
+
row.get('id'),
|
| 184 |
+
row.get('date'),
|
| 185 |
+
row.get('topic'),
|
| 186 |
+
row.get('summary'),
|
| 187 |
+
row.get('source_url'),
|
| 188 |
+
row.get('quality_score'),
|
| 189 |
+
row.get('engagement_score'),
|
| 190 |
+
row.get('hashtags'),
|
| 191 |
+
row.get('created_at'),
|
| 192 |
+
row.get('updated_at')
|
| 193 |
+
))
|
| 194 |
+
|
| 195 |
+
conn.commit()
|
| 196 |
+
conn.close()
|
| 197 |
+
|
| 198 |
+
print(f"โ
Synced {len(dataset)} posts from HF Dataset")
|
| 199 |
+
return True
|
| 200 |
+
|
| 201 |
+
except Exception as e:
|
| 202 |
+
if "not found" in str(e).lower() or "doesn't exist" in str(e).lower():
|
| 203 |
+
print(f"โน๏ธ Dataset '{HF_DATASET_REPO}' doesn't exist yet, will create on first save")
|
| 204 |
+
return False
|
| 205 |
+
else:
|
| 206 |
+
print(f"โ ๏ธ Error loading dataset: {e}")
|
| 207 |
+
return False
|
| 208 |
+
|
| 209 |
+
except Exception as e:
|
| 210 |
+
print(f"โ ๏ธ Error syncing from HF: {e}")
|
| 211 |
+
return False
|
| 212 |
+
|
| 213 |
+
def sync_to_hf():
|
| 214 |
+
"""Upload database to HF Dataset"""
|
| 215 |
+
if not HF_TOKEN or not hf_api:
|
| 216 |
+
print("โ ๏ธ No HF_TOKEN found, skipping sync to HF")
|
| 217 |
+
return False
|
| 218 |
+
|
| 219 |
+
try:
|
| 220 |
+
print("๐ค Syncing database to Hugging Face...")
|
| 221 |
+
|
| 222 |
+
conn = get_db_connection()
|
| 223 |
+
cursor = conn.cursor()
|
| 224 |
+
cursor.execute('SELECT * FROM posts')
|
| 225 |
+
|
| 226 |
+
columns = [description[0] for description in cursor.description]
|
| 227 |
+
rows = cursor.fetchall()
|
| 228 |
+
conn.close()
|
| 229 |
+
|
| 230 |
+
if not rows:
|
| 231 |
+
print("โน๏ธ No posts to sync")
|
| 232 |
+
return False
|
| 233 |
+
|
| 234 |
+
# Convert to dictionary format for HF Dataset
|
| 235 |
+
data = {col: [] for col in columns}
|
| 236 |
+
for row in rows:
|
| 237 |
+
for col, value in zip(columns, row):
|
| 238 |
+
data[col].append(value)
|
| 239 |
+
|
| 240 |
+
# Create and push dataset
|
| 241 |
+
dataset = Dataset.from_dict(data)
|
| 242 |
+
|
| 243 |
+
# Try to push, create repo if it doesn't exist
|
| 244 |
+
try:
|
| 245 |
+
dataset.push_to_hub(
|
| 246 |
+
HF_DATASET_REPO,
|
| 247 |
+
token=HF_TOKEN,
|
| 248 |
+
private=True # Make it private by default
|
| 249 |
+
)
|
| 250 |
+
print(f"โ
Synced {len(rows)} posts to HF Dataset: {HF_DATASET_REPO}")
|
| 251 |
+
return True
|
| 252 |
+
|
| 253 |
+
except Exception as push_error:
|
| 254 |
+
if "not found" in str(push_error).lower():
|
| 255 |
+
# Create the repo first
|
| 256 |
+
print(f"๐ฆ Creating new dataset repository: {HF_DATASET_REPO}")
|
| 257 |
+
hf_api.create_repo(
|
| 258 |
+
repo_id=HF_DATASET_REPO,
|
| 259 |
+
token=HF_TOKEN,
|
| 260 |
+
repo_type="dataset",
|
| 261 |
+
private=True
|
| 262 |
+
)
|
| 263 |
+
# Try pushing again
|
| 264 |
+
dataset.push_to_hub(
|
| 265 |
+
HF_DATASET_REPO,
|
| 266 |
+
token=HF_TOKEN,
|
| 267 |
+
private=True
|
| 268 |
+
)
|
| 269 |
+
print(f"โ
Created and synced to new dataset: {HF_DATASET_REPO}")
|
| 270 |
+
return True
|
| 271 |
+
else:
|
| 272 |
+
raise push_error
|
| 273 |
+
|
| 274 |
+
except Exception as e:
|
| 275 |
+
print(f"โ Error syncing to HF: {e}")
|
| 276 |
+
return False
|
| 277 |
+
|
| 278 |
+
def init_database():
|
| 279 |
+
"""Initialize local database and sync from HF if available"""
|
| 280 |
+
conn = get_db_connection()
|
| 281 |
+
cursor = conn.cursor()
|
| 282 |
+
cursor.execute('''
|
| 283 |
+
CREATE TABLE IF NOT EXISTS posts (
|
| 284 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 285 |
+
date TEXT NOT NULL,
|
| 286 |
+
topic TEXT NOT NULL,
|
| 287 |
+
summary TEXT NOT NULL,
|
| 288 |
+
source_url TEXT,
|
| 289 |
+
quality_score REAL CHECK(quality_score >= 0 AND quality_score <= 10),
|
| 290 |
+
engagement_score REAL,
|
| 291 |
+
hashtags TEXT,
|
| 292 |
+
created_at TEXT NOT NULL,
|
| 293 |
+
updated_at TEXT
|
| 294 |
+
)
|
| 295 |
+
''')
|
| 296 |
+
cursor.execute('CREATE INDEX IF NOT EXISTS idx_date ON posts(date)')
|
| 297 |
+
cursor.execute('CREATE INDEX IF NOT EXISTS idx_quality_score ON posts(quality_score)')
|
| 298 |
+
cursor.execute('CREATE INDEX IF NOT EXISTS idx_created_at ON posts(created_at)')
|
| 299 |
+
cursor.execute('''
|
| 300 |
+
CREATE TABLE IF NOT EXISTS metadata (
|
| 301 |
+
key TEXT PRIMARY KEY, value TEXT, updated_at TEXT NOT NULL
|
| 302 |
+
)
|
| 303 |
+
''')
|
| 304 |
+
cursor.execute('''
|
| 305 |
+
INSERT OR REPLACE INTO metadata (key, value, updated_at)
|
| 306 |
+
VALUES ('db_version', '1.0', ?)
|
| 307 |
+
''', (datetime.now().isoformat(),))
|
| 308 |
+
conn.commit()
|
| 309 |
+
conn.close()
|
| 310 |
+
|
| 311 |
+
# Sync from HF after initializing schema
|
| 312 |
+
sync_from_hf()
|
| 313 |
+
|
| 314 |
+
# Initialize immediately
|
| 315 |
+
init_database()
|
| 316 |
+
|
| 317 |
+
# --- 5. TOOLS ---
|
| 318 |
+
|
| 319 |
+
@tool
|
| 320 |
+
def search_science_breakthroughs(query: str) -> str:
|
| 321 |
+
"""Search for recent scientific breakthroughs."""
|
| 322 |
+
try:
|
| 323 |
+
search = TavilySearchResults(
|
| 324 |
+
max_results=10,
|
| 325 |
+
include_domains=["sciencedaily.com", "nature.com", "science.org", "newscientist.com", "scientificamerican.com"],
|
| 326 |
+
search_depth="advanced"
|
| 327 |
+
)
|
| 328 |
+
results = search.invoke(query)
|
| 329 |
+
return json.dumps(results, indent=2)
|
| 330 |
+
except Exception as e:
|
| 331 |
+
return f"Error in search: {str(e)}"
|
| 332 |
+
|
| 333 |
+
@tool
|
| 334 |
+
def fetch_article_content(url: str) -> str:
|
| 335 |
+
"""Fetch the full text content of an article."""
|
| 336 |
+
try:
|
| 337 |
+
response = requests.get(url, timeout=10, headers={'User-Agent': 'Mozilla/5.0'})
|
| 338 |
+
response.raise_for_status()
|
| 339 |
+
soup = BeautifulSoup(response.content, 'html.parser')
|
| 340 |
+
for script in soup(["script", "style", "nav", "footer", "header"]):
|
| 341 |
+
script.decompose()
|
| 342 |
+
text = soup.get_text()
|
| 343 |
+
lines = (line.strip() for line in text.splitlines())
|
| 344 |
+
chunks = (phrase.strip() for line in lines for phrase in line.split(" "))
|
| 345 |
+
text = ' '.join(chunk for chunk in chunks if chunk)
|
| 346 |
+
return text[:5000] if len(text) > 5000 else text
|
| 347 |
+
except Exception as e:
|
| 348 |
+
return f"Error fetching article: {str(e)}"
|
| 349 |
+
|
| 350 |
+
@tool
|
| 351 |
+
def get_all_previous_posts() -> str:
|
| 352 |
+
"""Retrieve all previously published posts."""
|
| 353 |
+
conn = None
|
| 354 |
+
try:
|
| 355 |
+
conn = get_db_connection()
|
| 356 |
+
cursor = conn.cursor()
|
| 357 |
+
cursor.execute('SELECT id, date, topic, summary, source_url, quality_score FROM posts ORDER BY date DESC')
|
| 358 |
+
posts = cursor.fetchall()
|
| 359 |
+
if not posts: return "No previous posts found."
|
| 360 |
+
formatted = []
|
| 361 |
+
for p in posts:
|
| 362 |
+
formatted.append({"id": p[0], "date": p[1], "topic": p[2], "summary": p[3][:100], "source_url": p[4], "quality_score": p[5]})
|
| 363 |
+
return json.dumps(formatted, indent=2)
|
| 364 |
+
except Exception as e:
|
| 365 |
+
return f"Error: {str(e)}"
|
| 366 |
+
finally:
|
| 367 |
+
if conn: conn.close()
|
| 368 |
+
|
| 369 |
+
@tool
|
| 370 |
+
def get_top_quality_posts(limit: int = 5) -> str:
|
| 371 |
+
"""Retrieve top quality posts."""
|
| 372 |
+
conn = None
|
| 373 |
+
try:
|
| 374 |
+
conn = get_db_connection()
|
| 375 |
+
cursor = conn.cursor()
|
| 376 |
+
cursor.execute('SELECT id, date, topic, summary, quality_score, hashtags FROM posts WHERE quality_score >= 7 ORDER BY quality_score DESC LIMIT ?', (limit,))
|
| 377 |
+
posts = cursor.fetchall()
|
| 378 |
+
if not posts: return "No high-quality example posts found."
|
| 379 |
+
formatted = []
|
| 380 |
+
for p in posts:
|
| 381 |
+
formatted.append({"id": p[0], "date": p[1], "topic": p[2], "summary": p[3], "quality_score": p[4], "hashtags": p[5]})
|
| 382 |
+
return json.dumps(formatted, indent=2)
|
| 383 |
+
except Exception as e:
|
| 384 |
+
return f"Error: {str(e)}"
|
| 385 |
+
finally:
|
| 386 |
+
if conn: conn.close()
|
| 387 |
+
|
| 388 |
+
@tool
|
| 389 |
+
def save_approved_post(topic: str, summary: str, source_url: str, quality_score: float, hashtags: str) -> str:
|
| 390 |
+
"""Save an approved post to the database and sync to HF."""
|
| 391 |
+
conn = None
|
| 392 |
+
try:
|
| 393 |
+
conn = get_db_connection()
|
| 394 |
+
cursor = conn.cursor()
|
| 395 |
+
now = datetime.now()
|
| 396 |
+
cursor.execute('''
|
| 397 |
+
INSERT INTO posts (date, topic, summary, source_url, quality_score, hashtags, created_at, updated_at)
|
| 398 |
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
| 399 |
+
''', (now.strftime('%Y-%m-%d'), topic, summary, source_url, quality_score, hashtags, now.isoformat(), now.isoformat()))
|
| 400 |
+
conn.commit()
|
| 401 |
+
post_id = cursor.lastrowid
|
| 402 |
+
conn.close()
|
| 403 |
+
|
| 404 |
+
# Sync to HF after saving
|
| 405 |
+
sync_result = sync_to_hf()
|
| 406 |
+
sync_msg = "and synced to HF Dataset โ๏ธ" if sync_result else "(HF sync skipped)"
|
| 407 |
+
|
| 408 |
+
return f"Post saved successfully with ID: {post_id} {sync_msg}"
|
| 409 |
+
except Exception as e:
|
| 410 |
+
return f"Error saving post: {str(e)}"
|
| 411 |
+
finally:
|
| 412 |
+
if conn:
|
| 413 |
+
try:
|
| 414 |
+
conn.close()
|
| 415 |
+
except:
|
| 416 |
+
pass
|
| 417 |
+
|
| 418 |
+
@tool
|
| 419 |
+
def check_topic_similarity(new_topic: str) -> str:
|
| 420 |
+
"""Check topic similarity."""
|
| 421 |
+
conn = None
|
| 422 |
+
try:
|
| 423 |
+
conn = get_db_connection()
|
| 424 |
+
cursor = conn.cursor()
|
| 425 |
+
cursor.execute('SELECT topic, summary FROM posts')
|
| 426 |
+
previous = cursor.fetchall()
|
| 427 |
+
if not previous: return json.dumps({"is_duplicate": False, "similar_posts": []})
|
| 428 |
+
|
| 429 |
+
new_keywords = set(new_topic.lower().split())
|
| 430 |
+
similar = []
|
| 431 |
+
for prev_topic, prev_summary in previous:
|
| 432 |
+
prev_keywords = set(prev_topic.lower().split())
|
| 433 |
+
if not new_keywords: continue
|
| 434 |
+
overlap = len(new_keywords & prev_keywords)
|
| 435 |
+
similarity = overlap / len(new_keywords)
|
| 436 |
+
if similarity > 0.5:
|
| 437 |
+
similar.append({"topic": prev_topic, "similarity": similarity})
|
| 438 |
+
|
| 439 |
+
return json.dumps({"is_duplicate": len(similar) > 0, "similar_posts": similar}, indent=2)
|
| 440 |
+
except Exception as e:
|
| 441 |
+
return f"Error: {str(e)}"
|
| 442 |
+
finally:
|
| 443 |
+
if conn: conn.close()
|
| 444 |
+
|
| 445 |
+
@tool
|
| 446 |
+
def count_words(text: str) -> str:
|
| 447 |
+
return f"Word count: {len(text.split())}"
|
| 448 |
+
|
| 449 |
+
@tool
|
| 450 |
+
def get_example_posts_for_writer() -> str:
|
| 451 |
+
examples = [
|
| 452 |
+
{"title": "Researchers Have Developed Next-Generation Plant Immune System",
|
| 453 |
+
"content": "Full example content here...",
|
| 454 |
+
"structure": "Title -> 2 Paragraphs -> Hashtags"},
|
| 455 |
+
]
|
| 456 |
+
return json.dumps(examples, indent=2)
|
| 457 |
+
|
| 458 |
+
@tool
|
| 459 |
+
def get_example_posts_for_critic() -> str:
|
| 460 |
+
examples = [
|
| 461 |
+
{"title": "Scientists Found 14 Mysterious Creatures", "quality_score": 9.0},
|
| 462 |
+
]
|
| 463 |
+
return json.dumps(examples, indent=2)
|
| 464 |
+
|
| 465 |
+
# --- 6. WORKFLOW STAGES ENUM ---
|
| 466 |
+
|
| 467 |
+
class WorkflowStage(Enum):
|
| 468 |
+
IDLE = "idle"
|
| 469 |
+
DISCOVERY = "discovery"
|
| 470 |
+
CHECKPOINT_1 = "checkpoint_1"
|
| 471 |
+
CURATOR = "curator"
|
| 472 |
+
CHECKPOINT_2 = "checkpoint_2"
|
| 473 |
+
WRITER = "writer"
|
| 474 |
+
CRITIC = "critic"
|
| 475 |
+
CHECKPOINT_3 = "checkpoint_3"
|
| 476 |
+
FINALIZE = "finalize"
|
| 477 |
+
COMPLETE = "complete"
|
| 478 |
+
ERROR = "error"
|
| 479 |
+
|
| 480 |
+
# --- 7. ENHANCED STATE ---
|
| 481 |
+
|
| 482 |
+
class EnhancedAgentState(TypedDict):
|
| 483 |
+
stage: str
|
| 484 |
+
search_topic: str
|
| 485 |
+
candidates: List[Dict]
|
| 486 |
+
selected_story: Dict
|
| 487 |
+
draft_summary: str
|
| 488 |
+
quality_score: float
|
| 489 |
+
critic_feedback: str
|
| 490 |
+
retry_count: int
|
| 491 |
+
error_message: str
|
| 492 |
+
progress_log: List[str]
|
| 493 |
+
|
| 494 |
+
# --- 8. AGENT FUNCTIONS WITH PROGRESS TRACKING ---
|
| 495 |
+
|
| 496 |
+
def run_discovery(state: EnhancedAgentState, progress_callback=None) -> EnhancedAgentState:
|
| 497 |
+
"""Discovery Agent with progress tracking"""
|
| 498 |
+
try:
|
| 499 |
+
if progress_callback:
|
| 500 |
+
progress_callback("๐ Searching for scientific breakthroughs...")
|
| 501 |
+
|
| 502 |
+
topic = state.get("search_topic", "general science")
|
| 503 |
+
|
| 504 |
+
system_msg = SystemMessage(content=f"""You are the Discovery Agent for Tuesday Trivia.
|
| 505 |
+
Mission: Find 10-15 recent scientific breakthroughs (2-4 weeks).
|
| 506 |
+
Tools: search_science_breakthroughs, get_all_previous_posts, check_topic_similarity.
|
| 507 |
+
Output: List 10-15 unique breakthroughs with Title, Desc, URL, Why interesting.""")
|
| 508 |
+
|
| 509 |
+
user_msg = HumanMessage(content=f"Search for recent breakthroughs in {topic}.")
|
| 510 |
+
|
| 511 |
+
discovery_llm = llm_small.bind_tools([search_science_breakthroughs, get_all_previous_posts, check_topic_similarity])
|
| 512 |
+
|
| 513 |
+
if progress_callback:
|
| 514 |
+
progress_callback("๐ค Running discovery agent...")
|
| 515 |
+
|
| 516 |
+
response = discovery_llm.invoke([system_msg, user_msg])
|
| 517 |
+
conversation = [system_msg, user_msg, response]
|
| 518 |
+
|
| 519 |
+
max_steps = 5
|
| 520 |
+
steps = 0
|
| 521 |
+
while hasattr(response, 'tool_calls') and response.tool_calls and steps < max_steps:
|
| 522 |
+
if progress_callback:
|
| 523 |
+
progress_callback(f"๐ง Executing tools (step {steps+1}/{max_steps})...")
|
| 524 |
+
|
| 525 |
+
tool_messages = []
|
| 526 |
+
for tool_call in response.tool_calls:
|
| 527 |
+
name = tool_call['name']
|
| 528 |
+
args = tool_call['args']
|
| 529 |
+
|
| 530 |
+
if name == 'search_science_breakthroughs':
|
| 531 |
+
res = search_science_breakthroughs.invoke(args)
|
| 532 |
+
elif name == 'get_all_previous_posts':
|
| 533 |
+
res = get_all_previous_posts.invoke(args)
|
| 534 |
+
elif name == 'check_topic_similarity':
|
| 535 |
+
res = check_topic_similarity.invoke(args)
|
| 536 |
+
else:
|
| 537 |
+
res = f"Unknown tool: {name}"
|
| 538 |
+
|
| 539 |
+
tool_messages.append(ToolMessage(content=str(res), tool_call_id=tool_call['id']))
|
| 540 |
+
|
| 541 |
+
conversation.extend(tool_messages)
|
| 542 |
+
response = discovery_llm.invoke(conversation)
|
| 543 |
+
conversation.append(response)
|
| 544 |
+
steps += 1
|
| 545 |
+
|
| 546 |
+
# Parse candidates from response
|
| 547 |
+
candidates_text = response.content
|
| 548 |
+
|
| 549 |
+
if progress_callback:
|
| 550 |
+
progress_callback("โ
Discovery complete!")
|
| 551 |
+
|
| 552 |
+
state["candidates"] = [{"raw": candidates_text}]
|
| 553 |
+
state["stage"] = WorkflowStage.CHECKPOINT_1.value
|
| 554 |
+
state["progress_log"].append("Discovery completed successfully")
|
| 555 |
+
|
| 556 |
+
return state
|
| 557 |
+
|
| 558 |
+
except Exception as e:
|
| 559 |
+
state["stage"] = WorkflowStage.ERROR.value
|
| 560 |
+
state["error_message"] = f"Discovery failed: {str(e)}"
|
| 561 |
+
state["progress_log"].append(f"โ Error: {str(e)}")
|
| 562 |
+
return state
|
| 563 |
+
|
| 564 |
+
def run_curator(state: EnhancedAgentState, progress_callback=None) -> EnhancedAgentState:
|
| 565 |
+
"""Curator Agent with progress tracking"""
|
| 566 |
+
try:
|
| 567 |
+
if progress_callback:
|
| 568 |
+
progress_callback("๐ฏ Ranking and selecting best story...")
|
| 569 |
+
|
| 570 |
+
candidates = state.get("candidates", [])
|
| 571 |
+
candidates_text = candidates[0].get("raw", "") if candidates else ""
|
| 572 |
+
|
| 573 |
+
system_msg = SystemMessage(content="""You are the Curator Agent.
|
| 574 |
+
Rank candidates (1-10) on Recency, Significance, Engagement. Select TOP story.
|
| 575 |
+
Output: RANKED CANDIDATES list, then SELECTED STORY with Justification.""")
|
| 576 |
+
|
| 577 |
+
user_msg = HumanMessage(content=f"Evaluate these candidates:\n{candidates_text}")
|
| 578 |
+
|
| 579 |
+
curator_llm = llm_large.bind_tools([])
|
| 580 |
+
response = curator_llm.invoke([system_msg, user_msg])
|
| 581 |
+
|
| 582 |
+
if progress_callback:
|
| 583 |
+
progress_callback("โ
Story selected!")
|
| 584 |
+
|
| 585 |
+
state["selected_story"] = {"raw": response.content}
|
| 586 |
+
state["stage"] = WorkflowStage.CHECKPOINT_2.value
|
| 587 |
+
state["progress_log"].append("Curation completed")
|
| 588 |
+
|
| 589 |
+
return state
|
| 590 |
+
|
| 591 |
+
except Exception as e:
|
| 592 |
+
state["stage"] = WorkflowStage.ERROR.value
|
| 593 |
+
state["error_message"] = f"Curation failed: {str(e)}"
|
| 594 |
+
state["progress_log"].append(f"โ Error: {str(e)}")
|
| 595 |
+
return state
|
| 596 |
+
|
| 597 |
+
def run_writer(state: EnhancedAgentState, progress_callback=None) -> EnhancedAgentState:
|
| 598 |
+
"""Writer Agent with progress tracking"""
|
| 599 |
+
try:
|
| 600 |
+
if progress_callback:
|
| 601 |
+
progress_callback("โ๏ธ Writing draft post...")
|
| 602 |
+
|
| 603 |
+
selected_story = state.get("selected_story", {})
|
| 604 |
+
story_text = selected_story.get("raw", "")
|
| 605 |
+
retry_count = state.get("retry_count", 0)
|
| 606 |
+
|
| 607 |
+
retry_context = ""
|
| 608 |
+
if retry_count > 0:
|
| 609 |
+
retry_context = f"\nPrevious attempt rejected. Feedback: {state.get('critic_feedback')}"
|
| 610 |
+
|
| 611 |
+
system_msg = SystemMessage(content="""You are the Writer Agent.
|
| 612 |
+
Structure: Title, Para 1 (Hook), Para 2 (Tech/Impact), Hashtags (#TuesdayTrivia #RnDCell #CCA).
|
| 613 |
+
Tone: Conversational, active voice, no questions. 140-180 words.
|
| 614 |
+
Use tools to get examples and count words.""")
|
| 615 |
+
|
| 616 |
+
user_msg = HumanMessage(content=f"Write post for:\n{story_text}\n{retry_context}")
|
| 617 |
+
|
| 618 |
+
writer_llm = llm_medium.bind_tools([get_example_posts_for_writer, fetch_article_content, count_words])
|
| 619 |
+
|
| 620 |
+
response = writer_llm.invoke([system_msg, user_msg])
|
| 621 |
+
conversation = [system_msg, user_msg, response]
|
| 622 |
+
|
| 623 |
+
steps = 0
|
| 624 |
+
while hasattr(response, 'tool_calls') and response.tool_calls and steps < 5:
|
| 625 |
+
if progress_callback:
|
| 626 |
+
progress_callback(f"๐ง Writer using tools (step {steps+1})...")
|
| 627 |
+
|
| 628 |
+
tool_messages = []
|
| 629 |
+
for tool_call in response.tool_calls:
|
| 630 |
+
name = tool_call['name']
|
| 631 |
+
if name == 'get_example_posts_for_writer':
|
| 632 |
+
res = get_example_posts_for_writer.invoke(tool_call['args'])
|
| 633 |
+
elif name == 'fetch_article_content':
|
| 634 |
+
res = fetch_article_content.invoke(tool_call['args'])
|
| 635 |
+
elif name == 'count_words':
|
| 636 |
+
res = count_words.invoke(tool_call['args'])
|
| 637 |
+
else:
|
| 638 |
+
res = "Unknown"
|
| 639 |
+
tool_messages.append(ToolMessage(content=str(res), tool_call_id=tool_call['id']))
|
| 640 |
+
conversation.extend(tool_messages)
|
| 641 |
+
response = writer_llm.invoke(conversation)
|
| 642 |
+
conversation.append(response)
|
| 643 |
+
steps += 1
|
| 644 |
+
|
| 645 |
+
if progress_callback:
|
| 646 |
+
progress_callback("โ
Draft completed!")
|
| 647 |
+
|
| 648 |
+
state["draft_summary"] = response.content
|
| 649 |
+
state["retry_count"] = retry_count + 1
|
| 650 |
+
state["stage"] = WorkflowStage.CRITIC.value
|
| 651 |
+
state["progress_log"].append(f"Draft written (attempt {retry_count + 1})")
|
| 652 |
+
|
| 653 |
+
return state
|
| 654 |
+
|
| 655 |
+
except Exception as e:
|
| 656 |
+
state["stage"] = WorkflowStage.ERROR.value
|
| 657 |
+
state["error_message"] = f"Writing failed: {str(e)}"
|
| 658 |
+
state["progress_log"].append(f"โ Error: {str(e)}")
|
| 659 |
+
return state
|
| 660 |
+
|
| 661 |
+
def run_critic(state: EnhancedAgentState, progress_callback=None) -> EnhancedAgentState:
|
| 662 |
+
"""Critic Agent with progress tracking"""
|
| 663 |
+
try:
|
| 664 |
+
if progress_callback:
|
| 665 |
+
progress_callback("๐ Evaluating draft quality...")
|
| 666 |
+
|
| 667 |
+
draft = state.get("draft_summary", "")
|
| 668 |
+
|
| 669 |
+
system_msg = SystemMessage(content="""You are the Critic Agent.
|
| 670 |
+
Evaluate: Structure, Content, Style, Hashtags, Word Count.
|
| 671 |
+
Output: Quality Score (0-10) and Decision (APPROVED/REJECTED). Approved if score >= 8.""")
|
| 672 |
+
|
| 673 |
+
user_msg = HumanMessage(content=f"Evaluate:\n{draft}")
|
| 674 |
+
|
| 675 |
+
critic_llm = llm_large.bind_tools([get_example_posts_for_critic])
|
| 676 |
+
response = critic_llm.invoke([system_msg, user_msg])
|
| 677 |
+
conversation = [system_msg, user_msg, response]
|
| 678 |
+
|
| 679 |
+
if hasattr(response, 'tool_calls') and response.tool_calls:
|
| 680 |
+
for tool_call in response.tool_calls:
|
| 681 |
+
res = get_example_posts_for_critic.invoke(tool_call['args'])
|
| 682 |
+
conversation.append(ToolMessage(content=str(res), tool_call_id=tool_call['id']))
|
| 683 |
+
response = critic_llm.invoke(conversation)
|
| 684 |
+
|
| 685 |
+
text = response.content
|
| 686 |
+
score = 5.0
|
| 687 |
+
try:
|
| 688 |
+
if "TOTAL SCORE:" in text:
|
| 689 |
+
score = float(text.split("TOTAL SCORE:")[1].split("/")[0].strip())
|
| 690 |
+
except:
|
| 691 |
+
pass
|
| 692 |
+
|
| 693 |
+
if progress_callback:
|
| 694 |
+
progress_callback(f"โ
Evaluation complete! Score: {score}/10")
|
| 695 |
+
|
| 696 |
+
state["quality_score"] = score
|
| 697 |
+
state["critic_feedback"] = text
|
| 698 |
+
state["stage"] = WorkflowStage.CHECKPOINT_3.value
|
| 699 |
+
state["progress_log"].append(f"Critic evaluation: {score}/10")
|
| 700 |
+
|
| 701 |
+
return state
|
| 702 |
+
|
| 703 |
+
except Exception as e:
|
| 704 |
+
state["stage"] = WorkflowStage.ERROR.value
|
| 705 |
+
state["error_message"] = f"Critic failed: {str(e)}"
|
| 706 |
+
state["progress_log"].append(f"โ Error: {str(e)}")
|
| 707 |
+
return state
|
| 708 |
+
|
| 709 |
+
def run_finalize(state: EnhancedAgentState, progress_callback=None) -> EnhancedAgentState:
|
| 710 |
+
"""Finalize and save to database"""
|
| 711 |
+
try:
|
| 712 |
+
if progress_callback:
|
| 713 |
+
progress_callback("๐พ Saving to database...")
|
| 714 |
+
|
| 715 |
+
draft = state.get("draft_summary", "")
|
| 716 |
+
score = state.get("quality_score", 0.0)
|
| 717 |
+
|
| 718 |
+
topic = "Scientific Breakthrough"
|
| 719 |
+
hashtags = ""
|
| 720 |
+
lines = draft.split('\n')
|
| 721 |
+
for line in lines:
|
| 722 |
+
if "#" in line:
|
| 723 |
+
hashtags = line
|
| 724 |
+
break
|
| 725 |
+
|
| 726 |
+
res = save_approved_post.invoke({
|
| 727 |
+
"topic": topic,
|
| 728 |
+
"summary": draft,
|
| 729 |
+
"source_url": "N/A",
|
| 730 |
+
"quality_score": score,
|
| 731 |
+
"hashtags": hashtags
|
| 732 |
+
})
|
| 733 |
+
|
| 734 |
+
if progress_callback:
|
| 735 |
+
progress_callback("โ
Post saved successfully!")
|
| 736 |
+
|
| 737 |
+
state["stage"] = WorkflowStage.COMPLETE.value
|
| 738 |
+
state["progress_log"].append(f"Finalized: {res}")
|
| 739 |
+
|
| 740 |
+
return state
|
| 741 |
+
|
| 742 |
+
except Exception as e:
|
| 743 |
+
state["stage"] = WorkflowStage.ERROR.value
|
| 744 |
+
state["error_message"] = f"Finalization failed: {str(e)}"
|
| 745 |
+
state["progress_log"].append(f"โ Error: {str(e)}")
|
| 746 |
+
return state
|
| 747 |
+
|
| 748 |
+
# --- 9. GRADIO INTERFACE ---
|
| 749 |
+
|
| 750 |
+
def create_initial_state(topic: str) -> EnhancedAgentState:
|
| 751 |
+
return {
|
| 752 |
+
"stage": WorkflowStage.IDLE.value,
|
| 753 |
+
"search_topic": topic,
|
| 754 |
+
"candidates": [],
|
| 755 |
+
"selected_story": {},
|
| 756 |
+
"draft_summary": "",
|
| 757 |
+
"quality_score": 0.0,
|
| 758 |
+
"critic_feedback": "",
|
| 759 |
+
"retry_count": 0,
|
| 760 |
+
"error_message": "",
|
| 761 |
+
"progress_log": []
|
| 762 |
+
}
|
| 763 |
+
|
| 764 |
+
def start_workflow(topic: str, progress=gr.Progress()):
|
| 765 |
+
"""Start the discovery process"""
|
| 766 |
+
state = create_initial_state(topic)
|
| 767 |
+
|
| 768 |
+
def update_progress(msg):
|
| 769 |
+
progress(0.3, desc=msg)
|
| 770 |
+
|
| 771 |
+
state = run_discovery(state, update_progress)
|
| 772 |
+
|
| 773 |
+
if state["stage"] == WorkflowStage.ERROR.value:
|
| 774 |
+
return (
|
| 775 |
+
state,
|
| 776 |
+
f"โ **Error:** {state['error_message']}",
|
| 777 |
+
gr.update(visible=False),
|
| 778 |
+
gr.update(visible=False),
|
| 779 |
+
gr.update(visible=False),
|
| 780 |
+
gr.update(visible=True), # Show restart
|
| 781 |
+
"",
|
| 782 |
+
gr.update(visible=False)
|
| 783 |
+
)
|
| 784 |
+
|
| 785 |
+
candidates_text = state["candidates"][0]["raw"] if state["candidates"] else "No candidates found"
|
| 786 |
+
|
| 787 |
+
return (
|
| 788 |
+
state,
|
| 789 |
+
f"## ๐ Discovery Results\n\n{candidates_text}",
|
| 790 |
+
gr.update(visible=True), # Approve button
|
| 791 |
+
gr.update(visible=True), # Reject button
|
| 792 |
+
gr.update(visible=False),
|
| 793 |
+
gr.update(visible=False),
|
| 794 |
+
"",
|
| 795 |
+
gr.update(visible=False)
|
| 796 |
+
)
|
| 797 |
+
|
| 798 |
+
def handle_checkpoint1_approve(state, progress=gr.Progress()):
|
| 799 |
+
"""Handle approval at checkpoint 1"""
|
| 800 |
+
def update_progress(msg):
|
| 801 |
+
progress(0.5, desc=msg)
|
| 802 |
+
|
| 803 |
+
state = run_curator(state, update_progress)
|
| 804 |
+
|
| 805 |
+
if state["stage"] == WorkflowStage.ERROR.value:
|
| 806 |
+
return (
|
| 807 |
+
state,
|
| 808 |
+
f"โ **Error:** {state['error_message']}",
|
| 809 |
+
gr.update(visible=False),
|
| 810 |
+
gr.update(visible=False),
|
| 811 |
+
gr.update(visible=False),
|
| 812 |
+
gr.update(visible=True),
|
| 813 |
+
"",
|
| 814 |
+
gr.update(visible=False)
|
| 815 |
+
)
|
| 816 |
+
|
| 817 |
+
story_text = state["selected_story"]["raw"] if state["selected_story"] else "No story selected"
|
| 818 |
+
|
| 819 |
+
return (
|
| 820 |
+
state,
|
| 821 |
+
f"## ๐ฏ Selected Story\n\n{story_text}",
|
| 822 |
+
gr.update(visible=True), # Approve button
|
| 823 |
+
gr.update(visible=True), # Different story
|
| 824 |
+
gr.update(visible=False),
|
| 825 |
+
gr.update(visible=False),
|
| 826 |
+
"",
|
| 827 |
+
gr.update(visible=False)
|
| 828 |
+
)
|
| 829 |
+
|
| 830 |
+
def handle_checkpoint1_reject(state, progress=gr.Progress()):
|
| 831 |
+
"""Handle rejection at checkpoint 1 - restart discovery"""
|
| 832 |
+
def update_progress(msg):
|
| 833 |
+
progress(0.3, desc=msg)
|
| 834 |
+
|
| 835 |
+
state = run_discovery(state, update_progress)
|
| 836 |
+
|
| 837 |
+
if state["stage"] == WorkflowStage.ERROR.value:
|
| 838 |
+
return (
|
| 839 |
+
state,
|
| 840 |
+
f"โ **Error:** {state['error_message']}",
|
| 841 |
+
gr.update(visible=False),
|
| 842 |
+
gr.update(visible=False),
|
| 843 |
+
gr.update(visible=False),
|
| 844 |
+
gr.update(visible=True),
|
| 845 |
+
"",
|
| 846 |
+
gr.update(visible=False)
|
| 847 |
+
)
|
| 848 |
+
|
| 849 |
+
candidates_text = state["candidates"][0]["raw"] if state["candidates"] else "No candidates found"
|
| 850 |
+
|
| 851 |
+
return (
|
| 852 |
+
state,
|
| 853 |
+
f"## ๐ Discovery Results (New Search)\n\n{candidates_text}",
|
| 854 |
+
gr.update(visible=True),
|
| 855 |
+
gr.update(visible=True),
|
| 856 |
+
gr.update(visible=False),
|
| 857 |
+
gr.update(visible=False),
|
| 858 |
+
"",
|
| 859 |
+
gr.update(visible=False)
|
| 860 |
+
)
|
| 861 |
+
|
| 862 |
+
def handle_checkpoint2_approve(state, progress=gr.Progress()):
|
| 863 |
+
"""Handle approval at checkpoint 2 - proceed to writing"""
|
| 864 |
+
def update_progress(msg):
|
| 865 |
+
progress(0.6, desc=msg)
|
| 866 |
+
|
| 867 |
+
state = run_writer(state, update_progress)
|
| 868 |
+
|
| 869 |
+
if state["stage"] == WorkflowStage.ERROR.value:
|
| 870 |
+
return (
|
| 871 |
+
state,
|
| 872 |
+
f"โ **Error:** {state['error_message']}",
|
| 873 |
+
gr.update(visible=False),
|
| 874 |
+
gr.update(visible=False),
|
| 875 |
+
gr.update(visible=False),
|
| 876 |
+
gr.update(visible=True),
|
| 877 |
+
"",
|
| 878 |
+
gr.update(visible=False)
|
| 879 |
+
)
|
| 880 |
+
|
| 881 |
+
progress(0.8, desc="Evaluating draft...")
|
| 882 |
+
state = run_critic(state, lambda x: progress(0.8, desc=x))
|
| 883 |
+
|
| 884 |
+
draft = state.get("draft_summary", "")
|
| 885 |
+
score = state.get("quality_score", 0)
|
| 886 |
+
feedback = state.get("critic_feedback", "")
|
| 887 |
+
|
| 888 |
+
return (
|
| 889 |
+
state,
|
| 890 |
+
f"## โ๏ธ Draft Post\n\n{draft}\n\n---\n\n**Quality Score:** {score}/10\n\n**Feedback:**\n{feedback}",
|
| 891 |
+
gr.update(visible=True), # Finalize
|
| 892 |
+
gr.update(visible=True), # Edit/Rewrite
|
| 893 |
+
gr.update(visible=False),
|
| 894 |
+
gr.update(visible=False),
|
| 895 |
+
"",
|
| 896 |
+
gr.update(visible=True) # Show edit instructions
|
| 897 |
+
)
|
| 898 |
+
|
| 899 |
+
def handle_checkpoint2_different(state, progress=gr.Progress()):
|
| 900 |
+
"""Request different story - re-run curator"""
|
| 901 |
+
def update_progress(msg):
|
| 902 |
+
progress(0.5, desc=msg)
|
| 903 |
+
|
| 904 |
+
state = run_curator(state, update_progress)
|
| 905 |
+
|
| 906 |
+
if state["stage"] == WorkflowStage.ERROR.value:
|
| 907 |
+
return (
|
| 908 |
+
state,
|
| 909 |
+
f"โ **Error:** {state['error_message']}",
|
| 910 |
+
gr.update(visible=False),
|
| 911 |
+
gr.update(visible=False),
|
| 912 |
+
gr.update(visible=False),
|
| 913 |
+
gr.update(visible=True),
|
| 914 |
+
"",
|
| 915 |
+
gr.update(visible=False)
|
| 916 |
+
)
|
| 917 |
+
|
| 918 |
+
story_text = state["selected_story"]["raw"] if state["selected_story"] else "No story selected"
|
| 919 |
+
|
| 920 |
+
return (
|
| 921 |
+
state,
|
| 922 |
+
f"## ๐ฏ Selected Story (Alternative)\n\n{story_text}",
|
| 923 |
+
gr.update(visible=True),
|
| 924 |
+
gr.update(visible=True),
|
| 925 |
+
gr.update(visible=False),
|
| 926 |
+
gr.update(visible=False),
|
| 927 |
+
"",
|
| 928 |
+
gr.update(visible=False)
|
| 929 |
+
)
|
| 930 |
+
|
| 931 |
+
def handle_checkpoint3_finalize(state, progress=gr.Progress()):
|
| 932 |
+
"""Finalize and save the post"""
|
| 933 |
+
def update_progress(msg):
|
| 934 |
+
progress(0.9, desc=msg)
|
| 935 |
+
|
| 936 |
+
state = run_finalize(state, update_progress)
|
| 937 |
+
|
| 938 |
+
draft = state.get("draft_summary", "")
|
| 939 |
+
|
| 940 |
+
return (
|
| 941 |
+
state,
|
| 942 |
+
f"## โ
Post Saved Successfully!\n\n{draft}\n\n---\n\n**Status:** Saved to database\n**Quality Score:** {state.get('quality_score', 0)}/10",
|
| 943 |
+
gr.update(visible=False),
|
| 944 |
+
gr.update(visible=False),
|
| 945 |
+
gr.update(visible=False),
|
| 946 |
+
gr.update(visible=True), # Show restart
|
| 947 |
+
"",
|
| 948 |
+
gr.update(visible=False)
|
| 949 |
+
)
|
| 950 |
+
|
| 951 |
+
def handle_checkpoint3_edit(state, edit_instructions, progress=gr.Progress()):
|
| 952 |
+
"""Edit the draft based on instructions"""
|
| 953 |
+
if not edit_instructions:
|
| 954 |
+
return (
|
| 955 |
+
state,
|
| 956 |
+
"โ ๏ธ Please provide edit instructions",
|
| 957 |
+
gr.update(visible=True),
|
| 958 |
+
gr.update(visible=True),
|
| 959 |
+
gr.update(visible=False),
|
| 960 |
+
gr.update(visible=False),
|
| 961 |
+
edit_instructions,
|
| 962 |
+
gr.update(visible=True)
|
| 963 |
+
)
|
| 964 |
+
|
| 965 |
+
# Add edit instructions to state for writer to use
|
| 966 |
+
if "critic_feedback" in state:
|
| 967 |
+
state["critic_feedback"] += f"\n\nUser edit request: {edit_instructions}"
|
| 968 |
+
|
| 969 |
+
def update_progress(msg):
|
| 970 |
+
progress(0.6, desc=msg)
|
| 971 |
+
|
| 972 |
+
state = run_writer(state, update_progress)
|
| 973 |
+
|
| 974 |
+
if state["stage"] == WorkflowStage.ERROR.value:
|
| 975 |
+
return (
|
| 976 |
+
state,
|
| 977 |
+
f"โ **Error:** {state['error_message']}",
|
| 978 |
+
gr.update(visible=False),
|
| 979 |
+
gr.update(visible=False),
|
| 980 |
+
gr.update(visible=False),
|
| 981 |
+
gr.update(visible=True),
|
| 982 |
+
"",
|
| 983 |
+
gr.update(visible=False)
|
| 984 |
+
)
|
| 985 |
+
|
| 986 |
+
progress(0.8, desc="Re-evaluating draft...")
|
| 987 |
+
state = run_critic(state, lambda x: progress(0.8, desc=x))
|
| 988 |
+
|
| 989 |
+
draft = state.get("draft_summary", "")
|
| 990 |
+
score = state.get("quality_score", 0)
|
| 991 |
+
feedback = state.get("critic_feedback", "")
|
| 992 |
+
|
| 993 |
+
return (
|
| 994 |
+
state,
|
| 995 |
+
f"## โ๏ธ Revised Draft\n\n{draft}\n\n---\n\n**Quality Score:** {score}/10\n\n**Feedback:**\n{feedback}",
|
| 996 |
+
gr.update(visible=True),
|
| 997 |
+
gr.update(visible=True),
|
| 998 |
+
gr.update(visible=False),
|
| 999 |
+
gr.update(visible=False),
|
| 1000 |
+
"",
|
| 1001 |
+
gr.update(visible=True)
|
| 1002 |
+
)
|
| 1003 |
+
|
| 1004 |
+
def restart_workflow():
|
| 1005 |
+
"""Reset everything to start fresh"""
|
| 1006 |
+
return (
|
| 1007 |
+
None,
|
| 1008 |
+
"๐ Ready to start! Enter a topic and click 'Start Discovery'",
|
| 1009 |
+
gr.update(visible=False),
|
| 1010 |
+
gr.update(visible=False),
|
| 1011 |
+
gr.update(visible=True), # Show start button
|
| 1012 |
+
gr.update(visible=False),
|
| 1013 |
+
"",
|
| 1014 |
+
gr.update(visible=False)
|
| 1015 |
+
)
|
| 1016 |
+
|
| 1017 |
+
# --- 10. BUILD GRADIO UI ---
|
| 1018 |
+
|
| 1019 |
+
css = """
|
| 1020 |
+
.output-box {
|
| 1021 |
+
min-height: 400px;
|
| 1022 |
+
max-height: 600px;
|
| 1023 |
+
overflow-y: auto;
|
| 1024 |
+
padding: 20px;
|
| 1025 |
+
border-radius: 8px;
|
| 1026 |
+
background: #f8f9fa;
|
| 1027 |
+
}
|
| 1028 |
+
.button-row {
|
| 1029 |
+
display: flex;
|
| 1030 |
+
gap: 10px;
|
| 1031 |
+
margin-top: 10px;
|
| 1032 |
+
}
|
| 1033 |
+
.status-badge {
|
| 1034 |
+
display: inline-block;
|
| 1035 |
+
padding: 5px 10px;
|
| 1036 |
+
border-radius: 5px;
|
| 1037 |
+
font-weight: bold;
|
| 1038 |
+
margin-bottom: 10px;
|
| 1039 |
+
}
|
| 1040 |
+
"""
|
| 1041 |
+
|
| 1042 |
+
with gr.Blocks(css=css, title="Tuesday Trivia Agent", theme=gr.themes.Soft()) as demo:
|
| 1043 |
+
gr.Markdown("""
|
| 1044 |
+
# ๐งช Tuesday Trivia Multi-Agent System
|
| 1045 |
+
|
| 1046 |
+
An intelligent workflow for discovering, curating, and writing science trivia posts with human oversight at key checkpoints.
|
| 1047 |
+
|
| 1048 |
+
**โ๏ธ Cloud Storage:** Posts are automatically synced to Hugging Face Datasets for persistence.
|
| 1049 |
+
""")
|
| 1050 |
+
|
| 1051 |
+
# State
|
| 1052 |
+
state = gr.State()
|
| 1053 |
+
|
| 1054 |
+
with gr.Row():
|
| 1055 |
+
with gr.Column(scale=2):
|
| 1056 |
+
# Main output area
|
| 1057 |
+
output_display = gr.Markdown("๐ Ready to start! Enter a topic and click 'Start Discovery'", elem_classes="output-box")
|
| 1058 |
+
|
| 1059 |
+
with gr.Column(scale=1):
|
| 1060 |
+
# Control panel
|
| 1061 |
+
gr.Markdown("### ๐ฎ Control Panel")
|
| 1062 |
+
|
| 1063 |
+
topic_input = gr.Textbox(
|
| 1064 |
+
label="Search Topic",
|
| 1065 |
+
placeholder="e.g., quantum computing, biotechnology, space exploration",
|
| 1066 |
+
value="general science"
|
| 1067 |
+
)
|
| 1068 |
+
|
| 1069 |
+
with gr.Row():
|
| 1070 |
+
start_btn = gr.Button("๐ Start Discovery", variant="primary", visible=True)
|
| 1071 |
+
restart_btn = gr.Button("๐ Start New", variant="secondary", visible=False)
|
| 1072 |
+
|
| 1073 |
+
gr.Markdown("---")
|
| 1074 |
+
gr.Markdown("### ๐ Decision Points")
|
| 1075 |
+
|
| 1076 |
+
# Checkpoint buttons (hidden initially)
|
| 1077 |
+
approve_btn = gr.Button("โ
Approve", variant="primary", visible=False)
|
| 1078 |
+
reject_btn = gr.Button("โ Reject / Different", variant="stop", visible=False)
|
| 1079 |
+
|
| 1080 |
+
edit_instructions = gr.Textbox(
|
| 1081 |
+
label="Edit Instructions (optional)",
|
| 1082 |
+
placeholder="Provide specific instructions for changes...",
|
| 1083 |
+
visible=False,
|
| 1084 |
+
lines=3
|
| 1085 |
+
)
|
| 1086 |
+
|
| 1087 |
+
# Add sync status
|
| 1088 |
+
with gr.Accordion("โ๏ธ Cloud Sync Status", open=False):
|
| 1089 |
+
sync_status = gr.Markdown(f"""
|
| 1090 |
+
**HF Dataset:** `{HF_DATASET_REPO}`
|
| 1091 |
+
**Status:** {'โ
Connected' if HF_TOKEN else 'โ Not configured'}
|
| 1092 |
+
|
| 1093 |
+
Posts are automatically synced to Hugging Face Datasets after saving.
|
| 1094 |
+
""")
|
| 1095 |
+
|
| 1096 |
+
manual_sync_btn = gr.Button("๐ Manual Sync to HF", size="sm")
|
| 1097 |
+
|
| 1098 |
+
gr.Markdown("---")
|
| 1099 |
+
gr.Markdown("""
|
| 1100 |
+
### โน๏ธ Instructions
|
| 1101 |
+
|
| 1102 |
+
**Workflow Steps:**
|
| 1103 |
+
1. Enter topic โ Start Discovery
|
| 1104 |
+
2. Review candidates โ Approve/Reject
|
| 1105 |
+
3. Review story selection โ Approve/Different
|
| 1106 |
+
4. Review draft โ Finalize/Edit
|
| 1107 |
+
5. Post saved to database & HF!
|
| 1108 |
+
|
| 1109 |
+
**Cloud Storage:**
|
| 1110 |
+
- All posts auto-sync to HF Dataset
|
| 1111 |
+
- Survives Space restarts
|
| 1112 |
+
- Access your data anytime
|
| 1113 |
+
|
| 1114 |
+
**Tips:**
|
| 1115 |
+
- Be specific with topics for better results
|
| 1116 |
+
- Edit instructions help refine drafts
|
| 1117 |
+
- All posts are saved with quality scores
|
| 1118 |
+
""")
|
| 1119 |
+
|
| 1120 |
+
# Manual sync function
|
| 1121 |
+
def manual_sync():
|
| 1122 |
+
result = sync_to_hf()
|
| 1123 |
+
if result:
|
| 1124 |
+
return "โ
Successfully synced to Hugging Face Dataset!"
|
| 1125 |
+
else:
|
| 1126 |
+
return "โ ๏ธ Sync failed or no data to sync. Check logs."
|
| 1127 |
+
|
| 1128 |
+
manual_sync_btn.click(
|
| 1129 |
+
fn=manual_sync,
|
| 1130 |
+
outputs=sync_status
|
| 1131 |
+
)
|
| 1132 |
+
|
| 1133 |
+
# Event handlers
|
| 1134 |
+
start_btn.click(
|
| 1135 |
+
fn=start_workflow,
|
| 1136 |
+
inputs=[topic_input],
|
| 1137 |
+
outputs=[state, output_display, approve_btn, reject_btn, start_btn, restart_btn, edit_instructions, edit_instructions]
|
| 1138 |
+
)
|
| 1139 |
+
|
| 1140 |
+
# Checkpoint 1 handlers
|
| 1141 |
+
def route_checkpoint1_approve(s):
|
| 1142 |
+
if s and s.get("stage") == WorkflowStage.CHECKPOINT_1.value:
|
| 1143 |
+
return handle_checkpoint1_approve(s)
|
| 1144 |
+
return s, "Invalid state", gr.update(), gr.update(), gr.update(), gr.update(), "", gr.update()
|
| 1145 |
+
|
| 1146 |
+
def route_checkpoint1_reject(s):
|
| 1147 |
+
if s and s.get("stage") == WorkflowStage.CHECKPOINT_1.value:
|
| 1148 |
+
return handle_checkpoint1_reject(s)
|
| 1149 |
+
return s, "Invalid state", gr.update(), gr.update(), gr.update(), gr.update(), "", gr.update()
|
| 1150 |
+
|
| 1151 |
+
# Checkpoint 2 handlers
|
| 1152 |
+
def route_checkpoint2_approve(s):
|
| 1153 |
+
if s and s.get("stage") == WorkflowStage.CHECKPOINT_2.value:
|
| 1154 |
+
return handle_checkpoint2_approve(s)
|
| 1155 |
+
return s, "Invalid state", gr.update(), gr.update(), gr.update(), gr.update(), "", gr.update()
|
| 1156 |
+
|
| 1157 |
+
def route_checkpoint2_different(s):
|
| 1158 |
+
if s and s.get("stage") == WorkflowStage.CHECKPOINT_2.value:
|
| 1159 |
+
return handle_checkpoint2_different(s)
|
| 1160 |
+
return s, "Invalid state", gr.update(), gr.update(), gr.update(), gr.update(), "", gr.update()
|
| 1161 |
+
|
| 1162 |
+
# Checkpoint 3 handlers
|
| 1163 |
+
def route_checkpoint3_finalize(s):
|
| 1164 |
+
if s and s.get("stage") == WorkflowStage.CHECKPOINT_3.value:
|
| 1165 |
+
return handle_checkpoint3_finalize(s)
|
| 1166 |
+
return s, "Invalid state", gr.update(), gr.update(), gr.update(), gr.update(), "", gr.update()
|
| 1167 |
+
|
| 1168 |
+
def route_checkpoint3_edit(s, instructions):
|
| 1169 |
+
if s and s.get("stage") == WorkflowStage.CHECKPOINT_3.value:
|
| 1170 |
+
return handle_checkpoint3_edit(s, instructions)
|
| 1171 |
+
return s, "Invalid state", gr.update(), gr.update(), gr.update(), gr.update(), instructions, gr.update()
|
| 1172 |
+
|
| 1173 |
+
# Wire up the routing based on current stage
|
| 1174 |
+
def smart_approve(s):
|
| 1175 |
+
if not s:
|
| 1176 |
+
return s, "No active workflow", gr.update(), gr.update(), gr.update(), gr.update(), "", gr.update()
|
| 1177 |
+
|
| 1178 |
+
stage = s.get("stage")
|
| 1179 |
+
if stage == WorkflowStage.CHECKPOINT_1.value:
|
| 1180 |
+
return route_checkpoint1_approve(s)
|
| 1181 |
+
elif stage == WorkflowStage.CHECKPOINT_2.value:
|
| 1182 |
+
return route_checkpoint2_approve(s)
|
| 1183 |
+
elif stage == WorkflowStage.CHECKPOINT_3.value:
|
| 1184 |
+
return route_checkpoint3_finalize(s)
|
| 1185 |
+
|
| 1186 |
+
return s, "Invalid stage for approval", gr.update(), gr.update(), gr.update(), gr.update(), "", gr.update()
|
| 1187 |
+
|
| 1188 |
+
def smart_reject(s):
|
| 1189 |
+
if not s:
|
| 1190 |
+
return s, "No active workflow", gr.update(), gr.update(), gr.update(), gr.update(), "", gr.update()
|
| 1191 |
+
|
| 1192 |
+
stage = s.get("stage")
|
| 1193 |
+
if stage == WorkflowStage.CHECKPOINT_1.value:
|
| 1194 |
+
return route_checkpoint1_reject(s)
|
| 1195 |
+
elif stage == WorkflowStage.CHECKPOINT_2.value:
|
| 1196 |
+
return route_checkpoint2_different(s)
|
| 1197 |
+
elif stage == WorkflowStage.CHECKPOINT_3.value:
|
| 1198 |
+
return route_checkpoint3_edit(s, "")
|
| 1199 |
+
|
| 1200 |
+
return s, "Invalid stage for rejection", gr.update(), gr.update(), gr.update(), gr.update(), "", gr.update()
|
| 1201 |
+
|
| 1202 |
+
approve_btn.click(
|
| 1203 |
+
fn=smart_approve,
|
| 1204 |
+
inputs=[state],
|
| 1205 |
+
outputs=[state, output_display, approve_btn, reject_btn, start_btn, restart_btn, edit_instructions, edit_instructions]
|
| 1206 |
+
)
|
| 1207 |
+
|
| 1208 |
+
reject_btn.click(
|
| 1209 |
+
fn=smart_reject,
|
| 1210 |
+
inputs=[state],
|
| 1211 |
+
outputs=[state, output_display, approve_btn, reject_btn, start_btn, restart_btn, edit_instructions, edit_instructions]
|
| 1212 |
+
)
|
| 1213 |
+
|
| 1214 |
+
restart_btn.click(
|
| 1215 |
+
fn=restart_workflow,
|
| 1216 |
+
outputs=[state, output_display, approve_btn, reject_btn, start_btn, restart_btn, edit_instructions, edit_instructions]
|
| 1217 |
+
)
|
| 1218 |
+
|
| 1219 |
+
if __name__ == "__main__":
|
| 1220 |
demo.launch()
|