MasterOfHugs commited on
Commit
abfaec9
·
verified ·
1 Parent(s): d0dcc49

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -45
app.py CHANGED
@@ -10,6 +10,7 @@ import re
10
  import requests
11
  import pandas as pd
12
  import gradio as gr
 
13
 
14
  from smolagents import CodeAgent, HfApiModel, tool
15
 
@@ -41,10 +42,12 @@ def safe_calc(expr: str):
41
  def calculator(expr: str) -> str:
42
  """
43
  Safely evaluate a mathematical expression.
 
44
  Args:
45
- expr (str): mathematical expression like "2 + 2 * 3"
 
46
  Returns:
47
- str: JSON string {"expression": expr, "result": value} or {"error": "..."}
48
  """
49
  try:
50
  val = safe_calc(expr)
@@ -52,15 +55,16 @@ def calculator(expr: str) -> str:
52
  except Exception as e:
53
  return json.dumps({"error": f"Calc error: {e}"})
54
 
55
-
56
  @tool
57
  def get_current_time_in_timezone(timezone: str) -> str:
58
  """
59
  Get the current local time in a specified timezone.
 
60
  Args:
61
- timezone (str): valid timezone string (e.g., "Europe/Paris")
 
62
  Returns:
63
- str: JSON string {"timezone": timezone, "local_time": "..."} or {"error": "..."}
64
  """
65
  try:
66
  tz = pytz.timezone(timezone)
@@ -69,25 +73,24 @@ def get_current_time_in_timezone(timezone: str) -> str:
69
  except Exception as e:
70
  return json.dumps({"error": f"Timezone error: {e}"})
71
 
72
-
73
  # -------------------------
74
  # Load prompts.yaml if exists
75
  # -------------------------
76
  prompt_templates = None
77
  try:
78
- import yaml
79
  with open("prompts.yaml", "r") as fh:
80
  prompt_templates = yaml.safe_load(fh)
81
  except Exception:
82
  prompt_templates = None
83
 
84
-
85
  # -------------------------
86
- # HfApiModel + CodeAgent
87
  # -------------------------
 
 
88
  model = HfApiModel(
89
- model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
90
- max_tokens=2048,
91
  temperature=0.5,
92
  api_key=hf_token
93
  )
@@ -100,7 +103,6 @@ code_agent = CodeAgent(
100
  prompt_templates=prompt_templates
101
  )
102
 
103
-
104
  # -------------------------
105
  # GAIA Agent wrapper
106
  # -------------------------
@@ -118,22 +120,13 @@ class GaiaAgentMinimal:
118
  def run(self, question: str) -> str:
119
  try:
120
  q = question.strip()
121
-
122
- # Calculator queries
123
  if self._is_calc(q):
124
  m = re.search(r'([0-9\.\s\+\-\*\/\^\%\(\)]+)', q)
125
  expr = m.group(1) if m else q
126
  return calculator(expr)
127
-
128
- # Time queries
129
  if self._is_time(q):
130
- if "paris" in q.lower() or "france" in q.lower():
131
- tz = "Europe/Paris"
132
- else:
133
- tz = "UTC"
134
  return get_current_time_in_timezone(tz)
135
-
136
- # fallback LLM via HfApiModel
137
  resp = self.code_agent.run(q)
138
  if isinstance(resp, dict):
139
  for key in ("final_answer", "answer", "result", "output"):
@@ -144,11 +137,8 @@ class GaiaAgentMinimal:
144
  except Exception as e:
145
  return json.dumps({"error": f"Agent internal error: {e}"})
146
 
147
-
148
- # instantiate GAIA agent
149
  gaia_agent = GaiaAgentMinimal(code_agent)
150
 
151
-
152
  # -------------------------
153
  # GAIA runner
154
  # -------------------------
@@ -156,15 +146,11 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
156
 
157
  def run_and_submit_all(profile: gr.OAuthProfile | None):
158
  space_id = os.getenv("SPACE_ID")
159
-
160
  if not profile:
161
  return "Please Login to Hugging Face with the button.", None
162
-
163
- username = f"{profile.username}"
164
- api_url = DEFAULT_API_URL
165
- questions_url = f"{api_url}/questions"
166
- submit_url = f"{api_url}/submit"
167
-
168
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" if space_id else "unknown"
169
 
170
  # Fetch questions
@@ -177,7 +163,6 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
177
  except Exception as e:
178
  return f"Error fetching questions: {e}", None
179
 
180
- # Run agent
181
  results_log = []
182
  answers_payload = []
183
  for item in questions_data:
@@ -195,9 +180,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
195
  if not answers_payload:
196
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
197
 
198
- submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
199
-
200
- # Submit
201
  try:
202
  response = requests.post(submit_url, json=submission_data, timeout=60)
203
  response.raise_for_status()
@@ -209,26 +192,20 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
209
  f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
210
  f"Message: {result_data.get('message', 'No message received.')}"
211
  )
212
- results_df = pd.DataFrame(results_log)
213
- return final_status, results_df
214
  except Exception as e:
215
- results_df = pd.DataFrame(results_log)
216
- return f"Submission failed: {e}", results_df
217
-
218
 
219
  # -------------------------
220
  # Gradio UI
221
  # -------------------------
222
  with gr.Blocks() as demo:
223
  gr.Markdown("# Minimal GAIA Agent Runner")
224
- gr.Markdown(
225
- "Log in to Hugging Face, click 'Run Evaluation & Submit All Answers' to fetch questions, run the agent, and submit answers."
226
- )
227
  gr.LoginButton()
228
  run_button = gr.Button("Run Evaluation & Submit All Answers")
229
  status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
230
  results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
231
-
232
  run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
233
 
234
  if __name__ == "__main__":
 
10
  import requests
11
  import pandas as pd
12
  import gradio as gr
13
+ import yaml
14
 
15
  from smolagents import CodeAgent, HfApiModel, tool
16
 
 
42
  def calculator(expr: str) -> str:
43
  """
44
  Safely evaluate a mathematical expression.
45
+
46
  Args:
47
+ expr (str): The math expression to calculate.
48
+
49
  Returns:
50
+ str: JSON string with {"expression": expr, "result": value} or {"error": "..."} on failure.
51
  """
52
  try:
53
  val = safe_calc(expr)
 
55
  except Exception as e:
56
  return json.dumps({"error": f"Calc error: {e}"})
57
 
 
58
  @tool
59
  def get_current_time_in_timezone(timezone: str) -> str:
60
  """
61
  Get the current local time in a specified timezone.
62
+
63
  Args:
64
+ timezone (str): A valid timezone string (e.g., "Europe/Paris").
65
+
66
  Returns:
67
+ str: JSON string with {"timezone": timezone, "local_time": "..."} or {"error": "..."} on failure.
68
  """
69
  try:
70
  tz = pytz.timezone(timezone)
 
73
  except Exception as e:
74
  return json.dumps({"error": f"Timezone error: {e}"})
75
 
 
76
  # -------------------------
77
  # Load prompts.yaml if exists
78
  # -------------------------
79
  prompt_templates = None
80
  try:
 
81
  with open("prompts.yaml", "r") as fh:
82
  prompt_templates = yaml.safe_load(fh)
83
  except Exception:
84
  prompt_templates = None
85
 
 
86
  # -------------------------
87
+ # HfApiModel + CodeAgent minimal
88
  # -------------------------
89
+ hf_token = os.getenv("HF_API_TOKEN") # token injecté automatiquement dans Spaces
90
+
91
  model = HfApiModel(
92
+ model_id="HuggingFaceTB/SmolLM-135M-Instruct",
93
+ max_tokens=1024,
94
  temperature=0.5,
95
  api_key=hf_token
96
  )
 
103
  prompt_templates=prompt_templates
104
  )
105
 
 
106
  # -------------------------
107
  # GAIA Agent wrapper
108
  # -------------------------
 
120
  def run(self, question: str) -> str:
121
  try:
122
  q = question.strip()
 
 
123
  if self._is_calc(q):
124
  m = re.search(r'([0-9\.\s\+\-\*\/\^\%\(\)]+)', q)
125
  expr = m.group(1) if m else q
126
  return calculator(expr)
 
 
127
  if self._is_time(q):
128
+ tz = "Europe/Paris" if "paris" in q.lower() or "france" in q.lower() else "UTC"
 
 
 
129
  return get_current_time_in_timezone(tz)
 
 
130
  resp = self.code_agent.run(q)
131
  if isinstance(resp, dict):
132
  for key in ("final_answer", "answer", "result", "output"):
 
137
  except Exception as e:
138
  return json.dumps({"error": f"Agent internal error: {e}"})
139
 
 
 
140
  gaia_agent = GaiaAgentMinimal(code_agent)
141
 
 
142
  # -------------------------
143
  # GAIA runner
144
  # -------------------------
 
146
 
147
  def run_and_submit_all(profile: gr.OAuthProfile | None):
148
  space_id = os.getenv("SPACE_ID")
 
149
  if not profile:
150
  return "Please Login to Hugging Face with the button.", None
151
+ username = profile.username.strip()
152
+ questions_url = f"{DEFAULT_API_URL}/questions"
153
+ submit_url = f"{DEFAULT_API_URL}/submit"
 
 
 
154
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" if space_id else "unknown"
155
 
156
  # Fetch questions
 
163
  except Exception as e:
164
  return f"Error fetching questions: {e}", None
165
 
 
166
  results_log = []
167
  answers_payload = []
168
  for item in questions_data:
 
180
  if not answers_payload:
181
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
182
 
183
+ submission_data = {"username": username, "agent_code": agent_code, "answers": answers_payload}
 
 
184
  try:
185
  response = requests.post(submit_url, json=submission_data, timeout=60)
186
  response.raise_for_status()
 
192
  f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
193
  f"Message: {result_data.get('message', 'No message received.')}"
194
  )
195
+ return final_status, pd.DataFrame(results_log)
 
196
  except Exception as e:
197
+ return f"Submission failed: {e}", pd.DataFrame(results_log)
 
 
198
 
199
  # -------------------------
200
  # Gradio UI
201
  # -------------------------
202
  with gr.Blocks() as demo:
203
  gr.Markdown("# Minimal GAIA Agent Runner")
204
+ gr.Markdown("Log in to Hugging Face, click 'Run Evaluation & Submit All Answers' to fetch questions, run the agent, and submit answers.")
 
 
205
  gr.LoginButton()
206
  run_button = gr.Button("Run Evaluation & Submit All Answers")
207
  status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
208
  results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
 
209
  run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
210
 
211
  if __name__ == "__main__":