Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,94 +1,72 @@
|
|
1 |
-
#
|
2 |
-
#
|
3 |
-
|
4 |
-
import
|
5 |
-
|
6 |
-
import gradio as gr, requests, pandas as pd
|
7 |
-
from langchain_core.messages import HumanMessage
|
8 |
-
from agent import agent_executor
|
9 |
|
10 |
-
|
11 |
-
|
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 |
-
#
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
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)
|
41 |
try:
|
42 |
-
|
43 |
except Exception as e:
|
44 |
-
return f"Error
|
45 |
-
|
46 |
-
progress(0, desc=f"Fetched {len(questions)} questions – processing …")
|
47 |
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
# korrekte Reihenfolge: Ergebnisse von gather ↔ Reihenfolge in work
|
54 |
-
tasks = [
|
55 |
-
run_agent_async(ex, tid, qst) # liefert str-Antwort
|
56 |
-
for tid, qst in work
|
57 |
-
]
|
58 |
-
# gather wartet auf alle, behält Reihenfolge
|
59 |
-
results = await asyncio.gather(*tasks)
|
60 |
-
for idx, answer in enumerate(results):
|
61 |
-
tid, qst = work[idx]
|
62 |
-
|
63 |
-
answers.append({"task_id": tid, "submitted_answer": answer})
|
64 |
-
logs.append({"Task ID": tid, "Question": qst, "Answer": answer})
|
65 |
|
66 |
-
|
|
|
|
|
|
|
67 |
|
68 |
-
#
|
69 |
-
|
70 |
-
|
71 |
-
"
|
72 |
-
"
|
73 |
-
"answers": answers,
|
74 |
}
|
75 |
-
|
76 |
-
|
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 |
-
#
|
|
|
|
|
85 |
with gr.Blocks() as demo:
|
86 |
-
gr.Markdown("#
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
gr.LoginButton()
|
88 |
-
run_btn = gr.Button("Run & Submit")
|
89 |
-
|
90 |
-
|
91 |
-
|
|
|
|
|
92 |
|
|
|
93 |
if __name__ == "__main__":
|
94 |
-
demo.launch(
|
|
|
1 |
+
# app.py – Gradio UI + Orchestrierung
|
2 |
+
# =====================================
|
3 |
+
import os
|
4 |
+
import gradio as gr
|
5 |
+
import pandas as pd
|
|
|
|
|
|
|
6 |
|
7 |
+
from agent import GaiaAgent # deine LangChain/LangGraph-Implementierung
|
8 |
+
import logic # Netzwerk- / Loader- / Submit-Utilities
|
|
|
9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
+
# ---------------------------------------------------------------------
|
12 |
+
# Callback für den Gradio-Button
|
13 |
+
# ---------------------------------------------------------------------
|
14 |
+
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
15 |
+
# 0) Login prüfen
|
16 |
+
if profile is None:
|
17 |
+
return "⚠️ Please log in with the Hugging Face button.", None
|
18 |
|
|
|
|
|
|
|
|
|
19 |
username = profile.username
|
20 |
+
space_id = os.getenv("SPACE_ID", "your-space-id") # Fallback für lokales Testen
|
21 |
+
agent_code_url = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
22 |
+
print(f"👤 User: {username} | Repo: {agent_code_url}")
|
23 |
|
24 |
+
# 1) Agent instanziieren
|
25 |
try:
|
26 |
+
gaia_agent = GaiaAgent()
|
27 |
except Exception as e:
|
28 |
+
return f"❌ Error initialising GaiaAgent: {e}", None
|
|
|
|
|
29 |
|
30 |
+
# 2) Fragen (und evtl. Dateien) laden
|
31 |
+
try:
|
32 |
+
questions = logic.fetch_all_questions()
|
33 |
+
except Exception as e:
|
34 |
+
return f"❌ Could not fetch questions: {e}", None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
+
# 3) Agent auf alle Fragen loslassen
|
37 |
+
results_log, answers_payload = logic.run_agent(gaia_agent, questions)
|
38 |
+
if not answers_payload:
|
39 |
+
return "⚠️ Agent produced no answers.", pd.DataFrame(results_log)
|
40 |
|
41 |
+
# 4) Einsenden & Score abrufen
|
42 |
+
submission = {
|
43 |
+
"username": username.strip(),
|
44 |
+
"agent_code": agent_code_url,
|
45 |
+
"answers": answers_payload,
|
|
|
46 |
}
|
47 |
+
status_msg, results_df = logic.submit_answers(submission, results_log)
|
48 |
+
return status_msg, results_df
|
|
|
|
|
|
|
|
|
49 |
|
|
|
50 |
|
51 |
+
# ---------------------------------------------------------------------
|
52 |
+
# Gradio-Interface
|
53 |
+
# ---------------------------------------------------------------------
|
54 |
with gr.Blocks() as demo:
|
55 |
+
gr.Markdown("# GAIA Level-1 Agent – Evaluation Runner")
|
56 |
+
gr.Markdown(
|
57 |
+
"1. **Clone** this Space and implement your logic in `agent.py`.\n"
|
58 |
+
"2. **Log in** with your HF account.\n"
|
59 |
+
"3. Click **Run** to fetch questions, run the agent, and submit answers."
|
60 |
+
)
|
61 |
+
|
62 |
gr.LoginButton()
|
63 |
+
run_btn = gr.Button("Run Evaluation & Submit All Answers")
|
64 |
+
|
65 |
+
status_box = gr.Textbox(label="Status / Score", lines=4, interactive=False)
|
66 |
+
results_df = gr.DataFrame(label="Questions & Answers", wrap=True)
|
67 |
+
|
68 |
+
run_btn.click(run_and_submit_all, outputs=[status_box, results_df])
|
69 |
|
70 |
+
# Standard-Start – auch lokal lauffähig
|
71 |
if __name__ == "__main__":
|
72 |
+
demo.launch()
|