anssio commited on
Commit
851b030
·
verified ·
1 Parent(s): e57c8b0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -38
app.py CHANGED
@@ -1,49 +1,24 @@
1
  # 17/08/2025 -AO
 
2
  import os
3
  import gradio as gr
4
  import requests
5
  import inspect
6
  import pandas as pd
7
- import torch
8
- from transformers import AutoModelForCausalLM, AutoTokenizer
9
- from transformers.agents import HfAgent
10
- from transformers.tools import DuckDuckGoSearchTool
11
- from huggingface_hub import InferenceClient
12
 
13
  # --- Constants ---
14
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
15
 
16
- # --- Advanced Agent Definition ---
17
- # ----- THIS IS WHERE YOU BUILD YOUR AGENT ------
18
- class MyAgent:
19
- def __init__(self):
20
- # Initialize the model and tokenizer for tool use
21
- print("Initializing MyAgent with Qwen2.5-72B-Instruct...")
22
-
23
- # Muista asettaa -> HF_TOKEN secret in your space settings.
24
- self.agent = HfAgent(
25
- llm="Qwen/Qwen2.5-72B-Instruct",
26
- additional_tools=[DuckDuckGoSearchTool()],
27
- )
28
- print("Agent initialized with InferenceClient.")
29
-
30
- def __call__(self, question: str) -> str:
31
- print(f"Agent received question: {question[:50]}...")
32
- try:
33
- result = self.agent.run(question)
34
-
35
- # The GAIA scoring is an "EXACT MATCH".
36
- final_answer = str(result)
37
- print(f"Agent returning answer: {final_answer}")
38
- return final_answer
39
- except Exception as e:
40
- print(f"An error occurred during agent execution: {e}")
41
- return f"Error: {e}"
42
 
43
- # The rest of the `run_and_submit_all` and Gradio code remains the same as in the template.
44
- def run_and_submit_all(profile: gr.OAuthProfile | None):
 
 
 
45
  # --- Determine HF Space Runtime URL and Repo URL ---
46
- space_id = os.getenv("SPACE_ID")
47
 
48
  if profile:
49
  username= f"{profile.username}"
@@ -58,7 +33,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
58
 
59
  # 1. Instantiate Agent ( modify this part to create your agent)
60
  try:
61
- agent = MyAgent()
62
  except Exception as e:
63
  print(f"Error instantiating agent: {e}")
64
  return f"Error initializing agent: {e}", None
@@ -94,11 +69,12 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
94
  for item in questions_data:
95
  task_id = item.get("task_id")
96
  question_text = item.get("question")
 
97
  if not task_id or question_text is None:
98
  print(f"Skipping item with missing task_id or question: {item}")
99
  continue
100
  try:
101
- submitted_answer = agent(question_text)
102
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
103
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
104
  except Exception as e:
@@ -109,7 +85,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
109
  print("Agent did not produce any answers to submit.")
110
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
111
 
112
- # 4. Prepare Submission
113
  submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
114
  status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
115
  print(status_update)
@@ -157,16 +133,20 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
157
  results_df = pd.DataFrame(results_log)
158
  return status_message, results_df
159
 
 
160
  # --- Build Gradio Interface using Blocks ---
161
  with gr.Blocks() as demo:
162
  gr.Markdown("# Basic Agent Evaluation Runner")
163
  gr.Markdown(
164
  """
165
  **Instructions:**
166
-
167
  1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
168
  2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
169
  3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
 
 
 
 
170
  """
171
  )
172
 
@@ -175,6 +155,7 @@ with gr.Blocks() as demo:
175
  run_button = gr.Button("Run Evaluation & Submit All Answers")
176
 
177
  status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
 
178
  results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
179
 
180
  run_button.click(
 
1
  # 17/08/2025 -AO
2
+
3
  import os
4
  import gradio as gr
5
  import requests
6
  import inspect
7
  import pandas as pd
8
+ from agent import BasicAgent
9
+
 
 
 
10
 
11
  # --- Constants ---
12
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
+ def run_and_submit_all( profile: gr.OAuthProfile | None):
16
+ """
17
+ Fetches all questions, runs the BasicAgent on them, submits all answers,
18
+ and displays the results.
19
+ """
20
  # --- Determine HF Space Runtime URL and Repo URL ---
21
+ space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
22
 
23
  if profile:
24
  username= f"{profile.username}"
 
33
 
34
  # 1. Instantiate Agent ( modify this part to create your agent)
35
  try:
36
+ agent = BasicAgent()
37
  except Exception as e:
38
  print(f"Error instantiating agent: {e}")
39
  return f"Error initializing agent: {e}", None
 
69
  for item in questions_data:
70
  task_id = item.get("task_id")
71
  question_text = item.get("question")
72
+ file_name_text = item.get("file_name")
73
  if not task_id or question_text is None:
74
  print(f"Skipping item with missing task_id or question: {item}")
75
  continue
76
  try:
77
+ submitted_answer = agent(question_text, file_name_text)
78
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
79
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
80
  except Exception as e:
 
85
  print("Agent did not produce any answers to submit.")
86
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
87
 
88
+ # 4. Prepare Submission
89
  submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
90
  status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
91
  print(status_update)
 
133
  results_df = pd.DataFrame(results_log)
134
  return status_message, results_df
135
 
136
+
137
  # --- Build Gradio Interface using Blocks ---
138
  with gr.Blocks() as demo:
139
  gr.Markdown("# Basic Agent Evaluation Runner")
140
  gr.Markdown(
141
  """
142
  **Instructions:**
 
143
  1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
144
  2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
145
  3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
146
+ ---
147
+ **Disclaimers:**
148
+ Once clicking on the "submit button, it can take quite some time ( this is the time for the agent to go through all the questions).
149
+ This space provides a basic setup and is intentionally sub-optimal to encourage you to develop your own, more robust solution. For instance for the delay process of the submit button, a solution could be to cache the answers and submit in a seperate action or even to answer the questions in async.
150
  """
151
  )
152
 
 
155
  run_button = gr.Button("Run Evaluation & Submit All Answers")
156
 
157
  status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
158
+ # Removed max_rows=10 from DataFrame constructor
159
  results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
160
 
161
  run_button.click(