Spaces:
Running
Running
File size: 78,509 Bytes
d1eb779 384eeae 631a418 f45845d 631a418 97fec7a 631a418 f45845d af1d89d f45845d 384eeae af1d89d 384eeae 9b58814 f45845d 384eeae f45845d d1eb779 384eeae af1d89d d1eb779 af1d89d 9b58814 af1d89d 9b58814 384eeae 9b58814 af1d89d 9b58814 af1d89d 384eeae 9b58814 af1d89d 9b58814 af1d89d 9b58814 384eeae 9b58814 af1d89d 9b58814 af1d89d 384eeae 9b58814 af1d89d 9b58814 af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 9b58814 af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae 631a418 f45845d af1d89d 384eeae f45845d af1d89d 384eeae f45845d af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 631a418 9b58814 af1d89d 384eeae 9b58814 af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 9b58814 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 9b58814 384eeae 9b58814 af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 9b58814 384eeae f45845d 384eeae af1d89d 384eeae f45845d af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae 626f8a0 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae 1d993fc 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae 1cac78f af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 1cac78f 384eeae af1d89d 384eeae af1d89d 1cac78f 384eeae af1d89d 384eeae f45845d 384eeae af1d89d 384eeae f45845d af1d89d 384eeae af1d89d f45845d 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 9b58814 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 9b58814 384eeae c27baae af1d89d 384eeae af1d89d 384eeae af1d89d c27baae 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 631a418 af1d89d 631a418 af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d d0a5380 384eeae d0a5380 384eeae d0a5380 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae 1cac78f 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae 631a418 af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae af1d89d 384eeae 631a418 af1d89d 631a418 384eeae af1d89d 384eeae af1d89d 384eeae 9b58814 af1d89d 631a418 384eeae 631a418 d1eb779 af1d89d f45845d 384eeae f45845d af1d89d f45845d 384eeae f45845d af1d89d |
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 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 |
import gradio as gr
# import anthropic # Removed Anthropic import
import PyPDF2
import pandas as pd
import numpy as np
import io
import os
import json
import zipfile
import tempfile
from typing import Dict, List, Tuple, Union, Optional
import re
from pathlib import Path
import openpyxl # Needed for reading .xlsx
from dataclasses import dataclass
from enum import Enum
# No need for docx, reportlab, matplotlib if only text/code output is used and not generating them internally
# import docx
# from docx.shared import Inches, Pt, RGBColor
# from docx.enum.text import WD_ALIGN_PARAGRAPH
# import reportlab
# from reportlab.lib import colors
# from reportlab.lib.pagesizes import letter, A4
# from reportlab.platypus import SimpleDocTemplate, Table, TableStyle, Paragraph, Spacer, PageBreak
# from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
# from reportlab.lib.units import inch
# from reportlab.pdfbase import pdfmetrics
# from reportlab.pdfbase.ttfonts import TTFont
# import matplotlib.pyplot as plt # Moved to the implementation code section
from datetime import datetime
# Import OpenAI for Qwen access
from openai import OpenAI
# Configuración para HuggingFace
os.environ['GRADIO_ANALYTICS_ENABLED'] = 'False'
# Initialize OpenAI client for Qwen
# Read API key from NEBIUS_API_KEY environment variable
NEBIUS_API_KEY = os.environ.get("NEBIUS_API_KEY")
if NEBIUS_API_KEY:
openai_client = OpenAI(
base_url="https://api.studio.nebius.com/v1/",
api_key=NEBIUS_API_KEY
)
print("OpenAI client initialized for Nebius Qwen endpoint.")
else:
openai_client = None
print("NEBIUS_API_KEY not found. OpenAI client not initialized.")
# Sistema de traducción - Actualizado con nuevas entradas
TRANSLATIONS = {
'en': {
'title': '🧬 Comparative Analyzer of Biotechnological Models',
'subtitle': 'Specialized in comparative analysis of mathematical model fitting results',
'upload_files': '📁 Upload fitting results (CSV/Excel)',
'select_model': '🤖 Qwen Model', # Changed label
'select_language': '🌐 Language',
'select_theme': '🎨 Theme',
'detail_level': '📋 Analysis detail level',
'detailed': 'Detailed',
'summarized': 'Summarized',
'analyze_button': '🚀 Analyze and Compare Models',
'export_format': '📄 Export format',
'export_button': '💾 Export Report',
'comparative_analysis': '📊 Comparative Analysis',
'implementation_code': '💻 Implementation Code',
'data_format': '📋 Expected data format',
'examples': '📚 Analysis examples',
'light': 'Light',
'dark': 'Dark',
'best_for': 'Best for',
'loading': 'Loading...',
'error_no_api': 'Please configure NEBIUS_API_KEY in HuggingFace Space secrets', # Changed message
'error_no_files': 'Please upload fitting result files to analyze',
'report_exported': 'Report exported successfully as',
'specialized_in': '🎯 Specialized in:',
'metrics_analyzed': '📊 Analyzed metrics:',
'what_analyzes': '🔍 What it specifically analyzes:',
'tips': '💡 Tips for better results:',
'additional_specs': '📝 Additional specifications for analysis',
'additional_specs_placeholder': 'Add any specific requirements or focus areas for the analysis...'
},
'es': {
'title': '🧬 Analizador Comparativo de Modelos Biotecnológicos',
'subtitle': 'Especializado en análisis comparativo de resultados de ajuste de modelos matemáticos',
'upload_files': '📁 Subir resultados de ajuste (CSV/Excel)',
'select_model': '🤖 Modelo Qwen', # Changed label
'select_language': '🌐 Idioma',
'select_theme': '🎨 Tema',
'detail_level': '📋 Nivel de detalle del análisis',
'detailed': 'Detallado',
'summarized': 'Resumido',
'analyze_button': '🚀 Analizar y Comparar Modelos',
'export_format': '📄 Formato de exportación',
'export_button': '💾 Exportar Reporte',
'comparative_analysis': '📊 Análisis Comparativo',
'implementation_code': '💻 Código de Implementación',
'data_format': '📋 Formato de datos esperado',
'examples': '📚 Ejemplos de análisis',
'light': 'Claro',
'dark': 'Oscuro',
'best_for': 'Mejor para',
'loading': 'Cargando...',
'error_no_api': 'Por favor configura NEBIUS_API_KEY en los secretos del Space', # Changed message
'error_no_files': 'Por favor sube archivos con resultados de ajuste para analizar',
'report_exported': 'Reporte exportado exitosamente como',
'specialized_in': '🎯 Especializado en:',
'metrics_analyzed': '📊 Métricas analizadas:',
'what_analyzes': '🔍 Qué analiza específicamente:',
'tips': '💡 Tips para mejores resultados:',
'additional_specs': '📝 Especificaciones adicionales para el análisis',
'additional_specs_placeholder': 'Agregue cualquier requerimiento específico o áreas de enfoque para el análisis...'
},
'fr': {
'title': '🧬 Analyseur Comparatif de Modèles Biotechnologiques',
'subtitle': 'Spécialisé dans l\'analyse comparative des résultats d\'ajustement',
'upload_files': '📁 Télécharger les résultats (CSV/Excel)',
'select_model': '🤖 Modèle Qwen', # Changed label
'select_language': '🌐 Langue',
'select_theme': '🎨 Thème',
'detail_level': '📋 Niveau de détail',
'detailed': 'Détaillé',
'summarized': 'Résumé',
'analyze_button': '🚀 Analyser et Comparer',
'export_format': '📄 Format d\'export',
'export_button': '💾 Exporter le Rapport',
'comparative_analysis': '📊 Analyse Comparative',
'implementation_code': '💻 Code d\'Implémentation',
'data_format': '📋 Format de données attendu',
'examples': '📚 Exemples d\'analyse',
'light': 'Clair',
'dark': 'Sombre',
'best_for': 'Meilleur pour',
'loading': 'Chargement...',
'error_no_api': 'Veuillez configurer NEBIUS_API_KEY', # Changed message
'error_no_files': 'Veuillez télécharger des fichiers à analyser',
'report_exported': 'Rapport exporté avec succès comme',
'specialized_in': '🎯 Spécialisé dans:',
'metrics_analyzed': '📊 Métriques analysées:',
'what_analyzes': '🔍 Ce qu\'il analyse spécifiquement:',
'tips': '💡 Conseils pour de meilleurs résultats:',
'additional_specs': '📝 Spécifications supplémentaires pour l\'analyse',
'additional_specs_placeholder': 'Ajoutez des exigences spécifiques ou des domaines d\'intérêt pour l\'analyse...'
},
'de': {
'title': '🧬 Vergleichender Analysator für Biotechnologische Modelle',
'subtitle': 'Spezialisiert auf vergleichende Analyse von Modellanpassungsergebnissen',
'upload_files': '📁 Ergebnisse hochladen (CSV/Excel)',
'select_model': '🤖 Qwen Modell', # Changed label
'select_language': '🌐 Sprache',
'select_theme': '🎨 Thema',
'detail_level': '📋 Detailgrad der Analyse',
'detailed': 'Detailliert',
'summarized': 'Zusammengefasst',
'analyze_button': '🚀 Analysieren und Vergleichen',
'export_format': '📄 Exportformat',
'export_button': '💾 Bericht Exportieren',
'comparative_analysis': '📊 Vergleichende Analyse',
'implementation_code': '💻 Implementierungscode',
'data_format': '📋 Erwartetes Datenformat',
'examples': '📚 Analysebeispiele',
'light': 'Hell',
'dark': 'Dunkel',
'best_for': 'Am besten für',
'loading': 'Laden...',
'error_no_api': 'Bitte konfigurieren Sie NEBIUS_API_KEY', # Changed message
'error_no_files': 'Bitte laden Sie Dateien zur Analyse hoch',
'report_exported': 'Bericht erfolgreich exportiert als',
'specialized_in': '🎯 Spezialisiert auf:',
'metrics_analyzed': '📊 Analysierte Metriken:',
'what_analyzes': '🔍 Was spezifisch analysiert wird:',
'tips': '💡 Tipps für bessere Ergebnisse:',
'additional_specs': '📝 Zusätzliche Spezifikationen für die Analyse',
'additional_specs_placeholder': 'Fügen Sie spezifische Anforderungen oder Schwerpunktbereiche für die Analyse hinzu...'
},
'pt': {
'title': '🧬 Analisador Comparativo de Modelos Biotecnológicos',
'subtitle': 'Especializado em análise comparativa de resultados de ajuste',
'upload_files': '📁 Carregar resultados (CSV/Excel)',
'select_model': '🤖 Modelo Qwen', # Changed label
'select_language': '🌐 Idioma',
'select_theme': '🎨 Tema',
'detail_level': '📋 Nível de detalhe',
'detailed': 'Detalhado',
'summarized': 'Resumido',
'analyze_button': '🚀 Analisar e Comparar',
'export_format': '📄 Formato de exportação',
'export_button': '💾 Exportar Relatório',
'comparative_analysis': '📊 Análise Comparativa',
'implementation_code': '💻 Código de Implementação',
'data_format': '📋 Formato de dados esperado',
'examples': '📚 Exemplos de análise',
'light': 'Claro',
'dark': 'Escuro',
'best_for': 'Melhor para',
'loading': 'Carregando...',
'error_no_api': 'Por favor configure NEBIUS_API_KEY', # Changed message
'error_no_files': 'Por favor carregue arquivos para analisar',
'report_exported': 'Relatório exportado com sucesso como',
'specialized_in': '🎯 Especializado em:',
'metrics_analyzed': '📊 Métricas analisadas:',
'what_analyzes': '🔍 O que analisa especificamente:',
'tips': '💡 Dicas para melhores resultados:',
'additional_specs': '📝 Especificações adicionais para a análise',
'additional_specs_placeholder': 'Adicione requisitos específicos ou áreas de foco para a análise...'
}
}
# Temas disponibles
THEMES = {
'light': gr.themes.Soft(),
'dark': gr.themes.Base(
primary_hue="blue",
secondary_hue="gray",
neutral_hue="gray",
font=["Arial", "sans-serif"]
).set(
body_background_fill="dark",
body_background_fill_dark="*neutral_950",
button_primary_background_fill="*primary_600",
button_primary_background_fill_hover="*primary_500",
button_primary_text_color="white",
block_background_fill="*neutral_800",
block_border_color="*neutral_700",
block_label_text_color="*neutral_200",
block_title_text_color="*neutral_100",
checkbox_background_color="*neutral_700",
checkbox_background_color_selected="*primary_600",
input_background_fill="*neutral_700",
input_border_color="*neutral_600",
input_placeholder_color="*neutral_400"
)
}
# Enum para tipos de análisis
class AnalysisType(Enum):
MATHEMATICAL_MODEL = "mathematical_model"
DATA_FITTING = "data_fitting"
FITTING_RESULTS = "fitting_results"
UNKNOWN = "unknown"
# Estructura modular para modelos
@dataclass
class MathematicalModel:
name: str
equation: str
parameters: List[str]
application: str
sources: List[str]
category: str
biological_meaning: str
# Sistema de registro de modelos escalable
class ModelRegistry:
def __init__(self):
self.models = {}
self._initialize_default_models()
def register_model(self, model: MathematicalModel):
"""Registra un nuevo modelo matemático"""
if model.category not in self.models:
self.models[model.category] = {}
self.models[model.category][model.name] = model
def get_model(self, category: str, name: str) -> MathematicalModel:
"""Obtiene un modelo específico"""
return self.models.get(category, {}).get(name)
def get_all_models(self) -> Dict:
"""Retorna todos los modelos registrados"""
return self.models
def _initialize_default_models(self):
"""Inicializa los modelos por defecto"""
# Modelos de crecimiento
self.register_model(MathematicalModel(
name="Monod",
equation="μ = μmax × (S / (Ks + S))",
parameters=["μmax (h⁻¹)", "Ks (g/L)"],
application="Crecimiento limitado por sustrato único",
sources=["Cambridge", "MIT", "DTU"],
category="crecimiento_biomasa",
biological_meaning="Describe cómo la velocidad de crecimiento depende de la concentración de sustrato limitante"
))
self.register_model(MathematicalModel(
name="Logístico",
equation="dX/dt = μmax × X × (1 - X/Xmax)",
parameters=["μmax (h⁻¹)", "Xmax (g/L)"],
application="Sistemas cerrados batch",
sources=["Cranfield", "Swansea", "HAL Theses"],
category="crecimiento_biomasa",
biological_meaning="Modela crecimiento limitado por capacidad de carga del sistema"
))
self.register_model(MathematicalModel(
name="Gompertz",
equation="X(t) = Xmax × exp(-exp((μmax × e / Xmax) × (λ - t) + 1))",
parameters=["λ (h)", "μmax (h⁻¹)", "Xmax (g/L)"],
application="Crecimiento con fase lag pronunciada",
sources=["Lund University", "NC State"],
category="crecimiento_biomasa",
biological_meaning="Incluye fase de adaptación (lag) seguida de crecimiento exponencial y estacionario"
))
# Instancia global del registro
model_registry = ModelRegistry()
# Available Qwen Models (Updated from Claude)
QWEN_MODELS = {
"Qwen/Qwen3-14B": { # Using the model specified by the user
"name": "Qwen 3-14B",
"description": "A powerful Qwen model suitable for complex analysis.",
"max_tokens": 8192, # Example context window, adjust based on actual model specs
"best_for": "Detailed analysis and code generation"
},
# Add other Qwen models if available and desired, e.g.:
# "Qwen/Qwen3-7B": {
# "name": "Qwen 3-7B",
# "description": "Faster Qwen model",
# "max_tokens": 8192,
# "best_for": "Quicker analysis"
# }
}
class FileProcessor:
"""Clase para procesar diferentes tipos de archivos"""
@staticmethod
def extract_text_from_pdf(pdf_file) -> str:
"""Extrae texto de un archivo PDF"""
try:
pdf_reader = PyPDF2.PdfReader(io.BytesIO(pdf_file))
text = ""
for page in pdf_reader.pages:
text += page.extract_text() + "\n"
return text
except Exception as e:
return f"Error reading PDF: {str(e)}"
@staticmethod
def read_csv(csv_file) -> pd.DataFrame:
"""Lee archivo CSV"""
try:
return pd.read_csv(io.BytesIO(csv_file))
except Exception as e:
print(f"Error reading CSV: {e}")
return None
@staticmethod
def read_excel(excel_file) -> pd.DataFrame:
"""Lee archivo Excel"""
try:
return pd.read_excel(io.BytesIO(excel_file))
except Exception as e:
print(f"Error reading Excel: {e}")
return None
@staticmethod
def extract_from_zip(zip_file) -> List[Tuple[str, bytes]]:
"""Extrae archivos de un ZIP"""
files = []
try:
with zipfile.ZipFile(io.BytesIO(zip_file), 'r') as zip_ref:
for file_name in zip_ref.namelist():
if not file_name.startswith('__MACOSX'):
file_data = zip_ref.read(file_name)
files.append((file_name, file_data))
except Exception as e:
print(f"Error processing ZIP: {e}")
return files
class ReportExporter:
"""Clase para exportar reportes a diferentes formatos"""
# Keep ReportExporter as is, as it processes the analysis text,
# not the AI interaction itself. It might need docx/reportlab imports
# re-added if generating those formats. Assuming they are needed for export_to_docx/pdf.
# Re-adding necessary imports for ReportExporter
from docx import Document
from docx.shared import Inches, Pt, RGBColor
from docx.enum.text import WD_ALIGN_PARAGRAPH
from reportlab.lib import colors
from reportlab.lib.pagesizes import letter, A4
from reportlab.platypus import SimpleDocTemplate, Table, TableStyle, Paragraph, Spacer, PageBreak
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib.units import inch
# pdfmetrics and TTFont might be needed for custom fonts if used, keep them for now.
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
@staticmethod
def export_to_docx(content: str, filename: str, language: str = 'en') -> str:
"""Exporta el contenido a un archivo DOCX"""
doc = ReportExporter.Document()
# Configurar estilos
title_style = doc.styles['Title']
title_style.font.size = ReportExporter.Pt(24)
title_style.font.bold = True
heading_style = doc.styles['Heading 1']
heading_style.font.size = ReportExporter.Pt(18)
heading_style.font.bold = True
# Título
title_text = {
'en': 'Comparative Analysis Report - Biotechnological Models',
'es': 'Informe de Análisis Comparativo - Modelos Biotecnológicos',
'fr': 'Rapport d\'Analyse Comparative - Modèles Biotechnologiques',
'de': 'Vergleichsanalysebericht - Biotechnologische Modelle',
'pt': 'Relatório de Análise Comparativa - Modelos Biotecnológicos'
}
doc.add_heading(title_text.get(language, title_text['en']), 0)
# Fecha
date_text = {
'en': 'Generated on',
'es': 'Generado el',
'fr': 'Généré le',
'de': 'Erstellt am',
'pt': 'Gerado em'
}
doc.add_paragraph(f"{date_text.get(language, date_text['en'])}: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
doc.add_paragraph()
# Procesar contenido
lines = content.split('\n')
current_paragraph = None
for line in lines:
line = line.strip()
if line.startswith('###'):
doc.add_heading(line.replace('###', '').strip(), level=2)
elif line.startswith('##'):
doc.add_heading(line.replace('##', '').strip(), level=1)
elif line.startswith('#'):
doc.add_heading(line.replace('#', '').strip(), level=0)
elif line.startswith('**') and line.endswith('**'):
# Texto en negrita
p = doc.add_paragraph()
run = p.add_run(line.replace('**', ''))
run.bold = True
elif line.startswith('- ') or line.startswith('* '):
# Lista
doc.add_paragraph(line[2:], style='List Bullet')
elif line.startswith(tuple('0123456789')):
# Lista numerada
doc.add_paragraph(line, style='List Number')
elif line == '---' or line.startswith('==='):
# Separador
doc.add_paragraph('_' * 50)
elif line:
# Párrafo normal
doc.add_paragraph(line)
# Guardar documento
doc.save(filename)
return filename
@staticmethod
def export_to_pdf(content: str, filename: str, language: str = 'en') -> str:
"""Exporta el contenido a un archivo PDF"""
# Crear documento PDF
doc = ReportExporter.SimpleDocTemplate(filename, pagesize=ReportExporter.letter)
story = []
styles = ReportExporter.getSampleStyleSheet()
# Estilos personalizados
title_style = ReportExporter.ParagraphStyle(
'CustomTitle',
parent=styles['Title'],
fontSize=24,
textColor=ReportExporter.colors.HexColor('#1f4788'),
spaceAfter=30
)
heading_style = ReportExporter.ParagraphStyle(
'CustomHeading',
parent=styles['Heading1'],
fontSize=16,
textColor=ReportExporter.colors.HexColor('#2e5090'),
spaceAfter=12
)
# Título
title_text = {
'en': 'Comparative Analysis Report - Biotechnological Models',
'es': 'Informe de Análisis Comparativo - Modelos Biotecnológicos',
'fr': 'Rapport d\'Analyse Comparative - Modèles Biotechnologiques',
'de': 'Vergleichsanalysebericht - Biotechnologische Modelle',
'pt': 'Relatório de Análise Comparativa - Modelos Biotecnológicos'
}
story.append(ReportExporter.Paragraph(title_text.get(language, title_text['en']), title_style))
# Fecha
date_text = {
'en': 'Generated on',
'es': 'Generado el',
'fr': 'Généré le',
'de': 'Erstellt am',
'pt': 'Gerado em'
}
story.append(ReportExporter.Paragraph(f"{date_text.get(language, date_text['en'])}: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}", styles['Normal']))
story.append(ReportExporter.Spacer(1, 0.5*ReportExporter.inch))
# Procesar contenido
lines = content.split('\n')
for line in lines:
line = line.strip()
if not line:
story.append(ReportExporter.Spacer(1, 0.2*ReportExporter.inch))
elif line.startswith('###'):
story.append(ReportExporter.Paragraph(line.replace('###', '').strip(), styles['Heading3']))
elif line.startswith('##'):
story.append(ReportExporter.Paragraph(line.replace('##', '').strip(), styles['Heading2']))
elif line.startswith('#'):
story.append(ReportExporter.Paragraph(line.replace('#', '').strip(), heading_style))
elif line.startswith('**') and line.endswith('**'):
text = line.replace('**', '')
story.append(ReportExporter.Paragraph(f"<b>{text}</b>", styles['Normal']))
elif line.startswith('- ') or line.startswith('* '):
story.append(ReportExporter.Paragraph(f"• {line[2:]}", styles['Normal']))
elif line == '---' or line.startswith('==='):
story.append(ReportExporter.Spacer(1, 0.3*ReportExporter.inch))
story.append(ReportExporter.Paragraph("_" * 70, styles['Normal']))
story.append(ReportExporter.Spacer(1, 0.3*ReportExporter.inch))
else:
# Limpiar caracteres especiales para PDF
clean_line = line.replace('📊', '[GRAPH]').replace('🎯', '[TARGET]').replace('🔍', '[SEARCH]').replace('💡', '[TIP]')
story.append(ReportExporter.Paragraph(clean_line, styles['Normal']))
# Construir PDF
doc.build(story)
return filename
class AIAnalyzer:
"""Clase para análisis con IA (usando OpenAI for Qwen)"""
def __init__(self, client, model_registry):
# client is now an OpenAI client instance
self.client = client
self.model_registry = model_registry
# Qwen specific parameters from user example
self.temperature = 0.6
self.top_p = 0.95
def detect_analysis_type(self, content: Union[str, pd.DataFrame]) -> AnalysisType:
"""Detecta el tipo de análisis necesario"""
if isinstance(content, pd.DataFrame):
columns = [col.lower() for col in content.columns]
fitting_indicators = [
'r2', 'r_squared', 'rmse', 'mse', 'aic', 'bic',
'parameter', 'param', 'coefficient', 'fit',
'model', 'equation', 'goodness', 'chi_square',
'p_value', 'confidence', 'standard_error', 'se'
]
has_fitting_results = any(indicator in ' '.join(columns) for indicator in fitting_indicators)
if has_fitting_results:
return AnalysisType.FITTING_RESULTS
else:
# Assuming any dataframe without clear fitting metrics is raw data
return AnalysisType.DATA_FITTING
# Use a quick Qwen model for type detection
# Using the same model as the main analysis for simplicity, could use a smaller one if available
model_for_detection = list(QWEN_MODELS.keys())[0] # Use the first available Qwen model
prompt = """
Analyze this content and determine if it is:
1. A scientific article describing biotechnological mathematical models
2. Experimental data for parameter fitting
3. Model fitting results (with parameters, R², RMSE, etc.)
Reply only with: "MODEL", "DATA" or "RESULTS". Be very concise.
"""
try:
response = self.client.chat.completions.create(
model=model_for_detection,
temperature=0.1, # Lower temp for deterministic output
max_tokens=10,
messages=[{"role": "user", "content": f"{prompt}\n\n{content[:1000]}"}]
)
# Extract text from OpenAI response
result = response.choices[0].message.content.strip().upper()
if "MODEL" in result:
return AnalysisType.MATHEMATICAL_MODEL
elif "RESULTS" in result:
return AnalysisType.FITTING_RESULTS
elif "DATA" in result:
return AnalysisType.DATA_FITTING
else:
return AnalysisType.UNKNOWN
except Exception as e:
print(f"Error during analysis type detection: {e}")
return AnalysisType.UNKNOWN
def get_language_prompt_prefix(self, language: str) -> str:
"""Obtiene el prefijo del prompt según el idioma"""
prefixes = {
'en': "Please respond exclusively in English. ",
'es': "Por favor responde exclusivamente en español. ",
'fr': "Veuillez répondre exclusivement en français. ",
'de': "Bitte antworten Sie ausschließlich auf Deutsch. ",
'pt': "Por favor responda exclusivamente em português. "
}
return prefixes.get(language, prefixes['en'])
def analyze_fitting_results(self, data: pd.DataFrame, qwen_model: str, detail_level: str = "detailed",
language: str = "en", additional_specs: str = "") -> Dict:
"""Analiza resultados de ajuste de modelos con soporte multiidioma y especificaciones adicionales"""
# Prepare comprehensive data summary for the model
data_summary = f"""
FITTING RESULTS DATA (as JSON records for parsing):
{json.dumps(data.to_dict('records'), indent=2)}
DATA OVERVIEW:
- Columns: {list(data.columns)}
- Number of models evaluated: {len(data)}
"""
# Get language prefix
lang_prefix = self.get_language_prompt_prefix(language)
# Add user additional specifications if they exist
user_specs_section = f"""
USER ADDITIONAL SPECIFICATIONS / FOCUS AREAS:
{additional_specs}
Please ensure your analysis incorporates these specific requirements and focus areas.
""" if additional_specs else ""
# Prompt enhanced with specific instructions for each level
# Added system message for better role adherence
if detail_level == "detailed":
messages = [
{"role": "system", "content": f"{lang_prefix} You are an expert in biotechnology and mathematical modeling, specializing in the comparative analysis of model fitting results. Your task is to provide a comprehensive, structured analysis of the provided data, focusing on the comparative performance of models across different experimental conditions. Include specific numerical values from the data in your analysis. Use Markdown formatting."}
]
prompt_content = f"""
Analyze these kinetic/biotechnological model fitting results.
{user_specs_section}
DETAIL LEVEL: DETAILED - Provide comprehensive analysis structured BY EXPERIMENT/CONDITION.
PERFORM A COMPREHENSIVE COMPARATIVE ANALYSIS PER EXPERIMENT/CONDITION:
1. **IDENTIFY ALL EXPERIMENTS/CONDITIONS:** List and describe each unique experimental condition present in the data (e.g., pH levels, temperatures, media compositions).
2. **MODELS TESTED PER EXPERIMENT:** For EACH experiment, list ALL fitted mathematical models tested. Classify them (Biomass, Substrate, Product, etc.) if a 'Type' column exists.
3. **DETAILED COMPARISON PER EXPERIMENT:** Create a dedicated section for *each* experiment. Within each experiment section:
* Report the experimental condition.
* For each Variable Type (Biomass, Substrate, Product) analyzed in this experiment (if applicable):
* Identify the **Best Model** based on R² (primary metric) and RMSE (secondary metric). State its name and the exact R² and RMSE values for this experiment.
* List the values of the main parameters obtained for the best model in this specific experiment.
* Provide a ranked list of *all* models tested for this variable type in this experiment, showing Model Name, R², and RMSE.
4. **COMPARATIVE TABLES (Across Experiments):**
* Create a summary table showing the Best Model, R², and RMSE for EACH Variable Type within EACH Experiment.
* Create a table summarizing the performance (Average R², Average RMSE, Number of experiments tested) of key models across *all* experiments where they were applied.
5. **PARAMETER ANALYSIS ACROSS EXPERIMENTS:** Analyze how the key parameters (e.g., μmax, Ks, Xmax) for frequently used or important models change from one experimental condition to another. Identify trends or sensitivities to experimental conditions.
6. **BIOLOGICAL INTERPRETATION & EXPERIMENTAL INSIGHTS:** For each experiment, provide a brief biological interpretation based on the fitting results and parameter values. Discuss whether the parameter values are biologically reasonable for the given conditions. Highlight key differences or findings between experiments.
7. **OVERALL BEST MODELS:** Based on performance across *all* experiments, identify the overall best model(s) for Biomass, Substrate, and Product (if applicable). Justify your selection with average metrics and consistency across conditions, citing numerical evidence.
8. **CONCLUSIONS AND RECOMMENDATIONS:** Summarize the main findings. Recommend which models are most robust or suitable for different types of analysis or specific experimental conditions. Discuss practical implications, confidence levels, and potential considerations for scale-up or further research based on the analysis.
Use clear Markdown headings (`#`, `##`, `###`), bold text (`**text**`), and lists (`- ` or `1. `). Include ALL relevant numerical values from the provided data.
"""
messages.append({"role": "user", "content": f"{prompt_content}\n\n{data_summary}"})
else: # summarized
messages = [
{"role": "system", "content": f"{lang_prefix} You are an expert in biotechnology, providing a concise comparative analysis of mathematical model fitting results across different experiments. Focus on identifying the best models per experiment and overall winners. Include essential numerical information. Use Markdown formatting."}
]
prompt_content = f"""
Analyze these kinetic/biotechnological model fitting results CONCISELY but completely, structured BY EXPERIMENT/CONDITION.
{user_specs_section}
DETAIL LEVEL: SUMMARIZED - Be concise but include all experiments and essential information.
PROVIDE A FOCUSED COMPARATIVE ANALYSIS:
1. **EXPERIMENTS OVERVIEW:** Briefly state the total number of experiments/conditions analyzed and list the types of experimental conditions covered. Mention the variables measured (Biomass, Substrate, Product).
2. **BEST MODELS QUICK SUMMARY BY EXPERIMENT:** For *each* experiment/condition, clearly state:
* The experimental condition name.
* The Best Model found for Biomass (with its R² value).
* The Best Model found for Substrate (with its R² value).
* The Best Model found for Product (with its R² value).
(Only include variable types present in the experiment).
3. **OVERALL BEST MODELS ACROSS ALL EXPERIMENTS:** Identify the single best model overall for Biomass, Substrate, and Product based on average performance or frequency of being the best model across experiments. State their average R² (if applicable) and mention how many experiments they were tested in.
4. **SUMMARY TABLE:** Provide a concise table summarizing the Best Model and its R²/RMSE for each Experiment and Variable Type combination.
5. **KEY FINDINGS & PARAMETER RANGES:** Highlight the most important findings. Briefly mention the observed range or average values for key parameters (e.g., μmax, Ks) across the experiments.
6. **PRACTICAL RECOMMENDATIONS:** Offer concise recommendations on which models are most suitable for which variables or conditions based on the analysis.
Keep it concise but include ALL experiments, model names, and their key R² or RMSE metrics. Use Markdown.
"""
messages.append({"role": "user", "content": f"{prompt_content}\n\n{data_summary}"})
try:
# Main analysis call
response = self.client.chat.completions.create(
model=qwen_model,
messages=messages,
temperature=self.temperature, # Use defined temperature
top_p=self.top_p, # Use defined top_p
max_tokens=QWEN_MODELS.get(qwen_model, {}).get("max_tokens", 4000) # Use model max tokens, default 4000
)
# Extract analysis text
analysis_text = response.choices[0].message.content
# Generate implementation code - This prompt is adjusted to match the analysis structure
# Also using a system message for the code generation role
code_messages = [
{"role": "system", "content": f"{lang_prefix} You are an expert Python programmer specializing in biotechnological modeling and data analysis. Your task is to generate executable Python code based on the provided data and analysis. The code should implement the comparison of models by experiment and variable type, identify best models, and include basic plotting functions. Ensure actual numerical values from the data are used where appropriate for demonstration or analysis within the code."}
]
code_prompt_content = f"""
Generate complete, executable Python code to analyze and visualize the biotechnological model fitting results provided earlier.
Use the actual data, which looks like this (as JSON records):
{json.dumps(data.to_dict('records'), indent=2)}
The code should:
1. Load this specific dataset.
2. Implement a class or functions to analyze model fitting results.
3. Perform analysis BY EXPERIMENT AND VARIABLE TYPE (Biomass, Substrate, Product), identifying the best model for each combination based on R² and RMSE.
4. Identify overall best models across all experiments for each variable type.
5. Include functions to generate visualizations comparing model performance (e.g., R² values) across experiments and variable types.
6. Include comments explaining the logic and findings, especially which model was best for which category/experiment and why.
7. Provide example usage of the code with the embedded data.
Make the code robust and well-commented. Focus on clear data handling, analysis, and visualization.
"""
code_messages.append({"role": "user", "content": code_prompt_content})
code_response = self.client.chat.completions.create(
model=qwen_model, # Use the same Qwen model for consistency
messages=code_messages,
temperature=0.5, # Slightly lower temp for more structured code
top_p=0.9,
max_tokens=3000 # Code might be shorter than analysis
)
# Extract code text, handle potential code block markdown
code_text_raw = code_response.choices[0].message.content
# Remove markdown code block fences if present
if code_text_raw.startswith("```python"):
code_text = code_text_raw.strip().replace("```python\n", "", 1).strip("```")
elif code_text_raw.startswith("```"):
# Handle generic code blocks
code_text = code_text_raw.strip().replace("```\n", "", 1).strip("```")
else:
code_text = code_text_raw
return {
"tipo": "Comparative Analysis of Mathematical Models",
"analisis_completo": analysis_text,
"codigo_implementacion": code_text,
"resumen_datos": {
"n_modelos": len(data),
"columnas": list(data.columns),
"metricas_disponibles": [col for col in data.columns if any(metric in col.lower()
for metric in ['r2', 'rmse', 'aic', 'bic', 'mse'])],
# Safely get best R2 and model name if columns exist
"mejor_r2": data['R2'].max() if 'R2' in data.columns else None,
"mejor_modelo_r2": data.loc[data['R2'].idxmax()]['Model'] if 'R2' in data.columns and 'Model' in data.columns else None,
"datos_completos": data.to_dict('records') # Include all data for code
}
}
except Exception as e:
print(f"Error during AI analysis: {e}")
return {"error": str(e)}
def process_files(files, qwen_model: str, detail_level: str = "detailed",
language: str = "en", additional_specs: str = "") -> Tuple[str, str]:
"""Procesa múltiples archivos con soporte de idioma y especificaciones adicionales"""
# Check if the OpenAI client was successfully initialized
if openai_client is None:
error_msg = TRANSLATIONS.get(language, TRANSLATIONS['en'])['error_no_api']
return error_msg, generate_implementation_code(error_msg) # Return error message and fallback code
processor = FileProcessor()
analyzer = AIAnalyzer(openai_client, model_registry) # Pass the initialized openai_client
results = []
all_code = []
for file in files:
if file is None:
continue
file_name = file.name if hasattr(file, 'name') else "archivo"
file_ext = Path(file_name).suffix.lower()
try:
# Use tempfile to get the actual file path provided by Gradio
if isinstance(file, str):
# Gradio >= 4.0 might pass strings (file paths)
file_path = file
else:
# Handle older Gradio or other file-like objects if necessary
# For now, assume Gradio provides path string
raise TypeError("Unexpected file input type")
file_content = None # Process using path or read bytes as needed
df = None
if file_ext in ['.csv', '.xlsx', '.xls']:
if language == 'es':
results.append(f"## 📊 Análisis de Resultados: {file_name}")
else:
results.append(f"## 📊 Results Analysis: {file_name}")
# Read dataframe directly from path
if file_ext == '.csv':
df = pd.read_csv(file_path)
else:
df = pd.read_excel(file_path)
if df is not None and not df.empty:
analysis_type = analyzer.detect_analysis_type(df)
if analysis_type == AnalysisType.FITTING_RESULTS:
result = analyzer.analyze_fitting_results(
df, qwen_model, detail_level, language, additional_specs
)
if "error" in result:
results.append(f"Error during analysis of {file_name}: {result['error']}")
else:
if language == 'es':
results.append("### 🎯 ANÁLISIS COMPARATIVO DE MODELOS MATEMÁTICOS")
else:
results.append("### 🎯 COMPARATIVE ANALYSIS OF MATHEMATICAL MODELS")
results.append(result.get("analisis_completo", ""))
if "codigo_implementacion" in result:
all_code.append(result["codigo_implementacion"])
elif analysis_type == AnalysisType.DATA_FITTING:
# Handle raw data - Could add a prompt here for Qwen
if language == 'es':
results.append(f"### 📈 Datos Experimentales Detectados: {file_name}")
results.append("Se detectaron datos experimentales. Esta herramienta se especializa en *resultados de ajuste*.")
else:
results.append(f"### 📈 Experimental Data Detected: {file_name}")
results.append("Experimental data was detected. This tool specializes in *fitting results*.")
# Optionally call Qwen to suggest fitting approach
elif analysis_type == AnalysisType.MATHEMATICAL_MODEL:
if language == 'es':
results.append(f"### 🔬 Descripción de Modelo Detectada: {file_name}")
results.append("Se detectó una descripción de modelo matemático. Esta herramienta se especializa en análisis comparativos de *resultados de ajuste*.")
else:
results.append(f"### 🔬 Mathematical Model Description Detected: {file_name}")
results.append("A mathematical model description was detected. This tool specializes in comparative analysis of *fitting results*.")
else: # Unknown
if language == 'es':
results.append(f"### 🤔 Tipo de Contenido Desconocido: {file_name}")
results.append("El tipo de contenido en este archivo no pudo ser determinado. Por favor, sube archivos con resultados de ajuste de modelos (con columnas como 'R2', 'RMSE', 'Model', etc.).")
else:
results.append(f"### 🤔 Unknown Content Type: {file_name}")
results.append("The type of content in this file could not be determined. Please upload files containing model fitting results (with columns like 'R2', 'RMSE', 'Model', etc.).")
else:
if language == 'es':
results.append(f"### ⚠️ Error al leer o archivo vacío: {file_name}")
else:
results.append(f"### ⚠️ Error reading or empty file: {file_name}")
# Add handling for PDF, ZIP if necessary, though the core tool is for CSV/Excel
# elif file_ext == '.pdf':
# # Process PDF text if needed for model description analysis
# text = processor.extract_text_from_pdf(file_content)
# # Could call Qwen to analyze text here if needed
# results.append(f"Processed PDF {file_name}. Text extracted.")
# elif file_ext == '.zip':
# extracted_files = processor.extract_from_zip(file_content)
# # Process extracted files recursively or as needed
# results.append(f"Processed ZIP {file_name}. Found {len(extracted_files)} files.")
else:
if language == 'es':
results.append(f"### ⚠️ Formato de archivo no soportado: {file_name}")
else:
results.append(f"### ⚠️ Unsupported file format: {file_name}")
except Exception as e:
# Catch any unexpected errors during file processing
if language == 'es':
results.append(f"### ❌ Error inesperado al procesar {file_name}: {str(e)}")
else:
results.append(f"### ❌ Unexpected error processing {file_name}: {str(e)}")
results.append("\n---\n") # Separator between files
analysis_text = "\n".join(results)
# Combine all generated code snippets
# The fallback code generator is less critical now that the API generates code
# But keep it as a safeguard or example if API fails
code_text = "\n\n# === Combined Implementation Code ===\n\n" + "\n\n".join(all_code) if all_code else generate_implementation_code(analysis_text)
return analysis_text, code_text
def generate_implementation_code(analysis_results: str) -> str:
"""Generates a default or fallback implementation code structure."""
# This function is less critical if the AI generates code, but kept as a fallback.
# The generated code structure from the AI is preferred.
# This fallback provides a basic template if AI fails to produce code.
code = """
# Fallback Implementation Code (Generated if AI code generation fails)
# This code provides a basic structure for analyzing fitting results.
# Replace placeholder data with your actual results dataframe.
import numpy as np
import pandas as pd
# Matplotlib and Seaborn imports moved here as they are for the generated code
import matplotlib.pyplot as plt
import seaborn as sns
# Visualization configuration
plt.style.use('seaborn-v0_8-darkgrid')
sns.set_palette("husl")
class ExperimentalModelAnalyzer:
\"\"\"
Basic class for comparative analysis of biotechnological models across multiple experiments.
This is a fallback implementation.
\"\"\"
def __init__(self, results_df: pd.DataFrame = None):
self.results_df = results_df
if self.results_df is not None and 'Experiment' not in self.results_df.columns:
# Add a default experiment if none exists
self.results_df['Experiment'] = 'Default_Experiment'
def load_results(self, file_path: str = None, data_dict: dict = None):
\"\"\"Load fitting results from CSV/Excel file or dictionary\"\"\"
if data_dict:
self.results_df = pd.DataFrame(data_dict)
elif file_path:
if file_path.endswith('.csv'):
self.results_df = pd.read_csv(file_path)
else:
self.results_df = pd.read_excel(file_path)
if self.results_df is not None and 'Experiment' not in self.results_df.columns:
self.results_df['Experiment'] = 'Default_Experiment'
if self.results_df is not None:
print(f"✅ Data loaded: {len(self.results_df)} models")
print(f"📊 Available columns: {list(self.results_df.columns)}")
if 'Experiment' in self.results_df.columns:
print(f"🧪 Experiments found: {self.results_df['Experiment'].unique()}")
def analyze_by_experiment(self,
experiment_col: str = 'Experiment',
model_col: str = 'Model',
type_col: str = 'Type',
r2_col: str = 'R2',
rmse_col: str = 'RMSE') -> Dict:
\"\"\"
Analyze models by experiment and variable type.
Identifies best models for biomass, substrate, and product in each experiment.
\"\"\"
if self.results_df is None or self.results_df.empty:
print("⚠️ No data loaded for analysis.")
return {}
results_by_exp = {}
experiments = self.results_df[experiment_col].unique()
print("\\n" + "="*80)
print("📊 ANALYSIS BY EXPERIMENT AND VARIABLE TYPE")
print("="*80)
for exp in experiments:
print(f"\\n🧪 EXPERIMENT: {exp}")
print("-"*50)
exp_data = self.results_df[self.results_df[experiment_col] == exp].copy() # Use copy to avoid SettingWithCopyWarning
results_by_exp[exp] = {}
var_types = exp_data[type_col].unique() if type_col in exp_data.columns else ['All_Types']
for var_type in var_types:
if type_col in exp_data.columns:
var_data = exp_data[exp_data[type_col] == var_type]
else:
var_data = exp_data # Analyze all together if no type column
if not var_data.empty and r2_col in var_data.columns:
# Find best model for this variable type (or all) based on R2
best_idx = var_data[r2_col].idxmax()
best_model = var_data.loc[best_idx]
results_by_exp[exp][var_type] = {
'best_model': best_model.get(model_col, 'N/A'),
'r2': best_model.get(r2_col, np.nan),
'rmse': best_model.get(rmse_col, np.nan),
'all_models': var_data[[model_col, r2_col, rmse_col]].to_dict('records') if {model_col, r2_col, rmse_col}.issubset(var_data.columns) else var_data.to_dict('records')
}
print(f"\\n 📈 {var_type.upper()}:")
print(f" Best Model: {results_by_exp[exp][var_type]['best_model']}")
print(f" R² = {results_by_exp[exp][var_type]['r2']:.4f}" if not np.isnan(results_by_exp[exp][var_type]['r2']) else "R² = N/A")
print(f" RMSE = {results_by_exp[exp][var_type]['rmse']:.4f}" if not np.isnan(results_by_exp[exp][var_type]['rmse']) else "RMSE = N/A")
# Show all models for this variable
if 'all_models' in results_by_exp[exp][var_type]:
print(f"\\n All {var_type} models tested:")
for model_entry in results_by_exp[exp][var_type]['all_models']:
r2_val = model_entry.get(r2_col, np.nan)
rmse_val = model_entry.get(rmse_col, np.nan)
model_name = model_entry.get(model_col, 'N/A')
print(f" - {model_name}: R²={r2_val:.4f}" if not np.isnan(r2_val) else f" - {model_name}: R²=N/A", end="")
print(f", RMSE={rmse_val:.4f}" if not np.isnan(rmse_val) else ", RMSE=N/A")
elif not var_data.empty:
print(f"\\n 📈 {var_type.upper()}:")
print(f" No '{r2_col}' column found for comparison.")
else:
print(f"\\n 📈 {var_type.upper()}:")
print(f" No data found for this variable type.")
self.best_models_by_experiment = results_by_exp
return results_by_exp
def _determine_overall_best_models(self):
\"\"\"Determine the best models across all experiments\"\"\"
if not hasattr(self, 'best_models_by_experiment') or not self.best_models_by_experiment:
print("⚠️ No experimental analysis available to determine overall models.")
return {}
print("\\n" + "="*80)
print("🏆 OVERALL BEST MODELS ACROSS ALL EXPERIMENTS")
print("="*80)
model_performance = {}
for exp, exp_results in self.best_models_by_experiment.items():
for var_type, var_results in exp_results.items():
if var_type not in model_performance:
model_performance[var_type] = {}
# Use the list of all models analyzed for this type in this experiment
models_in_exp_type = var_results.get('all_models', [])
for model_data in models_in_exp_type:
model_name = model_data.get('Model', 'Unknown Model') # Use .get for safety
r2_val = model_data.get('R2')
rmse_val = model_data.get('RMSE')
if model_name not in model_performance[var_type]:
model_performance[var_type][model_name] = {
'r2_values': [],
'rmse_values': [],
'experiments': []
}
if r2_val is not None:
model_performance[var_type][model_name]['r2_values'].append(r2_val)
if rmse_val is not None:
model_performance[var_type][model_name]['rmse_values'].append(rmse_val)
if exp not in model_performance[var_type][model_name]['experiments']:
model_performance[var_type][model_name]['experiments'].append(exp)
overall_best_models = {}
# Calculate average performance and select best
for var_type, models in model_performance.items():
best_avg_r2 = -np.inf # Use -infinity to ensure any valid R2 is better
best_model_info = None
print(f"\\n📊 {var_type.upper()} MODELS:")
if not models:
print(" No models found for this type.")
continue
for model_name, perf_data in models.items():
# Calculate average R2, ignoring NaNs
r2_values = [v for v in perf_data['r2_values'] if v is not None and not np.isnan(v)]
avg_r2 = np.mean(r2_values) if r2_values else -np.inf # Handle case with no valid R2
# Calculate average RMSE, ignoring NaNs
rmse_values = [v for v in perf_data['rmse_values'] if v is not None and not np.isnan(v)]
avg_rmse = np.mean(rmse_values) if rmse_values else np.inf # Handle case with no valid RMSE
n_exp = len(perf_data['experiments'])
print(f" {model_name}:")
print(f" Average R² = {avg_r2:.4f}" if avg_r2 > -np.inf else " Average R² = N/A")
print(f" Average RMSE = {avg_rmse:.4f}" if avg_rmse < np.inf else " Average RMSE = N/A")
print(f" Tested in {n_exp} experiments")
# Selection logic: prioritize higher average R2. Could add secondary criteria (e.g., lower RMSE, consistency)
if avg_r2 > best_avg_r2:
best_avg_r2 = avg_r2
best_model_info = {
'name': model_name,
'avg_r2': avg_r2,
'avg_rmse': avg_rmse,
'n_experiments': n_exp
}
elif avg_r2 == best_avg_r2 and avg_rmse < (best_model_info['avg_rmse'] if best_model_info and best_model_info['avg_rmse'] < np.inf else np.inf):
# Tie-breaking: prefer lower average RMSE if R2 is the same
best_model_info = {
'name': model_name,
'avg_r2': avg_r2,
'avg_rmse': avg_rmse,
'n_experiments': n_exp
}
if best_model_info and var_type.lower() in ['biomass', 'substrate', 'product', 'all_types']:
# Assign to standard keys if they exist
target_key = var_type.lower() if var_type.lower() in ['biomass', 'substrate', 'product'] else 'overall'
overall_best_models[target_key] = best_model_info
print(f"\\n 🏆 BEST {var_type.upper()} MODEL: {best_model_info['name']} (Avg R²={best_model_info['avg_r2']:.4f})" if best_model_info['avg_r2'] > -np.inf else f"\\n 🏆 BEST {var_type.upper()} MODEL: {best_model_info['name']} (Avg R²=N/A)")
elif best_model_info:
# Add other types found
overall_best_models[var_type] = best_model_info
self.overall_best_models = overall_best_models
return overall_best_models
def create_comparison_visualizations(self):
\"\"\"Create visualizations comparing models across experiments\"\"\"
if not hasattr(self, 'best_models_by_experiment') or not self.best_models_by_experiment:
print("⚠️ No analysis results to visualize.")
return # Exit if no data
# Prepare data for visualization - focusing on R2 for best models per experiment/type
plot_data = []
for exp, results in self.best_models_by_experiment.items():
for var_type, var_results in results.items():
plot_data.append({
'Experiment': exp,
'Variable_Type': var_type,
'Best_Model': var_results.get('best_model'),
'R2': var_results.get('r2')
})
plot_df = pd.DataFrame(plot_data)
plot_df = plot_df.dropna(subset=['R2']) # Only plot entries with R2
if plot_df.empty:
print("⚠️ No valid R² data available for visualization.")
return
# Use Seaborn for better aesthetics
plt.figure(figsize=(14, 8))
sns.barplot(data=plot_df, x='Experiment', y='R2', hue='Variable_Type', palette='viridis')
plt.title('Best Model R² Comparison by Experiment and Variable Type', fontsize=16)
plt.xlabel('Experiment', fontsize=12)
plt.ylabel('R²', fontsize=12)
plt.xticks(rotation=45, ha='right')
plt.ylim(0, 1.05) # R2 is typically between 0 and 1
plt.legend(title='Variable Type')
plt.tight_layout() # Adjust layout to prevent labels overlapping
plt.grid(axis='y', linestyle='--', alpha=0.7)
plt.show()
# Optional: Add more plots if needed, e.g., parameter trends
def generate_summary_table(self) -> pd.DataFrame:
\"\"\"Generate a summary table of best models by experiment and type\"\"\"
if not hasattr(self, 'best_models_by_experiment') or not self.best_models_by_experiment:
print("⚠️ No analysis results to generate summary table.")
return pd.DataFrame()
summary_data = []
for exp, results in self.best_models_by_experiment.items():
for var_type, var_results in results.items():
summary_data.append({
'Experiment': exp,
'Variable_Type': var_type,
'Best_Model': var_results.get('best_model', 'N/A'),
'R2': var_results.get('r2', np.nan),
'RMSE': var_results.get('rmse', np.nan)
})
summary_df = pd.DataFrame(summary_data)
print("\\n📋 SUMMARY TABLE: BEST MODELS BY EXPERIMENT AND VARIABLE TYPE")
print("="*80)
if not summary_df.empty:
# Format R2 and RMSE for display
summary_df_display = summary_df.copy()
if 'R2' in summary_df_display.columns:
summary_df_display['R2'] = summary_df_display['R2'].apply(lambda x: f'{x:.4f}' if pd.notna(x) else 'N/A')
if 'RMSE' in summary_df_display.columns:
summary_df_display['RMSE'] = summary_df_display['RMSE'].apply(lambda x: f'{x:.4f}' if pd.notna(x) else 'N/A')
print(summary_df_display.to_string(index=False))
else:
print("No data to display in the summary table.")
return summary_df
# Example usage for the fallback code structure
# Note: The AI-generated code should ideally replace this example usage
# but this part demonstrates how the generated code might be used.
if __name__ == "__main__":
print("🧬 Experimental Model Comparison System (Fallback Code Example)")
print("="*60)
# --- Placeholder Example Data ---
# This data structure should match the format the AI expects and uses
# in the generated code. It includes 'Experiment' and 'Type'.
fallback_example_data = {
'Experiment': ['pH_7.0', 'pH_7.0', 'pH_7.0', 'pH_7.5', 'pH_7.5', 'pH_7.5',
'pH_7.0', 'pH_7.0', 'pH_7.5', 'pH_7.5',
'pH_7.0', 'pH_7.0', 'pH_7.5', 'pH_7.5'],
'Model': ['Monod', 'Logistic', 'Gompertz', 'Monod', 'Logistic', 'Gompertz',
'First_Order', 'Monod_Substrate', 'First_Order', 'Monod_Substrate',
'Luedeking_Piret', 'Linear', 'Luedeking_Piret', 'Linear'],
'Type': ['Biomass', 'Biomass', 'Biomass', 'Biomass', 'Biomass', 'Biomass',
'Substrate', 'Substrate', 'Substrate', 'Substrate',
'Product', 'Product', 'Product', 'Product'],
'R2': [0.9845, 0.9912, 0.9956, 0.9789, 0.9834, 0.9901,
0.9723, 0.9856, 0.9698, 0.9812,
0.9634, 0.9512, 0.9687, 0.9423],
'RMSE': [0.0234, 0.0189, 0.0145, 0.0267, 0.0223, 0.0178,
0.0312, 0.0245, 0.0334, 0.0289,
0.0412, 0.0523, 0.0389, 0.0567],
'mu_max': [0.45, 0.48, 0.52, 0.42, 0.44, 0.49,
np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan],
'Ks': [np.nan, np.nan, np.nan, np.nan, np.nan, np.nan,
2.1, 1.8, 2.3, 1.9, np.nan, np.nan, np.nan, np.nan]
}
# Create analyzer instance using the fallback data
analyzer = ExperimentalModelAnalyzer(results_df=pd.DataFrame(fallback_example_data))
# Analyze by experiment
analysis_results = analyzer.analyze_by_experiment()
# Determine overall best models
overall_best = analyzer._determine_overall_best_models()
print(f"Overall Best Models (Determined by Fallback): {overall_best}")
# Create visualizations (will use the best_models_by_experiment attribute)
print("\\nAttempting to create visualizations...")
try:
analyzer.create_comparison_visualizations()
except Exception as e:
print(f"Error creating visualization: {e}")
print("This might happen if data structure or plotting logic is not fully compatible.")
# Generate summary table
summary_table = analyzer.generate_summary_table()
print("\\n✨ Fallback Analysis complete!")
# --- End of Fallback Code Example ---
# Estado global para almacenar resultados
class AppState:
def __init__(self):
self.current_analysis = ""
self.current_code = ""
self.current_language = "en"
app_state = AppState()
def export_report(export_format: str, language: str) -> Tuple[str, str]:
"""Exporta el reporte al formato seleccionado"""
if not app_state.current_analysis:
error_msg = {
'en': "No analysis available to export",
'es': "No hay análisis disponible para exportar",
'fr': "Aucune analyse disponible pour exporter",
'de': "Keine Analyse zum Exportieren verfügbar",
'pt': "Nenhuma análise disponível para exportar"
}
return error_msg.get(language, error_msg['en']), ""
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
try:
# Ensure ReportExporter is used correctly with its static methods
if export_format == "DOCX":
filename = f"biotech_analysis_report_{timestamp}.docx"
ReportExporter.export_to_docx(app_state.current_analysis, filename, language)
else: # PDF
filename = f"biotech_analysis_report_{timestamp}.pdf"
ReportExporter.export_to_pdf(app_state.current_analysis, filename, language)
success_msg = TRANSLATIONS[language]['report_exported']
return f"{success_msg} {filename}", filename
except Exception as e:
# Provide more specific error details for export
return f"Error exporting report: {str(e)}", ""
# Interfaz Gradio con soporte multiidioma y temas
def create_interface():
# Estado inicial
current_theme = "light"
current_language = "en"
def update_interface_language(language):
"""Actualiza el idioma de la interfaz"""
app_state.current_language = language
t = TRANSLATIONS[language]
# Build model choices string with descriptions for info text
model_info_str = ""
# Default model might change based on QWEN_MODELS keys
default_model_key = list(QWEN_MODELS.keys())[0] if QWEN_MODELS else "Qwen/Qwen3-14B"
if default_model_key in QWEN_MODELS:
model_info_str = f"{t['best_for']}: {QWEN_MODELS[default_model_key]['best_for']}"
return [
gr.update(value=f"# {t['title']}"), # title_text
gr.update(value=t['subtitle']), # subtitle_text
gr.update(label=t['upload_files']), # files_input
gr.update(label=t['select_model'], info=model_info_str), # model_selector
gr.update(label=t['select_language']), # language_selector
gr.update(label=t['select_theme']), # theme_selector
gr.update(label=t['detail_level'], choices=[(t['detailed'], "detailed"), (t['summarized'], "summarized")]), # detail_level
gr.update(label=t['additional_specs'], placeholder=t['additional_specs_placeholder']), # additional_specs
gr.update(value=t['analyze_button']), # analyze_btn
gr.update(label=t['export_format']), # export_format
gr.update(value=t['export_button']), # export_btn
gr.update(label=t['comparative_analysis']), # analysis_output
gr.update(label=t['implementation_code']), # code_output
gr.update(label=t['data_format']) # data_format_accordion
]
def process_and_store(files, model, detail, language, additional_specs):
"""Procesa files y almacena resultados"""
if not files:
error_msg = TRANSLATIONS[language]['error_no_files']
app_state.current_analysis = error_msg
app_state.current_code = generate_implementation_code(error_msg) # Provide fallback code even on file error
return error_msg, app_state.current_code
# Assuming files is a list of strings (filepaths) from Gradio
analysis, code = process_files(files, model, detail, language, additional_specs)
# Store results in app state
app_state.current_analysis = analysis
app_state.current_code = code
return analysis, code
# Get default model key safely
default_qwen_model_key = list(QWEN_MODELS.keys())[0] if QWEN_MODELS else ""
default_qwen_model_info = QWEN_MODELS.get(default_qwen_model_key, {})
default_model_info_str = f"{TRANSLATIONS[current_language]['best_for']}: {default_qwen_model_info.get('best_for', 'N/A')}"
with gr.Blocks(theme=THEMES[current_theme]) as demo:
# Componentes de UI
with gr.Row():
with gr.Column(scale=3):
title_text = gr.Markdown(f"# {TRANSLATIONS[current_language]['title']}")
subtitle_text = gr.Markdown(TRANSLATIONS[current_language]['subtitle'])
with gr.Column(scale=1):
with gr.Row():
language_selector = gr.Dropdown(
choices=[("English", "en"), ("Español", "es"), ("Français", "fr"),
("Deutsch", "de"), ("Português", "pt")],
value=current_language,
label=TRANSLATIONS[current_language]['select_language'],
interactive=True
)
theme_selector = gr.Dropdown(
choices=[("Light", "light"), ("Dark", "dark")],
value=current_theme,
label=TRANSLATIONS[current_language]['select_theme'],
interactive=True
)
with gr.Row():
with gr.Column(scale=1):
files_input = gr.File(
label=TRANSLATIONS[current_language]['upload_files'],
file_count="multiple",
file_types=[".csv", ".xlsx", ".xls"], # Focusing on data files
type="filepath" # Get file path as string
)
model_selector = gr.Dropdown(
choices=list(QWEN_MODELS.keys()),
value=default_qwen_model_key,
label=TRANSLATIONS[current_language]['select_model'],
info=default_model_info_str
)
detail_level = gr.Radio(
choices=[
(TRANSLATIONS[current_language]['detailed'], "detailed"),
(TRANSLATIONS[current_language]['summarized'], "summarized")
],
value="detailed",
label=TRANSLATIONS[current_language]['detail_level']
)
# Nueva entrada para especificaciones adicionales
additional_specs = gr.Textbox(
label=TRANSLATIONS[current_language]['additional_specs'],
placeholder=TRANSLATIONS[current_language]['additional_specs_placeholder'],
lines=3,
max_lines=5,
interactive=True
)
analyze_btn = gr.Button(
TRANSLATIONS[current_language]['analyze_button'],
variant="primary",
size="lg"
)
gr.Markdown("---")
export_format = gr.Radio(
choices=["DOCX", "PDF"],
value="PDF",
label=TRANSLATIONS[current_language]['export_format']
)
export_btn = gr.Button(
TRANSLATIONS[current_language]['export_button'],
variant="secondary"
)
export_status = gr.Textbox(
label="Export Status",
interactive=False,
visible=False
)
export_file = gr.File(
label="Download Report",
visible=False
)
with gr.Column(scale=2):
analysis_output = gr.Markdown(
label=TRANSLATIONS[current_language]['comparative_analysis']
)
code_output = gr.Code(
label=TRANSLATIONS[current_language]['implementation_code'],
language="python",
interactive=True,
lines=20
)
data_format_accordion = gr.Accordion(
label=TRANSLATIONS[current_language]['data_format'],
open=False
)
with data_format_accordion:
gr.Markdown("""
### Expected CSV/Excel structure:
| Experiment | Model | Type | R2 | RMSE | AIC | BIC | mu_max | Ks | Parameters |
|------------|-------|------|-----|------|-----|-----|--------|-------|------------|
| pH_7.0 | Monod | Biomass | 0.985 | 0.023 | -45.2 | -42.1 | 0.45 | 2.1 | {...} |
| pH_7.0 | Logistic | Biomass | 0.976 | 0.031 | -42.1 | -39.5 | 0.42 | - | {...} |
| pH_7.0 | First_Order | Substrate | 0.992 | 0.018 | -48.5 | -45.2 | - | 1.8 | {...} |
| pH_7.5 | Monod | Biomass | 0.978 | 0.027 | -44.1 | -41.2 | 0.43 | 2.2 | {...} |
**Important columns:**
- **Experiment**: Experimental condition identifier (Optional, but recommended for comparative analysis)
- **Model**: Model name (e.g., Monod, Logistic)
- **Type**: Variable type (Biomass, Substrate, Product) (Optional, but recommended for analysis by type)
- **R2, RMSE**: Fit quality metrics (At least one needed for comparison)
- **Parameters**: Columns for model-specific parameters (e.g., mu_max, Ks, Xmax)
""")
# Definir ejemplos (Update example paths if necessary)
examples = gr.Examples(
examples=[
[["examples/biomass_models_comparison.csv"], list(QWEN_MODELS.keys())[0] if QWEN_MODELS else "", "detailed", ""],
[["examples/substrate_kinetics_results.xlsx"], list(QWEN_MODELS.keys())[0] if QWEN_MODELS else "", "summarized", "Focus on temperature effects"]
],
inputs=[files_input, model_selector, detail_level, additional_specs],
label=TRANSLATIONS[current_language]['examples']
)
# Eventos - Actualizado para incluir additional_specs
language_selector.change(
update_interface_language,
inputs=[language_selector],
outputs=[
title_text, subtitle_text, files_input, model_selector,
language_selector, theme_selector, detail_level, additional_specs,
analyze_btn, export_format, export_btn, analysis_output,
code_output, data_format_accordion
]
)
def change_theme(theme_name):
"""Cambia el tema de la interfaz"""
# Note: Dynamic theme switching in Gradio might require a page reload for full effect.
# This function primarily triggers the UI update but the theme itself is set at gr.Blocks creation.
# Returning gr.Info is a common way to indicate the change.
# To truly change theme dynamically, you might need Javascript or specific Gradio features.
return gr.Info("Theme applied. May require page refresh for full effect on all components.")
theme_selector.change(
change_theme,
inputs=[theme_selector],
outputs=[] # No direct UI output change from this function in the current structure
)
analyze_btn.click(
fn=process_and_store,
inputs=[files_input, model_selector, detail_level, language_selector, additional_specs],
outputs=[analysis_output, code_output]
)
def handle_export(format, language):
status, file = export_report(format, language)
# Check if the file was successfully created before making the download button visible
if file and os.path.exists(file):
return gr.update(value=status, visible=True), gr.update(value=file, visible=True, label=f"Download {format}")
else:
# Hide the download button if no file was created
return gr.update(value=status, visible=True), gr.update(value=None, visible=False)
export_btn.click(
fn=handle_export,
inputs=[export_format, language_selector],
outputs=[export_status, export_file]
)
return demo
# Función principal
def main():
# Check for the specific API key required for Qwen
if openai_client is None:
print("⚠️ NEBIUS_API_KEY environment variable not found. Please configure it.")
return gr.Interface(
fn=lambda: TRANSLATIONS['en']['error_no_api'], # Display error message in UI
inputs=None, # No inputs needed for just showing error
outputs=gr.Textbox(label="Configuration Error"),
title=TRANSLATIONS['en']['title'],
description="Failed to initialize AI client.",
theme=THEMES['light'] # Use a default theme
)
# Proceed with creating the interface if client is initialized
return create_interface()
# Para ejecución local
if __name__ == "__main__":
# Ensure Gradio example paths exist for the examples section
if not os.path.exists("examples"):
os.makedirs("examples")
# Create dummy example files if they don't exist
if not os.path.exists("examples/biomass_models_comparison.csv"):
dummy_csv_data = {'Experiment': ['ExpA', 'ExpA', 'ExpB', 'ExpB'],
'Model': ['Monod', 'Logistic', 'Monod', 'Logistic'],
'Type': ['Biomass', 'Biomass', 'Biomass', 'Biomass'],
'R2': [0.98, 0.97, 0.95, 0.96],
'RMSE': [0.02, 0.03, 0.04, 0.035],
'mu_max': [0.5, 0.48, 0.4, 0.38]}
pd.DataFrame(dummy_csv_data).to_csv("examples/biomass_models_comparison.csv", index=False)
if not os.path.exists("examples/substrate_kinetics_results.xlsx"):
dummy_excel_data = {'Experiment': ['Temp25', 'Temp25', 'Temp30', 'Temp30'],
'Model': ['First_Order', 'Monod_Substrate', 'First_Order', 'Monod_Substrate'],
'Type': ['Substrate', 'Substrate', 'Substrate', 'Substrate'],
'R2': [0.99, 0.98, 0.97, 0.985],
'RMSE': [0.015, 0.02, 0.025, 0.018],
'Ks': [1.5, 1.2, 1.8, 1.4]}
pd.DataFrame(dummy_excel_data).to_excel("examples/substrate_kinetics_results.xlsx", index=False)
demo = main()
if demo:
demo.launch(
server_name="0.0.0.0",
server_port=7860,
share=False
) |