omershahar commited on
Commit
da9da7d
·
verified ·
1 Parent(s): 9509a7d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +164 -51
app.py CHANGED
@@ -7,11 +7,28 @@ import gradio as gr
7
  import requests
8
  import pandas as pd
9
  import spaces
 
10
  from smolagents import CodeAgent, DuckDuckGoSearchTool, VisitWebpageTool, OpenAIServerModel, tool
11
  from huggingface_hub import InferenceClient
12
 
13
  # --- Constants ---
14
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
  # Satisfies ZeroGPU scanner requirement
17
  @spaces.GPU
@@ -53,6 +70,25 @@ def transcribe_audio(file_path: str) -> str:
53
  Args:
54
  file_path: Local path to the audio file.
55
  """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
  token = os.getenv("HF_TOKEN")
57
  client = InferenceClient(token=token)
58
  try:
@@ -70,28 +106,114 @@ def transcribe_audio(file_path: str) -> str:
70
  except Exception as e:
71
  return f"Speech-to-text transcription failed: {e}"
72
 
73
- # --- Model & Agent Construction ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
 
75
  def build_gaia_agent() -> CodeAgent:
76
- """Build CodeAgent using Groq (free, high-capacity) or fallback to HF."""
77
  groq_key = os.getenv("GROQ_API_KEY")
78
-
79
- if groq_key:
80
- print("Using Groq API provider (llama-3.3-70b-versatile)...")
81
- model = OpenAIServerModel(
82
- model_id="llama-3.3-70b-versatile",
83
- api_base="https://api.groq.com/openai/v1",
84
- api_key=groq_key
85
- )
86
- else:
87
- print("Warning: GROQ_API_KEY not found. Falling back to HF Inference...")
88
- token = os.getenv("HF_TOKEN")
89
- model = OpenAIServerModel(
90
- model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
91
- api_base="https://router.huggingface.co/v1",
92
- api_key=token
93
- )
94
 
 
95
  search_tool = DuckDuckGoSearchTool()
96
  web_visit_tool = VisitWebpageTool()
97
 
@@ -124,12 +246,12 @@ def build_gaia_agent() -> CodeAgent:
124
  "zipfile",
125
  "base64"
126
  ],
127
- max_steps=12
128
  )
129
  return agent
130
 
131
  class GAIAgentWrapper:
132
- """Wrapper to run agent with rate-limit retries and strict output normalization."""
133
  def __init__(self):
134
  print("Initializing CodeAgent...")
135
  self.agent = build_gaia_agent()
@@ -147,39 +269,29 @@ class GAIAgentWrapper:
147
  "6. Never start with 'FINAL ANSWER:'. Output ONLY the pure value."
148
  )
149
 
150
- for attempt in range(3):
151
- try:
152
- raw_result = self.agent.run(strict_instruction)
153
- cleaned = str(raw_result).strip()
154
-
155
- # Clean common prefixes
156
- for prefix in ["final answer:", "answer:", "the answer is"]:
157
- if cleaned.lower().startswith(prefix):
158
- cleaned = cleaned[len(prefix):].strip(" :\n*`")
159
-
160
- cleaned = cleaned.strip(" '\"`*")
161
 
162
- # Normalize whole-number floats (e.g., '42.0' -> '42')
163
- try:
164
- as_float = float(cleaned)
165
- if as_float == int(as_float):
166
- cleaned = str(int(as_float))
167
- except ValueError:
168
- pass
169
 
170
- return cleaned
171
 
172
- except Exception as e:
173
- err = str(e)
174
- if "429" in err or "rate limit" in err.lower():
175
- wait = 20 * (attempt + 1)
176
- print(f"Rate limit hit. Waiting {wait}s before retry {attempt + 1}/3...")
177
- time.sleep(wait)
178
- else:
179
- print(f"Agent execution error: {e}")
180
- return f"AGENT ERROR: {e}"
181
 
182
- return "AGENT ERROR: rate limit exhausted after 3 retries"
 
 
 
183
 
184
  # --- File Handling ---
185
 
@@ -278,13 +390,14 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
278
  results_log = []
279
  answers_payload = []
280
  print(f"Running agent on {len(questions_data)} questions...")
281
- for item in questions_data:
282
  task_id = item.get("task_id")
283
  question_text = item.get("question")
284
  suggested_filename = item.get("file_name")
285
  if not task_id or question_text is None:
286
  continue
287
 
 
288
  file_path = download_task_file(api_url, task_id, suggested_name=suggested_filename)
289
  if file_path:
290
  file_ext = os.path.splitext(file_path)[-1]
@@ -304,7 +417,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
304
  print(f"Error running agent on task {task_id}: {e}")
305
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
306
 
307
- time.sleep(2)
308
 
309
  if not answers_payload:
310
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
 
7
  import requests
8
  import pandas as pd
9
  import spaces
10
+ from openai import OpenAI
11
  from smolagents import CodeAgent, DuckDuckGoSearchTool, VisitWebpageTool, OpenAIServerModel, tool
12
  from huggingface_hub import InferenceClient
13
 
14
  # --- Constants ---
15
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
16
+ GROQ_BASE_URL = "https://api.groq.com/openai/v1"
17
+
18
+ # Preferred order of active Groq reasoning/coding models
19
+ PREFERRED_GROQ_MODELS = [
20
+ "openai/gpt-oss-120b",
21
+ "openai/gpt-oss-20b",
22
+ "qwen/qwen3.8-27b",
23
+ "qwen/qwen3.6-27b",
24
+ "qwen/qwen3-32b",
25
+ "qwen-qwq-32b",
26
+ "deepseek-r1-distill-llama-70b",
27
+ "meta-llama/llama-4-maverick-17b-128e-instruct",
28
+ "meta-llama/llama-4-scout-17b-16e-instruct",
29
+ "llama-3.3-70b-versatile",
30
+ "llama-3.1-8b-instant",
31
+ ]
32
 
33
  # Satisfies ZeroGPU scanner requirement
34
  @spaces.GPU
 
70
  Args:
71
  file_path: Local path to the audio file.
72
  """
73
+ groq_key = os.getenv("GROQ_API_KEY")
74
+ # Priority 1: Use Groq's free Whisper endpoint
75
+ if groq_key:
76
+ try:
77
+ client = OpenAI(base_url=GROQ_BASE_URL, api_key=groq_key)
78
+ for whisper_model in ["whisper-large-v3-turbo", "whisper-large-v3"]:
79
+ try:
80
+ with open(file_path, "rb") as audio_file:
81
+ transcription = client.audio.transcriptions.create(
82
+ model=whisper_model,
83
+ file=audio_file
84
+ )
85
+ return transcription.text
86
+ except Exception as inner_e:
87
+ print(f"Groq Whisper ({whisper_model}) error: {inner_e}")
88
+ except Exception as e:
89
+ print(f"Groq audio transcription error: {e}")
90
+
91
+ # Priority 2: Fallback to Hugging Face InferenceClient
92
  token = os.getenv("HF_TOKEN")
93
  client = InferenceClient(token=token)
94
  try:
 
106
  except Exception as e:
107
  return f"Speech-to-text transcription failed: {e}"
108
 
109
+ # --- Dynamic Model Discovery & Per-Step Resilient Wrapper ---
110
+
111
+ def discover_groq_models(api_key: str) -> list[str]:
112
+ """Query Groq API for active models on this key and verify with a live ping."""
113
+ client = OpenAI(base_url=GROQ_BASE_URL, api_key=api_key)
114
+ available_ids = []
115
+ try:
116
+ models_resp = client.models.list()
117
+ available_ids = [m.id for m in models_resp.data]
118
+ print(f"Groq models available on account: {available_ids}")
119
+ except Exception as e:
120
+ print(f"Could not list Groq models, using fallback list: {e}")
121
+ available_ids = PREFERRED_GROQ_MODELS
122
+
123
+ # Filter out non-chat models (whisper, guard, tts, embed)
124
+ excluded_keywords = ["whisper", "guard", "safeguard", "tts", "orpheus", "embed", "playai"]
125
+ chat_candidates = []
126
+
127
+ # First add preferred models that exist in available_ids
128
+ for pref in PREFERRED_GROQ_MODELS:
129
+ if pref in available_ids and pref not in chat_candidates:
130
+ chat_candidates.append(pref)
131
+
132
+ # Then add any remaining chat models reported by the API
133
+ for mid in available_ids:
134
+ if mid not in chat_candidates and not any(k in mid.lower() for k in excluded_keywords):
135
+ chat_candidates.append(mid)
136
+
137
+ # Verify up to 3 working models with a 1-token test call
138
+ verified = []
139
+ for candidate in chat_candidates:
140
+ try:
141
+ client.chat.completions.create(
142
+ model=candidate,
143
+ messages=[{"role": "user", "content": "Reply with 1"}],
144
+ max_tokens=16
145
+ )
146
+ print(f"Verified working Groq model: {candidate}")
147
+ verified.append(candidate)
148
+ if len(verified) >= 3:
149
+ break
150
+ except Exception as e:
151
+ print(f"Skipping model {candidate} (ping failed): {e}")
152
+
153
+ return verified
154
+
155
+ def create_resilient_groq_model(api_key: str) -> OpenAIServerModel:
156
+ """Create an OpenAIServerModel with per-step retry and automatic model failover."""
157
+ verified_models = discover_groq_models(api_key)
158
+ if not verified_models:
159
+ raise RuntimeError("No active Groq chat models responded to ping test. Check GROQ_API_KEY.")
160
+
161
+ primary_model = verified_models[0]
162
+ print(f"Primary model set to: {primary_model} | Backups: {verified_models[1:]}")
163
+
164
+ model = OpenAIServerModel(
165
+ model_id=primary_model,
166
+ api_base=GROQ_BASE_URL,
167
+ api_key=api_key
168
+ )
169
+
170
+ original_create = model.client.chat.completions.create
171
+
172
+ def resilient_create(*args, **kwargs):
173
+ # Gentle pacing between LLM steps to avoid hitting 30 RPM / 8000 TPM limits
174
+ time.sleep(2.0)
175
+
176
+ last_err = None
177
+ for attempt in range(5):
178
+ # Rotate to backup models if primary hits rate limit
179
+ active_model = verified_models[attempt % len(verified_models)]
180
+ kwargs["model"] = active_model
181
+
182
+ try:
183
+ return original_create(*args, **kwargs)
184
+ except Exception as e:
185
+ last_err = e
186
+ err_str = str(e).lower()
187
+
188
+ # Handle unsupported 'stop' parameter on reasoning models
189
+ if "stop" in err_str and "400" in err_str and "stop" in kwargs:
190
+ kwargs.pop("stop", None)
191
+ try:
192
+ return original_create(*args, **kwargs)
193
+ except Exception as inner_e:
194
+ last_err = inner_e
195
+ err_str = str(inner_e).lower()
196
+
197
+ # Handle rate limit (429), payload/TPM limit (413), or server overload (503)
198
+ if any(code in err_str for code in ["429", "rate limit", "rate_limit", "413", "503", "404"]):
199
+ wait_time = 6 if len(verified_models) > 1 and attempt < len(verified_models) else 15 * (attempt + 1)
200
+ print(f"Step limit/error on {active_model} (attempt {attempt + 1}/5). Waiting {wait_time}s and switching model...")
201
+ time.sleep(wait_time)
202
+ else:
203
+ raise e
204
+
205
+ raise last_err
206
+
207
+ model.client.chat.completions.create = resilient_create
208
+ return model
209
 
210
  def build_gaia_agent() -> CodeAgent:
211
+ """Build CodeAgent using verified Groq models."""
212
  groq_key = os.getenv("GROQ_API_KEY")
213
+ if not groq_key:
214
+ raise RuntimeError("GROQ_API_KEY secret is missing in Space settings.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
215
 
216
+ model = create_resilient_groq_model(groq_key)
217
  search_tool = DuckDuckGoSearchTool()
218
  web_visit_tool = VisitWebpageTool()
219
 
 
246
  "zipfile",
247
  "base64"
248
  ],
249
+ max_steps=10
250
  )
251
  return agent
252
 
253
  class GAIAgentWrapper:
254
+ """Wrapper to run agent and enforce strict raw output normalization."""
255
  def __init__(self):
256
  print("Initializing CodeAgent...")
257
  self.agent = build_gaia_agent()
 
269
  "6. Never start with 'FINAL ANSWER:'. Output ONLY the pure value."
270
  )
271
 
272
+ try:
273
+ raw_result = self.agent.run(strict_instruction)
274
+ cleaned = str(raw_result).strip()
 
 
 
 
 
 
 
 
275
 
276
+ # Clean common prefixes
277
+ for prefix in ["final answer:", "answer:", "the answer is"]:
278
+ if cleaned.lower().startswith(prefix):
279
+ cleaned = cleaned[len(prefix):].strip(" :\n*`")
 
 
 
280
 
281
+ cleaned = cleaned.strip(" '\"`*")
282
 
283
+ # Normalize whole-number floats (e.g., '42.0' -> '42')
284
+ try:
285
+ as_float = float(cleaned)
286
+ if as_float == int(as_float):
287
+ cleaned = str(int(as_float))
288
+ except ValueError:
289
+ pass
 
 
290
 
291
+ return cleaned
292
+ except Exception as e:
293
+ print(f"Agent execution error: {e}")
294
+ return f"AGENT ERROR: {e}"
295
 
296
  # --- File Handling ---
297
 
 
390
  results_log = []
391
  answers_payload = []
392
  print(f"Running agent on {len(questions_data)} questions...")
393
+ for idx, item in enumerate(questions_data, 1):
394
  task_id = item.get("task_id")
395
  question_text = item.get("question")
396
  suggested_filename = item.get("file_name")
397
  if not task_id or question_text is None:
398
  continue
399
 
400
+ print(f"--- Solving Question {idx}/{len(questions_data)} (Task {task_id}) ---")
401
  file_path = download_task_file(api_url, task_id, suggested_name=suggested_filename)
402
  if file_path:
403
  file_ext = os.path.splitext(file_path)[-1]
 
417
  print(f"Error running agent on task {task_id}: {e}")
418
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
419
 
420
+ time.sleep(3)
421
 
422
  if not answers_payload:
423
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)