Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -607,170 +607,119 @@ You have access to the following tools to gather information and perform actions
|
|
| 607 |
|
| 608 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 609 |
"""
|
| 610 |
-
Fetches
|
| 611 |
-
and displays the results.
|
| 612 |
"""
|
| 613 |
space_id = os.getenv("SPACE_ID")
|
| 614 |
if profile:
|
| 615 |
username= f"{profile.username}"
|
| 616 |
print(f"User logged in: {username}")
|
| 617 |
else:
|
| 618 |
-
|
| 619 |
-
|
|
|
|
|
|
|
| 620 |
|
| 621 |
api_url = DEFAULT_API_URL
|
| 622 |
-
questions_url = f"{api_url}/questions"
|
| 623 |
-
submit_url = f"{api_url}/submit"
|
| 624 |
|
| 625 |
print("Instantiating agent...")
|
| 626 |
try:
|
|
|
|
|
|
|
| 627 |
agent = BasicAgent()
|
| 628 |
-
if agent.asr_pipeline is None:
|
| 629 |
-
print("⚠️ ASR Pipeline
|
| 630 |
|
| 631 |
except Exception as e:
|
| 632 |
print(f"Error instantiating agent: {e}")
|
| 633 |
-
import traceback
|
| 634 |
-
traceback.print_exc() # Print full traceback for init errors
|
| 635 |
return f"Error initializing agent: {e}", None
|
| 636 |
print("Agent instantiated successfully.")
|
| 637 |
|
| 638 |
-
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 639 |
print(f"Agent code URL: {agent_code}")
|
| 640 |
|
| 641 |
-
|
| 642 |
-
|
| 643 |
-
|
| 644 |
-
|
| 645 |
-
|
| 646 |
-
|
| 647 |
-
|
| 648 |
-
|
| 649 |
-
|
| 650 |
-
|
| 651 |
-
|
| 652 |
-
|
| 653 |
-
|
| 654 |
-
|
| 655 |
-
|
| 656 |
-
|
| 657 |
-
|
| 658 |
-
|
| 659 |
-
|
|
|
|
|
|
|
| 660 |
|
| 661 |
results_log = []
|
| 662 |
answers_payload = []
|
| 663 |
total_questions = len(questions_data)
|
| 664 |
-
print(f"Running agent on {total_questions} questions...")
|
| 665 |
|
| 666 |
-
#
|
| 667 |
-
# question_limit = 5 # Uncomment and set a number (e.g., 5) to test fewer questions
|
| 668 |
-
# questions_to_run = questions_data[:question_limit]
|
| 669 |
-
# print(f"--- RUNNING WITH QUESTION LIMIT: {question_limit} ---")
|
| 670 |
-
questions_to_run = questions_data # Comment this line out if using the limit above
|
| 671 |
|
| 672 |
for i, item in enumerate(questions_to_run):
|
| 673 |
task_id = item.get("task_id")
|
| 674 |
question_text = item.get("question")
|
| 675 |
-
if not task_id or question_text is None:
|
| 676 |
-
print(f"Skipping item {i+1} with missing task_id or question: {item}")
|
| 677 |
-
continue
|
| 678 |
|
| 679 |
-
print(f"\n--- Running Task {i+1}/{len(questions_to_run)} (ID: {task_id}) ---")
|
| 680 |
try:
|
| 681 |
file_path = item.get("file_path")
|
|
|
|
| 682 |
if file_path:
|
| 683 |
# Check existence relative to script dir first, then CWD
|
| 684 |
-
script_dir = os.path.dirname(os.path.realpath(__file__))
|
|
|
|
| 685 |
potential_script_path = os.path.join(script_dir, file_path)
|
| 686 |
potential_cwd_path = os.path.join(os.getcwd(), file_path) # Check CWD too
|
| 687 |
|
| 688 |
-
if os.path.exists(potential_script_path):
|
| 689 |
-
|
| 690 |
-
|
| 691 |
-
file_context = f"[Attached File (exists in cwd): {file_path}]" # Path relative to cwd
|
| 692 |
-
else:
|
| 693 |
-
file_context = f"[Attached File (path provided): {file_path}]" # Agent needs to handle finding it
|
| 694 |
|
| 695 |
question_text_with_context = f"{question_text}\n\n{file_context}"
|
| 696 |
print(f"Question includes file reference: {file_path}")
|
| 697 |
-
else:
|
| 698 |
-
question_text_with_context = question_text
|
| 699 |
|
| 700 |
submitted_answer = agent(question_text_with_context)
|
| 701 |
-
# Ensure answer is a string, even if agent returns None or other types
|
| 702 |
submitted_answer_str = str(submitted_answer) if submitted_answer is not None else ""
|
| 703 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer_str})
|
| 704 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer_str})
|
| 705 |
-
print(f"--- Task {task_id} Complete ---")
|
| 706 |
except Exception as e:
|
| 707 |
-
print(f"FATAL ERROR running agent graph on task {task_id}: {e}")
|
| 708 |
-
import traceback
|
| 709 |
-
traceback.print_exc()
|
| 710 |
submitted_answer = f"AGENT CRASH ERROR: {e}"
|
| 711 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 712 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 713 |
|
| 714 |
-
if not answers_payload:
|
| 715 |
-
print("Agent did not produce any answers to submit.")
|
| 716 |
-
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
| 717 |
|
| 718 |
-
# 4. Prepare Submission
|
| 719 |
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
| 720 |
-
status_update = f"Agent finished.
|
| 721 |
print(status_update)
|
|
|
|
| 722 |
|
| 723 |
-
# 5.
|
| 724 |
-
|
| 725 |
-
|
| 726 |
-
|
| 727 |
-
|
| 728 |
-
|
| 729 |
-
|
| 730 |
-
f"Submission Successful!\n"
|
| 731 |
-
f"User: {result_data.get('username')}\n"
|
| 732 |
-
f"Overall Score: {result_data.get('score', 'N/A')}% "
|
| 733 |
-
f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
|
| 734 |
-
f"Message: {result_data.get('message', 'No message received.')}"
|
| 735 |
-
)
|
| 736 |
-
print("Submission successful.")
|
| 737 |
-
results_df = pd.DataFrame(results_log)
|
| 738 |
-
# Add score details if available
|
| 739 |
-
if 'scores' in result_data:
|
| 740 |
-
scores_dict = {item['task_id']: item['score'] for item in result_data['scores']}
|
| 741 |
-
results_df['Correct'] = results_df['Task ID'].map(lambda x: scores_dict.get(x, None))
|
| 742 |
-
results_df['Correct'] = results_df['Correct'].apply(lambda x: 'Yes' if x == 1 else ('No' if x == 0 else 'N/A'))
|
| 743 |
-
|
| 744 |
-
|
| 745 |
-
return final_status, results_df
|
| 746 |
-
except requests.exceptions.HTTPError as e:
|
| 747 |
-
error_detail = f"Server responded with status {e.response.status_code}."
|
| 748 |
-
try:
|
| 749 |
-
error_json = e.response.json()
|
| 750 |
-
error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
|
| 751 |
-
except requests.exceptions.JSONDecodeError:
|
| 752 |
-
error_detail += f" Response: {e.response.text[:500]}"
|
| 753 |
-
status_message = f"Submission Failed: {error_detail}"
|
| 754 |
-
print(status_message)
|
| 755 |
-
results_df = pd.DataFrame(results_log)
|
| 756 |
-
return status_message, results_df
|
| 757 |
-
except requests.exceptions.Timeout:
|
| 758 |
-
status_message = "Submission Failed: The submission request timed out."
|
| 759 |
-
print(status_message)
|
| 760 |
-
results_df = pd.DataFrame(results_log)
|
| 761 |
-
return status_message, results_df
|
| 762 |
-
except requests.exceptions.RequestException as e:
|
| 763 |
-
status_message = f"Submission Failed: Network error during submission - {e}"
|
| 764 |
-
print(status_message)
|
| 765 |
-
results_df = pd.DataFrame(results_log)
|
| 766 |
-
return status_message, results_df
|
| 767 |
-
except Exception as e:
|
| 768 |
-
status_message = f"An unexpected error occurred during submission processing: {e}"
|
| 769 |
-
print(status_message)
|
| 770 |
-
import traceback
|
| 771 |
-
traceback.print_exc()
|
| 772 |
-
results_df = pd.DataFrame(results_log)
|
| 773 |
-
return status_message, results_df
|
| 774 |
|
| 775 |
# --- Build Gradio Interface using Blocks ---
|
| 776 |
with gr.Blocks() as demo:
|
|
|
|
| 607 |
|
| 608 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 609 |
"""
|
| 610 |
+
Fetches MOCK questions, runs the BasicAgent on them, simulates submission prep,
|
| 611 |
+
and displays the results. DOES NOT SUBMIT.
|
| 612 |
"""
|
| 613 |
space_id = os.getenv("SPACE_ID")
|
| 614 |
if profile:
|
| 615 |
username= f"{profile.username}"
|
| 616 |
print(f"User logged in: {username}")
|
| 617 |
else:
|
| 618 |
+
# For local testing without login, create a dummy username
|
| 619 |
+
username = "local_test_user"
|
| 620 |
+
print("User not logged in, using dummy username for testing.")
|
| 621 |
+
# return "Please Login to Hugging Face with the button.", None # Don't block local test
|
| 622 |
|
| 623 |
api_url = DEFAULT_API_URL
|
| 624 |
+
# questions_url = f"{api_url}/questions" # Skip fetching
|
| 625 |
+
submit_url = f"{api_url}/submit" # Keep for context
|
| 626 |
|
| 627 |
print("Instantiating agent...")
|
| 628 |
try:
|
| 629 |
+
# This assumes the BasicAgent class is defined in the same scope
|
| 630 |
+
# when this function is actually run in the full app.py
|
| 631 |
agent = BasicAgent()
|
| 632 |
+
if not hasattr(agent, 'asr_pipeline') or agent.asr_pipeline is None:
|
| 633 |
+
print("⚠️ ASR Pipeline might not have loaded correctly. Audio questions could fail.")
|
| 634 |
|
| 635 |
except Exception as e:
|
| 636 |
print(f"Error instantiating agent: {e}")
|
| 637 |
+
import traceback; traceback.print_exc()
|
|
|
|
| 638 |
return f"Error initializing agent: {e}", None
|
| 639 |
print("Agent instantiated successfully.")
|
| 640 |
|
| 641 |
+
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" if space_id else "local_run"
|
| 642 |
print(f"Agent code URL: {agent_code}")
|
| 643 |
|
| 644 |
+
# ==================== MOCK QUESTIONS SECTION ====================
|
| 645 |
+
print("--- USING MOCK QUESTIONS FOR TESTING ---")
|
| 646 |
+
mock_questions_data = [
|
| 647 |
+
{
|
| 648 |
+
"task_id": "mock_task_search_001",
|
| 649 |
+
"question": "What is the capital of France?"
|
| 650 |
+
},
|
| 651 |
+
{
|
| 652 |
+
"task_id": "mock_task_code_001",
|
| 653 |
+
"question": "Calculate 15 factorial using python. Only output the final number."
|
| 654 |
+
},
|
| 655 |
+
# Add more mock questions here if needed, e.g., for file/audio/youtube
|
| 656 |
+
# {
|
| 657 |
+
# "task_id": "mock_task_audio_001",
|
| 658 |
+
# "question": "Transcribe the audio file 'sample.mp3'", # Make sure sample.mp3 exists
|
| 659 |
+
# "file_path": "sample.mp3"
|
| 660 |
+
# },
|
| 661 |
+
]
|
| 662 |
+
questions_data = mock_questions_data
|
| 663 |
+
print(f"Using {len(questions_data)} mock questions.")
|
| 664 |
+
# =============================================================
|
| 665 |
|
| 666 |
results_log = []
|
| 667 |
answers_payload = []
|
| 668 |
total_questions = len(questions_data)
|
| 669 |
+
print(f"Running agent on {total_questions} mock questions...")
|
| 670 |
|
| 671 |
+
questions_to_run = questions_data # Use the mock data
|
|
|
|
|
|
|
|
|
|
|
|
|
| 672 |
|
| 673 |
for i, item in enumerate(questions_to_run):
|
| 674 |
task_id = item.get("task_id")
|
| 675 |
question_text = item.get("question")
|
| 676 |
+
if not task_id or question_text is None: print(f"Skipping item {i+1}: missing task_id or question: {item}"); continue
|
|
|
|
|
|
|
| 677 |
|
| 678 |
+
print(f"\n--- Running Mock Task {i+1}/{len(questions_to_run)} (ID: {task_id}) ---")
|
| 679 |
try:
|
| 680 |
file_path = item.get("file_path")
|
| 681 |
+
question_text_with_context = question_text
|
| 682 |
if file_path:
|
| 683 |
# Check existence relative to script dir first, then CWD
|
| 684 |
+
try: script_dir = os.path.dirname(os.path.realpath(__file__))
|
| 685 |
+
except NameError: script_dir = os.getcwd()
|
| 686 |
potential_script_path = os.path.join(script_dir, file_path)
|
| 687 |
potential_cwd_path = os.path.join(os.getcwd(), file_path) # Check CWD too
|
| 688 |
|
| 689 |
+
if os.path.exists(potential_script_path): file_context = f"[Attached File (exists): {file_path}]"
|
| 690 |
+
elif os.path.exists(potential_cwd_path): file_context = f"[Attached File (exists in cwd): {file_path}]"
|
| 691 |
+
else: file_context = f"[Attached File (path provided, NOT FOUND): {file_path}]" # Indicate if not found
|
|
|
|
|
|
|
|
|
|
| 692 |
|
| 693 |
question_text_with_context = f"{question_text}\n\n{file_context}"
|
| 694 |
print(f"Question includes file reference: {file_path}")
|
|
|
|
|
|
|
| 695 |
|
| 696 |
submitted_answer = agent(question_text_with_context)
|
|
|
|
| 697 |
submitted_answer_str = str(submitted_answer) if submitted_answer is not None else ""
|
| 698 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer_str})
|
| 699 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer_str})
|
| 700 |
+
print(f"--- Mock Task {task_id} Complete ---")
|
| 701 |
except Exception as e:
|
| 702 |
+
print(f"FATAL ERROR running agent graph on mock task {task_id}: {e}")
|
| 703 |
+
import traceback; traceback.print_exc()
|
|
|
|
| 704 |
submitted_answer = f"AGENT CRASH ERROR: {e}"
|
| 705 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 706 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 707 |
|
| 708 |
+
if not answers_payload: print("Agent did not produce any answers."); return "Agent did not produce answers.", pd.DataFrame(results_log)
|
|
|
|
|
|
|
| 709 |
|
| 710 |
+
# 4. Prepare Submission Data (for display only)
|
| 711 |
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
| 712 |
+
status_update = f"Agent finished processing mock questions. Prepared {len(answers_payload)} answers for user '{username}'."
|
| 713 |
print(status_update)
|
| 714 |
+
print("--- MOCK RUN COMPLETE - SUBMISSION SKIPPED ---")
|
| 715 |
|
| 716 |
+
# 5. Skip Actual Submission for Mock Run
|
| 717 |
+
final_status = "--- MOCK RUN COMPLETE ---\n" + status_update + "\nSubmission to the scoring server was SKIPPED."
|
| 718 |
+
results_df = pd.DataFrame(results_log)
|
| 719 |
+
# Add a column indicating mock status
|
| 720 |
+
results_df['Correct'] = 'N/A (Mock)'
|
| 721 |
+
|
| 722 |
+
return final_status, results_df
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 723 |
|
| 724 |
# --- Build Gradio Interface using Blocks ---
|
| 725 |
with gr.Blocks() as demo:
|