diepala commited on
Commit
b2b5269
·
1 Parent(s): fe1c24a

Async answers

Browse files
Files changed (3) hide show
  1. agent.py +15 -0
  2. app.py +41 -30
  3. requirements.txt +2 -1
agent.py ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from smolagents import CodeAgent, HfApiModel, DuckDuckGoSearchTool
2
+
3
+ model = HfApiModel("Qwen/Qwen2.5-Coder-32B-Instruct")
4
+
5
+ agent = CodeAgent(
6
+ tools=[
7
+ DuckDuckGoSearchTool(),
8
+ ],
9
+ model=model,
10
+ )
11
+
12
+
13
+ def run_agent(question: str) -> str:
14
+ response = agent.run(question)
15
+ return str(response)
app.py CHANGED
@@ -2,6 +2,9 @@ import os
2
  import gradio as gr
3
  import requests
4
  import pandas as pd
 
 
 
5
 
6
  # (Keep Constants as is)
7
  # --- Constants ---
@@ -16,9 +19,34 @@ class BasicAgent:
16
 
17
  def __call__(self, question: str) -> str:
18
  print(f"Agent received question (first 50 chars): {question[:50]}...")
19
- fixed_answer = "This is my custom default answer."
20
- print(f"Agent returning fixed answer: {fixed_answer}")
21
- return fixed_answer
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
 
24
  def run_and_submit_all(profile: gr.OAuthProfile | None):
@@ -75,33 +103,16 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
75
  results_log = []
76
  answers_payload = []
77
  print(f"Running agent on {len(questions_data)} questions...")
78
- for item in questions_data:
79
- task_id = item.get("task_id")
80
- question_text = item.get("question")
81
- if not task_id or question_text is None:
82
- print(f"Skipping item with missing task_id or question: {item}")
83
- continue
84
- try:
85
- submitted_answer = agent(question_text)
86
- answers_payload.append(
87
- {"task_id": task_id, "submitted_answer": submitted_answer}
88
- )
89
- results_log.append(
90
- {
91
- "Task ID": task_id,
92
- "Question": question_text,
93
- "Submitted Answer": submitted_answer,
94
- }
95
- )
96
- except Exception as e:
97
- print(f"Error running agent on task {task_id}: {e}")
98
- results_log.append(
99
- {
100
- "Task ID": task_id,
101
- "Question": question_text,
102
- "Submitted Answer": f"AGENT ERROR: {e}",
103
- }
104
- )
105
 
106
  if not answers_payload:
107
  print("Agent did not produce any answers to submit.")
 
2
  import gradio as gr
3
  import requests
4
  import pandas as pd
5
+ from concurrent.futures import ThreadPoolExecutor
6
+
7
+ from agent import run_agent
8
 
9
  # (Keep Constants as is)
10
  # --- Constants ---
 
19
 
20
  def __call__(self, question: str) -> str:
21
  print(f"Agent received question (first 50 chars): {question[:50]}...")
22
+ answer = "test answer" # run_agent(question)
23
+ print(f"Agent returning fixed answer: {answer}")
24
+ return answer
25
+
26
+
27
+ def process_item(agent: BasicAgent, item: dict) -> tuple[dict | None, dict | None]:
28
+ task_id = item.get("task_id")
29
+ question_text = item.get("question")
30
+ if not task_id or question_text is None:
31
+ print(f"Skipping item with missing task_id or question: {item}")
32
+ return None, None
33
+ try:
34
+ submitted_answer = agent(question_text)
35
+ answer_payload = {"task_id": task_id, "submitted_answer": submitted_answer}
36
+ result_log = {
37
+ "Task ID": task_id,
38
+ "Question": question_text,
39
+ "Submitted Answer": submitted_answer,
40
+ }
41
+ except Exception as e:
42
+ print(f"Error running agent on task {task_id}: {e}")
43
+ answer_payload = None
44
+ result_log = {
45
+ "Task ID": task_id,
46
+ "Question": question_text,
47
+ "Submitted Answer": f"AGENT ERROR: {e}",
48
+ }
49
+ return answer_payload, result_log
50
 
51
 
52
  def run_and_submit_all(profile: gr.OAuthProfile | None):
 
103
  results_log = []
104
  answers_payload = []
105
  print(f"Running agent on {len(questions_data)} questions...")
106
+ with ThreadPoolExecutor(max_workers=20) as executor:
107
+ futures = {
108
+ executor.submit(process_item, agent, item): item for item in questions_data
109
+ }
110
+ for future in futures:
111
+ answer_payload, result_log = future.result()
112
+ if answer_payload is not None:
113
+ answers_payload.append(answer_payload)
114
+ if result_log is not None:
115
+ results_log.append(result_log)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
116
 
117
  if not answers_payload:
118
  print("Agent did not produce any answers to submit.")
requirements.txt CHANGED
@@ -1,2 +1,3 @@
1
  gradio
2
- requests
 
 
1
  gradio
2
+ requests
3
+ smolagents