Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,72 +1,71 @@
|
|
1 |
-
#
|
2 |
-
#
|
3 |
-
|
|
|
|
|
4 |
import gradio as gr, requests, pandas as pd
|
5 |
from langchain_core.messages import HumanMessage
|
6 |
-
from agent import agent_executor
|
7 |
-
|
8 |
|
9 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
10 |
-
|
11 |
-
|
12 |
|
13 |
-
#
|
14 |
-
# Sync-Wrapper um den Agent
|
15 |
-
# ------------------------------------------------------------------
|
16 |
def run_agent_sync(task_id: str, question: str) -> str:
|
17 |
-
|
18 |
"messages": [HumanMessage(content=question)],
|
19 |
-
"task_id":
|
20 |
}
|
21 |
try:
|
22 |
-
|
23 |
-
return
|
24 |
except Exception as e:
|
25 |
return f"AGENT ERROR: {e}"
|
26 |
|
27 |
-
|
28 |
async def run_agent_async(executor, task_id: str, question: str) -> str:
|
29 |
loop = asyncio.get_event_loop()
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
|
35 |
-
#
|
36 |
-
# Haupt-Callback (async) – holt Fragen, verarbeitet parallel
|
37 |
-
# ------------------------------------------------------------------
|
38 |
async def run_and_submit_all(profile: gr.OAuthProfile | None, progress=gr.Progress()):
|
39 |
if not profile:
|
40 |
return "Please login with your HF account.", None
|
41 |
username = profile.username
|
42 |
|
43 |
-
# 1)
|
44 |
-
q_url = f"{DEFAULT_API_URL}/questions"
|
45 |
try:
|
46 |
-
|
47 |
except Exception as e:
|
48 |
return f"Error fetching questions: {e}", None
|
49 |
|
50 |
-
progress(0, desc=f"Fetched {len(
|
51 |
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
|
|
|
|
|
|
60 |
answer = await coro
|
61 |
-
|
62 |
-
question = q_data[i-1]["question"]
|
63 |
|
64 |
-
answers.append({"task_id":
|
65 |
-
|
66 |
|
67 |
-
|
|
|
68 |
|
69 |
-
# 3)
|
70 |
submit_url = f"{DEFAULT_API_URL}/submit"
|
71 |
payload = {
|
72 |
"username": username,
|
@@ -75,27 +74,20 @@ async def run_and_submit_all(profile: gr.OAuthProfile | None, progress=gr.Progre
|
|
75 |
}
|
76 |
try:
|
77 |
res = requests.post(submit_url, json=payload, timeout=60).json()
|
78 |
-
status = (
|
79 |
-
|
80 |
-
f"({res.get('correct_count','?')}/{res.get('total_attempted','?')})"
|
81 |
-
)
|
82 |
except Exception as e:
|
83 |
status = f"Submission failed: {e}"
|
84 |
|
85 |
-
return status, pd.DataFrame(
|
86 |
-
|
87 |
|
88 |
-
#
|
89 |
-
# Gradio-UI
|
90 |
-
# ------------------------------------------------------------------
|
91 |
with gr.Blocks() as demo:
|
92 |
-
gr.Markdown("# Fast GAIA Agent Runner (
|
93 |
gr.LoginButton()
|
94 |
run_btn = gr.Button("Run & Submit")
|
95 |
-
|
96 |
-
out_status = gr.Textbox(label="Status / Score", lines=3, interactive=False)
|
97 |
out_table = gr.DataFrame(label="Answers", wrap=True)
|
98 |
-
|
99 |
run_btn.click(run_and_submit_all, outputs=[out_status, out_table])
|
100 |
|
101 |
if __name__ == "__main__":
|
|
|
1 |
+
# ------------------------------------------------------------
|
2 |
+
# fast async app.py (korrekte Zuordnung + Gemini-Throttle)
|
3 |
+
# ------------------------------------------------------------
|
4 |
+
import os, asyncio, concurrent.futures, functools, json
|
5 |
+
from pathlib import Path
|
6 |
import gradio as gr, requests, pandas as pd
|
7 |
from langchain_core.messages import HumanMessage
|
8 |
+
from agent import agent_executor
|
|
|
9 |
|
10 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
11 |
+
MAX_PAR_LLM = 3 # 3 gleichz. Requests stay < 15/min
|
12 |
+
SEMAPHORE = asyncio.Semaphore(MAX_PAR_LLM)
|
13 |
|
14 |
+
# ---------- synchroner Agent-Aufruf ---------------------------------
|
|
|
|
|
15 |
def run_agent_sync(task_id: str, question: str) -> str:
|
16 |
+
payload = {
|
17 |
"messages": [HumanMessage(content=question)],
|
18 |
+
"task_id": task_id,
|
19 |
}
|
20 |
try:
|
21 |
+
res = agent_executor.invoke(payload)
|
22 |
+
return res["messages"][-1].content.strip()
|
23 |
except Exception as e:
|
24 |
return f"AGENT ERROR: {e}"
|
25 |
|
26 |
+
# ---------- async Wrapper + Throttle --------------------------------
|
27 |
async def run_agent_async(executor, task_id: str, question: str) -> str:
|
28 |
loop = asyncio.get_event_loop()
|
29 |
+
async with SEMAPHORE: # Gemini-Quota Guard
|
30 |
+
return await loop.run_in_executor(
|
31 |
+
executor, functools.partial(run_agent_sync, task_id, question)
|
32 |
+
)
|
33 |
|
34 |
+
# ---------- Main Gradio Callback ------------------------------------
|
|
|
|
|
35 |
async def run_and_submit_all(profile: gr.OAuthProfile | None, progress=gr.Progress()):
|
36 |
if not profile:
|
37 |
return "Please login with your HF account.", None
|
38 |
username = profile.username
|
39 |
|
40 |
+
# 1) Fragen laden
|
|
|
41 |
try:
|
42 |
+
questions = requests.get(f"{DEFAULT_API_URL}/questions", timeout=15).json()
|
43 |
except Exception as e:
|
44 |
return f"Error fetching questions: {e}", None
|
45 |
|
46 |
+
progress(0, desc=f"Fetched {len(questions)} questions – processing …")
|
47 |
|
48 |
+
answers, logs = [], []
|
49 |
+
work = [(q["task_id"], q["question"]) for q in questions]
|
50 |
+
|
51 |
+
# 2) Parallel-Ausführung mit korrekt gemappten Tasks
|
52 |
+
with concurrent.futures.ThreadPoolExecutor(max_workers=MAX_PAR_LLM) as ex:
|
53 |
+
task_map = {
|
54 |
+
asyncio.create_task(run_agent_async(ex, tid, qst)): (tid, qst)
|
55 |
+
for tid, qst in work
|
56 |
+
}
|
57 |
+
done_total = 0
|
58 |
+
for coro in asyncio.as_completed(task_map):
|
59 |
answer = await coro
|
60 |
+
tid, qst = task_map[coro]
|
|
|
61 |
|
62 |
+
answers.append({"task_id": tid, "submitted_answer": answer})
|
63 |
+
logs.append({"Task ID": tid, "Question": qst, "Answer": answer})
|
64 |
|
65 |
+
done_total += 1
|
66 |
+
progress(done_total / len(work), desc=f"{done_total}/{len(work)} done")
|
67 |
|
68 |
+
# 3) Submit
|
69 |
submit_url = f"{DEFAULT_API_URL}/submit"
|
70 |
payload = {
|
71 |
"username": username,
|
|
|
74 |
}
|
75 |
try:
|
76 |
res = requests.post(submit_url, json=payload, timeout=60).json()
|
77 |
+
status = (f"Submission OK – Score {res.get('score','?')} % "
|
78 |
+
f"({res.get('correct_count','?')}/{res.get('total_attempted','?')})")
|
|
|
|
|
79 |
except Exception as e:
|
80 |
status = f"Submission failed: {e}"
|
81 |
|
82 |
+
return status, pd.DataFrame(logs)
|
|
|
83 |
|
84 |
+
# ---------- Gradio UI -----------------------------------------------
|
|
|
|
|
85 |
with gr.Blocks() as demo:
|
86 |
+
gr.Markdown("# Fast GAIA Agent Runner (async × progress)")
|
87 |
gr.LoginButton()
|
88 |
run_btn = gr.Button("Run & Submit")
|
89 |
+
out_status = gr.Textbox(label="Status / Score", lines=3)
|
|
|
90 |
out_table = gr.DataFrame(label="Answers", wrap=True)
|
|
|
91 |
run_btn.click(run_and_submit_all, outputs=[out_status, out_table])
|
92 |
|
93 |
if __name__ == "__main__":
|