Spaces:
No application file
No application file
Update app.py
Browse files
app.py
CHANGED
|
@@ -87,9 +87,22 @@ class Question(BaseModel):
|
|
| 87 |
|
| 88 |
@validator('text')
|
| 89 |
def validate_text(cls, v):
|
| 90 |
-
if not v.strip():
|
| 91 |
raise ValueError("Question text cannot be empty")
|
| 92 |
return v.strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
class Answer(BaseModel):
|
| 95 |
"""Enhanced answer structure with validation"""
|
|
@@ -622,36 +635,54 @@ async def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 622 |
|
| 623 |
for question in questions:
|
| 624 |
try:
|
| 625 |
-
|
| 626 |
-
|
| 627 |
-
|
| 628 |
-
|
| 629 |
-
|
| 630 |
-
|
| 631 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 632 |
)
|
| 633 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 634 |
processed_answers.append({
|
| 635 |
-
'task_id': question
|
| 636 |
'submitted_answer': answer.text
|
| 637 |
})
|
| 638 |
|
| 639 |
# Add to results log for display
|
| 640 |
results_log.append({
|
| 641 |
-
'Task ID': question
|
| 642 |
-
'Question':
|
| 643 |
-
'Answer': answer.text,
|
| 644 |
'Confidence': f"{answer.confidence:.2f}",
|
| 645 |
'Processing Time': f"{answer.processing_time:.2f}s"
|
| 646 |
})
|
| 647 |
|
| 648 |
-
logger.info(f"Processed question {question
|
| 649 |
|
| 650 |
except Exception as e:
|
| 651 |
-
logger.error(f"Error processing question
|
|
|
|
| 652 |
results_log.append({
|
| 653 |
'Task ID': question.get('task_id', 'unknown'),
|
| 654 |
-
'Question': question.get('text', 'unknown'),
|
| 655 |
'Answer': f"Error: {str(e)}",
|
| 656 |
'Confidence': '0.00',
|
| 657 |
'Processing Time': '0.00s'
|
|
@@ -659,7 +690,10 @@ async def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 659 |
continue
|
| 660 |
|
| 661 |
if not processed_answers:
|
| 662 |
-
|
|
|
|
|
|
|
|
|
|
| 663 |
|
| 664 |
# Submit answers
|
| 665 |
submission_result = await api_submit_answers(processed_answers, profile.username)
|
|
@@ -667,7 +701,8 @@ async def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 667 |
status_message = (
|
| 668 |
f"Successfully processed {len(processed_answers)} questions\n"
|
| 669 |
f"Username: {profile.username}\n"
|
| 670 |
-
f"Submission Result: {submission_result.get('message', 'No message')}"
|
|
|
|
| 671 |
)
|
| 672 |
|
| 673 |
return status_message, pd.DataFrame(results_log)
|
|
@@ -675,6 +710,9 @@ async def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 675 |
except Exception as e:
|
| 676 |
error_msg = f"Error in processing: {str(e)}"
|
| 677 |
logger.error(error_msg)
|
|
|
|
|
|
|
|
|
|
| 678 |
return error_msg, pd.DataFrame([{
|
| 679 |
'Task ID': 'Error',
|
| 680 |
'Question': 'Error occurred during processing',
|
|
|
|
| 87 |
|
| 88 |
@validator('text')
|
| 89 |
def validate_text(cls, v):
|
| 90 |
+
if not v or not v.strip():
|
| 91 |
raise ValueError("Question text cannot be empty")
|
| 92 |
return v.strip()
|
| 93 |
+
|
| 94 |
+
@classmethod
|
| 95 |
+
def from_api_response(cls, data: Dict[str, Any]) -> 'Question':
|
| 96 |
+
"""Create a Question instance from API response data"""
|
| 97 |
+
return cls(
|
| 98 |
+
text=data.get('question', data.get('text', '')),
|
| 99 |
+
context=data.get('context', ''),
|
| 100 |
+
attachments=data.get('attachments', []),
|
| 101 |
+
metadata={
|
| 102 |
+
'task_id': data.get('task_id', ''),
|
| 103 |
+
'original_data': data
|
| 104 |
+
}
|
| 105 |
+
)
|
| 106 |
|
| 107 |
class Answer(BaseModel):
|
| 108 |
"""Enhanced answer structure with validation"""
|
|
|
|
| 635 |
|
| 636 |
for question in questions:
|
| 637 |
try:
|
| 638 |
+
# Log the raw question for debugging
|
| 639 |
+
logger.debug(f"Raw question data: {json.dumps(question, indent=2)}")
|
| 640 |
+
|
| 641 |
+
# Extract question text safely
|
| 642 |
+
question_text = question.get('question', question.get('text', ''))
|
| 643 |
+
if not question_text:
|
| 644 |
+
logger.error(f"Question text not found in: {question}")
|
| 645 |
+
continue
|
| 646 |
+
|
| 647 |
+
# Create Question object with proper validation
|
| 648 |
+
agent_question = Question(
|
| 649 |
+
text=question_text,
|
| 650 |
+
context=question.get('context', ''),
|
| 651 |
+
attachments=question.get('attachments', []),
|
| 652 |
+
metadata={
|
| 653 |
+
'task_id': question.get('task_id', ''),
|
| 654 |
+
'original_data': question
|
| 655 |
+
}
|
| 656 |
)
|
| 657 |
|
| 658 |
+
answer = await agent.process_question(agent_question)
|
| 659 |
+
|
| 660 |
+
if not answer or not answer.text:
|
| 661 |
+
logger.error(f"No valid answer produced for question {question.get('task_id', 'unknown')}")
|
| 662 |
+
continue
|
| 663 |
+
|
| 664 |
processed_answers.append({
|
| 665 |
+
'task_id': question.get('task_id', ''),
|
| 666 |
'submitted_answer': answer.text
|
| 667 |
})
|
| 668 |
|
| 669 |
# Add to results log for display
|
| 670 |
results_log.append({
|
| 671 |
+
'Task ID': question.get('task_id', ''),
|
| 672 |
+
'Question': question_text[:200] + ('...' if len(question_text) > 200 else ''), # Truncate long questions
|
| 673 |
+
'Answer': answer.text[:200] + ('...' if len(answer.text) > 200 else ''), # Truncate long answers
|
| 674 |
'Confidence': f"{answer.confidence:.2f}",
|
| 675 |
'Processing Time': f"{answer.processing_time:.2f}s"
|
| 676 |
})
|
| 677 |
|
| 678 |
+
logger.info(f"Processed question {question.get('task_id', '')} with confidence {answer.confidence}")
|
| 679 |
|
| 680 |
except Exception as e:
|
| 681 |
+
logger.error(f"Error processing question: {str(e)}")
|
| 682 |
+
logger.error(f"Question data: {json.dumps(question, indent=2)}")
|
| 683 |
results_log.append({
|
| 684 |
'Task ID': question.get('task_id', 'unknown'),
|
| 685 |
+
'Question': question.get('question', question.get('text', 'unknown')),
|
| 686 |
'Answer': f"Error: {str(e)}",
|
| 687 |
'Confidence': '0.00',
|
| 688 |
'Processing Time': '0.00s'
|
|
|
|
| 690 |
continue
|
| 691 |
|
| 692 |
if not processed_answers:
|
| 693 |
+
logger.error("No answers were successfully processed")
|
| 694 |
+
if results_log:
|
| 695 |
+
return "No answers were successfully processed - check results table for errors", pd.DataFrame(results_log)
|
| 696 |
+
return "No answers were successfully processed - no questions were valid", None
|
| 697 |
|
| 698 |
# Submit answers
|
| 699 |
submission_result = await api_submit_answers(processed_answers, profile.username)
|
|
|
|
| 701 |
status_message = (
|
| 702 |
f"Successfully processed {len(processed_answers)} questions\n"
|
| 703 |
f"Username: {profile.username}\n"
|
| 704 |
+
f"Submission Result: {submission_result.get('message', 'No message')}\n"
|
| 705 |
+
f"Total Questions: {len(questions)}, Successfully Processed: {len(processed_answers)}"
|
| 706 |
)
|
| 707 |
|
| 708 |
return status_message, pd.DataFrame(results_log)
|
|
|
|
| 710 |
except Exception as e:
|
| 711 |
error_msg = f"Error in processing: {str(e)}"
|
| 712 |
logger.error(error_msg)
|
| 713 |
+
# Log the full traceback for debugging
|
| 714 |
+
import traceback
|
| 715 |
+
logger.error(f"Full traceback: {traceback.format_exc()}")
|
| 716 |
return error_msg, pd.DataFrame([{
|
| 717 |
'Task ID': 'Error',
|
| 718 |
'Question': 'Error occurred during processing',
|