Spaces:
Runtime error
Runtime error
File size: 62,353 Bytes
33523ce b1935a9 e68ca61 20172b4 33523ce cd53598 33523ce cd53598 20172b4 664f2bb cd53598 b1935a9 664f2bb cd53598 20172b4 cd53598 664f2bb cd53598 20172b4 cd53598 664f2bb cd53598 20172b4 664f2bb cd53598 20172b4 cd53598 664f2bb cd53598 8a0361c cd53598 664f2bb cd53598 b1935a9 664f2bb cd53598 b1935a9 664f2bb cd53598 b1935a9 cd53598 664f2bb cd53598 b1935a9 cd53598 664f2bb cd53598 b1935a9 cd53598 e68ca61 664f2bb cd53598 664f2bb c2f1156 cd53598 664f2bb cd53598 c2f1156 cd53598 c2f1156 664f2bb cd53598 664f2bb c2f1156 cd53598 664f2bb c2f1156 cd53598 664f2bb c2f1156 cd53598 c2f1156 cd53598 c2f1156 cd53598 c2f1156 cd53598 c2f1156 cd53598 c2f1156 cd53598 c2f1156 cd53598 c2f1156 cd53598 c2f1156 cd53598 c2f1156 cd53598 c2f1156 cd53598 c2f1156 cd53598 c2f1156 cd53598 c2f1156 cd53598 c2f1156 cd53598 c2f1156 cd53598 c2f1156 0b3b616 20172b4 c2f1156 0b3b616 c2f1156 0b3b616 c2f1156 20172b4 c2f1156 0b3b616 c2f1156 0b3b616 c2f1156 0b3b616 c2f1156 0b3b616 c2f1156 cd53598 c2f1156 0b3b616 c2f1156 0b3b616 c2f1156 0b3b616 c2f1156 0b3b616 c2f1156 0b3b616 c2f1156 0b3b616 c2f1156 0b3b616 c2f1156 0b3b616 c2f1156 0b3b616 c2f1156 0b3b616 c2f1156 0b3b616 c2f1156 0b3b616 c2f1156 0b3b616 c2f1156 20172b4 c2f1156 0b3b616 c2f1156 0b3b616 c2f1156 0b3b616 c2f1156 0b3b616 c2f1156 0b3b616 c2f1156 0b3b616 c2f1156 0b3b616 c2f1156 0b3b616 c2f1156 0b3b616 c2f1156 0b3b616 c2f1156 0b3b616 c2f1156 0b3b616 c2f1156 0b3b616 c2f1156 0b3b616 c2f1156 0b3b616 c2f1156 0b3b616 c2f1156 0b3b616 c2f1156 0b3b616 c2f1156 0b3b616 c2f1156 0b3b616 c2f1156 0b3b616 c2f1156 0b3b616 c2f1156 0b3b616 c2f1156 cd53598 c2f1156 cd53598 c2f1156 cd53598 c2f1156 cd53598 b1935a9 20172b4 cd53598 20172b4 cd53598 20172b4 cd53598 20172b4 cd53598 e68ca61 cd53598 20172b4 cd53598 20172b4 cd53598 20172b4 2d0be37 cd53598 c2f1156 cd53598 e68ca61 cd53598 c2f1156 cd53598 b1935a9 cd53598 c2f1156 b1935a9 cd53598 b1935a9 20172b4 b1935a9 cd53598 b1935a9 cd53598 b1935a9 cd53598 2d0be37 b1935a9 cd53598 20172b4 cd53598 20172b4 cd53598 b1935a9 c2f1156 20172b4 cd53598 b1935a9 c2f1156 e68ca61 b1935a9 20172b4 e68ca61 c2f1156 e68ca61 b1935a9 cd53598 b1935a9 2d0be37 cd53598 2d0be37 b1935a9 2d0be37 b1935a9 e68ca61 c2f1156 e68ca61 b1935a9 20172b4 e68ca61 c2f1156 e68ca61 b1935a9 cd53598 e68ca61 c2f1156 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 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 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 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 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 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 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 791 792 793 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 852 853 854 855 856 857 858 859 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 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 |
import gradio as gr
import numpy as np
import pandas as pd
import torch
import matplotlib.pyplot as plt
import json
import time
import os
from functools import partial
import datetime
import plotly.graph_objects as go
from plotly.subplots import make_subplots
# AI Snipper Custom CSS
ai_snipper_css = """
/* AI Snipper Color Variables */
:root {
--ai-cyan: #06b6d4;
--ai-blue: #3b82f6;
--ai-purple: #8b5cf6;
--ai-teal: #14b8a6;
--ai-indigo: #6366f1;
--bg-primary: #0a0e16;
--bg-secondary: #1a2332;
--bg-tertiary: #2a3441;
--bg-card: #1e293b;
--bg-card-hover: #334155;
--text-primary: #ffffff;
--text-secondary: #e2e8f0;
--text-muted: #94a3b8;
--text-accent: #06b6d4;
--border-primary: #475569;
--border-accent: #06b6d4;
--gradient-primary: linear-gradient(135deg, #06b6d4, #3b82f6, #8b5cf6);
--gradient-secondary: linear-gradient(135deg, #0a0e16 0%, #1a2332 50%, #2a3441 100%);
--gradient-button: linear-gradient(135deg, #06b6d4, #3b82f6);
--gradient-card: linear-gradient(135deg, #1e293b 0%, #334155 100%);
}
/* Main container styling */
.gradio-container {
background: var(--gradient-secondary) !important;
color: var(--text-primary) !important;
font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif !important;
min-height: 100vh !important;
}
/* Force text visibility */
.gradio-container * {
color: inherit !important;
}
/* Header styling */
.gradio-container h1 {
background: var(--gradient-primary) !important;
-webkit-background-clip: text !important;
-webkit-text-fill-color: transparent !important;
background-clip: text !important;
text-align: center !important;
font-size: 3rem !important;
font-weight: 800 !important;
margin-bottom: 1rem !important;
text-shadow: 0 0 20px rgba(6, 182, 212, 0.3) !important;
}
.gradio-container h2, .gradio-container h3, .gradio-container h4, .gradio-container h5, .gradio-container h6 {
color: var(--text-primary) !important;
font-weight: 600 !important;
}
/* Card/Panel styling */
.gr-box, .gr-form, .gr-panel {
background: var(--gradient-card) !important;
border: 1px solid var(--border-primary) !important;
border-radius: 16px !important;
box-shadow: 0 10px 15px -3px rgb(0 0 0 / 0.1) !important;
backdrop-filter: blur(10px) !important;
color: var(--text-primary) !important;
}
/* Group styling */
.gr-group {
background: var(--gradient-card) !important;
border: 1px solid var(--border-primary) !important;
border-radius: 12px !important;
padding: 1.5rem !important;
color: var(--text-primary) !important;
}
/* Text inputs - FIXED */
.gr-textbox textarea, .gr-textbox input {
background: var(--bg-secondary) !important;
border: 1px solid var(--border-primary) !important;
border-radius: 12px !important;
color: var(--text-primary) !important;
padding: 1rem !important;
font-family: inherit !important;
}
.gr-textbox textarea:focus, .gr-textbox input:focus {
border-color: var(--border-accent) !important;
box-shadow: 0 0 0 3px rgba(6, 182, 212, 0.1) !important;
outline: none !important;
color: var(--text-primary) !important;
}
.gr-textbox textarea::placeholder, .gr-textbox input::placeholder {
color: var(--text-muted) !important;
opacity: 0.8 !important;
}
/* Labels - FIXED */
.gr-textbox label, .gr-slider label, .gr-radio label, .gr-checkbox label, label {
color: var(--text-secondary) !important;
font-weight: 500 !important;
margin-bottom: 0.5rem !important;
}
/* All text elements */
p, span, div:not(.gr-button), .gr-markdown {
color: var(--text-secondary) !important;
}
/* Strong contrast for important text */
.gr-textbox label, .gr-form label {
color: var(--text-primary) !important;
font-weight: 600 !important;
}
/* Buttons */
.gr-button {
background: var(--gradient-button) !important;
border: none !important;
border-radius: 12px !important;
color: var(--text-primary) !important;
font-weight: 600 !important;
padding: 1rem 2rem !important;
transition: all 0.3s ease !important;
box-shadow: 0 4px 6px -1px rgb(0 0 0 / 0.1) !important;
font-family: inherit !important;
}
.gr-button:hover {
transform: translateY(-2px) !important;
box-shadow: 0 10px 20px rgba(6, 182, 212, 0.3) !important;
filter: brightness(1.1) !important;
color: var(--text-primary) !important;
}
.gr-button.secondary {
background: var(--bg-card) !important;
color: var(--text-primary) !important;
border: 1px solid var(--border-primary) !important;
}
.gr-button.secondary:hover {
background: var(--bg-card-hover) !important;
border-color: var(--border-accent) !important;
color: var(--text-primary) !important;
}
/* Tabs - FIXED */
.gr-tab-nav {
background: var(--gradient-card) !important;
border-radius: 12px !important;
padding: 0.5rem !important;
}
.gr-tab-nav button {
background: transparent !important;
color: var(--text-secondary) !important;
border: none !important;
border-radius: 8px !important;
margin: 0 4px !important;
padding: 0.75rem 1.5rem !important;
transition: all 0.3s ease !important;
font-weight: 500 !important;
}
.gr-tab-nav button.selected {
background: var(--gradient-button) !important;
color: var(--text-primary) !important;
box-shadow: 0 2px 4px rgba(6, 182, 212, 0.3) !important;
}
.gr-tab-nav button:hover:not(.selected) {
background: var(--bg-card-hover) !important;
color: var(--text-primary) !important;
}
/* Tab content */
.gr-tabitem {
background: var(--gradient-card) !important;
border: 1px solid var(--border-primary) !important;
border-radius: 12px !important;
padding: 1.5rem !important;
margin-top: 1rem !important;
color: var(--text-primary) !important;
}
/* Audio and file upload */
.gr-audio, .gr-file-upload {
background: var(--gradient-card) !important;
border: 1px solid var(--border-primary) !important;
border-radius: 12px !important;
color: var(--text-secondary) !important;
}
.gr-file-upload {
border: 2px dashed var(--border-accent) !important;
}
/* Sliders */
.gr-slider input[type="range"] {
background: var(--bg-secondary) !important;
}
.gr-slider input[type="range"]::-webkit-slider-track {
background: var(--bg-secondary) !important;
border-radius: 6px !important;
}
.gr-slider input[type="range"]::-webkit-slider-thumb {
background: var(--gradient-button) !important;
border: none !important;
border-radius: 50% !important;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2) !important;
}
/* Radio buttons and checkboxes */
.gr-radio input[type="radio"], .gr-checkbox input[type="checkbox"] {
accent-color: var(--ai-cyan) !important;
}
.gr-radio .wrap, .gr-checkbox .wrap {
color: var(--text-secondary) !important;
}
/* Accordion */
.gr-accordion {
background: var(--gradient-card) !important;
border: 1px solid var(--border-primary) !important;
border-radius: 12px !important;
}
.gr-accordion summary {
background: var(--bg-card) !important;
color: var(--text-primary) !important;
padding: 1rem !important;
border-radius: 12px !important;
cursor: pointer !important;
font-weight: 600 !important;
}
.gr-accordion[open] summary {
border-bottom: 1px solid var(--border-primary) !important;
border-radius: 12px 12px 0 0 !important;
}
/* JSON and HTML output */
.gr-json, .gr-html {
background: var(--gradient-card) !important;
border: 1px solid var(--border-primary) !important;
border-radius: 12px !important;
color: var(--text-primary) !important;
padding: 1rem !important;
}
/* Plot containers */
.gr-plot {
background: var(--gradient-card) !important;
border: 1px solid var(--border-primary) !important;
border-radius: 12px !important;
padding: 1rem !important;
}
/* Progress bars */
.gr-progress {
background: var(--bg-secondary) !important;
border-radius: 6px !important;
}
.gr-progress-bar {
background: var(--gradient-button) !important;
border-radius: 6px !important;
}
/* Markdown content */
.gr-markdown, .gr-markdown * {
color: var(--text-secondary) !important;
}
.gr-markdown h1, .gr-markdown h2, .gr-markdown h3, .gr-markdown h4, .gr-markdown h5, .gr-markdown h6 {
color: var(--text-primary) !important;
font-weight: 600 !important;
}
/* Footer hiding */
footer {
visibility: hidden !important;
}
/* Scrollbars */
::-webkit-scrollbar {
width: 8px;
height: 8px;
}
::-webkit-scrollbar-track {
background: var(--bg-secondary);
border-radius: 4px;
}
::-webkit-scrollbar-thumb {
background: var(--gradient-button);
border-radius: 4px;
}
::-webkit-scrollbar-thumb:hover {
background: var(--ai-cyan);
}
/* Specific fixes for form elements */
.gr-form .gr-box, .gr-form .gr-group {
color: var(--text-primary) !important;
}
/* Ensure all children inherit proper colors */
.gr-form *, .gr-group *, .gr-box * {
color: inherit !important;
}
/* Additional text visibility fixes */
.block.svelte-1t38q2d {
color: var(--text-primary) !important;
}
/* Mobile responsiveness */
@media (max-width: 768px) {
.gradio-container h1 {
font-size: 2rem !important;
}
.gr-button {
width: 100% !important;
justify-content: center !important;
}
.gr-row {
flex-direction: column !important;
}
}
"""
"""
# Global variables to store models
tokenizer = None
ner_pipeline = None
pos_pipeline = None
intent_classifier = None
semantic_model = None
stt_model = None # Speech-to-text model
models_loaded = False
# Database to store keyword ranking history (in-memory database for this example)
# In a real app, you would use a proper database
ranking_history = {}
def load_models(progress=gr.Progress()):
"""Lazy-load models only when needed"""
global tokenizer, ner_pipeline, pos_pipeline, intent_classifier, semantic_model, stt_model, models_loaded
if models_loaded:
return True
try:
progress(0.1, desc="Loading models...")
# Use smaller models and load them sequentially to reduce memory pressure
from transformers import AutoTokenizer, pipeline
progress(0.2, desc="Loading tokenizer...")
tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
progress(0.3, desc="Loading NER model...")
ner_pipeline = pipeline("ner", model="dslim/bert-base-NER")
progress(0.4, desc="Loading POS model...")
# Use smaller POS model
from transformers import AutoModelForTokenClassification, BertTokenizerFast
pos_model = AutoModelForTokenClassification.from_pretrained("vblagoje/bert-english-uncased-finetuned-pos")
pos_tokenizer = BertTokenizerFast.from_pretrained("vblagoje/bert-english-uncased-finetuned-pos")
pos_pipeline = pipeline("token-classification", model=pos_model, tokenizer=pos_tokenizer)
progress(0.6, desc="Loading intent classifier...")
# Use a smaller model for zero-shot classification
intent_classifier = pipeline(
"zero-shot-classification",
model="typeform/distilbert-base-uncased-mnli", # Smaller than BART
device=0 if torch.cuda.is_available() else -1 # Use GPU if available
)
progress(0.7, desc="Loading speech-to-text model...")
try:
# Load automatic speech recognition model
from transformers import WhisperProcessor, WhisperForConditionalGeneration
processor = WhisperProcessor.from_pretrained("openai/whisper-small.en")
stt_model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-small.en")
stt_model = (processor, stt_model)
except Exception as e:
print(f"Warning: Could not load speech-to-text model: {str(e)}")
stt_model = None # Set to None so we can check if it's available
progress(0.8, desc="Loading semantic model...")
try:
from sentence_transformers import SentenceTransformer
semantic_model = SentenceTransformer('all-MiniLM-L6-v2')
except Exception as e:
print(f"Warning: Could not load semantic model: {str(e)}")
semantic_model = None # Set to None so we can check if it's available
progress(1.0, desc="Models loaded successfully!")
models_loaded = True
return True
except Exception as e:
print(f"Error loading models: {str(e)}")
return f"Error: {str(e)}"
def speech_to_text(audio_path):
"""Convert speech to text using the loaded speech-to-text model"""
if stt_model is None:
return "Speech-to-text model not loaded. Please try text input instead."
try:
import librosa
import numpy as np
# Load audio file
audio, sr = librosa.load(audio_path, sr=16000)
# Process audio with Whisper
processor, model = stt_model
input_features = processor(audio, sampling_rate=16000, return_tensors="pt").input_features
# Generate token ids
predicted_ids = model.generate(input_features)
# Decode token ids to text
transcription = processor.batch_decode(predicted_ids, skip_special_tokens=True)[0]
return transcription
except Exception as e:
print(f"Error in speech_to_text: {str(e)}")
return f"Error processing speech: {str(e)}"
def handle_voice_input(audio):
"""Handle voice input and convert to text"""
if audio is None:
return "No audio detected. Please try again."
try:
# Convert speech to text
text = speech_to_text(audio)
return text
except Exception as e:
print(f"Error in handle_voice_input: {str(e)}")
return f"Error: {str(e)}"
def simulate_google_serp(keyword, num_results=10):
"""Simulate Google SERP results for a keyword"""
try:
# In a real implementation, this would call the Google API
# For now, we'll generate fake SERP data
# Deterministic seed for consistent results by keyword
np.random.seed(sum(ord(c) for c in keyword))
serp_results = []
domains = [
"example.com", "wikipedia.org", "medium.com", "github.com",
"stackoverflow.com", "amazon.com", "youtube.com", "reddit.com",
"linkedin.com", "twitter.com", "facebook.com", "instagram.com"
]
for i in range(1, num_results + 1):
domain = domains[i % len(domains)]
title = f"{keyword.title()} - {domain.split('.')[0].title()} Resource #{i}"
snippet = f"This is a simulated SERP result for '{keyword}'. Result #{i} would provide relevant information about this topic."
url = f"https://www.{domain}/{keyword.replace(' ', '-')}-resource-{i}"
position = i
ctr = round(0.3 * (0.85 ** (i - 1)), 4) # Simulate click-through rate decay
serp_results.append({
"position": position,
"title": title,
"url": url,
"domain": domain,
"snippet": snippet,
"ctr_estimate": ctr,
"impressions_estimate": np.random.randint(1000, 10000)
})
return serp_results
except Exception as e:
print(f"Error in simulate_google_serp: {str(e)}")
return []
def update_ranking_history(keyword, serp_results):
"""Update the ranking history for a keyword"""
try:
# Get current timestamp
timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
# Initialize if keyword not in history
if keyword not in ranking_history:
ranking_history[keyword] = []
# Add new entry
ranking_history[keyword].append({
"timestamp": timestamp,
"results": serp_results[:5] # Store top 5 results for history
})
# Keep only last 10 entries for each keyword
if len(ranking_history[keyword]) > 10:
ranking_history[keyword] = ranking_history[keyword][-10:]
return True
except Exception as e:
print(f"Error in update_ranking_history: {str(e)}")
return False
def get_semantic_similarity(token, comparison_terms):
"""Calculate semantic similarity between a token and comparison terms"""
try:
from sklearn.metrics.pairwise import cosine_similarity
token_embedding = semantic_model.encode([token])[0]
comparison_embeddings = semantic_model.encode(comparison_terms)
similarities = []
for i, emb in enumerate(comparison_embeddings):
similarity = cosine_similarity([token_embedding], [emb])[0][0]
similarities.append((comparison_terms[i], float(similarity)))
return sorted(similarities, key=lambda x: x[1], reverse=True)
except Exception as e:
print(f"Error in semantic similarity: {str(e)}")
# Return dummy data on error
return [(term, 0.5) for term in comparison_terms]
def get_token_colors(token_type):
colors = {
"prefix": "#D8BFD8", # Light purple
"suffix": "#AEDAA4", # Light green
"stem": "#A4C2F4", # Light blue
"compound_first": "#FFCC80", # Light orange
"compound_second": "#FFCC80", # Light orange
"word": "#E5E5E5" # Light gray
}
return colors.get(token_type, "#E5E5E5")
def simulate_historical_data(token):
"""Generate simulated historical usage data for a token"""
eras = ["1900s", "1950s", "1980s", "2000s", "2010s", "Present"]
# Different patterns based on token characteristics
if len(token) > 8:
# Possibly a technical term - recent growth
values = [10, 20, 30, 60, 85, 95]
elif token.startswith(("un", "re", "de", "pre")):
# Prefix words tend to be older
values = [45, 50, 60, 70, 75, 80]
else:
# Standard pattern for common words
# Use token hash value modulo instead of hash() directly to avoid different results across runs
base = 50 + (sum(ord(c) for c in token) % 30)
# Use a fixed seed for reproducibility
np.random.seed(sum(ord(c) for c in token))
noise = np.random.normal(0, 5, 6)
values = [max(5, min(95, base + i*5 + n)) for i, n in enumerate(noise)]
return list(zip(eras, values))
def generate_origin_data(token):
"""Generate simulated origin/etymology data for a token"""
origins = [
{"era": "Ancient", "language": "Latin"},
{"era": "Ancient", "language": "Greek"},
{"era": "Medieval", "language": "Old English"},
{"era": "16th century", "language": "French"},
{"era": "18th century", "language": "Germanic"},
{"era": "19th century", "language": "Anglo-Saxon"},
{"era": "20th century", "language": "Modern English"}
]
# Deterministic selection based on the token
index = sum(ord(c) for c in token) % len(origins)
origin = origins[index]
note = f"First appeared in {origin['era']} texts derived from {origin['language']}."
origin["note"] = note
return origin
def analyze_token_types(tokens):
"""Identify token types (prefix, suffix, compound, etc.)"""
processed_tokens = []
prefixes = ["un", "re", "de", "pre", "post", "anti", "pro", "inter", "sub", "super"]
suffixes = ["ing", "ed", "ly", "ment", "tion", "able", "ible", "ness", "ful", "less"]
for token in tokens:
token_text = token.lower()
token_type = "word"
# Check for prefixes
for prefix in prefixes:
if token_text.startswith(prefix) and len(token_text) > len(prefix) + 2:
if token_text != prefix: # Make sure the word isn't just the prefix
token_type = "prefix"
break
# Check for suffixes
if token_type == "word":
for suffix in suffixes:
if token_text.endswith(suffix) and len(token_text) > len(suffix) + 2:
token_type = "suffix"
break
# Check for compound words (simplified)
if token_type == "word" and len(token_text) > 8:
token_type = "compound_first" # Simplified - in reality would need more analysis
processed_tokens.append({
"text": token_text,
"type": token_type
})
return processed_tokens
def plot_historical_data(historical_data):
"""Create a plot of historical usage data, with error handling"""
try:
eras = [item[0] for item in historical_data]
values = [item[1] for item in historical_data]
plt.figure(figsize=(8, 3))
plt.bar(eras, values, color='skyblue')
plt.title('Historical Usage')
plt.xlabel('Era')
plt.ylabel('Usage Level')
plt.ylim(0, 100)
plt.xticks(rotation=45)
plt.tight_layout()
return plt
except Exception as e:
print(f"Error in plot_historical_data: {str(e)}")
# Return a simple error plot
plt.figure(figsize=(8, 3))
plt.text(0.5, 0.5, f"Error creating plot: {str(e)}",
horizontalalignment='center', verticalalignment='center')
plt.axis('off')
return plt
def create_evolution_chart(data, forecast_months=6, growth_scenario="Moderate"):
"""Create a simpler chart that's more compatible with Gradio"""
try:
import plotly.graph_objects as go
# Create a basic figure without subplots
fig = go.Figure()
# Add main trace for search volume
fig.add_trace(
go.Scatter(
x=[item["month"] for item in data],
y=[item["searchVolume"] for item in data],
name="Search Volume",
line=dict(color="#8884d8", width=3),
mode="lines+markers"
)
)
# Scale the other metrics to be visible on the same chart
max_volume = max([item["searchVolume"] for item in data])
scale_factor = max_volume / 100
# Add competition score (scaled)
fig.add_trace(
go.Scatter(
x=[item["month"] for item in data],
y=[item["competitionScore"] * scale_factor for item in data],
name="Competition Score",
line=dict(color="#82ca9d", width=2, dash="dot"),
mode="lines+markers"
)
)
# Add intent clarity (scaled)
fig.add_trace(
go.Scatter(
x=[item["month"] for item in data],
y=[item["intentClarity"] * scale_factor for item in data],
name="Intent Clarity",
line=dict(color="#ffc658", width=2, dash="dash"),
mode="lines+markers"
)
)
# Simple layout
fig.update_layout(
title=f"Keyword Evolution Forecast ({growth_scenario} Growth)",
xaxis_title="Month",
yaxis_title="Value",
legend=dict(orientation="h", y=1.1),
height=500
)
return fig
except Exception as e:
print(f"Error in chart creation: {str(e)}")
# Fallback to an even simpler chart
fig = go.Figure(data=go.Scatter(x=[1, 2, 3], y=[4, 1, 2]))
fig.update_layout(title="Fallback Chart (Error occurred)")
return fig
def create_ranking_history_chart(keyword_history):
"""Create a chart showing keyword ranking history over time"""
try:
if not keyword_history or len(keyword_history) < 2:
# Not enough data for a meaningful chart
fig = go.Figure()
fig.update_layout(
title="Insufficient Ranking Data",
annotations=[{
"text": "Need at least 2 data points for ranking history",
"showarrow": False,
"font": {"size": 16},
"xref": "paper",
"yref": "paper",
"x": 0.5,
"y": 0.5
}]
)
return fig
# Create a figure
fig = go.Figure()
# Extract timestamps and convert to datetime objects
timestamps = [entry["timestamp"] for entry in keyword_history]
dates = [datetime.datetime.strptime(ts, "%Y-%m-%d %H:%M:%S") for ts in timestamps]
# Get unique domains from all results
all_domains = set()
for entry in keyword_history:
for result in entry["results"]:
all_domains.add(result["domain"])
# Colors for different domains
domain_colors = {}
color_palette = [
"#1f77b4", "#ff7f0e", "#2ca02c", "#d62728", "#9467bd",
"#8c564b", "#e377c2", "#7f7f7f", "#bcbd22", "#17becf"
]
for i, domain in enumerate(all_domains):
domain_colors[domain] = color_palette[i % len(color_palette)]
# Track domains and their positions over time
domain_tracking = {domain: {"x": [], "y": [], "text": []} for domain in all_domains}
for i, entry in enumerate(keyword_history):
for result in entry["results"]:
domain = result["domain"]
position = result["position"]
title = result["title"]
domain_tracking[domain]["x"].append(dates[i])
domain_tracking[domain]["y"].append(position)
domain_tracking[domain]["text"].append(title)
# Add traces for each domain
for domain, data in domain_tracking.items():
if len(data["x"]) > 0: # Only add domains that have data
fig.add_trace(
go.Scatter(
x=data["x"],
y=data["y"],
mode="lines+markers",
name=domain,
line=dict(color=domain_colors[domain]),
hovertemplate="%{text}<br>Position: %{y}<br>Date: %{x}<extra></extra>",
text=data["text"],
marker=dict(size=8)
)
)
# Update layout
fig.update_layout(
title="Keyword Ranking History",
xaxis_title="Date",
yaxis_title="Position",
yaxis=dict(autorange="reversed"), # Invert y-axis so position 1 is on top
hovermode="closest",
height=500
)
return fig
except Exception as e:
print(f"Error in create_ranking_history_chart: {str(e)}")
# Return fallback chart
fig = go.Figure()
fig.update_layout(
title="Error Creating Ranking Chart",
annotations=[{
"text": f"Error: {str(e)}",
"showarrow": False,
"font": {"size": 14},
"xref": "paper",
"yref": "paper",
"x": 0.5,
"y": 0.5
}]
)
return fig
def generate_serp_html(keyword, serp_results):
"""Generate HTML for SERP results"""
if not serp_results:
return '<div style="color: #e2e8f0; text-align: center; padding: 2rem;">No SERP results available</div>'
html = f"""
<div style="font-family: 'Inter', sans-serif; padding: 20px; background: linear-gradient(135deg, #1e293b 0%, #334155 100%); border: 1px solid #475569; border-radius: 16px; color: #ffffff;">
<h2 style="margin-top: 0; color: #ffffff; background: linear-gradient(135deg, #06b6d4, #3b82f6, #8b5cf6); -webkit-background-clip: text; -webkit-text-fill-color: transparent; background-clip: text;">SERP Results for "{keyword}"</h2>
<div style="background: rgba(6, 182, 212, 0.1); border: 1px solid #06b6d4; padding: 12px; border-radius: 8px; margin-bottom: 20px;">
<div style="color: #06b6d4; font-size: 12px; font-weight: 500;">๐ This is a simulated SERP. In a real application, this would use the Google API.</div>
</div>
<div class="serp-results" style="display: flex; flex-direction: column; gap: 16px;">
"""
for result in serp_results:
position = result["position"]
title = result["title"]
url = result["url"]
snippet = result["snippet"]
domain = result["domain"]
ctr = result["ctr_estimate"]
impressions = result["impressions_estimate"]
html += f"""
<div class="serp-result" style="padding: 18px; background: linear-gradient(135deg, #1e293b 0%, #334155 100%); border: 1px solid #475569; border-radius: 12px; position: relative; transition: all 0.3s ease;">
<div style="position: absolute; top: -12px; left: -12px; background: linear-gradient(135deg, #06b6d4, #3b82f6); color: white; width: 28px; height: 28px; border-radius: 50%; display: flex; align-items: center; justify-content: center; font-size: 12px; font-weight: 600; box-shadow: 0 4px 8px rgba(6, 182, 212, 0.3);">
{position}
</div>
<div style="margin-bottom: 8px;">
<a href="#" style="font-size: 18px; color: #06b6d4; text-decoration: none; font-weight: 600; line-height: 1.4;">{title}</a>
</div>
<div style="margin-bottom: 10px; color: #14b8a6; font-size: 14px; font-weight: 500;">{url}</div>
<div style="color: #e2e8f0; font-size: 14px; line-height: 1.5; margin-bottom: 12px;">{snippet}</div>
<div style="display: flex; gap: 20px; font-size: 12px; color: #94a3b8; padding-top: 8px; border-top: 1px solid #475569;">
<div style="display: flex; align-items: center; gap: 4px;">
<span style="font-weight: 600; color: #06b6d4;">CTR:</span>
<span>{ctr:.2%}</span>
</div>
<div style="display: flex; align-items: center; gap: 4px;">
<span style="font-weight: 600; color: #06b6d4;">Est. Impressions:</span>
<span>{impressions:,}</span>
</div>
</div>
</div>
"""
html += """
</div>
</div>
"""
return html
def generate_token_visualization_html(token_analysis, full_analysis):
"""Generate HTML for token visualization"""
html = """
<div style="font-family: 'Inter', sans-serif; padding: 24px; background: linear-gradient(135deg, #1e293b 0%, #334155 100%); border: 1px solid #475569; border-radius: 16px; color: #ffffff;">
<h2 style="margin-top: 0; color: #ffffff; background: linear-gradient(135deg, #06b6d4, #3b82f6, #8b5cf6); -webkit-background-clip: text; -webkit-text-fill-color: transparent; background-clip: text;">๐ฌ Token Visualization</h2>
<div style="margin-bottom: 24px; padding: 20px; background: rgba(6, 182, 212, 0.1); border: 1px solid #06b6d4; border-radius: 12px;">
<div style="margin-bottom: 12px; font-weight: 600; color: #06b6d4; display: flex; align-items: center; gap: 8px;">
<span>๐๏ธ</span> Human View:
</div>
<div style="display: flex; flex-wrap: wrap; gap: 10px;">
"""
# Add human view tokens
for token in token_analysis:
html += f"""
<div style="padding: 8px 16px; background: linear-gradient(135deg, #1e293b, #334155); border: 1px solid #475569; border-radius: 8px; color: #e2e8f0; font-weight: 500; transition: all 0.3s ease;">
{token['text']}
</div>
"""
html += """
</div>
</div>
<div style="text-align: center; margin: 20px 0;">
<span style="font-size: 24px; color: #06b6d4;">โ</span>
</div>
<div style="padding: 20px; background: rgba(20, 184, 166, 0.1); border: 1px solid #14b8a6; border-radius: 12px;">
<div style="margin-bottom: 12px; font-weight: 600; color: #14b8a6; display: flex; align-items: center; gap: 8px;">
<span>๐ค</span> Machine View:
</div>
<div style="display: flex; flex-wrap: wrap; gap: 10px;">
"""
# Add machine view tokens with enhanced styling
color_map = {
"prefix": "linear-gradient(135deg, #8b5cf6, #a855f7)",
"suffix": "linear-gradient(135deg, #10b981, #14b8a6)",
"stem": "linear-gradient(135deg, #3b82f6, #06b6d4)",
"compound_first": "linear-gradient(135deg, #f59e0b, #d97706)",
"compound_second": "linear-gradient(135deg, #f59e0b, #d97706)",
"word": "linear-gradient(135deg, #6b7280, #4b5563)"
}
for token in full_analysis:
bg_gradient = color_map.get(token["type"], color_map["word"])
html += f"""
<div style="padding: 10px 14px; background: {bg_gradient}; border: 1px solid rgba(255,255,255,0.2); border-radius: 8px; font-family: 'Fira Code', monospace; color: #ffffff; font-weight: 500; box-shadow: 0 2px 4px rgba(0,0,0,0.3); transition: all 0.3s ease;">
<div style="font-size: 14px;">{token['token']}</div>
<div style="font-size: 10px; opacity: 0.8; margin-top: 2px; text-transform: uppercase; letter-spacing: 0.5px;">{token['type']}</div>
</div>
"""
html += """
</div>
</div>
<div style="margin-top: 24px; display: grid; grid-template-columns: repeat(3, 1fr); gap: 16px;">
"""
# Add stats with improved styling
word_count = len(token_analysis)
token_count = len(full_analysis)
ratio = round(token_count / max(1, word_count), 2)
html += f"""
<div style="background: linear-gradient(135deg, #06b6d4, #0891b2); padding: 20px; border-radius: 12px; text-align: center; color: #ffffff; box-shadow: 0 4px 8px rgba(6, 182, 212, 0.3);">
<div style="font-size: 28px; font-weight: 700; margin-bottom: 4px;">{word_count}</div>
<div style="font-size: 14px; opacity: 0.9; font-weight: 500;">Words</div>
</div>
<div style="background: linear-gradient(135deg, #14b8a6, #0d9488); padding: 20px; border-radius: 12px; text-align: center; color: #ffffff; box-shadow: 0 4px 8px rgba(20, 184, 166, 0.3);">
<div style="font-size: 28px; font-weight: 700; margin-bottom: 4px;">{token_count}</div>
<div style="font-size: 14px; opacity: 0.9; font-weight: 500;">Tokens</div>
</div>
<div style="background: linear-gradient(135deg, #8b5cf6, #7c3aed); padding: 20px; border-radius: 12px; text-align: center; color: #ffffff; box-shadow: 0 4px 8px rgba(139, 92, 246, 0.3);">
<div style="font-size: 28px; font-weight: 700; margin-bottom: 4px;">{ratio}</div>
<div style="font-size: 14px; opacity: 0.9; font-weight: 500;">Tokens/Word</div>
</div>
"""
html += """
</div>
</div>
"""
return html
def generate_full_analysis_html(keyword, token_analysis, intent_analysis, evolution_potential, trends):
"""Generate HTML for full keyword analysis"""
html = f"""
<div style="font-family: 'Inter', sans-serif; padding: 24px; background: linear-gradient(135deg, #1e293b 0%, #334155 100%); border: 1px solid #475569; border-radius: 16px; color: #ffffff;">
<h2 style="margin-top: 0; margin-bottom: 24px; color: #ffffff; background: linear-gradient(135deg, #06b6d4, #3b82f6, #8b5cf6); -webkit-background-clip: text; -webkit-text-fill-color: transparent; background-clip: text;">๐งฌ Keyword DNA Analysis for: {keyword}</h2>
<div style="display: grid; grid-template-columns: 1fr 1fr; gap: 20px; margin-bottom: 24px;">
<div style="padding: 20px; background: rgba(6, 182, 212, 0.1); border: 1px solid #06b6d4; border-radius: 12px;">
<h3 style="margin-top: 0; font-size: 18px; color: #06b6d4; display: flex; align-items: center; gap: 8px; margin-bottom: 16px;">
<span>๐ฏ</span> Intent Gene
</h3>
<div style="display: flex; justify-content: space-between; margin-bottom: 12px; color: #e2e8f0;">
<span style="font-weight: 500;">Type:</span>
<span style="font-weight: 600; color: #06b6d4;">{intent_analysis['type']}</span>
</div>
<div style="display: flex; justify-content: space-between; align-items: center;">
<span style="font-weight: 500; color: #e2e8f0;">Strength:</span>
<div style="width: 120px; height: 10px; background: rgba(255,255,255,0.1); border-radius: 5px; overflow: hidden; position: relative;">
<div style="height: 100%; background: linear-gradient(90deg, #06b6d4, #14b8a6); width: {intent_analysis['strength']}%; border-radius: 5px; box-shadow: 0 0 8px rgba(6, 182, 212, 0.4);"></div>
<div style="position: absolute; right: 8px; top: 50%; transform: translateY(-50%); font-size: 10px; color: #ffffff; font-weight: 600;">{intent_analysis['strength']}%</div>
</div>
</div>
</div>
<div style="padding: 20px; background: rgba(139, 92, 246, 0.1); border: 1px solid #8b5cf6; border-radius: 12px;">
<h3 style="margin-top: 0; font-size: 18px; color: #8b5cf6; display: flex; align-items: center; gap: 8px; margin-bottom: 16px;">
<span>โก</span> Evolution Potential
</h3>
<div style="display: flex; justify-content: center; align-items: center; height: 80px;">
<div style="position: relative; width: 80px; height: 80px;">
<div style="position: absolute; inset: 0; display: flex; align-items: center; justify-content: center;">
<span style="font-size: 24px; font-weight: 700; color: #8b5cf6;">{evolution_potential}%</span>
</div>
<svg width="80" height="80" viewBox="0 0 36 36">
<path
d="M18 2.0845 a 15.9155 15.9155 0 0 1 0 31.831 a 15.9155 15.9155 0 0 1 0 -31.831"
fill="none"
stroke="rgba(139, 92, 246, 0.3)"
stroke-width="3"
/>
<path
d="M18 2.0845 a 15.9155 15.9155 0 0 1 0 31.831 a 15.9155 15.9155 0 0 1 0 -31.831"
fill="none"
stroke="#8b5cf6"
stroke-width="3"
stroke-dasharray="{evolution_potential}, 100"
stroke-linecap="round"
style="filter: drop-shadow(0 0 6px rgba(139, 92, 246, 0.5));"
/>
</svg>
</div>
</div>
</div>
</div>
<div style="padding: 20px; background: rgba(20, 184, 166, 0.1); border: 1px solid #14b8a6; border-radius: 12px; margin-bottom: 24px;">
<h3 style="margin-top: 0; font-size: 18px; color: #14b8a6; display: flex; align-items: center; gap: 8px; margin-bottom: 16px;">
<span>๐ฎ</span> Future Mutations
</h3>
<div style="display: flex; flex-direction: column; gap: 10px;">
"""
# Add trends with enhanced styling
for trend in trends:
html += f"""
<div style="display: flex; align-items: center; gap: 12px; padding: 8px 0;">
<span style="color: #14b8a6; font-size: 16px;">โ</span>
<span style="color: #e2e8f0; font-weight: 500;">{trend}</span>
</div>
"""
html += """
</div>
</div>
<h3 style="margin-bottom: 16px; color: #ffffff; display: flex; align-items: center; gap: 8px;">
<span>๐</span> Token Details & Historical Analysis
</h3>
"""
# Add token details with enhanced styling
for i, token in enumerate(token_analysis):
gradient_colors = [
"linear-gradient(135deg, #06b6d4, #0891b2)",
"linear-gradient(135deg, #14b8a6, #0d9488)",
"linear-gradient(135deg, #8b5cf6, #7c3aed)",
"linear-gradient(135deg, #3b82f6, #1e40af)",
"linear-gradient(135deg, #f59e0b, #d97706)"
]
token_gradient = gradient_colors[i % len(gradient_colors)]
html += f"""
<div style="padding: 20px; background: rgba(30, 41, 59, 0.8); border: 1px solid #475569; border-radius: 12px; margin-bottom: 16px; backdrop-filter: blur(10px);">
<div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 16px;">
<div style="display: flex; align-items: center; gap: 12px;">
<span style="font-size: 20px; font-weight: 600; color: #ffffff; padding: 8px 16px; background: {token_gradient}; border-radius: 8px; box-shadow: 0 4px 8px rgba(0,0,0,0.3);">
{token['token']}
</span>
<span style="padding: 4px 12px; background: rgba(6, 182, 212, 0.2); color: #06b6d4; border: 1px solid #06b6d4; border-radius: 6px; font-size: 12px; font-weight: 600; text-transform: uppercase;">
{token['posTag']}
</span>
"""
if token['entityType']:
html += f"""
<span style="padding: 4px 12px; background: rgba(139, 92, 246, 0.2); color: #8b5cf6; border: 1px solid #8b5cf6; border-radius: 6px; font-size: 12px; font-weight: 600; display: flex; align-items: center; gap: 4px;">
<span>โ</span> {token['entityType']}
</span>
"""
html += f"""
</div>
<div style="display: flex; align-items: center; gap: 8px;">
<span style="font-size: 12px; color: #94a3b8; font-weight: 500;">Importance:</span>
<div style="width: 80px; height: 8px; background: rgba(255,255,255,0.1); border-radius: 4px; overflow: hidden;">
<div style="height: 100%; background: linear-gradient(90deg, #06b6d4, #14b8a6); width: {token['importance']}%; border-radius: 4px;"></div>
</div>
<span style="font-size: 11px; color: #06b6d4; font-weight: 600;">{token['importance']}%</span>
</div>
</div>
<div style="margin-top: 16px;">
<div style="font-size: 14px; color: #94a3b8; margin-bottom: 8px; font-weight: 500;">๐ Historical Relevance:</div>
<div style="border: 1px solid #475569; border-radius: 8px; padding: 16px; background: rgba(15, 23, 42, 0.8);">
<div style="font-size: 13px; margin-bottom: 8px; color: #e2e8f0;">
<span style="font-weight: 600; color: #06b6d4;">Origin:</span>
<span style="margin-left: 8px;">{token['origin']['era']}</span>
<span style="margin-left: 4px; color: #94a3b8;">โข</span>
<span style="font-style: italic; margin-left: 4px; color: #14b8a6;">{token['origin']['language']}</span>
</div>
<div style="font-size: 12px; margin-bottom: 16px; color: #94a3b8; line-height: 1.4;">{token['origin']['note']}</div>
<div style="display: flex; align-items: flex-end; height: 60px; gap: 6px; margin-top: 12px; padding: 8px; background: rgba(6, 182, 212, 0.05); border-radius: 6px;">
"""
# Add enhanced historical data bars
for period, value in token['historicalData']:
period_index = token['historicalData'].index((period, value))
opacity = 0.4 + (period_index * 0.1)
bar_height = max(8, (value / 100) * 40)
html += f"""
<div style="display: flex; flex-direction: column; align-items: center; flex: 1; position: relative;">
<div style="width: 100%; background: linear-gradient(to top, rgba(6, 182, 212, {opacity}), rgba(20, 184, 166, {opacity + 0.2})); border-radius: 3px 3px 0 0; height: {bar_height}px; transition: all 0.3s ease; box-shadow: 0 2px 4px rgba(0,0,0,0.2);"></div>
<div style="font-size: 8px; margin-top: 6px; color: #94a3b8; transform: rotate(45deg); transform-origin: top center; white-space: nowrap; font-weight: 500;">
{period}
</div>
<div style="position: absolute; top: -20px; left: 50%; transform: translateX(-50%); font-size: 8px; color: #06b6d4; font-weight: 600; opacity: 0; transition: opacity 0.3s ease;">
{int(value)}%
</div>
</div>
"""
html += """
</div>
</div>
</div>
</div>
"""
html += """
</div>
"""
return html
def analyze_keyword(keyword, forecast_months=6, growth_scenario="Moderate", get_serp=False, progress=gr.Progress()):
"""Main function to analyze a keyword"""
if not keyword or not keyword.strip():
return (
"<div>Please enter a keyword to analyze</div>",
"<div>Please enter a keyword to analyze</div>",
None,
None,
None,
None,
None
)
progress(0.1, desc="Starting analysis...")
# Load models if not already loaded
model_status = load_models(progress)
if isinstance(model_status, str) and model_status.startswith("Error"):
return (
f"<div style='color:red;'>{model_status}</div>",
f"<div style='color:red;'>{model_status}</div>",
None,
None,
None,
None,
None
)
try:
# Basic tokenization - just split on spaces for simplicity
words = keyword.strip().lower().split()
progress(0.2, desc="Analyzing tokens...")
# Get token types
token_analysis = analyze_token_types(words)
progress(0.3, desc="Running NER...")
# Get NER tags - handle potential errors
try:
ner_results = ner_pipeline(keyword)
except Exception as e:
print(f"NER error: {str(e)}")
ner_results = []
progress(0.4, desc="Running POS tagging...")
# Get POS tags - handle potential errors
try:
pos_results = pos_pipeline(keyword)
except Exception as e:
print(f"POS error: {str(e)}")
pos_results = []
# Process and organize results
full_token_analysis = []
for token in token_analysis:
# Find POS tag for this token
pos_tag = "NOUN" # Default
for pos_result in pos_results:
if pos_result["word"].lower() == token["text"]:
pos_tag = pos_result["entity"]
break
# Find entity type if any
entity_type = None
for ner_result in ner_results:
if ner_result["word"].lower() == token["text"]:
entity_type = ner_result["entity"]
break
# Generate historical data
historical_data = simulate_historical_data(token["text"])
# Generate origin data
origin = generate_origin_data(token["text"])
# Calculate importance (simplified algorithm)
importance = 60 + (len(token["text"]) * 2)
importance = min(95, importance)
# Generate more meaningful related terms using semantic similarity
if semantic_model is not None:
try:
# Generate some potential related terms
prefix_related = [f"about {token['text']}", f"what is {token['text']}", f"how to {token['text']}"]
synonym_candidates = ["similar", "equivalent", "comparable", "like", "related", "alternative"]
domain_terms = ["software", "marketing", "business", "science", "education", "technology"]
comparison_terms = prefix_related + synonym_candidates + domain_terms
# Get similarities
similarities = get_semantic_similarity(token['text'], comparison_terms)
# Use top 3 most similar terms
related_terms = [term for term, score in similarities[:3]]
except Exception as e:
print(f"Error generating semantic related terms: {str(e)}")
related_terms = [f"{token['text']}-related-1", f"{token['text']}-related-2"]
else:
# Fallback if semantic model isn't loaded
related_terms = [f"{token['text']}-related-1", f"{token['text']}-related-2"]
full_token_analysis.append({
"token": token["text"],
"type": token["type"],
"posTag": pos_tag,
"entityType": entity_type,
"importance": importance,
"historicalData": historical_data,
"origin": origin,
"relatedTerms": related_terms
})
progress(0.5, desc="Analyzing intent...")
# Intent analysis - handle potential errors
try:
intent_result = intent_classifier(
keyword,
candidate_labels=["informational", "navigational", "transactional"]
)
intent_analysis = {
"type": intent_result["labels"][0].capitalize(),
"strength": round(intent_result["scores"][0] * 100),
"mutations": [
f"{intent_result['labels'][0]}-variation-1",
f"{intent_result['labels'][0]}-variation-2"
]
}
except Exception as e:
print(f"Intent classification error: {str(e)}")
intent_analysis = {
"type": "Informational", # Default fallback
"strength": 70,
"mutations": ["fallback-variation-1", "fallback-variation-2"]
}
# Evolution potential (simplified calculation)
evolution_potential = min(95, 65 + (len(keyword) % 30))
# Predicted trends (simplified)
trends = [
"Voice search adaptation",
"Visual search integration"
]
# Generate more realistic and keyword-specific evolution data
base_volume = 1000 + (len(keyword) * 100)
# Adjust growth factor based on scenario
if growth_scenario == "Conservative":
growth_factor = 1.05 + (0.02 * (sum(ord(c) for c in keyword) % 5))
elif growth_scenario == "Aggressive":
growth_factor = 1.15 + (0.05 * (sum(ord(c) for c in keyword) % 5))
else: # Moderate
growth_factor = 1.1 + (0.03 * (sum(ord(c) for c in keyword) % 5))
evolution_data = []
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"][:int(forecast_months)]
current_volume = base_volume
for month in months:
# Add some randomness to make it look more realistic
np.random.seed(sum(ord(c) for c in month + keyword))
random_factor = 0.9 + (0.2 * np.random.random())
current_volume *= growth_factor * random_factor
evolution_data.append({
"month": month,
"searchVolume": int(current_volume),
"competitionScore": min(95, 45 + (months.index(month) * 3) + (sum(ord(c) for c in keyword) % 10)),
"intentClarity": min(95, 80 + (months.index(month) * 2) + (sum(ord(c) for c in keyword) % 5))
})
progress(0.6, desc="Creating visualizations...")
# Create interactive evolution chart
evolution_chart = create_evolution_chart(evolution_data, forecast_months, growth_scenario)
# SERP results and ranking history (new feature)
serp_results = None
ranking_chart = None
serp_html = None
if get_serp:
progress(0.7, desc="Fetching SERP data...")
# Get SERP results
serp_results = simulate_google_serp(keyword)
# Update ranking history
update_ranking_history(keyword, serp_results)
progress(0.8, desc="Creating ranking charts...")
# Create ranking history chart
if keyword in ranking_history and len(ranking_history[keyword]) > 0:
ranking_chart = create_ranking_history_chart(ranking_history[keyword])
# Generate SERP HTML
serp_html = generate_serp_html(keyword, serp_results)
# Generate HTML for token visualization
token_viz_html = generate_token_visualization_html(token_analysis, full_token_analysis)
# Generate HTML for full analysis
analysis_html = generate_full_analysis_html(
keyword,
full_token_analysis,
intent_analysis,
evolution_potential,
trends
)
# Generate JSON results
json_results = {
"keyword": keyword,
"tokenAnalysis": full_token_analysis,
"intentAnalysis": intent_analysis,
"evolutionPotential": evolution_potential,
"predictedTrends": trends,
"forecast": {
"months": forecast_months,
"scenario": growth_scenario,
"data": evolution_data
},
"serpResults": serp_results
}
progress(1.0, desc="Analysis complete!")
return token_viz_html, analysis_html, json_results, evolution_chart, serp_html, ranking_chart, keyword
except Exception as e:
error_message = f"<div style='color:red;padding:20px;'>Error analyzing keyword: {str(e)}</div>"
print(f"Error in analyze_keyword: {str(e)}")
return error_message, error_message, None, None, None, None, None
# Create the Gradio interface with AI Snipper styling
with gr.Blocks(
css=ai_snipper_css,
title="๐งฌ AI Snipper Keyword DNA Analyzer",
theme=gr.themes.Base(
primary_hue="cyan",
secondary_hue="blue",
neutral_hue="slate",
font=[gr.themes.GoogleFont("Inter"), "sans-serif"]
)
) as demo:
# Custom header with DNA theme
gr.HTML("""
<div style="text-align: center; padding: 2rem 0; margin-bottom: 2rem;">
<h1 style="font-size: 3rem; font-weight: 800; margin-bottom: 1rem; background: linear-gradient(135deg, #06b6d4, #3b82f6, #8b5cf6); -webkit-background-clip: text; -webkit-text-fill-color: transparent; background-clip: text;">
๐งฌ Keyword DNA Analyzer
</h1>
<p style="font-size: 1.2rem; color: #94a3b8; margin-top: 1rem; font-weight: 400;">
Decode the genetic structure of your keywords with AI-powered analysis
</p>
</div>
""")
with gr.Row():
with gr.Column(scale=1):
# Voice search capabilities with improved styling
with gr.Group():
gr.Markdown("### ๐ฏ Enter Keyword")
with gr.Row():
input_text = gr.Textbox(
label="Keyword to analyze",
placeholder="e.g. artificial intelligence, machine learning, SEO strategy...",
lines=2
)
with gr.Row():
audio_input = gr.Audio(
type="filepath",
label="๐ค Or use voice search"
)
voice_submit_btn = gr.Button(
"๐ Convert Voice to Text",
variant="secondary"
)
# SERP settings with better organization
with gr.Accordion("โ๏ธ Analysis Settings", open=False):
with gr.Row():
forecast_months = gr.Slider(
minimum=3,
maximum=12,
value=6,
step=1,
label="๐ Forecast Months"
)
include_serp = gr.Checkbox(
label="๐ Include SERP Analysis",
value=True
)
growth_scenario = gr.Radio(
["Conservative", "Moderate", "Aggressive"],
value="Moderate",
label="๐ Growth Scenario"
)
# Status indicator with custom styling
status_html = gr.HTML(
'<div style="text-align: center; padding: 1rem; border-radius: 8px; margin: 1rem 0; font-weight: 500; background: rgba(6, 182, 212, 0.1); border: 1px solid #06b6d4; color: #06b6d4;">๐ Enter a keyword and click "Analyze DNA" to begin</div>'
)
# Main analyze button
analyze_btn = gr.Button(
"๐งฌ Analyze DNA",
variant="primary"
)
# Example buttons with custom styling
gr.Markdown("### ๐ก Try These Examples")
with gr.Row():
example_btns = []
examples = [
"preprocessing",
"breakdown",
"artificial intelligence",
"transformer model",
"machine learning"
]
for example in examples:
example_btns.append(gr.Button(f"โจ {example}"))
with gr.Column(scale=2):
with gr.Tabs():
with gr.Tab("๐ฌ Token Visualization"):
token_viz_html = gr.HTML()
with gr.Tab("๐ Full Analysis"):
analysis_html = gr.HTML()
with gr.Tab("๐ Evolution Chart"):
evolution_chart = gr.Plot(label="Keyword Evolution Forecast")
with gr.Tab("๐ SERP Results"):
serp_html = gr.HTML()
with gr.Tab("๐ Ranking History"):
ranking_chart = gr.Plot(label="Keyword Ranking History")
with gr.Tab("๐พ Raw Data"):
json_output = gr.JSON()
# Event handlers
voice_submit_btn.click(
handle_voice_input,
inputs=[audio_input],
outputs=[input_text]
)
# Updated status messages with custom styling
analyze_btn.click(
lambda: '<div style="text-align: center; padding: 1rem; border-radius: 8px; margin: 1rem 0; font-weight: 500; background: rgba(6, 182, 212, 0.1); border: 1px solid #06b6d4; color: #06b6d4;">๐ Loading models and analyzing... This may take a moment.</div>',
outputs=status_html
).then(
analyze_keyword,
inputs=[input_text, forecast_months, growth_scenario, include_serp],
outputs=[token_viz_html, analysis_html, json_output, evolution_chart, serp_html, ranking_chart, input_text]
).then(
lambda: '<div style="text-align: center; padding: 1rem; border-radius: 8px; margin: 1rem 0; font-weight: 500; background: rgba(20, 184, 166, 0.1); border: 1px solid #14b8a6; color: #14b8a6;">โ
Analysis complete! Check the results above.</div>',
outputs=status_html
)
# Example buttons with enhanced interaction
for btn in example_btns:
def set_example(btn_label):
# Remove the emoji prefix for the actual keyword
return btn_label.replace("โจ ", "")
btn.click(
set_example,
inputs=[btn],
outputs=[input_text]
).then(
lambda: '<div style="text-align: center; padding: 1rem; border-radius: 8px; margin: 1rem 0; font-weight: 500; background: rgba(6, 182, 212, 0.1); border: 1px solid #06b6d4; color: #06b6d4;">๐ Loading models and analyzing... This may take a moment.</div>',
outputs=status_html
).then(
analyze_keyword,
inputs=[input_text, forecast_months, growth_scenario, include_serp],
outputs=[token_viz_html, analysis_html, json_output, evolution_chart, serp_html, ranking_chart, input_text]
).then(
lambda: '<div style="text-align: center; padding: 1rem; border-radius: 8px; margin: 1rem 0; font-weight: 500; background: rgba(20, 184, 166, 0.1); border: 1px solid #14b8a6; color: #14b8a6;">โ
Analysis complete! Check the results above.</div>',
outputs=status_html
)
# Launch configuration
if __name__ == "__main__":
demo.launch() |