Spaces:
No application file
No application file
Update app.py
Browse files
app.py
CHANGED
|
@@ -604,23 +604,22 @@ async def api_submit_answers(
|
|
| 604 |
async def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 605 |
"""Process and submit all questions with enhanced error handling."""
|
| 606 |
if not profile:
|
| 607 |
-
|
| 608 |
-
status_code=401,
|
| 609 |
-
detail="Authentication required"
|
| 610 |
-
)
|
| 611 |
|
| 612 |
try:
|
| 613 |
# Get questions
|
| 614 |
questions = await api_get_questions()
|
| 615 |
if not questions:
|
| 616 |
logger.warning("No questions received from API")
|
| 617 |
-
return
|
| 618 |
|
| 619 |
# Create agent
|
| 620 |
agent = create_agent(openai, Path("knowledge_base"))
|
| 621 |
|
| 622 |
# Process questions
|
| 623 |
processed_answers = []
|
|
|
|
|
|
|
| 624 |
for question in questions:
|
| 625 |
try:
|
| 626 |
answer = await agent.process_question(
|
|
@@ -637,36 +636,52 @@ async def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 637 |
'submitted_answer': answer.text
|
| 638 |
})
|
| 639 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 640 |
logger.info(f"Processed question {question['task_id']} with confidence {answer.confidence}")
|
| 641 |
|
| 642 |
except Exception as e:
|
| 643 |
logger.error(f"Error processing question {question.get('task_id', 'unknown')}: {str(e)}")
|
| 644 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 645 |
continue
|
| 646 |
|
| 647 |
if not processed_answers:
|
| 648 |
-
|
| 649 |
-
return {"status": "error", "message": "No answers were successfully processed"}
|
| 650 |
|
| 651 |
# Submit answers
|
| 652 |
submission_result = await api_submit_answers(processed_answers, profile.username)
|
| 653 |
|
| 654 |
-
|
| 655 |
-
"
|
| 656 |
-
"
|
| 657 |
-
"
|
| 658 |
-
|
| 659 |
-
|
| 660 |
-
|
| 661 |
-
}
|
| 662 |
-
}
|
| 663 |
|
| 664 |
except Exception as e:
|
| 665 |
-
|
| 666 |
-
|
| 667 |
-
|
| 668 |
-
|
| 669 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 670 |
|
| 671 |
# Create Gradio interface
|
| 672 |
with gr.Blocks() as demo:
|
|
@@ -675,8 +690,11 @@ with gr.Blocks() as demo:
|
|
| 675 |
|
| 676 |
gr.LoginButton()
|
| 677 |
run_button = gr.Button("Process Questions")
|
| 678 |
-
status_output = gr.Textbox(label="Status")
|
| 679 |
-
results_table = gr.DataFrame(
|
|
|
|
|
|
|
|
|
|
| 680 |
|
| 681 |
run_button.click(
|
| 682 |
fn=run_and_submit_all,
|
|
|
|
| 604 |
async def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 605 |
"""Process and submit all questions with enhanced error handling."""
|
| 606 |
if not profile:
|
| 607 |
+
return "Authentication required", None
|
|
|
|
|
|
|
|
|
|
| 608 |
|
| 609 |
try:
|
| 610 |
# Get questions
|
| 611 |
questions = await api_get_questions()
|
| 612 |
if not questions:
|
| 613 |
logger.warning("No questions received from API")
|
| 614 |
+
return "No questions available", None
|
| 615 |
|
| 616 |
# Create agent
|
| 617 |
agent = create_agent(openai, Path("knowledge_base"))
|
| 618 |
|
| 619 |
# Process questions
|
| 620 |
processed_answers = []
|
| 621 |
+
results_log = [] # Track detailed results for display
|
| 622 |
+
|
| 623 |
for question in questions:
|
| 624 |
try:
|
| 625 |
answer = await agent.process_question(
|
|
|
|
| 636 |
'submitted_answer': answer.text
|
| 637 |
})
|
| 638 |
|
| 639 |
+
# Add to results log for display
|
| 640 |
+
results_log.append({
|
| 641 |
+
'Task ID': question['task_id'],
|
| 642 |
+
'Question': question['text'],
|
| 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['task_id']} with confidence {answer.confidence}")
|
| 649 |
|
| 650 |
except Exception as e:
|
| 651 |
logger.error(f"Error processing question {question.get('task_id', 'unknown')}: {str(e)}")
|
| 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'
|
| 658 |
+
})
|
| 659 |
continue
|
| 660 |
|
| 661 |
if not processed_answers:
|
| 662 |
+
return "No answers were successfully processed", pd.DataFrame(results_log)
|
|
|
|
| 663 |
|
| 664 |
# Submit answers
|
| 665 |
submission_result = await api_submit_answers(processed_answers, profile.username)
|
| 666 |
|
| 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)
|
|
|
|
|
|
|
| 674 |
|
| 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',
|
| 681 |
+
'Answer': str(e),
|
| 682 |
+
'Confidence': '0.00',
|
| 683 |
+
'Processing Time': '0.00s'
|
| 684 |
+
}])
|
| 685 |
|
| 686 |
# Create Gradio interface
|
| 687 |
with gr.Blocks() as demo:
|
|
|
|
| 690 |
|
| 691 |
gr.LoginButton()
|
| 692 |
run_button = gr.Button("Process Questions")
|
| 693 |
+
status_output = gr.Textbox(label="Status", lines=4) # Increased lines for better visibility
|
| 694 |
+
results_table = gr.DataFrame(
|
| 695 |
+
headers=['Task ID', 'Question', 'Answer', 'Confidence', 'Processing Time'],
|
| 696 |
+
label="Results"
|
| 697 |
+
)
|
| 698 |
|
| 699 |
run_button.click(
|
| 700 |
fn=run_and_submit_all,
|