Ghisalbertifederico commited on
Commit
cc5fac8
·
verified ·
1 Parent(s): 38b675f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -21
app.py CHANGED
@@ -1,7 +1,9 @@
1
  import os
 
2
  import gradio as gr
3
  import requests
4
  import inspect
 
5
  import pandas as pd
6
  import markdownify
7
  from smolagents import tool, CodeAgent, DuckDuckGoSearchTool, InferenceClientModel, OpenAIServerModel, VisitWebpageTool
@@ -62,6 +64,15 @@ class WebSearchAgent:
62
  print("Agent error:", e)
63
  return f"AGENT ERROR: {e}"
64
 
 
 
 
 
 
 
 
 
 
65
  def run_and_submit_all( profile: gr.OAuthProfile | None):
66
  """
67
  Fetches all questions, runs the BasicAgent on them, submits all answers,
@@ -81,14 +92,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
81
  questions_url = f"{api_url}/questions"
82
  submit_url = f"{api_url}/submit"
83
 
84
- # 1. Instantiate Agent ( modify this part to create your agent)
85
- try:
86
- # agent = BasicAgent()
87
- agent = WebSearchAgent()
88
- except Exception as e:
89
- print(f"Error instantiating agent: {e}")
90
- return f"Error initializing agent: {e}", None
91
- # In the case of an app running as a hugging Face space, this link points toward your codebase ( usefull for others so please keep it public)
92
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
93
  print(agent_code)
94
 
@@ -113,23 +117,34 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
113
  print(f"An unexpected error occurred fetching questions: {e}")
114
  return f"An unexpected error occurred fetching questions: {e}", None
115
 
116
- # 3. Run your Agent
117
  results_log = []
118
  answers_payload = []
119
- print(f"Running agent on {len(questions_data)} questions...")
120
- for item in questions_data:
121
- task_id = item.get("task_id")
122
- question_text = item.get("question")
123
- if not task_id or question_text is None:
124
- print(f"Skipping item with missing task_id or question: {item}")
125
- continue
126
- try:
127
- submitted_answer = agent(question_text)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
128
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
129
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
130
- except Exception as e:
131
- print(f"Error running agent on task {task_id}: {e}")
132
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
133
 
134
  if not answers_payload:
135
  print("Agent did not produce any answers to submit.")
 
1
  import os
2
+ import concurrent.futures
3
  import gradio as gr
4
  import requests
5
  import inspect
6
+ import re
7
  import pandas as pd
8
  import markdownify
9
  from smolagents import tool, CodeAgent, DuckDuckGoSearchTool, InferenceClientModel, OpenAIServerModel, VisitWebpageTool
 
64
  print("Agent error:", e)
65
  return f"AGENT ERROR: {e}"
66
 
67
+
68
+ MAX_WORKERS = 4 # questions answered in parallel
69
+ QUESTION_TIMEOUT = 180 # seconds before a single question is abandoned
70
+
71
+
72
+ def _answer_question(question_text: str) -> str:
73
+ """Instantiate a fresh agent and answer one question (safe to run in a thread)."""
74
+ return WebSearchAgent()(question_text)
75
+
76
  def run_and_submit_all( profile: gr.OAuthProfile | None):
77
  """
78
  Fetches all questions, runs the BasicAgent on them, submits all answers,
 
92
  questions_url = f"{api_url}/questions"
93
  submit_url = f"{api_url}/submit"
94
 
95
+ # 1. Agent is instantiated per-question inside _answer_question for parallel execution
 
 
 
 
 
 
 
96
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
97
  print(agent_code)
98
 
 
117
  print(f"An unexpected error occurred fetching questions: {e}")
118
  return f"An unexpected error occurred fetching questions: {e}", None
119
 
120
+ # 3. Run your Agent (parallel)
121
  results_log = []
122
  answers_payload = []
123
+ valid_items = [
124
+ item for item in questions_data
125
+ if item.get("task_id") and item.get("question") is not None
126
+ ]
127
+ print(f"Running agent on {len(valid_items)} questions (up to {MAX_WORKERS} in parallel)...")
128
+
129
+ with concurrent.futures.ThreadPoolExecutor(max_workers=MAX_WORKERS) as executor:
130
+ future_to_item = {
131
+ executor.submit(_answer_question, item["question"]): item
132
+ for item in valid_items
133
+ }
134
+ for future in concurrent.futures.as_completed(future_to_item):
135
+ item = future_to_item[future]
136
+ task_id = item["task_id"]
137
+ question_text = item["question"]
138
+ try:
139
+ submitted_answer = future.result(timeout=QUESTION_TIMEOUT)
140
+ except concurrent.futures.TimeoutError:
141
+ print(f"Timeout on task {task_id}")
142
+ submitted_answer = "TIMEOUT"
143
+ except Exception as e:
144
+ print(f"Error running agent on task {task_id}: {e}")
145
+ submitted_answer = f"AGENT ERROR: {e}"
146
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
147
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
 
 
 
148
 
149
  if not answers_payload:
150
  print("Agent did not produce any answers to submit.")