Aya1610 commited on
Commit
31c5b8e
·
verified ·
1 Parent(s): 4c4fbb8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -21
app.py CHANGED
@@ -4,50 +4,77 @@ import requests
4
  import pandas as pd
5
  from agent import BasicAgent
6
 
 
7
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
8
 
 
9
  def run_and_submit_all(profile: gr.OAuthProfile | None):
 
 
 
10
  if not profile:
11
  return "Please log in with Hugging Face", None
12
 
 
13
  agent = BasicAgent()
14
- # 1) fetch questions
15
- questions = requests.get(f"{DEFAULT_API_URL}/questions", timeout=15).json()
16
 
17
- # 2) run agent
 
 
 
 
 
 
 
 
 
18
  answers = []
19
  log = []
20
  for item in questions:
21
- task_id = item["task_id"]
22
- q = item["question"]
23
- ans = agent(q)
24
- answers.append({"task_id": task_id, "submitted_answer": ans})
25
- log.append({"Task ID": task_id, "Question": q, "Answer": ans})
 
 
 
 
 
 
26
 
27
- # 3) submit
28
  payload = {
29
- "username": profile.username,
30
  "agent_code": f"https://huggingface.co/spaces/{os.getenv('SPACE_ID')}/tree/main",
31
  "answers": answers,
32
  }
33
- resp = requests.post(f"{DEFAULT_API_URL}/submit", json=payload, timeout=60)
34
- data = resp.json()
35
-
36
- status = (
37
- f"Score: {data['score']}% "
38
- f"({data['correct_count']}/{data['total_attempted']})\n"
39
- f"{data.get('message','')}"
40
- )
 
 
 
 
 
41
  return status, pd.DataFrame(log)
42
 
 
 
43
  with gr.Blocks() as demo:
44
  gr.Markdown("# GAIA Q&A with smolagents")
45
  login = gr.LoginButton()
46
  run_btn = gr.Button("Run & Submit All", variant="primary")
47
- status_out = gr.Textbox(lines=4, label="Submission Status")
48
- results_tbl = gr.DataFrame(label="Per-Question Log")
49
 
50
- run_btn.click(fn=run_and_submit_all, inputs=[login], outputs=[status_out, results_tbl])
 
 
 
51
 
52
  if __name__ == "__main__":
53
  demo.launch()
 
4
  import pandas as pd
5
  from agent import BasicAgent
6
 
7
+ # --- Constants ---
8
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
9
 
10
+
11
  def run_and_submit_all(profile: gr.OAuthProfile | None):
12
+ """
13
+ Fetches GAIA questions, queries the smolagents-based BasicAgent, and submits answers.
14
+ """
15
  if not profile:
16
  return "Please log in with Hugging Face", None
17
 
18
+ username = profile.username
19
  agent = BasicAgent()
 
 
20
 
21
+ # 1) Fetch questions
22
+ questions_url = f"{DEFAULT_API_URL}/questions"
23
+ try:
24
+ resp = requests.get(questions_url, timeout=15)
25
+ resp.raise_for_status()
26
+ questions = resp.json()
27
+ except Exception as e:
28
+ return f"Error fetching questions: {e}", None
29
+
30
+ # 2) Run agent on each question
31
  answers = []
32
  log = []
33
  for item in questions:
34
+ task_id = item.get("task_id")
35
+ question = item.get("question")
36
+ if not task_id or question is None:
37
+ continue
38
+
39
+ answer = agent(question)
40
+ answers.append({"task_id": task_id, "submitted_answer": answer})
41
+ log.append({"Task ID": task_id, "Question": question, "Answer": answer})
42
+
43
+ if not answers:
44
+ return "Agent did not produce any answers to submit.", pd.DataFrame(log)
45
 
46
+ # 3) Submit answers
47
  payload = {
48
+ "username": username,
49
  "agent_code": f"https://huggingface.co/spaces/{os.getenv('SPACE_ID')}/tree/main",
50
  "answers": answers,
51
  }
52
+ submit_url = f"{DEFAULT_API_URL}/submit"
53
+ try:
54
+ resp_s = requests.post(submit_url, json=payload, timeout=60)
55
+ resp_s.raise_for_status()
56
+ data = resp_s.json()
57
+ status = (
58
+ f"Score: {data.get('score', 'N/A')}% "
59
+ f"({data.get('correct_count', '?')}/{data.get('total_attempted', '?')})\n"
60
+ f"{data.get('message', '')}"
61
+ )
62
+ except Exception as e:
63
+ status = f"Submission Failed: {e}"
64
+
65
  return status, pd.DataFrame(log)
66
 
67
+
68
+ # ----- Gradio UI -----
69
  with gr.Blocks() as demo:
70
  gr.Markdown("# GAIA Q&A with smolagents")
71
  login = gr.LoginButton()
72
  run_btn = gr.Button("Run & Submit All", variant="primary")
 
 
73
 
74
+ status_out = gr.Textbox(label="Submission Status", lines=4)
75
+ results_table = gr.DataFrame(label="Per-Question Log")
76
+
77
+ run_btn.click(fn=run_and_submit_all, inputs=[login], outputs=[status_out, results_table])
78
 
79
  if __name__ == "__main__":
80
  demo.launch()