ZeroTimo commited on
Commit
23caa29
·
verified ·
1 Parent(s): c21f73b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -76
app.py CHANGED
@@ -1,94 +1,72 @@
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
- # 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
- progress((idx + 1) / len(work), desc=f"{idx+1}/{len(work)} done")
 
 
 
67
 
68
- # 3) Submit
69
- submit_url = f"{DEFAULT_API_URL}/submit"
70
- payload = {
71
- "username": username,
72
- "agent_code": f"https://huggingface.co/spaces/{os.getenv('SPACE_ID')}/tree/main",
73
- "answers": answers,
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__":
94
- demo.launch(debug=True, share=False)
 
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()