mhattingpete commited on
Commit
0e32f4e
·
1 Parent(s): 7a53059

updated app to run agent sync

Browse files
Files changed (1) hide show
  1. app.py +9 -13
app.py CHANGED
@@ -51,13 +51,12 @@ async def _submit_answers(session: aiohttp.ClientSession, url: str, data: dict):
51
  return await r.json()
52
 
53
 
54
- async def _run_agent_async(
55
  agent: Agent,
56
  question: str,
57
  task_id: str | int,
58
  file_name: str,
59
  cache: dict[str, str],
60
- semaphore: asyncio.Semaphore,
61
  ) -> tuple[str | int, str]:
62
  """
63
  Run the agent in a threadpool (because most agents are sync / blocking),
@@ -66,11 +65,9 @@ async def _run_agent_async(
66
  if str(task_id) in cache:
67
  return task_id, cache[str(task_id)]
68
 
69
- loop = asyncio.get_running_loop()
70
- async with semaphore:
71
- answer = await loop.run_in_executor(
72
- None, agent, question, task_id, file_name, DEFAULT_API_URL
73
- ) # execute in default thread‑pool
74
  cache[str(task_id)] = answer
75
  return task_id, answer
76
 
@@ -106,12 +103,11 @@ async def _async_main(profile: gr.OAuthProfile | None):
106
  if not questions:
107
  return "Fetched questions list is empty.", None
108
 
109
- # 3. Run agent with cache + limited concurrency
110
  cache = load_cache()
111
- sem = asyncio.Semaphore(MAX_CONCURRENCY)
112
- coros = [
113
- _run_agent_async(
114
- agent, q["question"], q["task_id"], q["file_name"], cache, sem
115
  )
116
  for q in questions
117
  if q.get("task_id") and q.get("question") is not None
@@ -120,7 +116,7 @@ async def _async_main(profile: gr.OAuthProfile | None):
120
  results_log: list[dict] = []
121
  answers_json: list[dict] = []
122
 
123
- for task_id, answer in await asyncio.gather(*coros):
124
  answers_json.append(
125
  {"task_id": task_id, "submitted_answer": answer}
126
  )
 
51
  return await r.json()
52
 
53
 
54
+ def _run_agent_sync(
55
  agent: Agent,
56
  question: str,
57
  task_id: str | int,
58
  file_name: str,
59
  cache: dict[str, str],
 
60
  ) -> tuple[str | int, str]:
61
  """
62
  Run the agent in a threadpool (because most agents are sync / blocking),
 
65
  if str(task_id) in cache:
66
  return task_id, cache[str(task_id)]
67
 
68
+ answer = agent(
69
+ question, task_id, file_name, DEFAULT_API_URL
70
+ ) # execute in default thread‑pool
 
 
71
  cache[str(task_id)] = answer
72
  return task_id, answer
73
 
 
103
  if not questions:
104
  return "Fetched questions list is empty.", None
105
 
106
+ # 3. Run agent with cache
107
  cache = load_cache()
108
+ results = [
109
+ _run_agent_sync(
110
+ agent, q["question"], q["task_id"], q["file_name"], cache
 
111
  )
112
  for q in questions
113
  if q.get("task_id") and q.get("question") is not None
 
116
  results_log: list[dict] = []
117
  answers_json: list[dict] = []
118
 
119
+ for task_id, answer in results:
120
  answers_json.append(
121
  {"task_id": task_id, "submitted_answer": answer}
122
  )