Macmill commited on
Commit
d81521e
·
verified ·
1 Parent(s): 86a2b95

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -131
app.py CHANGED
@@ -1,209 +1,142 @@
1
  import os
2
  import gradio as gr
3
  import requests
4
- # import inspect # No longer needed
5
  import pandas as pd
6
- from dotenv import load_dotenv # Keep for consistency if final_agent uses it
7
- import traceback # For better error logging
8
- from typing import Optional
9
 
10
  # --- Constants ---
11
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
12
 
13
- # ----- THIS IS WHERE YOU INTEGRATE YOUR AGENT -----
14
-
15
- # 1. IMPORT your agent function from your agent file
16
  try:
17
- # Make sure the filename 'final_agent' matches the file you uploaded
18
  from final_agent import answer_gaia_task
19
  print("Successfully imported answer_gaia_task from final_agent.py")
20
- except ImportError:
21
- print("ERROR: Could not import answer_gaia_task from final_agent.py. Check filename and function name.")
22
- # Define a dummy function for graceful failure of the app
23
- def answer_gaia_task(question: str, file_path: Optional[str] = None) -> str:
24
- return "ERROR: Agent function 'answer_gaia_task' not found in final_agent.py."
25
  except Exception as e:
26
  print(f"ERROR during import or initial setup in final_agent.py: {e}")
27
  traceback.print_exc()
28
- # Define a dummy function
 
 
 
29
  def answer_gaia_task(question: str, file_path: Optional[str] = None) -> str:
30
- return f"ERROR: Agent setup failed - {e}"
31
 
32
- class AgentRunner: # Renamed from BasicAgent
 
33
  def __init__(self):
34
- # Agent initialization (LLM, tools, graph compile) happens globally
35
- # when final_agent.py is imported. No specific actions needed here now.
36
- print("AgentRunner initialized. LangGraph agent from final_agent.py should be ready.")
37
- # Optional: Add checks here if needed (e.g., API key validation)
38
- if not os.getenv("GEMINI_API_KEY") or not os.getenv("TAVILY_API_KEY"):
39
- print("WARNING: GEMINI_API_KEY or TAVILY_API_KEY might not be set in Space secrets.")
40
 
41
  def __call__(self, question: str) -> str:
42
  """Runs the imported agent function on a single question."""
43
  print(f"AgentRunner received question (first 50 chars): {question[:50]}...")
 
 
44
  try:
45
- # Call the imported function. Assume file_path handling is within answer_gaia_task or via question text.
46
  final_answer = answer_gaia_task(question=question, file_path=None)
47
- # Ensure result is always a string for submission
48
  final_answer_str = str(final_answer)
49
  print(f"Agent function returned answer: {final_answer_str}")
50
  return final_answer_str
51
  except Exception as e:
52
  print(f"ERROR calling answer_gaia_task: {e}")
53
- traceback.print_exc() # Log the full error to Space logs
54
  return f"ERROR: Agent failed during execution - {e}"
55
 
56
- # ----- END OF AGENT INTEGRATION SECTION -----
57
-
58
-
59
  def run_and_submit_all( profile: gr.OAuthProfile | None):
60
- """
61
- Fetches all questions, runs the integrated agent on them, submits all answers,
62
- and displays the results.
63
- """
64
  space_id = os.getenv("SPACE_ID")
65
 
66
- if profile:
67
- username= f"{profile.username}"
68
- print(f"User logged in: {username}")
69
- else:
70
- print("User not logged in.")
71
- return "Please Login to Hugging Face using the button.", None
72
 
73
- api_url = DEFAULT_API_URL
74
- questions_url = f"{api_url}/questions"
75
- submit_url = f"{api_url}/submit"
76
 
77
- # 1. Instantiate Agent Runner (which uses your imported function)
78
  try:
79
- agent = AgentRunner() # This now sets up to use your LangGraph agent
80
  except Exception as e:
81
- print(f"Error instantiating AgentRunner: {e}")
82
- return f"Error initializing agent runner: {e}", None
83
 
84
- agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" if space_id else "Code URL not available (SPACE_ID not set)"
85
  print(f"Agent code reference: {agent_code}")
86
 
87
  # 2. Fetch Questions
88
  print(f"Fetching questions from: {questions_url}")
89
  try:
90
- response = requests.get(questions_url, timeout=30)
91
- response.raise_for_status()
92
  questions_data = response.json()
93
- if not questions_data:
94
- print("Fetched questions list is empty.")
95
- return "Fetched questions list is empty or invalid format.", None
96
  print(f"Fetched {len(questions_data)} questions.")
97
- except requests.exceptions.RequestException as e:
98
- print(f"Error fetching questions: {e}")
99
- return f"Error fetching questions: {e}", None
100
- except requests.exceptions.JSONDecodeError as e:
101
- print(f"Error decoding JSON response from questions endpoint: {e}")
102
- print(f"Response text: {response.text[:500]}")
103
- return f"Error decoding server response for questions: {e}", None
104
- except Exception as e:
105
- print(f"An unexpected error occurred fetching questions: {e}")
106
- return f"An unexpected error occurred fetching questions: {e}", None
107
 
108
- # 3. Run your Agent on each question
109
- results_log = []
110
- answers_payload = []
111
  print(f"Running agent on {len(questions_data)} questions...")
112
  for item in questions_data:
113
- task_id = item.get("task_id")
114
- question_text = item.get("question")
115
- # file_path = item.get("file_path") # Check if needed/provided
116
- if not task_id or question_text is None:
117
- print(f"Skipping item with missing task_id or question: {item}")
118
- continue
119
  try:
120
- # This call triggers AgentRunner.__call__, which runs your answer_gaia_task
121
- submitted_answer = agent(question_text)
122
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
123
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
124
  except Exception as e:
125
- # Catch errors during the agent's execution on a specific task
126
- print(f"Error running agent on task {task_id}: {e}")
127
- traceback.print_exc()
128
- # Log the error but continue to the next question
129
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT RUN ERROR: {e}"})
130
- # Optionally submit the error message, or skip submitting for this task_id
131
  answers_payload.append({"task_id": task_id, "submitted_answer": f"AGENT RUN ERROR: {e}"})
132
 
133
- if not answers_payload:
134
- print("Agent did not produce any answers to submit.")
135
- return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
136
 
137
  # 4. Prepare Submission
138
  submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
139
- status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
140
- print(status_update)
141
 
142
  # 5. Submit
143
- print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
144
  try:
145
- response = requests.post(submit_url, json=submission_data, timeout=60)
146
- response.raise_for_status()
147
  result_data = response.json()
148
- final_status = (
149
- f"Submission Successful!\n"
150
- f"User: {result_data.get('username')}\n"
151
- f"Overall Score: {result_data.get('score', 'N/A')}% "
152
- f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
153
- f"Message: {result_data.get('message', 'No message received.')}"
154
- )
155
- print("Submission successful.")
156
- results_df = pd.DataFrame(results_log)
157
- return final_status, results_df
158
  except requests.exceptions.HTTPError as e:
159
  error_detail = f"Server responded with status {e.response.status_code}."
160
  try: error_json = e.response.json(); error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
161
- except requests.exceptions.JSONDecodeError: error_detail += f" Response: {e.response.text[:500]}"
162
- status_message = f"Submission Failed: {error_detail}"; print(status_message)
163
- results_df = pd.DataFrame(results_log); return status_message, results_df
164
- except requests.exceptions.Timeout:
165
- status_message = "Submission Failed: The request timed out."; print(status_message)
166
- results_df = pd.DataFrame(results_log); return status_message, results_df
167
- except requests.exceptions.RequestException as e:
168
- status_message = f"Submission Failed: Network error - {e}"; print(status_message)
169
- results_df = pd.DataFrame(results_log); return status_message, results_df
170
- except Exception as e:
171
- status_message = f"An unexpected error occurred during submission: {e}"; print(status_message)
172
- traceback.print_exc(); results_df = pd.DataFrame(results_log); return status_message, results_df
173
 
174
 
175
  # --- Build Gradio Interface (Unchanged) ---
176
  with gr.Blocks() as demo:
177
- gr.Markdown("# GAIA Agent Evaluation Runner") # Updated Title
178
- gr.Markdown(
179
- """
180
- **Instructions:**
181
- 1. Ensure your agent logic is in `final_agent.py` and dependencies in `requirements.txt`. Set secrets in Space settings.
182
- 2. Log in to Hugging Face using the button below.
183
- 3. Click 'Run Evaluation & Submit All Answers' to run your agent. Check Logs for detailed progress.
184
- ---
185
- **Disclaimers:** Execution can take significant time.
186
- """
187
- )
188
  gr.LoginButton()
189
  run_button = gr.Button("Run Evaluation & Submit All Answers")
190
  status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
191
  results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
192
- run_button.click(
193
- fn=run_and_submit_all,
194
- outputs=[status_output, results_table]
195
- )
196
 
197
  # --- Main execution block (Unchanged) ---
198
  if __name__ == "__main__":
199
  print("\n" + "-"*30 + " App Starting " + "-"*30)
200
- space_host_startup = os.getenv("SPACE_HOST")
201
- space_id_startup = os.getenv("SPACE_ID")
202
- if space_host_startup: print(f"✅ SPACE_HOST found: {space_host_startup}")
203
- else: print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
204
- if space_id_startup: print(f"✅ SPACE_ID found: {space_id_startup}")
205
- else: print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
206
- print("-"*(60 + len(" App Starting ")) + "\n")
207
  print("Launching Gradio Interface for GAIA Agent Evaluation...")
208
- # Set debug=False for less verbose logs once stable
209
- demo.launch(debug=True, share=False)
 
1
  import os
2
  import gradio as gr
3
  import requests
 
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
+ # 1. IMPORT your main agent function
 
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
+ print(f"ERROR: Could not import answer_gaia_task from final_agent.py: {e}")
21
+ print("Ensure final_agent.py exists and has no syntax errors.")
22
+ AGENT_AVAILABLE = False
23
  except Exception as e:
24
  print(f"ERROR during import or initial setup in final_agent.py: {e}")
25
  traceback.print_exc()
26
+ AGENT_AVAILABLE = False
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. Check Space logs."
32
 
33
+ # 2. Define a Runner Class to use the imported function
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 (first 50 chars): {question[:50]}...")
46
+ if not AGENT_AVAILABLE:
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"Agent function returned answer: {final_answer_str}")
54
  return final_answer_str
55
  except Exception as e:
56
  print(f"ERROR calling answer_gaia_task: {e}")
57
+ traceback.print_exc()
58
  return f"ERROR: Agent failed during execution - {e}"
59
 
60
+ # --- Submission Logic (Mostly unchanged) ---
 
 
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"
 
 
70
 
71
+ # 1. Instantiate Agent Runner
72
  try:
73
+ agent = AgentRunner()
74
  except Exception as e:
75
+ print(f"Error instantiating AgentRunner: {e}"); return f"Error initializing agent runner: {e}", None
 
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}")
79
 
80
  # 2. Fetch Questions
81
  print(f"Fetching questions from: {questions_url}")
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("Fetched questions list is empty."); return "Questions list empty.", None
 
 
86
  print(f"Fetched {len(questions_data)} questions.")
87
+ except Exception as e: print(f"Error fetching questions: {e}"); return f"Error fetching questions: {e}", None
 
 
 
 
 
 
 
 
 
88
 
89
+ # 3. Run Agent on each question
90
+ results_log = []; answers_payload = []
 
91
  print(f"Running agent on {len(questions_data)} questions...")
92
  for item in questions_data:
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
+ print(f"Error running agent on task {task_id}: {e}"); traceback.print_exc()
 
 
 
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"Submitting {len(answers_payload)} answers for user '{username}'...")
 
109
 
110
  # 5. Submit
 
111
  try:
112
+ response = requests.post(submit_url, json=submission_data, timeout=60); response.raise_for_status()
 
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')}% "
116
+ f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
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 responded with status {e.response.status_code}."
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 Exception as e: status_message = f"Submission Failed: Unexpected error - {e}"
125
+ print(status_message); traceback.print_exc(); results_df = pd.DataFrame(results_log); return status_message, results_df
 
 
 
 
 
 
 
 
126
 
127
 
128
  # --- Build Gradio Interface (Unchanged) ---
129
  with gr.Blocks() as demo:
130
+ gr.Markdown("# GAIA Agent Evaluation Runner"); gr.Markdown("...") # Keep markdown content
 
 
 
 
 
 
 
 
 
 
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 (Unchanged) ---
138
  if __name__ == "__main__":
139
  print("\n" + "-"*30 + " App Starting " + "-"*30)
140
+ # ... Keep startup checks ...
 
 
 
 
 
 
141
  print("Launching Gradio Interface for GAIA Agent Evaluation...")
142
+ demo.launch(debug=True, share=False) # debug=True helps during development