ZeroTimo commited on
Commit
a884a74
·
verified ·
1 Parent(s): 5a388d4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -119
app.py CHANGED
@@ -1,145 +1,102 @@
1
- # app.py – vollständige, lauffähige Fassung
2
- # -------------------------------------------
3
- import os
4
- import gradio as gr
5
- import requests
6
- import pandas as pd
7
 
8
- from agent import agent_executor # dein LangGraph-Agent
9
- from langchain_core.messages import HumanMessage # NEU: benötigt für llm_input
10
 
11
-
12
- # (Keep Constants as is)
13
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
 
14
 
15
 
16
- # ---------------------------------------------------------------------------
17
- # BasicAgent-Wrapper: ruft den LangGraph-Executor auf
18
- # ---------------------------------------------------------------------------
19
- class BasicAgent:
20
- def __init__(self):
21
- print("LLM Tool-Enhanced Agent initialized.")
22
-
23
- # nimmt jetzt ein Dict (messages + task_id) entgegen
24
- def __call__(self, llm_input: dict) -> str:
25
- try:
26
- result = agent_executor.invoke(llm_input) # LangGraph ausführen
27
- answer = result["messages"][-1].content
28
- return answer.strip()
29
- except Exception as e:
30
- print(f"Agent error: {e}")
31
- return "I don't know."
32
-
33
-
34
- # ---------------------------------------------------------------------------
35
- # GAIA-Runner: Fragen holen → Agent laufen lassen → Ergebnis submitten
36
- # ---------------------------------------------------------------------------
37
- def run_and_submit_all(profile: gr.OAuthProfile | None):
38
- """Fetch GAIA questions, run agent, submit answers."""
39
- space_id = os.getenv("SPACE_ID")
40
-
41
- if profile:
42
- username = f"{profile.username}"
43
- print(f"User logged in: {username}")
44
- else:
45
- print("User not logged in.")
46
- return "Please Login to Hugging Face with the button.", None
47
-
48
- api_url = DEFAULT_API_URL
49
- questions_url = f"{api_url}/questions"
50
- submit_url = f"{api_url}/submit"
51
-
52
- # Agent instanziieren
53
  try:
54
- agent = BasicAgent()
 
55
  except Exception as e:
56
- return f"Error initializing agent: {e}", None
 
 
 
 
 
 
 
57
 
58
- agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
59
- print(agent_code)
60
 
61
- # Fragen holen
 
 
 
 
 
 
 
 
 
62
  try:
63
- response = requests.get(questions_url, timeout=15)
64
- response.raise_for_status()
65
- questions_data = response.json()
66
- if not questions_data:
67
- return "Fetched questions list is empty or invalid format.", None
68
- print(f"Fetched {len(questions_data)} questions.")
69
  except Exception as e:
70
  return f"Error fetching questions: {e}", None
71
 
72
- # Agent auf jede Frage anwenden
73
- results_log, answers_payload = [], []
74
- for item in questions_data:
75
- task_id = item.get("task_id")
76
- question_text = item.get("question")
77
- if not task_id or question_text is None:
78
- continue
79
-
80
- try:
81
- llm_input = {
82
- "messages": [HumanMessage(content=question_text)],
83
- "task_id": task_id, # ← WICHTIG!
84
- }
85
- submitted_answer = agent(llm_input)
86
-
87
- answers_payload.append(
88
- {"task_id": task_id, "submitted_answer": submitted_answer}
89
- )
90
- results_log.append(
91
- {"Task ID": task_id, "Question": question_text,
92
- "Submitted Answer": submitted_answer}
93
- )
94
- except Exception as e:
95
- results_log.append(
96
- {"Task ID": task_id, "Question": question_text,
97
- "Submitted Answer": f"AGENT ERROR: {e}"}
98
- )
99
-
100
- if not answers_payload:
101
- return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
102
-
103
- # Submission
104
- submission_data = {
105
- "username": username.strip(),
106
- "agent_code": agent_code,
107
- "answers": answers_payload,
108
  }
109
  try:
110
- response = requests.post(submit_url, json=submission_data, timeout=60)
111
- response.raise_for_status()
112
- result_data = response.json()
113
- final_status = (
114
- f"Submission Successful!\n"
115
- f"User: {result_data.get('username')}\n"
116
- f"Overall Score: {result_data.get('score', 'N/A')}% "
117
- f"({result_data.get('correct_count', '?')}/"
118
- f"{result_data.get('total_attempted', '?')} correct)\n"
119
- f"Message: {result_data.get('message', 'No message received.')}"
120
  )
121
- return final_status, pd.DataFrame(results_log)
122
-
123
  except Exception as e:
124
- status_message = f"Submission Failed: {e}"
125
- return status_message, pd.DataFrame(results_log)
126
 
 
127
 
128
- # ---------------------------------------------------------------------------
129
- # Gradio-UI (unverändert)
130
- # ---------------------------------------------------------------------------
131
- with gr.Blocks() as demo:
132
- gr.Markdown("# Basic Agent Evaluation Runner")
133
 
 
 
 
 
 
134
  gr.LoginButton()
135
- run_button = gr.Button("Run Evaluation & Submit All Answers")
136
 
137
- status_output = gr.Textbox(label="Run Status / Submission Result",
138
- lines=5, interactive=False)
139
- results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
140
 
141
- run_button.click(fn=run_and_submit_all,
142
- outputs=[status_output, results_table])
143
 
144
  if __name__ == "__main__":
145
  demo.launch(debug=True, share=False)
 
1
+ # app.py – async + progress, keine Cache-Logik
2
+ # ------------------------------------------------
3
+ import os, asyncio, concurrent.futures, functools
4
+ import gradio as gr, requests, pandas as pd
5
+ from langchain_core.messages import HumanMessage
6
+ from agent import agent_executor # dein LangGraph-Agent
7
 
 
 
8
 
 
 
9
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
10
+ MAX_PAR_TASKS = 5 # wie viele Fragen parallel laufen
11
 
12
 
13
+ # ------------------------------------------------------------------
14
+ # Sync-Wrapper um den Agent
15
+ # ------------------------------------------------------------------
16
+ def run_agent_sync(task_id: str, question: str) -> str:
17
+ llm_input = {
18
+ "messages": [HumanMessage(content=question)],
19
+ "task_id": task_id,
20
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  try:
22
+ result = agent_executor.invoke(llm_input)
23
+ return result["messages"][-1].content.strip()
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
+ return await loop.run_in_executor(
31
+ executor, functools.partial(run_agent_sync, task_id, question)
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) GAIA-Fragen holen
44
+ q_url = f"{DEFAULT_API_URL}/questions"
45
  try:
46
+ q_data = requests.get(q_url, timeout=15).json()
 
 
 
 
 
47
  except Exception as e:
48
  return f"Error fetching questions: {e}", None
49
 
50
+ progress(0, desc=f"Fetched {len(q_data)} questions processing …")
51
+
52
+ # 2) Parallel ausführen
53
+ answers, log_rows = [], []
54
+ with concurrent.futures.ThreadPoolExecutor(max_workers=MAX_PAR_TASKS) as ex:
55
+ tasks = [
56
+ run_agent_async(ex, itm["task_id"], itm["question"])
57
+ for itm in q_data
58
+ ]
59
+ for i, coro in enumerate(asyncio.as_completed(tasks), 1):
60
+ answer = await coro
61
+ task_id = q_data[i-1]["task_id"]
62
+ question = q_data[i-1]["question"]
63
+
64
+ answers.append({"task_id": task_id, "submitted_answer": answer})
65
+ log_rows.append({"Task ID": task_id, "Question": question, "Answer": answer})
66
+
67
+ progress(i / len(q_data), desc=f"{i}/{len(q_data)} done")
68
+
69
+ # 3) Antworten submitten
70
+ submit_url = f"{DEFAULT_API_URL}/submit"
71
+ payload = {
72
+ "username": username,
73
+ "agent_code": f"https://huggingface.co/spaces/{os.getenv('SPACE_ID')}/tree/main",
74
+ "answers": answers,
 
 
 
 
 
 
 
 
 
 
 
75
  }
76
  try:
77
+ res = requests.post(submit_url, json=payload, timeout=60).json()
78
+ status = (
79
+ f"Submission OK – Score: {res.get('score','?')} % "
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(log_rows)
86
 
 
 
 
 
 
87
 
88
+ # ------------------------------------------------------------------
89
+ # Gradio-UI
90
+ # ------------------------------------------------------------------
91
+ with gr.Blocks() as demo:
92
+ gr.Markdown("# Fast GAIA Agent Runner (Async + Progress)")
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__":
102
  demo.launch(debug=True, share=False)