gonzalo334 commited on
Commit
058619b
·
verified ·
1 Parent(s): 51d38a8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +99 -48
app.py CHANGED
@@ -12,40 +12,70 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
12
  from smolagents import (
13
  CodeAgent,
14
  DuckDuckGoSearchTool,
15
- InferenceClientModel,
16
  TransformersModel,
17
- LogLevel,
 
18
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  def build_model():
20
- hf_token = os.getenv("HF_TOKEN")
21
- if not hf_token:
22
- raise RuntimeError("HF_TOKEN is not set (Space → Settings → Variables and secrets).")
23
-
24
- # Try a prioritized list of chat-capable models on the serverless chat router
25
- serverless_candidates = [
26
- "Qwen/Qwen2.5-7B-Instruct-1M",
27
- "mistralai/Mistral-7B-Instruct-v0.3",
28
- "HuggingFaceH4/zephyr-7b-beta",
29
- ]
30
-
31
- last_err = None
32
- for mid in serverless_candidates:
33
- try:
34
- return InferenceClientModel(
35
- model_id=mid,
36
- token=hf_token, # let provider auto-select, don't force provider=
37
- timeout=120,
38
- )
39
- except Exception as e:
40
- print(f"[warn] Serverless model '{mid}' failed: {e!r}")
41
- last_err = e
42
 
43
  # Final fallback: local transformers (CPU; slower, but never hits the chat router)
44
  try:
45
  return TransformersModel(
46
- model_id="HuggingFaceTB/SmolLM2-1.7B-Instruct",
47
- max_new_tokens=256,
48
- )
 
49
  except Exception as e:
50
  raise RuntimeError(f"All model inits failed. Last serverless error: {last_err!r}; "
51
  f"Transformers fallback error: {e!r}")
@@ -56,9 +86,9 @@ class BasicAgent:
56
  def __init__(self):
57
  self.search = DuckDuckGoSearchTool()
58
 
59
- hf_token = os.getenv("HF_TOKEN")
60
- if not hf_token:
61
- raise RuntimeError("HF_TOKEN is not set (Space → Settings → Variables and secrets).")
62
 
63
  # Use a chat-capable model; let provider auto-route
64
  self.model = build_model()
@@ -67,16 +97,22 @@ class BasicAgent:
67
  self.agent = CodeAgent(
68
  tools=[self.search],
69
  model=self.model,
70
- max_steps=5,
71
  verbosity_level=1
72
  )
73
 
74
  # Health check WITHOUT verbosity_level in run()
75
  _ = self.agent.run("Reply with the single word: OK")
76
-
77
- def __call__(self, question: str) -> str:
78
- # No verbosity_level here either
79
- return self.agent.run(question)
 
 
 
 
 
 
80
 
81
 
82
 
@@ -136,17 +172,34 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
136
  print(f"Running agent on {len(questions_data)} questions...")
137
  for item in questions_data:
138
  task_id = item.get("task_id")
139
- question_text = item.get("question")
140
- if not task_id or question_text is None:
141
  print(f"Skipping item with missing task_id or question: {item}")
142
  continue
143
  try:
144
- submitted_answer = agent(question_text)
145
- answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
146
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
 
 
 
 
 
 
 
 
 
 
 
 
147
  except Exception as e:
148
- print(f"Error running agent on task {task_id}: {e}")
149
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
 
 
 
 
 
150
 
151
  if not answers_payload:
152
  print("Agent did not produce any answers to submit.")
@@ -219,18 +272,16 @@ with gr.Blocks() as demo:
219
  """
220
  )
221
 
222
- gr.LoginButton()
223
  run_button = gr.Button("Run Evaluation & Submit All Answers")
224
-
225
- status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
226
- # Removed max_rows=10 from DataFrame constructor
227
- results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
228
-
229
  run_button.click(
230
  fn=run_and_submit_all,
 
231
  outputs=[status_output, results_table]
232
  )
233
 
 
234
  if __name__ == "__main__":
235
  print("\n" + "-"*30 + " App Starting " + "-"*30)
236
  # Check for SPACE_HOST and SPACE_ID at startup for information
 
12
  from smolagents import (
13
  CodeAgent,
14
  DuckDuckGoSearchTool,
 
15
  TransformersModel,
16
+ #InferenceClientModel,
17
+ #LogLevel,
18
  )
19
+
20
+ import re
21
+
22
+ def answer_only(text: str) -> str:
23
+ """Keep the minimal exact answer; strip markdown/explanations/punctuation."""
24
+ if text is None:
25
+ return ""
26
+ s = str(text).strip()
27
+
28
+ # If there is a single integer in the output, return just that integer.
29
+ m = re.findall(r"-?\d+", s)
30
+ if len(m) == 1:
31
+ return m[0]
32
+
33
+ # If output is wrapped in markdown or quotes, strip them.
34
+ s = s.strip("`").strip('"').strip("'")
35
+
36
+ # Remove markdown like **bold** or *italics*
37
+ s = re.sub(r"\*\*(.*?)\*\*", r"\1", s)
38
+ s = re.sub(r"\*(.*?)\*", r"\1", s)
39
+
40
+ # Trim trailing period if answer looks like a number or a simple token.
41
+ if s.endswith(".") and re.fullmatch(r"[A-Za-z0-9\- ]+\.", s):
42
+ s = s[:-1]
43
+
44
+ return s.strip()
45
+
46
+
47
+
48
  def build_model():
49
+ # hf_token = os.getenv("HF_TOKEN")
50
+ # if not hf_token:
51
+ # raise RuntimeError("HF_TOKEN is not set (Space → Settings → Variables and secrets).")
52
+
53
+ # # Try a prioritized list of chat-capable models on the serverless chat router
54
+ # serverless_candidates = [
55
+ # "Qwen/Qwen2.5-7B-Instruct-1M",
56
+ # "mistralai/Mistral-7B-Instruct-v0.3",
57
+ # "HuggingFaceH4/zephyr-7b-beta",
58
+ # ]
59
+
60
+ # last_err = None
61
+ # for mid in serverless_candidates:
62
+ # try:
63
+ # return InferenceClientModel(
64
+ # model_id=mid,
65
+ # token=hf_token, # let provider auto-select, don't force provider=
66
+ # timeout=120,
67
+ # )
68
+ # except Exception as e:
69
+ # print(f"[warn] Serverless model '{mid}' failed: {e!r}")
70
+ # last_err = e
71
 
72
  # Final fallback: local transformers (CPU; slower, but never hits the chat router)
73
  try:
74
  return TransformersModel(
75
+ model_id="HuggingFaceTB/SmolLM2-1.7B-Instruct", # or "Qwen/Qwen2.5-0.5B-Instruct"
76
+ max_new_tokens=220,
77
+ temperature=0.2,
78
+ )
79
  except Exception as e:
80
  raise RuntimeError(f"All model inits failed. Last serverless error: {last_err!r}; "
81
  f"Transformers fallback error: {e!r}")
 
86
  def __init__(self):
87
  self.search = DuckDuckGoSearchTool()
88
 
89
+ # hf_token = os.getenv("HF_TOKEN")
90
+ # if not hf_token:
91
+ # raise RuntimeError("HF_TOKEN is not set (Space → Settings → Variables and secrets).")
92
 
93
  # Use a chat-capable model; let provider auto-route
94
  self.model = build_model()
 
97
  self.agent = CodeAgent(
98
  tools=[self.search],
99
  model=self.model,
100
+ max_steps=3,
101
  verbosity_level=1
102
  )
103
 
104
  # Health check WITHOUT verbosity_level in run()
105
  _ = self.agent.run("Reply with the single word: OK")
106
+
107
+ def __call__(self, item: dict) -> str:
108
+ q = (item.get("question") or "").strip()
109
+ prompt = (
110
+ "Answer with the shortest exact phrase/number only. "
111
+ "No explanations.\n\n"
112
+ f"Question: {q}\nAnswer:"
113
+ )
114
+ raw = self.agent.run(prompt)
115
+ return answer_only(raw) # exact-match friendly
116
 
117
 
118
 
 
172
  print(f"Running agent on {len(questions_data)} questions...")
173
  for item in questions_data:
174
  task_id = item.get("task_id")
175
+ question_text = (item.get("question") or "").strip()
176
+ if not task_id or not question_text:
177
  print(f"Skipping item with missing task_id or question: {item}")
178
  continue
179
  try:
180
+ # pass the whole item so the agent can use task_id (e.g., GET /files/{task_id})
181
+ submitted_answer_raw = agent(item)
182
+
183
+ # ✅ keep answers exact-match friendly (only if you added answer_only())
184
+ submitted_answer = answer_only(submitted_answer_raw) if callable(globals().get("answer_only")) else submitted_answer_raw
185
+
186
+ answers_payload.append({
187
+ "task_id": task_id,
188
+ "submitted_answer": submitted_answer
189
+ })
190
+ results_log.append({
191
+ "Task ID": task_id,
192
+ "Question": question_text,
193
+ "Submitted Answer": submitted_answer
194
+ })
195
  except Exception as e:
196
+ print(f"Error running agent on task {task_id}: {e}")
197
+ results_log.append({
198
+ "Task ID": task_id,
199
+ "Question": question_text,
200
+ "Submitted Answer": f"AGENT ERROR: {e}"
201
+ })
202
+
203
 
204
  if not answers_payload:
205
  print("Agent did not produce any answers to submit.")
 
272
  """
273
  )
274
 
275
+ login_btn = gr.LoginButton() # capture the profile object
276
  run_button = gr.Button("Run Evaluation & Submit All Answers")
277
+
 
 
 
 
278
  run_button.click(
279
  fn=run_and_submit_all,
280
+ inputs=[login_btn], # ✅ pass the OAuth profile
281
  outputs=[status_output, results_table]
282
  )
283
 
284
+
285
  if __name__ == "__main__":
286
  print("\n" + "-"*30 + " App Starting " + "-"*30)
287
  # Check for SPACE_HOST and SPACE_ID at startup for information