Update app.py
Browse files
app.py
CHANGED
@@ -222,7 +222,7 @@ class BasicAgent:
|
|
222 |
print(f"Error in agent processing: {e}")
|
223 |
return f"I encountered an error while processing your question: {str(e)}"
|
224 |
|
225 |
-
def run_and_submit_all(
|
226 |
"""
|
227 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
228 |
and displays the results.
|
@@ -231,7 +231,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
231 |
space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
|
232 |
|
233 |
if profile:
|
234 |
-
username= f"{profile.username}"
|
235 |
print(f"User logged in: {username}")
|
236 |
else:
|
237 |
print("User not logged in.")
|
@@ -241,15 +241,17 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
241 |
questions_url = f"{api_url}/questions"
|
242 |
submit_url = f"{api_url}/submit"
|
243 |
|
244 |
-
# 1. Instantiate Agent
|
245 |
try:
|
246 |
agent = BasicAgent()
|
|
|
247 |
except Exception as e:
|
248 |
print(f"Error instantiating agent: {e}")
|
249 |
return f"Error initializing agent: {e}", None
|
250 |
-
|
|
|
251 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
252 |
-
print(agent_code)
|
253 |
|
254 |
# 2. Fetch Questions
|
255 |
print(f"Fetching questions from: {questions_url}")
|
@@ -258,16 +260,16 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
258 |
response.raise_for_status()
|
259 |
questions_data = response.json()
|
260 |
if not questions_data:
|
261 |
-
|
262 |
-
|
263 |
print(f"Fetched {len(questions_data)} questions.")
|
264 |
except requests.exceptions.RequestException as e:
|
265 |
print(f"Error fetching questions: {e}")
|
266 |
return f"Error fetching questions: {e}", None
|
267 |
except requests.exceptions.JSONDecodeError as e:
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
except Exception as e:
|
272 |
print(f"An unexpected error occurred fetching questions: {e}")
|
273 |
return f"An unexpected error occurred fetching questions: {e}", None
|
@@ -275,28 +277,71 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
275 |
# 3. Run your Agent
|
276 |
results_log = []
|
277 |
answers_payload = []
|
278 |
-
print(f"Running agent on {len(questions_data)} questions...")
|
|
|
279 |
for item in questions_data:
|
280 |
task_id = item.get("task_id")
|
281 |
question_text = item.get("question")
|
282 |
if not task_id or question_text is None:
|
283 |
print(f"Skipping item with missing task_id or question: {item}")
|
284 |
continue
|
|
|
285 |
try:
|
286 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
287 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
288 |
-
results_log.append({
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
289 |
except Exception as e:
|
290 |
-
|
291 |
-
|
|
|
|
|
|
|
|
|
|
|
292 |
|
293 |
if not answers_payload:
|
294 |
print("Agent did not produce any answers to submit.")
|
295 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
296 |
|
297 |
# 4. Prepare Submission
|
298 |
-
submission_data = {
|
299 |
-
|
|
|
|
|
|
|
|
|
300 |
print(status_update)
|
301 |
|
302 |
# 5. Submit
|
|
|
222 |
print(f"Error in agent processing: {e}")
|
223 |
return f"I encountered an error while processing your question: {str(e)}"
|
224 |
|
225 |
+
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
226 |
"""
|
227 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
228 |
and displays the results.
|
|
|
231 |
space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
|
232 |
|
233 |
if profile:
|
234 |
+
username = f"{profile.username}"
|
235 |
print(f"User logged in: {username}")
|
236 |
else:
|
237 |
print("User not logged in.")
|
|
|
241 |
questions_url = f"{api_url}/questions"
|
242 |
submit_url = f"{api_url}/submit"
|
243 |
|
244 |
+
# 1. Instantiate Agent
|
245 |
try:
|
246 |
agent = BasicAgent()
|
247 |
+
print("Agent initialized successfully with workflow.")
|
248 |
except Exception as e:
|
249 |
print(f"Error instantiating agent: {e}")
|
250 |
return f"Error initializing agent: {e}", None
|
251 |
+
|
252 |
+
# In the case of an app running as a hugging Face space, this link points toward your codebase
|
253 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
254 |
+
print(f"Agent code location: {agent_code}")
|
255 |
|
256 |
# 2. Fetch Questions
|
257 |
print(f"Fetching questions from: {questions_url}")
|
|
|
260 |
response.raise_for_status()
|
261 |
questions_data = response.json()
|
262 |
if not questions_data:
|
263 |
+
print("Fetched questions list is empty.")
|
264 |
+
return "Fetched questions list is empty or invalid format.", None
|
265 |
print(f"Fetched {len(questions_data)} questions.")
|
266 |
except requests.exceptions.RequestException as e:
|
267 |
print(f"Error fetching questions: {e}")
|
268 |
return f"Error fetching questions: {e}", None
|
269 |
except requests.exceptions.JSONDecodeError as e:
|
270 |
+
print(f"Error decoding JSON response from questions endpoint: {e}")
|
271 |
+
print(f"Response text: {response.text[:500]}")
|
272 |
+
return f"Error decoding server response for questions: {e}", None
|
273 |
except Exception as e:
|
274 |
print(f"An unexpected error occurred fetching questions: {e}")
|
275 |
return f"An unexpected error occurred fetching questions: {e}", None
|
|
|
277 |
# 3. Run your Agent
|
278 |
results_log = []
|
279 |
answers_payload = []
|
280 |
+
print(f"Running agent workflow on {len(questions_data)} questions...")
|
281 |
+
|
282 |
for item in questions_data:
|
283 |
task_id = item.get("task_id")
|
284 |
question_text = item.get("question")
|
285 |
if not task_id or question_text is None:
|
286 |
print(f"Skipping item with missing task_id or question: {item}")
|
287 |
continue
|
288 |
+
|
289 |
try:
|
290 |
+
# Initialize the state for this question
|
291 |
+
initial_state = AgentState(
|
292 |
+
question=question_text,
|
293 |
+
current_step="analyze",
|
294 |
+
tool_output="",
|
295 |
+
final_answer="",
|
296 |
+
history=[],
|
297 |
+
needs_more_info=False,
|
298 |
+
search_query=""
|
299 |
+
)
|
300 |
+
|
301 |
+
# Run the workflow for this question
|
302 |
+
print(f"\nProcessing question {task_id}: {question_text[:50]}...")
|
303 |
+
final_state = agent.workflow.invoke(initial_state)
|
304 |
+
|
305 |
+
# Log the workflow history
|
306 |
+
workflow_history = "\n".join([
|
307 |
+
f"Step: {h['step']}\n" +
|
308 |
+
f"Input: {h.get('input', h.get('query', ''))}\n" +
|
309 |
+
f"Output: {h.get('output', h.get('results', h.get('error', '')))}"
|
310 |
+
for h in final_state.history
|
311 |
+
])
|
312 |
+
|
313 |
+
# Add to results
|
314 |
+
submitted_answer = final_state.final_answer
|
315 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
316 |
+
results_log.append({
|
317 |
+
"Task ID": task_id,
|
318 |
+
"Question": question_text,
|
319 |
+
"Submitted Answer": submitted_answer,
|
320 |
+
"Workflow History": workflow_history
|
321 |
+
})
|
322 |
+
|
323 |
+
print(f"Completed question {task_id} with {len(final_state.history)} workflow steps")
|
324 |
+
|
325 |
except Exception as e:
|
326 |
+
print(f"Error running agent workflow on task {task_id}: {e}")
|
327 |
+
results_log.append({
|
328 |
+
"Task ID": task_id,
|
329 |
+
"Question": question_text,
|
330 |
+
"Submitted Answer": f"WORKFLOW ERROR: {e}",
|
331 |
+
"Workflow History": "Error occurred before workflow completion"
|
332 |
+
})
|
333 |
|
334 |
if not answers_payload:
|
335 |
print("Agent did not produce any answers to submit.")
|
336 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
337 |
|
338 |
# 4. Prepare Submission
|
339 |
+
submission_data = {
|
340 |
+
"username": username.strip(),
|
341 |
+
"agent_code": agent_code,
|
342 |
+
"answers": answers_payload
|
343 |
+
}
|
344 |
+
status_update = f"Agent workflow finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
345 |
print(status_update)
|
346 |
|
347 |
# 5. Submit
|