MasterOfHugs commited on
Commit
2f42c05
·
verified ·
1 Parent(s): b4888ab

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -17
app.py CHANGED
@@ -11,7 +11,7 @@ import requests
11
  import pandas as pd
12
  import gradio as gr
13
 
14
- from smolagents import CodeAgent, TransformersModel, tool
15
 
16
  # -------------------------
17
  # Minimal tools
@@ -41,13 +41,10 @@ def safe_calc(expr: str):
41
  def calculator(expr: str) -> str:
42
  """
43
  Safely evaluate a mathematical expression.
44
-
45
  Args:
46
- expr (str): Mathematical expression to evaluate, e.g., "2 + 2 * 3".
47
-
48
  Returns:
49
- str: JSON string with {"expression": expr, "result": value}
50
- or {"error": "..."} on failure.
51
  """
52
  try:
53
  val = safe_calc(expr)
@@ -60,13 +57,10 @@ def calculator(expr: str) -> str:
60
  def get_current_time_in_timezone(timezone: str) -> str:
61
  """
62
  Get the current local time in a specified timezone.
63
-
64
  Args:
65
- timezone (str): A valid timezone string (e.g., "Europe/Paris").
66
-
67
  Returns:
68
- str: JSON string with {"timezone": timezone, "local_time": "..."}
69
- or {"error": "..."} on failure.
70
  """
71
  try:
72
  tz = pytz.timezone(timezone)
@@ -87,10 +81,15 @@ try:
87
  except Exception:
88
  prompt_templates = None
89
 
 
90
  # -------------------------
91
- # TransformersModel + CodeAgent minimal
92
  # -------------------------
93
- model = TransformersModel(model_id="HuggingFaceTB/SmolLM-135M-Instruct")
 
 
 
 
94
 
95
  code_agent = CodeAgent(
96
  model=model,
@@ -100,6 +99,7 @@ code_agent = CodeAgent(
100
  prompt_templates=prompt_templates
101
  )
102
 
 
103
  # -------------------------
104
  # GAIA Agent wrapper
105
  # -------------------------
@@ -132,7 +132,7 @@ class GaiaAgentMinimal:
132
  tz = "UTC"
133
  return get_current_time_in_timezone(tz)
134
 
135
- # fallback LLM
136
  resp = self.code_agent.run(q)
137
  if isinstance(resp, dict):
138
  for key in ("final_answer", "answer", "result", "output"):
@@ -143,9 +143,11 @@ class GaiaAgentMinimal:
143
  except Exception as e:
144
  return json.dumps({"error": f"Agent internal error: {e}"})
145
 
 
146
  # instantiate GAIA agent
147
  gaia_agent = GaiaAgentMinimal(code_agent)
148
 
 
149
  # -------------------------
150
  # GAIA runner
151
  # -------------------------
@@ -154,11 +156,10 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
154
  def run_and_submit_all(profile: gr.OAuthProfile | None):
155
  space_id = os.getenv("SPACE_ID")
156
 
157
- if profile:
158
- username = f"{profile.username}"
159
- else:
160
  return "Please Login to Hugging Face with the button.", None
161
 
 
162
  api_url = DEFAULT_API_URL
163
  questions_url = f"{api_url}/questions"
164
  submit_url = f"{api_url}/submit"
@@ -213,6 +214,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
213
  results_df = pd.DataFrame(results_log)
214
  return f"Submission failed: {e}", results_df
215
 
 
216
  # -------------------------
217
  # Gradio UI
218
  # -------------------------
 
11
  import pandas as pd
12
  import gradio as gr
13
 
14
+ from smolagents import CodeAgent, HfApiModel, tool
15
 
16
  # -------------------------
17
  # Minimal tools
 
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)
 
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)
 
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
+ )
93
 
94
  code_agent = CodeAgent(
95
  model=model,
 
99
  prompt_templates=prompt_templates
100
  )
101
 
102
+
103
  # -------------------------
104
  # GAIA Agent wrapper
105
  # -------------------------
 
132
  tz = "UTC"
133
  return get_current_time_in_timezone(tz)
134
 
135
+ # fallback LLM via HfApiModel
136
  resp = self.code_agent.run(q)
137
  if isinstance(resp, dict):
138
  for key in ("final_answer", "answer", "result", "output"):
 
143
  except Exception as e:
144
  return json.dumps({"error": f"Agent internal error: {e}"})
145
 
146
+
147
  # instantiate GAIA agent
148
  gaia_agent = GaiaAgentMinimal(code_agent)
149
 
150
+
151
  # -------------------------
152
  # GAIA runner
153
  # -------------------------
 
156
  def run_and_submit_all(profile: gr.OAuthProfile | None):
157
  space_id = os.getenv("SPACE_ID")
158
 
159
+ if not profile:
 
 
160
  return "Please Login to Hugging Face with the button.", None
161
 
162
+ username = f"{profile.username}"
163
  api_url = DEFAULT_API_URL
164
  questions_url = f"{api_url}/questions"
165
  submit_url = f"{api_url}/submit"
 
214
  results_df = pd.DataFrame(results_log)
215
  return f"Submission failed: {e}", results_df
216
 
217
+
218
  # -------------------------
219
  # Gradio UI
220
  # -------------------------