Ubik80 commited on
Commit
fd5a08b
·
verified ·
1 Parent(s): ee4d1e5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -37
app.py CHANGED
@@ -11,28 +11,26 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
11
 
12
  def run_and_submit_all(profile):
13
  """
14
- Fetch all questions, run the SmolAgent on them, submit all answers,
15
- and display the results.
16
  """
17
- space_id = os.getenv("SPACE_ID")
18
-
19
  if profile is None:
20
- return "Please login to Hugging Face with the button.", None
21
  username = profile.username
22
 
23
- # Instantiate the agent
24
  try:
25
  agent = create_agent()
26
  except Exception as e:
27
  return f"Error initializing agent: {e}", None
28
 
29
- agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
30
 
31
  # Fetch questions
32
  try:
33
- response = requests.get(f"{DEFAULT_API_URL}/questions", timeout=15)
34
- response.raise_for_status()
35
- questions = response.json()
36
  except Exception as e:
37
  return f"Error fetching questions: {e}", None
38
 
@@ -42,24 +40,24 @@ def run_and_submit_all(profile):
42
  # Run agent on each question
43
  results = []
44
  payload = []
45
- for q in questions:
46
- task_id = q.get("task_id")
47
- question_text = q.get("question")
48
- if not task_id or not question_text:
49
  continue
50
  try:
51
- answer = agent.run(question=question_text)
52
  except Exception as e:
53
- answer = f"ERROR: {e}"
54
- results.append({"Task ID": task_id, "Question": question_text, "Answer": answer})
55
- payload.append({"task_id": task_id, "submitted_answer": answer})
56
 
57
  # Submit answers
58
- submit_payload = {"username": username, "agent_code": agent_code, "answers": payload}
59
  try:
60
- resp = requests.post(f"{DEFAULT_API_URL}/submit", json=submit_payload, timeout=60)
61
- resp.raise_for_status()
62
- data = resp.json()
63
  status = (
64
  f"Submission Successful!\n"
65
  f"User: {data.get('username')}\n"
@@ -74,10 +72,10 @@ def run_and_submit_all(profile):
74
 
75
  def test_random_question(profile):
76
  """
77
- Fetch a random GAIA question and return its answer by the agent.
78
  """
79
  if profile is None:
80
- return "Please login to test.", ""
81
  try:
82
  q = fetch_random_question()
83
  agent = create_agent()
@@ -86,31 +84,32 @@ def test_random_question(profile):
86
  except Exception as e:
87
  return f"Error: {e}", ""
88
 
89
-
90
  # --- Gradio Interface ---
91
  with gr.Blocks() as demo:
92
  gr.Markdown("# SmolAgent Evaluation Runner")
93
  gr.Markdown(
94
  """
95
  **Instructions:**
96
- 1. Clone this space and define your agent logic in agent.py.
97
- 2. Log in with your Hugging Face account using the login button.
98
- 3. Use 'Run Evaluation & Submit All Answers' or 'Test Random Question'.
99
  """
100
  )
101
 
102
  login = gr.LoginButton()
103
- run_all = gr.Button("Run Evaluation & Submit All Answers")
104
- test = gr.Button("Test Random Question")
 
 
 
105
 
106
- status = gr.Textbox(label="Status / Result", lines=5, interactive=False)
107
- table = gr.DataFrame(label="Results Table", wrap=True)
108
- qbox = gr.Textbox(label="Random Question", lines=3, interactive=False)
109
- abox = gr.Textbox(label="Agent Answer", lines=3, interactive=False)
110
 
111
- # Attach login component directly as input for profile
112
- run_all.click(fn=run_and_submit_all, inputs=[login], outputs=[status, table])
113
- test.click(fn=test_random_question, inputs=[login], outputs=[qbox, abox])
114
 
115
  if __name__ == "__main__":
116
  demo.launch(debug=True, share=False)
 
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
  if profile is None:
18
+ return "Please login to Hugging Face with the login button.", None
19
  username = profile.username
20
 
21
+ # Instantiate agent
22
  try:
23
  agent = create_agent()
24
  except Exception as e:
25
  return f"Error initializing agent: {e}", None
26
 
27
+ agent_code = f"https://huggingface.co/spaces/{os.getenv('SPACE_ID')}/tree/main"
28
 
29
  # Fetch questions
30
  try:
31
+ resp = requests.get(f"{DEFAULT_API_URL}/questions", timeout=15)
32
+ resp.raise_for_status()
33
+ questions = resp.json()
34
  except Exception as e:
35
  return f"Error fetching questions: {e}", None
36
 
 
40
  # Run agent on each question
41
  results = []
42
  payload = []
43
+ for item in questions:
44
+ task_id = item.get("task_id")
45
+ question_text = item.get("question")
46
+ if not task_id or question_text is None:
47
  continue
48
  try:
49
+ ans = agent.run(question=question_text)
50
  except Exception as e:
51
+ ans = f"ERROR: {e}"
52
+ results.append({"Task ID": task_id, "Question": question_text, "Answer": ans})
53
+ payload.append({"task_id": task_id, "submitted_answer": ans})
54
 
55
  # Submit answers
56
+ submission = {"username": username, "agent_code": agent_code, "answers": payload}
57
  try:
58
+ r = requests.post(f"{DEFAULT_API_URL}/submit", json=submission, timeout=60)
59
+ r.raise_for_status()
60
+ data = r.json()
61
  status = (
62
  f"Submission Successful!\n"
63
  f"User: {data.get('username')}\n"
 
72
 
73
  def test_random_question(profile):
74
  """
75
+ Fetch a random question and return the question and agent's answer.
76
  """
77
  if profile is None:
78
+ return "Please login to Hugging Face with the login button.", ""
79
  try:
80
  q = fetch_random_question()
81
  agent = create_agent()
 
84
  except Exception as e:
85
  return f"Error: {e}", ""
86
 
 
87
  # --- Gradio Interface ---
88
  with gr.Blocks() as demo:
89
  gr.Markdown("# SmolAgent Evaluation Runner")
90
  gr.Markdown(
91
  """
92
  **Instructions:**
93
+ 1. Clone this space and implement your agent logic in agent.py.
94
+ 2. Log in with your Hugging Face account using the login button below.
95
+ 3. Use the buttons to run evaluation or test a random question.
96
  """
97
  )
98
 
99
  login = gr.LoginButton()
100
+ state = gr.State(value=None)
101
+ login.click(lambda user: user, inputs=[login], outputs=[state])
102
+
103
+ run_all_btn = gr.Button("Run Evaluation & Submit All Answers")
104
+ test_btn = gr.Button("Test Random Question")
105
 
106
+ status_output = gr.Textbox(label="Status / Result", lines=5, interactive=False)
107
+ results_table = gr.DataFrame(label="Results Table", wrap=True)
108
+ question_box = gr.Textbox(label="Random Question", lines=3, interactive=False)
109
+ answer_box = gr.Textbox(label="Agent Answer", lines=3, interactive=False)
110
 
111
+ run_all_btn.click(fn=run_and_submit_all, inputs=[state], outputs=[status_output, results_table])
112
+ test_btn.click(fn=test_random_question, inputs=[state], outputs=[question_box, answer_box])
 
113
 
114
  if __name__ == "__main__":
115
  demo.launch(debug=True, share=False)