Spaces:
Sleeping
Sleeping
File size: 58,749 Bytes
7bbd836 82ca3ff 7bbd836 82ca3ff 7bbd836 37184d4 2e0e95f 37184d4 7bbd836 37184d4 2e0e95f 7bbd836 82ca3ff 7bbd836 2e0e95f 7bbd836 2e0e95f 7bbd836 fd0d61e 798f2fe fd0d61e 798f2fe fd0d61e 2e0e95f fd0d61e 2e0e95f fd0d61e 798f2fe fd0d61e 7bbd836 3e1ef65 7bbd836 3e1ef65 7bbd836 3e1ef65 7bbd836 82ca3ff 7bbd836 82ca3ff 7bbd836 82ca3ff 7bbd836 37184d4 7bbd836 37184d4 7bbd836 5657336 7bbd836 82ca3ff 7bbd836 5083460 82ca3ff 7bbd836 82ca3ff 7bbd836 798f2fe 353a6ce 7bbd836 353a6ce 7bbd836 65b972a 7bbd836 fd0d61e 798f2fe fd0d61e 7bbd836 fd0d61e 7bbd836 fd0d61e 7bbd836 fd0d61e 7bbd836 65b972a 7bbd836 65b972a 7bbd836 65b972a 7bbd836 65b972a 7bbd836 65b972a 7bbd836 65b972a 7bbd836 65b972a 7bbd836 65b972a 7bbd836 65b972a 7bbd836 65b972a 7bbd836 65b972a 7bbd836 65b972a 7bbd836 65b972a 7bbd836 65b972a 7bbd836 82ca3ff 7bbd836 |
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 |
# file_upload_interface.py
"""File Upload Interface for Enhanced Verification Modes.
Provides interface for uploading CSV files, validating content,
batch processing with progress tracking, and exporting results.
Requirements: 4.1, 4.3, 4.4, 4.5, 4.6, 4.7, 12.1, 12.2, 12.3, 12.4, 12.5
"""
import gradio as gr
import tempfile
import os
import uuid
from typing import List, Dict, Tuple, Optional, Any
from datetime import datetime
from src.core.file_processing_service import FileProcessingService
from src.core.verification_models import (
EnhancedVerificationSession,
VerificationRecord,
TestMessage,
FileUploadResult,
)
from src.core.verification_store import JSONVerificationStore
from src.core.ai_client import AIClientManager
from src.config.prompts import SYSTEM_PROMPT_ENTRY_CLASSIFIER
from src.core.enhanced_progress_tracker import EnhancedProgressTracker, VerificationMode
from src.interface.enhanced_progress_components import ProgressTrackingMixin
from src.interface.ui_consistency_components import (
StandardizedComponents,
ClassificationDisplay,
ProgressDisplay,
ErrorDisplay,
SessionDisplay,
HelpDisplay
)
class FileUploadInterfaceController(ProgressTrackingMixin):
"""Controller for file upload mode interface."""
def __init__(self):
"""Initialize the file upload interface controller."""
super().__init__(VerificationMode.FILE_UPLOAD)
self.file_processor = FileProcessingService()
self.store = JSONVerificationStore()
self.ai_client = AIClientManager()
# Optional per-session model overrides (UI Model Settings tab)
self.model_overrides = {}
self.ai_client.set_model_overrides(self.model_overrides)
# Optional per-session prompt overrides (UI Edit Prompts tab)
self.prompt_overrides = {}
self.ai_client.set_prompt_overrides(self.prompt_overrides)
self.current_session = None
self.current_file_result = None
self.current_message_index = 0
self.batch_processing_start_time = None
def set_model_overrides(self, overrides: Optional[Dict[str, str]] = None) -> None:
"""Set per-session model overrides from the UI."""
self.model_overrides = dict(overrides or {})
self.ai_client.set_model_overrides(self.model_overrides)
def set_prompt_overrides(self, overrides: Optional[Dict[str, str]] = None) -> None:
"""Set per-session prompt overrides from the UI."""
self.prompt_overrides = dict(overrides or {})
self.ai_client.set_prompt_overrides(self.prompt_overrides)
def process_uploaded_file(self, file_path: str) -> Tuple[bool, str, Optional[FileUploadResult], str]:
"""
Process an uploaded file and return validation results.
Args:
file_path: Path to the uploaded file
Returns:
Tuple of (success, status_message, file_result, preview_html)
"""
if not file_path or not file_path.endswith('.csv'):
return False, "β No file uploaded", None, ""
try:
# Process the file
file_result = self.file_processor.process_uploaded_file(file_path)
if file_result.validation_errors:
# File has validation errors
error_details = self.file_processor.get_validation_error_details(file_result.validation_errors)
error_html = self._format_validation_errors(error_details)
status_msg = f"β File validation failed ({len(file_result.validation_errors)} errors)"
return False, status_msg, file_result, error_html
else:
# File is valid - generate preview
preview_html = self._generate_file_preview(file_result)
status_msg = f"β
File processed successfully: {file_result.valid_rows} valid test cases found"
return True, status_msg, file_result, preview_html
except Exception as e:
error_msg = f"β Error processing file: {str(e)}"
return False, error_msg, None, ""
def _format_validation_errors(self, error_details: Dict[str, Any]) -> str:
"""
Format validation errors as HTML using standardized components.
Args:
error_details: Error details from file processor
Returns:
HTML string with formatted errors
"""
# Create main error message
main_message = f"File validation failed ({error_details['total_errors']} errors)"
# Prepare suggestions list
suggestions = []
# Add first 10 errors as suggestions
errors_to_show = error_details['errors'][:10]
suggestions.extend(errors_to_show)
if len(error_details['errors']) > 10:
remaining = len(error_details['errors']) - 10
suggestions.append(f"... and {remaining} more errors")
# Add format suggestions
if error_details.get('suggestions'):
suggestions.extend(error_details['suggestions'])
# Add format help
format_help = error_details.get('format_help', {})
if format_help:
suggestions.extend([
f"Required columns: {', '.join(format_help.get('required_columns', []))}",
f"Valid classifications: {', '.join(format_help.get('valid_classifications', []))}",
"Supported delimiters (CSV): comma, semicolon, tab"
])
return ErrorDisplay.create_error_html_display(
main_message,
"error",
suggestions
)
def _generate_file_preview(self, file_result: FileUploadResult) -> str:
"""
Generate HTML preview of successfully processed file.
Args:
file_result: File processing result
Returns:
HTML string with file preview
"""
html = f"""
<div style="font-family: system-ui; padding: 1em; background-color: #f0fdf4; border-left: 4px solid #16a34a; border-radius: 4px;">
<h4 style="color: #16a34a; margin-top: 0;">β
File Preview: {file_result.original_filename}</h4>
<div style="margin-bottom: 1em;">
<strong>File Statistics:</strong><br>
β’ Format: {file_result.file_format.upper()}<br>
β’ Total rows: {file_result.total_rows}<br>
β’ Valid test cases: {file_result.valid_rows}<br>
β’ Upload time: {file_result.upload_timestamp.strftime('%Y-%m-%d %H:%M:%S')}
</div>
"""
if file_result.parsed_test_cases:
html += """
<div style="margin-bottom: 1em;">
<strong>Sample Test Cases (first 5):</strong>
</div>
<div style="background-color: white; border-radius: 4px; padding: 0.5em; border: 1px solid #d1d5db;">
<table style="width: 100%; border-collapse: collapse;">
<thead>
<tr style="background-color: #f9fafb;">
<th style="padding: 0.5em; text-align: left; border-bottom: 1px solid #e5e7eb;">#</th>
<th style="padding: 0.5em; text-align: left; border-bottom: 1px solid #e5e7eb;">Message Preview</th>
<th style="padding: 0.5em; text-align: left; border-bottom: 1px solid #e5e7eb;">Expected Classification</th>
</tr>
</thead>
<tbody>
"""
# Show first 5 test cases
for i, test_case in enumerate(file_result.parsed_test_cases[:5], 1):
message_preview = test_case.text[:80] + "..." if len(test_case.text) > 80 else test_case.text
classification_badge = self._get_classification_badge(test_case.pre_classified_label)
html += f"""
<tr>
<td style="padding: 0.5em; border-bottom: 1px solid #f3f4f6;">{i}</td>
<td style="padding: 0.5em; border-bottom: 1px solid #f3f4f6;">{message_preview}</td>
<td style="padding: 0.5em; border-bottom: 1px solid #f3f4f6;">{classification_badge}</td>
</tr>
"""
html += """
</tbody>
</table>
</div>
"""
html += """
<div style="margin-top: 1em; padding: 0.75em; background-color: #ecfdf5; border-radius: 4px; border: 1px solid #a7f3d0;">
<p style="margin: 0; color: #065f46;">
<strong>β
Ready for batch processing!</strong><br>
Click "Start Batch Processing" to begin verification of all test cases.
</p>
</div>
</div>
"""
return html
def _get_classification_badge(self, classification: str) -> str:
"""
Get HTML badge for classification using standardized components.
Args:
classification: Classification label
Returns:
HTML badge string
"""
return ClassificationDisplay.format_classification_html_badge(classification)
def start_batch_processing(self, verifier_name: str, file_result: FileUploadResult) -> Tuple[bool, str, Optional[EnhancedVerificationSession]]:
"""
Start batch processing session.
Args:
verifier_name: Name of the verifier
file_result: File processing result
Returns:
Tuple of (success, message, session)
"""
if not verifier_name.strip():
return False, "β Please enter your name to start verification", None
if not file_result or not file_result.parsed_test_cases:
return False, "β No valid test cases to process", None
try:
# Create enhanced verification session
session_id = uuid.uuid4().hex
session = EnhancedVerificationSession(
session_id=session_id,
verifier_name=verifier_name.strip(),
dataset_id=file_result.file_id,
dataset_name=f"File Upload: {file_result.original_filename}",
mode_type="file_upload",
mode_metadata={
"file_id": file_result.file_id,
"original_filename": file_result.original_filename,
"file_format": file_result.file_format,
"total_file_rows": file_result.total_rows,
"valid_file_rows": file_result.valid_rows,
},
file_source=file_result.original_filename,
total_messages=len(file_result.parsed_test_cases),
message_queue=[tc.message_id for tc in file_result.parsed_test_cases],
current_queue_index=0,
)
# Save session
self.store.save_session(session)
# Set current session and file result
self.current_session = session
self.current_file_result = file_result
self.current_message_index = 0
# Setup progress tracking for batch processing
self.setup_progress_tracking(len(file_result.parsed_test_cases))
return True, f"β
Batch processing started for {len(file_result.parsed_test_cases)} test cases", session
except Exception as e:
return False, f"β Error starting batch processing: {str(e)}", None
def get_current_message_for_batch_processing(self) -> Tuple[Optional[TestMessage], Optional[Dict[str, Any]]]:
"""
Get current message for batch processing.
Returns:
Tuple of (test_message, classification_result)
"""
if not self.current_session or not self.current_file_result:
return None, None
if self.current_message_index >= len(self.current_file_result.parsed_test_cases):
return None, None
# Get current test message
test_message = self.current_file_result.parsed_test_cases[self.current_message_index]
try:
# Record batch processing start time for progress tracking
self.batch_processing_start_time = datetime.now()
# Call AI classifier using the same approach as manual input
user_prompt = f"Please analyze this patient message for spiritual distress:\n\n{test_message.text}"
response = self.ai_client.call_entry_classifier_api(
system_prompt=SYSTEM_PROMPT_ENTRY_CLASSIFIER,
user_prompt=user_prompt,
temperature=0.3,
)
# Parse the response to extract classification details
classification_result = self._parse_classification_response(response)
return test_message, classification_result
except Exception as e:
# Return error result
error_result = {
"decision": "error",
"confidence": 0.0,
"indicators": [f"Classification error: {str(e)}"],
"error": str(e)
}
return test_message, error_result
def _parse_classification_response(self, response: str) -> Dict[str, Any]:
"""
Parse AI response to extract classification details.
Args:
response: Raw AI response
Returns:
Dictionary with classification details
"""
# Default classification structure
classification = {
"decision": "unknown",
"confidence": 0.0,
"indicators": [],
"raw_response": response
}
# Simple parsing logic - look for key indicators in response
response_lower = response.lower()
# Determine decision based on keywords
if "red" in response_lower or "severe" in response_lower or "high risk" in response_lower:
classification["decision"] = "red"
classification["confidence"] = 0.8
elif "yellow" in response_lower or "moderate" in response_lower or "potential" in response_lower:
classification["decision"] = "yellow"
classification["confidence"] = 0.7
elif "green" in response_lower or "low" in response_lower or "no distress" in response_lower:
classification["decision"] = "green"
classification["confidence"] = 0.9
# Extract indicators (simple keyword matching)
indicators = []
indicator_keywords = [
"hopelessness", "despair", "meaninglessness", "isolation",
"anger at god", "spiritual pain", "guilt", "shame",
"questioning faith", "loss of purpose", "existential crisis"
]
for keyword in indicator_keywords:
if keyword in response_lower:
indicators.append(keyword.title())
if not indicators:
indicators = ["General spiritual assessment"]
classification["indicators"] = indicators
return classification
def run_batch_classification(self, progress: Optional[gr.Progress] = None) -> Tuple[bool, str, Dict[str, Any]]:
"""Run classification for the whole uploaded dataset and persist results.
File Upload Mode is already labeled (ground truth provided in the file), so we
don't need interactive message-by-message verification. Instead, we:
- classify every message
- store the model output as reasoning in `verifier_notes`
- mark each record as correct/incorrect by comparing to ground truth
"""
if not self.current_session or not self.current_file_result:
return False, "β No active session", {}
total = len(self.current_file_result.parsed_test_cases)
if total == 0:
return False, "β No messages to process", {}
try:
# Reset any prior run state
self.current_session.verifications = []
self.current_session.verified_count = 0
self.current_session.correct_count = 0
self.current_session.incorrect_count = 0
self.current_session.verified_message_ids = []
self.setup_progress_tracking(total)
for idx, test_message in enumerate(self.current_file_result.parsed_test_cases):
if progress is not None:
progress(
(idx) / total,
desc=f"Processing {idx + 1}/{total}"
)
self.batch_processing_start_time = datetime.now()
user_prompt = (
"Please analyze this patient message for spiritual distress:\n\n"
f"{test_message.text}"
)
raw_response = self.ai_client.call_entry_classifier_api(
system_prompt=SYSTEM_PROMPT_ENTRY_CLASSIFIER,
user_prompt=user_prompt,
temperature=0.3,
model_override=self.model_overrides.get("EntryClassifier"),
)
classification_result = self._parse_classification_response(raw_response)
classifier_decision = classification_result.get("decision", "green")
if classifier_decision not in ["green", "yellow", "red"]:
classifier_decision = "green"
ground_truth = test_message.pre_classified_label
if ground_truth not in ["green", "yellow", "red"]:
ground_truth = "green"
is_correct = classifier_decision == ground_truth
verification_record = VerificationRecord(
message_id=test_message.message_id,
original_message=test_message.text,
classifier_decision=classifier_decision,
classifier_confidence=classification_result.get("confidence", 0.0),
classifier_indicators=classification_result.get("indicators", []),
ground_truth_label=ground_truth,
verifier_notes=raw_response, # store full LLM output as reasoning
is_correct=is_correct,
)
self.current_session.verifications.append(verification_record)
self.current_session.verified_count += 1
self.current_session.verified_message_ids.append(test_message.message_id)
if is_correct:
self.current_session.correct_count += 1
else:
self.current_session.incorrect_count += 1
self.record_verification_with_timing(is_correct, self.batch_processing_start_time)
self.current_session.current_queue_index = idx + 1
self.current_session.is_complete = True
self.current_session.completed_at = datetime.now()
if progress is not None:
progress(1.0, desc=f"Completed {total}/{total}")
self.store.save_session(self.current_session)
accuracy = (
(self.current_session.correct_count / self.current_session.verified_count * 100)
if self.current_session.verified_count
else 0
)
stats = {
"processed": self.current_session.verified_count,
"total": total,
"correct": self.current_session.correct_count,
"incorrect": self.current_session.incorrect_count,
"accuracy": accuracy,
"is_complete": True,
}
return True, f"β
Batch classification completed. Accuracy: {accuracy:.1f}%", stats
except Exception as e:
return False, f"β Error during batch classification: {str(e)}", {}
def export_batch_results_with_reasoning(self, format_type: str) -> Tuple[bool, str, Optional[str]]:
"""Export results including LLM reasoning.
We rely on `verifier_notes` field to carry reasoning (raw model output).
"""
return self.export_batch_results(format_type)
def submit_batch_verification(self, is_correct: bool, correction: Optional[str] = None, notes: str = "") -> Tuple[bool, str, Dict[str, Any]]:
"""
Submit verification for current message in batch processing.
Args:
is_correct: Whether the classification is correct
correction: Correct classification if incorrect
notes: Additional notes
Returns:
Tuple of (success, message, session_stats)
"""
if not self.current_session or not self.current_file_result:
return False, "β No active batch processing session", {}
if self.current_message_index >= len(self.current_file_result.parsed_test_cases):
return False, "β No more messages to process", {}
try:
# Get current test message and classification
test_message = self.current_file_result.parsed_test_cases[self.current_message_index]
current_message, classification_result = self.get_current_message_for_batch_processing()
if not current_message or not classification_result:
return False, "β Error getting current message", {}
# Create verification record
# Ensure valid classification values (green, yellow, red only)
classifier_decision = classification_result.get("decision", "green")
if classifier_decision not in ["green", "yellow", "red"]:
classifier_decision = "green" # Safe fallback
ground_truth = correction if correction else test_message.pre_classified_label
if ground_truth not in ["green", "yellow", "red"]:
ground_truth = "green" # Safe fallback
verification_record = VerificationRecord(
message_id=test_message.message_id,
original_message=test_message.text,
classifier_decision=classifier_decision,
classifier_confidence=classification_result.get("confidence", 0.0),
classifier_indicators=classification_result.get("indicators", []),
ground_truth_label=ground_truth,
verifier_notes=notes,
is_correct=is_correct,
)
# Add to session
self.current_session.verifications.append(verification_record)
self.current_session.verified_count += 1
self.current_session.verified_message_ids.append(test_message.message_id)
if is_correct:
self.current_session.correct_count += 1
else:
self.current_session.incorrect_count += 1
# Record verification with timing for progress tracking
self.record_verification_with_timing(is_correct, self.batch_processing_start_time)
# Move to next message
self.current_message_index += 1
self.current_session.current_queue_index = self.current_message_index
# Check if session is complete
if self.current_message_index >= len(self.current_file_result.parsed_test_cases):
self.current_session.is_complete = True
self.current_session.completed_at = datetime.now()
# Save session
self.store.save_session(self.current_session)
# Calculate stats
stats = {
"processed": self.current_session.verified_count,
"total": self.current_session.total_messages,
"correct": self.current_session.correct_count,
"incorrect": self.current_session.incorrect_count,
"accuracy": (self.current_session.correct_count / self.current_session.verified_count * 100) if self.current_session.verified_count > 0 else 0,
"is_complete": self.current_session.is_complete,
}
if self.current_session.is_complete:
message = f"β
Batch processing completed! Final accuracy: {stats['accuracy']:.1f}%"
else:
message = f"β
Verification recorded. Progress: {stats['processed']}/{stats['total']}"
return True, message, stats
except Exception as e:
return False, f"β Error submitting verification: {str(e)}", {}
def export_batch_results(self, format_type: str) -> Tuple[bool, str, Optional[str]]:
"""
Export batch processing results.
Args:
format_type: Export format ("csv", "json")
Returns:
Tuple of (success, message, file_path)
"""
if not self.current_session:
return False, "β No active session to export", None
try:
if format_type == "csv":
content = self.store.export_to_csv(self.current_session.session_id)
# Save to temporary file
temp_file = tempfile.NamedTemporaryFile(mode='w', suffix='.csv', delete=False)
temp_file.write(content)
temp_file.close()
file_path = temp_file.name
elif format_type == "json":
content = self.store.export_to_json(self.current_session.session_id)
# Save to temporary file
temp_file = tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False)
temp_file.write(content)
temp_file.close()
file_path = temp_file.name
else:
return False, f"β Unsupported export format: {format_type}", None
if file_path:
return True, f"β
Results exported to {format_type.upper()} format", file_path
else:
return False, f"β Failed to export results in {format_type.upper()} format", None
except Exception as e:
return False, f"β Export error: {str(e)}", None
def get_enhanced_progress_info(self) -> Dict[str, Any]:
"""
Get enhanced progress information for display.
Returns:
Dictionary containing progress information
"""
if not hasattr(self, 'progress_tracker') or not self.progress_tracker:
return {
"progress_display": "π Progress: Ready to start",
"accuracy_display": "π― Current Accuracy: No verifications yet",
"speed_display": "β‘ Processing Speed: Calculating...",
"time_display": "β±οΈ Time: Not started",
"error_display": "",
"stats_summary": "No active session"
}
return {
"progress_display": self.progress_tracker.get_progress_display(),
"accuracy_display": self.progress_tracker.get_accuracy_display(),
"speed_display": self.progress_tracker.get_processing_speed_display(),
"time_display": self.progress_tracker.get_time_tracking_display(),
"error_display": self.progress_tracker.get_error_display(),
"stats_summary": self._get_session_stats_summary()
}
def record_batch_processing_error(self, error_message: str, can_continue: bool = True) -> None:
"""
Record a batch processing error.
Args:
error_message: Description of the error
can_continue: Whether processing can continue
"""
if hasattr(self, 'progress_tracker') and self.progress_tracker:
self.progress_tracker.record_error(error_message, can_continue)
def pause_batch_processing(self) -> Tuple[bool, bool, bool]:
"""
Pause the current batch processing session.
Returns:
Tuple of control button visibility states
"""
if hasattr(self, 'progress_tracker') and self.progress_tracker:
return self.handle_session_pause()
return False, False, True
def resume_batch_processing(self) -> Tuple[bool, bool, bool]:
"""
Resume the current batch processing session.
Returns:
Tuple of control button visibility states
"""
if hasattr(self, 'progress_tracker') and self.progress_tracker:
return self.handle_session_resume()
return True, False, True
def _get_session_stats_summary(self) -> str:
"""Get formatted session statistics summary."""
if not self.current_session:
return "No active session"
accuracy = (self.current_session.correct_count / self.current_session.verified_count * 100) if self.current_session.verified_count > 0 else 0
return f"""
**Batch Processing Session:**
- File: {self.current_session.file_source or 'Unknown'}
- Processed: {self.current_session.verified_count}/{self.current_session.total_messages}
- Accuracy: {accuracy:.1f}%
- Correct: {self.current_session.correct_count}
- Incorrect: {self.current_session.incorrect_count}
- Processing Speed: {self.progress_tracker.get_processing_speed_display() if hasattr(self, 'progress_tracker') else 'Unknown'}
"""
def get_template_files(self) -> Tuple[str, Optional[bytes]]:
"""
Get template files for download.
Returns:
Tuple of (csv_content, xlsx_bytes)
"""
csv_content = self.file_processor.generate_csv_template()
xlsx_bytes = None # Removed XLSX template generation
return csv_content, xlsx_bytes
def create_file_upload_interface(model_overrides_state: Optional[gr.State] = None) -> gr.Blocks:
"""
Create the complete file upload mode interface.
Returns:
Gradio Blocks component for file upload mode
"""
controller = FileUploadInterfaceController()
# Apply any provided model overrides at build time.
# Note: this is safe even if the state is mutated later, because the click
# handlers also refresh overrides before calls.
if model_overrides_state is not None:
try:
controller.set_model_overrides(model_overrides_state.value or {})
except Exception:
# Don't fail UI creation if state isn't initialized yet
pass
with gr.Blocks() as file_upload_interface:
# Headers and back button are in parent interface
# Application state
current_file_result_state = gr.State(value=None)
current_session_state = gr.State(value=None)
# File upload section
with gr.Row():
with gr.Column(scale=2):
gr.Markdown("## π€ Upload Test File")
file_upload = gr.File(
label="Select CSV File",
file_types=[".csv"],
type="filepath"
)
with gr.Row():
process_file_btn = StandardizedComponents.create_primary_button("Process File", "π")
process_file_btn.scale = 2
clear_file_btn = StandardizedComponents.create_secondary_button("Clear", "ποΈ")
clear_file_btn.scale = 1
with gr.Column(scale=1):
gr.Markdown("## π Template Files")
gr.Markdown("Download template files to see the required format:")
with gr.Column():
# Use DownloadButton for direct file download
download_csv_template_btn = gr.DownloadButton(
"π Download CSV Template",
value="exports/template_test_messages.csv",
size="sm"
)
# XLSX template removed (CSV-only workflow)
gr.Markdown("### π Format Requirements")
gr.Markdown("""
**Required columns:**
- `message` (or `text`): Patient message text
- `expected_classification` (or `classification`): Expected result
**Valid classifications:**
- `green`: No distress
- `yellow`: Potential distress
- `red`: Severe distress
**Supported formats:**
- CSV with comma, semicolon, or tab delimiters
""")
# File processing results section
file_results_section = gr.Row(visible=False)
with file_results_section:
with gr.Column():
gr.Markdown("## π File Processing Results")
file_preview_display = gr.HTML(
value="",
label="File Preview"
)
# Batch processing section
batch_processing_section = gr.Row(visible=False)
with batch_processing_section:
with gr.Column():
gr.Markdown("## π Batch Processing")
# Processing controls
with gr.Row():
with gr.Column(scale=2):
verifier_name_input = gr.Textbox(
label="Verifier Name",
placeholder="Enter your name...",
interactive=True
)
with gr.Column(scale=1):
start_batch_btn = StandardizedComponents.create_primary_button(
"Start Batch Processing",
"π",
"lg"
)
# Visual progress bar (updates during batch classification)
batch_progress_bar = gr.Progress()
# Progress text display
batch_progress_display = gr.Markdown(
"Ready to start batch processing",
label="Progress"
)
# Export results (visible after batch completes)
gr.Markdown("### πΎ Download Results")
with gr.Row():
export_csv_btn = gr.DownloadButton(
label="Download CSV",
variant="secondary",
visible=False,
)
export_json_btn = gr.DownloadButton(
label="Download JSON",
variant="secondary",
visible=False,
)
# Message processing section (initially hidden)
message_processing_section = gr.Row(visible=False)
with message_processing_section:
with gr.Column(scale=2):
# Current message display
current_message_display = gr.Textbox(
label="π Current Message",
interactive=False,
lines=4
)
# Expected vs Actual comparison
with gr.Row():
with gr.Column():
expected_classification_display = gr.Markdown(
"Expected: Loading...",
label="π Expected Classification"
)
with gr.Column():
actual_classification_display = gr.Markdown(
"Actual: Loading...",
label="π― AI Classification"
)
# Classification details
classifier_confidence_display = gr.Markdown(
"Confidence: Loading...",
label="π Confidence Level"
)
classifier_indicators_display = gr.Markdown(
"Indicators: Loading...",
label="π Detected Indicators"
)
# Verification buttons
with gr.Row():
correct_classification_btn = StandardizedComponents.create_primary_button("Correct", "β")
correct_classification_btn.scale = 1
incorrect_classification_btn = StandardizedComponents.create_stop_button("Incorrect", "β")
incorrect_classification_btn.scale = 1
# Correction section (initially hidden)
correction_section = gr.Row(visible=False)
with correction_section:
correction_selector = ClassificationDisplay.create_classification_radio()
correction_notes = gr.Textbox(
label="Notes (Optional)",
placeholder="Why is this incorrect?",
lines=2,
interactive=True
)
submit_correction_btn = StandardizedComponents.create_primary_button("Submit", "β")
with gr.Column(scale=1):
# Batch statistics
gr.Markdown("### π Batch Statistics")
batch_stats_display = gr.Markdown(
"""
**Messages Processed:** 0
**Correct Classifications:** 0
**Incorrect Classifications:** 0
**Accuracy:** 0%
**Processing Speed:** 0 msg/min
""",
label="Statistics"
)
gr.Markdown("### πΎ Export Results")
gr.Markdown("Download buttons appear in the 'Batch Processing' section after completion.")
# Status messages
status_message = gr.Markdown("", visible=True)
# Event handlers
def on_process_file(file_path):
"""Handle file processing."""
if not file_path:
return (
gr.Row(visible=False), # file_results_section
gr.Row(visible=False), # batch_processing_section
"", # file_preview_display
None, # current_file_result_state
"β Please select a file to upload" # status_message
)
success, status_msg, file_result, preview_html = controller.process_uploaded_file(file_path)
if success:
return (
gr.Row(visible=True), # file_results_section
gr.Row(visible=True), # batch_processing_section
preview_html, # file_preview_display
file_result, # current_file_result_state
status_msg # status_message
)
else:
return (
gr.Row(visible=True), # file_results_section
gr.Row(visible=False), # batch_processing_section
preview_html, # file_preview_display
file_result, # current_file_result_state
status_msg # status_message
)
def on_clear_file():
"""Handle file clearing."""
return (
gr.Row(visible=False), # file_results_section
gr.Row(visible=False), # batch_processing_section
gr.Row(visible=False), # message_processing_section
"", # file_preview_display
None, # current_file_result_state
None, # current_session_state
gr.DownloadButton(visible=False), # export_csv_btn
gr.DownloadButton(visible=False), # export_json_btn
"File cleared" # status_message
)
def on_start_batch_processing(verifier_name, file_result):
"""Handle starting batch processing."""
if not file_result:
return (
gr.Row(visible=False), # message_processing_section
None, # current_session_state
"β No file processed" # status_message
)
success, message, session = controller.start_batch_processing(verifier_name, file_result)
if success:
# Simplified behavior: dataset is already labeled, so run full batch
# classification immediately and generate results for export.
run_ok, run_msg, stats = controller.run_batch_classification(progress=batch_progress_bar)
if run_ok:
progress_text = f"β
Completed: {stats.get('processed', 0)}/{stats.get('total', 0)} messages"
return (
gr.Row(visible=False), # message_processing_section (not used in simplified flow)
session, # current_session_state
"", # current_message_display
"", # expected_classification_display
"", # actual_classification_display
"", # classifier_confidence_display
"", # classifier_indicators_display
progress_text, # batch_progress_display
gr.DownloadButton(visible=True), # export_csv_btn
gr.DownloadButton(visible=True), # export_json_btn
run_msg # status_message
)
return (
gr.Row(visible=False), # message_processing_section
session, # current_session_state
"", # current_message_display
"", # expected_classification_display
"", # actual_classification_display
"", # classifier_confidence_display
"", # classifier_indicators_display
"β Batch classification failed", # batch_progress_display
gr.DownloadButton(visible=False), # export_csv_btn
gr.DownloadButton(visible=False), # export_json_btn
run_msg # status_message
)
else:
return (
gr.Row(visible=False), # message_processing_section
None, # current_session_state
"", # current_message_display
"", # expected_classification_display
"", # actual_classification_display
"", # classifier_confidence_display
"", # classifier_indicators_display
"", # batch_progress_display
gr.DownloadButton(visible=False), # export_csv_btn
gr.DownloadButton(visible=False), # export_json_btn
message # status_message
)
def on_correct_classification():
"""Handle correct classification feedback."""
success, message, stats = controller.submit_batch_verification(True)
if success and not stats.get('is_complete', False):
# Load next message
current_message, classification_result = controller.get_current_message_for_batch_processing()
if current_message:
expected_badge = controller._get_classification_badge(current_message.pre_classified_label)
actual_badge = controller._get_classification_badge(classification_result.get('decision', 'unknown'))
confidence_text = f"π {classification_result.get('confidence', 0) * 100:.1f}% confident"
indicators_text = "π " + ", ".join(classification_result.get('indicators', ['No indicators']))
progress_text = f"Progress: {stats['processed'] + 1} of {stats['total']} messages"
stats_text = f"""
**Messages Processed:** {stats['processed']}
**Correct Classifications:** {stats['correct']}
**Incorrect Classifications:** {stats['incorrect']}
**Accuracy:** {stats['accuracy']:.1f}%
**Processing Speed:** {stats['processed']} msg/min
"""
return (
current_message.text, # current_message_display
f"Expected: {expected_badge}", # expected_classification_display
f"AI Result: {actual_badge}", # actual_classification_display
confidence_text, # classifier_confidence_display
indicators_text, # classifier_indicators_display
progress_text, # batch_progress_display
stats_text, # batch_stats_display
gr.Row(visible=False), # correction_section
gr.DownloadButton(visible=True), # export_csv_btn
gr.DownloadButton(visible=True), # export_json_btn
message # status_message
)
else:
# Batch complete
stats_text = f"""
**Batch Complete!**
**Messages Processed:** {stats['processed']}
**Correct Classifications:** {stats['correct']}
**Incorrect Classifications:** {stats['incorrect']}
**Final Accuracy:** {stats['accuracy']:.1f}%
"""
return (
"Batch processing completed!", # current_message_display
"β
All messages processed", # expected_classification_display
"", # actual_classification_display
"", # classifier_confidence_display
"", # classifier_indicators_display
"β
Batch processing complete", # batch_progress_display
stats_text, # batch_stats_display
gr.Row(visible=False), # correction_section
gr.DownloadButton(visible=True), # export_csv_btn
gr.DownloadButton(visible=True), # export_json_btn
message # status_message
)
else:
return (
gr.Textbox(value=""), # current_message_display (no change)
gr.Markdown(value=""), # expected_classification_display (no change)
gr.Markdown(value=""), # actual_classification_display (no change)
gr.Markdown(value=""), # classifier_confidence_display (no change)
gr.Markdown(value=""), # classifier_indicators_display (no change)
gr.Markdown(value=""), # batch_progress_display (no change)
gr.Markdown(value=""), # batch_stats_display (no change)
gr.Row(visible=False), # correction_section
gr.DownloadButton(visible=False), # export_csv_btn
gr.DownloadButton(visible=False), # export_json_btn
message # status_message
)
def on_incorrect_classification():
"""Handle incorrect classification - show correction options."""
return (
gr.Row(visible=True), # correction_section
"Please select the correct classification" # status_message
)
def on_submit_correction(correction, notes):
"""Handle correction submission."""
success, message, stats = controller.submit_batch_verification(
False, correction, notes
)
if success and not stats.get('is_complete', False):
# Load next message
current_message, classification_result = controller.get_current_message_for_batch_processing()
if current_message:
expected_badge = controller._get_classification_badge(current_message.pre_classified_label)
actual_badge = controller._get_classification_badge(classification_result.get('decision', 'unknown'))
confidence_text = f"π {classification_result.get('confidence', 0) * 100:.1f}% confident"
indicators_text = "π " + ", ".join(classification_result.get('indicators', ['No indicators']))
progress_text = f"Progress: {stats['processed'] + 1} of {stats['total']} messages"
stats_text = f"""
**Messages Processed:** {stats['processed']}
**Correct Classifications:** {stats['correct']}
**Incorrect Classifications:** {stats['incorrect']}
**Accuracy:** {stats['accuracy']:.1f}%
**Processing Speed:** {stats['processed']} msg/min
"""
return (
current_message.text, # current_message_display
f"Expected: {expected_badge}", # expected_classification_display
f"AI Result: {actual_badge}", # actual_classification_display
confidence_text, # classifier_confidence_display
indicators_text, # classifier_indicators_display
progress_text, # batch_progress_display
stats_text, # batch_stats_display
gr.Row(visible=False), # correction_section
"", # correction_notes (clear)
gr.DownloadButton(visible=True), # export_csv_btn
gr.DownloadButton(visible=True), # export_json_btn
message # status_message
)
else:
# Batch complete
stats_text = f"""
**Batch Complete!**
**Messages Processed:** {stats['processed']}
**Correct Classifications:** {stats['correct']}
**Incorrect Classifications:** {stats['incorrect']}
**Final Accuracy:** {stats['accuracy']:.1f}%
"""
return (
"Batch processing completed!", # current_message_display
"β
All messages processed", # expected_classification_display
"", # actual_classification_display
"", # classifier_confidence_display
"", # classifier_indicators_display
"β
Batch processing complete", # batch_progress_display
stats_text, # batch_stats_display
gr.Row(visible=False), # correction_section
"", # correction_notes (clear)
gr.DownloadButton(visible=True), # export_csv_btn
gr.DownloadButton(visible=True), # export_json_btn
message # status_message
)
else:
return (
gr.Textbox(value=""), # current_message_display (no change)
gr.Markdown(value=""), # expected_classification_display (no change)
gr.Markdown(value=""), # actual_classification_display (no change)
gr.Markdown(value=""), # classifier_confidence_display (no change)
gr.Markdown(value=""), # classifier_indicators_display (no change)
gr.Markdown(value=""), # batch_progress_display (no change)
gr.Markdown(value=""), # batch_stats_display (no change)
gr.Row(visible=True), # correction_section (keep visible)
notes, # correction_notes (keep)
gr.DownloadButton(visible=False), # export_csv_btn
gr.DownloadButton(visible=False), # export_json_btn
message # status_message
)
def on_export_results_file(format_type):
"""Handle results export and return the generated file for download."""
success, message, file_path = controller.export_batch_results(format_type)
if success and file_path:
return file_path
return None
def on_download_csv_template():
"""Handle CSV template download."""
csv_content, _ = controller.get_template_files()
# Create temporary file
temp_file = tempfile.NamedTemporaryFile(mode='w', suffix='.csv', delete=False)
temp_file.write(csv_content)
temp_file.close()
return temp_file.name
# Bind event handlers
process_file_btn.click(
on_process_file,
inputs=[file_upload],
outputs=[
file_results_section,
batch_processing_section,
file_preview_display,
current_file_result_state,
status_message
]
)
clear_file_btn.click(
on_clear_file,
outputs=[
file_results_section,
batch_processing_section,
message_processing_section,
file_preview_display,
current_file_result_state,
current_session_state,
export_csv_btn,
export_json_btn,
status_message
]
)
start_batch_btn.click(
on_start_batch_processing,
inputs=[verifier_name_input, current_file_result_state],
outputs=[
message_processing_section,
current_session_state,
current_message_display,
expected_classification_display,
actual_classification_display,
classifier_confidence_display,
classifier_indicators_display,
batch_progress_display,
export_csv_btn,
export_json_btn,
status_message
]
)
correct_classification_btn.click(
on_correct_classification,
outputs=[
current_message_display,
expected_classification_display,
actual_classification_display,
classifier_confidence_display,
classifier_indicators_display,
batch_progress_display,
batch_stats_display,
correction_section,
export_csv_btn,
export_json_btn,
status_message
]
)
incorrect_classification_btn.click(
on_incorrect_classification,
outputs=[correction_section, status_message]
)
submit_correction_btn.click(
on_submit_correction,
inputs=[correction_selector, correction_notes],
outputs=[
current_message_display,
expected_classification_display,
actual_classification_display,
classifier_confidence_display,
classifier_indicators_display,
batch_progress_display,
batch_stats_display,
correction_section,
correction_notes,
export_csv_btn,
export_json_btn,
status_message
]
)
export_csv_btn.click(lambda: on_export_results_file("csv"), outputs=[export_csv_btn])
export_json_btn.click(lambda: on_export_results_file("json"), outputs=[export_json_btn])
download_csv_template_btn.click(
on_download_csv_template,
outputs=[gr.File(visible=False)]
)
return file_upload_interface |