Ubik80 commited on
Commit
bd7cd5b
·
verified ·
1 Parent(s): 6478b83

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -71
app.py CHANGED
@@ -5,82 +5,57 @@ import pandas as pd
5
 
6
  from agent import create_agent, fetch_random_question
7
 
8
- # --- Constants ---
9
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
10
 
11
-
12
  def run_and_submit_all(profile):
13
- """
14
- Fetch all questions, run the agent on them, submit all answers,
15
- and return the status and results table.
16
- """
17
- # Check login
18
  if not profile:
19
  return "Please login to Hugging Face with the login button.", None
20
-
21
- # Extract username (support both .username and .name)
22
  username = getattr(profile, "username", None) or getattr(profile, "name", None)
23
  if not username:
24
  return "Login error: username not found.", None
25
 
26
- # Build URLs
27
- questions_url = f"{DEFAULT_API_URL}/questions"
28
- submit_url = f"{DEFAULT_API_URL}/submit"
29
-
30
- # Instantiate agent
31
- try:
32
- agent = create_agent()
33
- except Exception as e:
34
- return f"Error initializing agent: {e}", None
35
-
36
  # Fetch questions
37
  try:
38
- resp = requests.get(questions_url, timeout=15)
39
- resp.raise_for_status()
40
- questions = resp.json()
41
  except Exception as e:
42
  return f"Error fetching questions: {e}", None
43
 
44
- # Run agent
45
- results = []
46
- answers_payload = []
 
 
47
  for q in questions:
48
- tid = q.get("task_id")
49
- text = q.get("question")
50
- if not tid or not text:
51
  continue
52
  try:
53
- ans = agent.run(question=text)
54
  except Exception as e:
55
  ans = f"ERROR: {e}"
56
- results.append({
57
- "Task ID": tid,
58
- "Question": text,
59
- "Answer": ans
60
- })
61
- answers_payload.append({
62
- "task_id": tid,
63
- "submitted_answer": ans
64
- })
65
-
66
- if not answers_payload:
67
  return "Agent returned no answers.", pd.DataFrame(results)
68
 
69
- # Submit answers
70
- payload = {
71
  "username": username,
72
  "agent_code": f"https://huggingface.co/spaces/{os.getenv('SPACE_ID')}/tree/main",
73
- "answers": answers_payload
74
  }
75
  try:
76
- resp = requests.post(submit_url, json=payload, timeout=60)
77
  resp.raise_for_status()
78
  data = resp.json()
79
  status = (
80
  f"Submission Successful!\n"
81
- f"User: {data.get('username')}\n"
82
- f"Score: {data.get('score')}% ({data.get('correct_count')}/{data.get('total_attempted')})\n"
83
- f"Message: {data.get('message')}"
84
  )
85
  except Exception as e:
86
  status = f"Submission Failed: {e}"
@@ -89,24 +64,15 @@ def run_and_submit_all(profile):
89
 
90
 
91
  def test_random_question(profile):
92
- """
93
- Fetch a random GAIA question and return the agent's answer.
94
- """
95
  if not profile:
96
  return "Please login to Hugging Face with the login button.", ""
97
-
98
- # Get question and run agent
99
- try:
100
- q = fetch_random_question()
101
- question = q.get("question", "")
102
- agent = create_agent()
103
- answer = agent.run(question=question)
104
- return question, answer
105
- except Exception as e:
106
- return f"Error during test: {e}", ""
107
 
108
 
109
- # --- Gradio Interface ---
110
  with gr.Blocks() as demo:
111
  gr.Markdown("# SmolAgent Evaluation Runner")
112
  gr.Markdown(
@@ -119,18 +85,16 @@ with gr.Blocks() as demo:
119
  )
120
 
121
  login = gr.LoginButton()
 
 
122
 
123
- run_button = gr.Button("Run Evaluation & Submit All Answers")
124
- test_button = gr.Button("Test Random Question")
125
-
126
- status_output = gr.Textbox(label="Status / Result", lines=5, interactive=False)
127
- results_table = gr.DataFrame(label="Full Results Table", wrap=True)
128
- question_box = gr.Textbox(label="Random Question", lines=3, interactive=False)
129
- answer_box = gr.Textbox(label="Agent Answer", lines=3, interactive=False)
130
 
131
- # Wire the login component directly into the callbacks
132
- run_button.click(fn=run_and_submit_all, inputs=[login], outputs=[status_output, results_table])
133
- test_button.click(fn=test_random_question, inputs=[login], outputs=[question_box, answer_box])
134
 
135
  if __name__ == "__main__":
136
  demo.launch(debug=True, share=False)
 
5
 
6
  from agent import create_agent, fetch_random_question
7
 
 
8
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
9
 
 
10
  def run_and_submit_all(profile):
 
 
 
 
 
11
  if not profile:
12
  return "Please login to Hugging Face with the login button.", None
 
 
13
  username = getattr(profile, "username", None) or getattr(profile, "name", None)
14
  if not username:
15
  return "Login error: username not found.", None
16
 
 
 
 
 
 
 
 
 
 
 
17
  # Fetch questions
18
  try:
19
+ qs = requests.get(f"{DEFAULT_API_URL}/questions", timeout=15)
20
+ qs.raise_for_status()
21
+ questions = qs.json()
22
  except Exception as e:
23
  return f"Error fetching questions: {e}", None
24
 
25
+ # Instantiate agent
26
+ agent = create_agent()
27
+
28
+ # Run and collect answers
29
+ results, payload = [], []
30
  for q in questions:
31
+ tid, text = q.get("task_id"), q.get("question")
32
+ if not (tid and text):
 
33
  continue
34
  try:
35
+ ans = agent.run(text) # <-- positional!
36
  except Exception as e:
37
  ans = f"ERROR: {e}"
38
+ results.append({"Task ID": tid, "Question": text, "Answer": ans})
39
+ payload.append({"task_id": tid, "submitted_answer": ans})
40
+
41
+ if not payload:
 
 
 
 
 
 
 
42
  return "Agent returned no answers.", pd.DataFrame(results)
43
 
44
+ # Submit
45
+ submission = {
46
  "username": username,
47
  "agent_code": f"https://huggingface.co/spaces/{os.getenv('SPACE_ID')}/tree/main",
48
+ "answers": payload
49
  }
50
  try:
51
+ resp = requests.post(f"{DEFAULT_API_URL}/submit", json=submission, timeout=60)
52
  resp.raise_for_status()
53
  data = resp.json()
54
  status = (
55
  f"Submission Successful!\n"
56
+ f"User: {data['username']}\n"
57
+ f"Score: {data['score']}% ({data['correct_count']}/{data['total_attempted']})\n"
58
+ f"Message: {data['message']}"
59
  )
60
  except Exception as e:
61
  status = f"Submission Failed: {e}"
 
64
 
65
 
66
  def test_random_question(profile):
 
 
 
67
  if not profile:
68
  return "Please login to Hugging Face with the login button.", ""
69
+ q = fetch_random_question()
70
+ agent = create_agent()
71
+ # Positional call again
72
+ answer = agent.run(q["question"])
73
+ return q["question"], answer
 
 
 
 
 
74
 
75
 
 
76
  with gr.Blocks() as demo:
77
  gr.Markdown("# SmolAgent Evaluation Runner")
78
  gr.Markdown(
 
85
  )
86
 
87
  login = gr.LoginButton()
88
+ run_btn = gr.Button("Run Evaluation & Submit All Answers")
89
+ test_btn = gr.Button("Test Random Question")
90
 
91
+ status_out = gr.Textbox(label="Status / Result", lines=5, interactive=False)
92
+ table_out = gr.DataFrame(label="Full Results Table", wrap=True)
93
+ question_out = gr.Textbox(label="Random Question", lines=3, interactive=False)
94
+ answer_out = gr.Textbox(label="Agent Answer", lines=3, interactive=False)
 
 
 
95
 
96
+ run_btn.click(fn=run_and_submit_all, inputs=[login], outputs=[status_out, table_out])
97
+ test_btn.click(fn=test_random_question, inputs=[login], outputs=[question_out, answer_out])
 
98
 
99
  if __name__ == "__main__":
100
  demo.launch(debug=True, share=False)