Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,66 +4,70 @@ import requests
|
|
| 4 |
import pandas as pd
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
import traceback
|
| 7 |
-
from typing import Optional #
|
| 8 |
|
| 9 |
# --- Constants ---
|
| 10 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 11 |
|
| 12 |
# --- Agent Integration ---
|
| 13 |
-
|
|
|
|
| 14 |
try:
|
| 15 |
# Ensure this matches the filename and function name exactly
|
| 16 |
from final_agent import answer_gaia_task
|
| 17 |
print("Successfully imported answer_gaia_task from final_agent.py")
|
| 18 |
AGENT_AVAILABLE = True
|
| 19 |
except ImportError as e:
|
| 20 |
-
|
| 21 |
-
print(
|
| 22 |
-
|
| 23 |
except Exception as e:
|
| 24 |
-
|
|
|
|
|
|
|
| 25 |
traceback.print_exc()
|
| 26 |
-
|
| 27 |
|
| 28 |
# Define a dummy function if import fails, so Gradio app can still load
|
| 29 |
if not AGENT_AVAILABLE:
|
|
|
|
| 30 |
def answer_gaia_task(question: str, file_path: Optional[str] = None) -> str:
|
| 31 |
-
return "ERROR: Agent function could not be loaded.
|
| 32 |
|
| 33 |
-
#
|
| 34 |
class AgentRunner:
|
| 35 |
def __init__(self):
|
| 36 |
print("AgentRunner initialized.")
|
| 37 |
if not AGENT_AVAILABLE:
|
| 38 |
-
print("WARNING: Agent function failed to load during startup.")
|
| 39 |
# Optional: Add environment variable checks if needed
|
| 40 |
# if not os.getenv("GROQ_API_KEY") or not os.getenv("TAVILY_API_KEY"):
|
| 41 |
# print("WARNING: Required API keys might not be set in Space secrets.")
|
| 42 |
|
| 43 |
def __call__(self, question: str) -> str:
|
| 44 |
"""Runs the imported agent function on a single question."""
|
| 45 |
-
print(f"AgentRunner received question
|
| 46 |
-
|
| 47 |
-
return "ERROR: Agent function could not be loaded."
|
| 48 |
try:
|
| 49 |
-
# Call the imported function
|
|
|
|
| 50 |
final_answer = answer_gaia_task(question=question, file_path=None)
|
| 51 |
-
# Ensure result is always a string
|
| 52 |
final_answer_str = str(final_answer)
|
| 53 |
-
print(f"
|
| 54 |
return final_answer_str
|
| 55 |
except Exception as e:
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
| 59 |
|
| 60 |
-
# --- Submission Logic
|
| 61 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 62 |
"""Fetches questions, runs agent, submits answers."""
|
| 63 |
space_id = os.getenv("SPACE_ID")
|
| 64 |
|
| 65 |
-
if not profile:
|
| 66 |
-
print("User not logged in."); return "Please Login to Hugging Face.", None
|
| 67 |
username= f"{profile.username}"; print(f"User logged in: {username}")
|
| 68 |
|
| 69 |
api_url = DEFAULT_API_URL; questions_url = f"{api_url}/questions"; submit_url = f"{api_url}/submit"
|
|
@@ -71,8 +75,10 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 71 |
# 1. Instantiate Agent Runner
|
| 72 |
try:
|
| 73 |
agent = AgentRunner()
|
| 74 |
-
|
| 75 |
-
|
|
|
|
|
|
|
| 76 |
|
| 77 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" if space_id else "Code URL N/A"
|
| 78 |
print(f"Agent code reference: {agent_code}")
|
|
@@ -82,34 +88,37 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 82 |
try:
|
| 83 |
response = requests.get(questions_url, timeout=30); response.raise_for_status()
|
| 84 |
questions_data = response.json()
|
| 85 |
-
if not questions_data: print("
|
| 86 |
print(f"Fetched {len(questions_data)} questions.")
|
| 87 |
-
except Exception as e: print(f"Error fetching questions: {e}"); return f"
|
| 88 |
|
| 89 |
# 3. Run Agent on each question
|
| 90 |
results_log = []; answers_payload = []
|
| 91 |
print(f"Running agent on {len(questions_data)} questions...")
|
| 92 |
-
|
|
|
|
| 93 |
task_id = item.get("task_id"); question_text = item.get("question")
|
|
|
|
| 94 |
if not task_id or question_text is None: print(f"Skipping item: {item}"); continue
|
| 95 |
try:
|
| 96 |
submitted_answer = agent(question_text) # Calls AgentRunner.__call__
|
| 97 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 98 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 99 |
except Exception as e:
|
| 100 |
-
|
|
|
|
| 101 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT RUN ERROR: {e}"})
|
| 102 |
-
answers_payload.append({"task_id": task_id, "submitted_answer": f"AGENT RUN ERROR: {e}"})
|
| 103 |
|
| 104 |
if not answers_payload: print("Agent produced no answers."); return "Agent produced no answers.", pd.DataFrame(results_log)
|
| 105 |
|
| 106 |
# 4. Prepare Submission
|
| 107 |
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
| 108 |
-
print(f"
|
| 109 |
|
| 110 |
# 5. Submit
|
| 111 |
try:
|
| 112 |
-
response = requests.post(submit_url, json=submission_data, timeout=
|
| 113 |
result_data = response.json()
|
| 114 |
final_status = (f"Submission Successful!\nUser: {result_data.get('username')}\n"
|
| 115 |
f"Overall Score: {result_data.get('score', 'N/A')}% "
|
|
@@ -117,26 +126,38 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 117 |
f"Message: {result_data.get('message', 'N/A')}")
|
| 118 |
print("Submission successful."); results_df = pd.DataFrame(results_log); return final_status, results_df
|
| 119 |
except requests.exceptions.HTTPError as e:
|
| 120 |
-
error_detail = f"Server
|
| 121 |
try: error_json = e.response.json(); error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
|
| 122 |
except requests.exceptions.JSONDecodeError: error_detail += f" Response: {e.response.text[:200]}"
|
| 123 |
status_message = f"Submission Failed: {error_detail}"
|
| 124 |
-
except
|
| 125 |
-
|
|
|
|
|
|
|
| 126 |
|
| 127 |
|
| 128 |
-
# --- Build Gradio Interface
|
| 129 |
with gr.Blocks() as demo:
|
| 130 |
-
gr.Markdown("# GAIA Agent Evaluation Runner")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
gr.LoginButton()
|
| 132 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 133 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
| 134 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
| 135 |
run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
|
| 136 |
|
| 137 |
-
# --- Main execution block
|
| 138 |
if __name__ == "__main__":
|
| 139 |
print("\n" + "-"*30 + " App Starting " + "-"*30)
|
| 140 |
# ... Keep startup checks ...
|
| 141 |
-
print("Launching Gradio Interface
|
| 142 |
demo.launch(debug=True, share=False) # debug=True helps during development
|
|
|
|
| 4 |
import pandas as pd
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
import traceback
|
| 7 |
+
from typing import Optional # <<< MAKE SURE THIS IMPORT IS PRESENT <<<
|
| 8 |
|
| 9 |
# --- Constants ---
|
| 10 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 11 |
|
| 12 |
# --- Agent Integration ---
|
| 13 |
+
AGENT_AVAILABLE = False
|
| 14 |
+
AGENT_LOAD_ERROR = ""
|
| 15 |
try:
|
| 16 |
# Ensure this matches the filename and function name exactly
|
| 17 |
from final_agent import answer_gaia_task
|
| 18 |
print("Successfully imported answer_gaia_task from final_agent.py")
|
| 19 |
AGENT_AVAILABLE = True
|
| 20 |
except ImportError as e:
|
| 21 |
+
error_msg = f"ERROR: Could not import answer_gaia_task from final_agent.py: {e}"
|
| 22 |
+
print(error_msg)
|
| 23 |
+
AGENT_LOAD_ERROR = error_msg
|
| 24 |
except Exception as e:
|
| 25 |
+
# Catch errors during the global setup within final_agent.py
|
| 26 |
+
error_msg = f"ERROR during import or initial setup in final_agent.py: {e}"
|
| 27 |
+
print(error_msg)
|
| 28 |
traceback.print_exc()
|
| 29 |
+
AGENT_LOAD_ERROR = error_msg
|
| 30 |
|
| 31 |
# Define a dummy function if import fails, so Gradio app can still load
|
| 32 |
if not AGENT_AVAILABLE:
|
| 33 |
+
# This dummy function will be used if the import fails
|
| 34 |
def answer_gaia_task(question: str, file_path: Optional[str] = None) -> str:
|
| 35 |
+
return f"ERROR: Agent function could not be loaded. Details: {AGENT_LOAD_ERROR}"
|
| 36 |
|
| 37 |
+
# --- Agent Runner Class ---
|
| 38 |
class AgentRunner:
|
| 39 |
def __init__(self):
|
| 40 |
print("AgentRunner initialized.")
|
| 41 |
if not AGENT_AVAILABLE:
|
| 42 |
+
print(f"WARNING: Agent function failed to load during startup. Error: {AGENT_LOAD_ERROR}")
|
| 43 |
# Optional: Add environment variable checks if needed
|
| 44 |
# if not os.getenv("GROQ_API_KEY") or not os.getenv("TAVILY_API_KEY"):
|
| 45 |
# print("WARNING: Required API keys might not be set in Space secrets.")
|
| 46 |
|
| 47 |
def __call__(self, question: str) -> str:
|
| 48 |
"""Runs the imported agent function on a single question."""
|
| 49 |
+
print(f"\n--- AgentRunner received question: {question[:100]}... ---")
|
| 50 |
+
# Always call the potentially dummy function; it returns error if needed
|
|
|
|
| 51 |
try:
|
| 52 |
+
# Call the imported (or dummy) function
|
| 53 |
+
# Assuming file_path is handled by the agent based on question text for now
|
| 54 |
final_answer = answer_gaia_task(question=question, file_path=None)
|
| 55 |
+
# Ensure result is always a string for submission
|
| 56 |
final_answer_str = str(final_answer)
|
| 57 |
+
print(f"--- AgentRunner returning answer: {final_answer_str} ---")
|
| 58 |
return final_answer_str
|
| 59 |
except Exception as e:
|
| 60 |
+
# Catch unexpected errors during the function call itself
|
| 61 |
+
print(f"!!! ERROR calling answer_gaia_task function: {e} !!!")
|
| 62 |
+
traceback.print_exc() # Log the full error to Space logs
|
| 63 |
+
return f"ERROR: Agent function failed during execution - {e}"
|
| 64 |
|
| 65 |
+
# --- Submission Logic ---
|
| 66 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 67 |
"""Fetches questions, runs agent, submits answers."""
|
| 68 |
space_id = os.getenv("SPACE_ID")
|
| 69 |
|
| 70 |
+
if not profile: print("User not logged in."); return "Please Login.", None
|
|
|
|
| 71 |
username= f"{profile.username}"; print(f"User logged in: {username}")
|
| 72 |
|
| 73 |
api_url = DEFAULT_API_URL; questions_url = f"{api_url}/questions"; submit_url = f"{api_url}/submit"
|
|
|
|
| 75 |
# 1. Instantiate Agent Runner
|
| 76 |
try:
|
| 77 |
agent = AgentRunner()
|
| 78 |
+
# Check if agent loaded correctly before proceeding
|
| 79 |
+
if not AGENT_AVAILABLE:
|
| 80 |
+
return f"Agent failed to load. Check logs. Error: {AGENT_LOAD_ERROR}", None
|
| 81 |
+
except Exception as e: print(f"Error instantiating AgentRunner: {e}"); return f"Init error: {e}", None
|
| 82 |
|
| 83 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" if space_id else "Code URL N/A"
|
| 84 |
print(f"Agent code reference: {agent_code}")
|
|
|
|
| 88 |
try:
|
| 89 |
response = requests.get(questions_url, timeout=30); response.raise_for_status()
|
| 90 |
questions_data = response.json()
|
| 91 |
+
if not questions_data: print("Questions list empty."); return "Questions list empty.", None
|
| 92 |
print(f"Fetched {len(questions_data)} questions.")
|
| 93 |
+
except Exception as e: print(f"Error fetching questions: {e}"); return f"Fetch error: {e}", None
|
| 94 |
|
| 95 |
# 3. Run Agent on each question
|
| 96 |
results_log = []; answers_payload = []
|
| 97 |
print(f"Running agent on {len(questions_data)} questions...")
|
| 98 |
+
question_count = len(questions_data)
|
| 99 |
+
for i, item in enumerate(questions_data):
|
| 100 |
task_id = item.get("task_id"); question_text = item.get("question")
|
| 101 |
+
print(f"\n--- Processing Question {i+1}/{question_count} (ID: {task_id}) ---") # Add progress logging
|
| 102 |
if not task_id or question_text is None: print(f"Skipping item: {item}"); continue
|
| 103 |
try:
|
| 104 |
submitted_answer = agent(question_text) # Calls AgentRunner.__call__
|
| 105 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 106 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 107 |
except Exception as e:
|
| 108 |
+
# Catch errors during the agent's execution on a specific task
|
| 109 |
+
print(f"!! Error running agent on task {task_id}: {e} !!"); traceback.print_exc()
|
| 110 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT RUN ERROR: {e}"})
|
| 111 |
+
answers_payload.append({"task_id": task_id, "submitted_answer": f"AGENT RUN ERROR: {e}"}) # Submit error
|
| 112 |
|
| 113 |
if not answers_payload: print("Agent produced no answers."); return "Agent produced no answers.", pd.DataFrame(results_log)
|
| 114 |
|
| 115 |
# 4. Prepare Submission
|
| 116 |
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
| 117 |
+
print(f"\nSubmitting {len(answers_payload)} answers for user '{username}'...")
|
| 118 |
|
| 119 |
# 5. Submit
|
| 120 |
try:
|
| 121 |
+
response = requests.post(submit_url, json=submission_data, timeout=120); response.raise_for_status() # Increased timeout
|
| 122 |
result_data = response.json()
|
| 123 |
final_status = (f"Submission Successful!\nUser: {result_data.get('username')}\n"
|
| 124 |
f"Overall Score: {result_data.get('score', 'N/A')}% "
|
|
|
|
| 126 |
f"Message: {result_data.get('message', 'N/A')}")
|
| 127 |
print("Submission successful."); results_df = pd.DataFrame(results_log); return final_status, results_df
|
| 128 |
except requests.exceptions.HTTPError as e:
|
| 129 |
+
error_detail = f"Server error {e.response.status_code}."
|
| 130 |
try: error_json = e.response.json(); error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
|
| 131 |
except requests.exceptions.JSONDecodeError: error_detail += f" Response: {e.response.text[:200]}"
|
| 132 |
status_message = f"Submission Failed: {error_detail}"
|
| 133 |
+
except requests.exceptions.Timeout:
|
| 134 |
+
status_message = "Submission Failed: The request timed out (120 seconds)."
|
| 135 |
+
except Exception as e: status_message = f"Submission Failed: Unexpected error - {e}"; traceback.print_exc()
|
| 136 |
+
print(status_message); results_df = pd.DataFrame(results_log); return status_message, results_df
|
| 137 |
|
| 138 |
|
| 139 |
+
# --- Build Gradio Interface ---
|
| 140 |
with gr.Blocks() as demo:
|
| 141 |
+
gr.Markdown("# GAIA Agent Evaluation Runner")
|
| 142 |
+
gr.Markdown(
|
| 143 |
+
"""
|
| 144 |
+
**Instructions:**
|
| 145 |
+
1. Ensure your agent logic is in `final_agent.py` and dependencies in `requirements.txt`. Set secrets in Space settings.
|
| 146 |
+
2. Log in to Hugging Face using the button below.
|
| 147 |
+
3. Click 'Run Evaluation & Submit All Answers' to run your agent. Check Logs for detailed progress.
|
| 148 |
+
---
|
| 149 |
+
**Disclaimers:** Execution can take significant time.
|
| 150 |
+
"""
|
| 151 |
+
)
|
| 152 |
gr.LoginButton()
|
| 153 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 154 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
| 155 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
| 156 |
run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
|
| 157 |
|
| 158 |
+
# --- Main execution block ---
|
| 159 |
if __name__ == "__main__":
|
| 160 |
print("\n" + "-"*30 + " App Starting " + "-"*30)
|
| 161 |
# ... Keep startup checks ...
|
| 162 |
+
print("Launching Gradio Interface...")
|
| 163 |
demo.launch(debug=True, share=False) # debug=True helps during development
|