iPr0x commited on
Commit
a9a88cf
·
verified ·
1 Parent(s): 71fb867

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -9
app.py CHANGED
@@ -33,7 +33,12 @@ GROQ_API_KEY = os.getenv("GROQ_API_KEY")
33
  GROQ_MODEL = os.getenv("GROQ_MODEL") # optional override
34
  OPENROUTER_API_KEY = os.getenv("OPENROUTER_API_KEY")
35
  OPENROUTER_MODEL = os.getenv("OPENROUTER_MODEL", "meta-llama/llama-3.3-70b-instruct:free")
36
- ANY_KEY = bool(GROQ_API_KEY or OPENROUTER_API_KEY or GEMINI_API_KEY or HF_TOKEN)
 
 
 
 
 
37
 
38
 
39
  # ----------------------------------------------------------------- TOOLS
@@ -160,6 +165,39 @@ def youtube_transcript(url: str) -> str:
160
 
161
 
162
  # ----------------------------------------------------------------- AGENT
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
163
  def pick_groq_model() -> str:
164
  """Ask Groq which models this key can use and pick the strongest chat model."""
165
  if GROQ_MODEL:
@@ -184,7 +222,22 @@ def pick_groq_model() -> str:
184
 
185
 
186
  def build_model():
187
- '''
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
188
  if GROQ_API_KEY:
189
  return LiteLLMModel(
190
  model_id=f"groq/{pick_groq_model()}",
@@ -201,12 +254,11 @@ def build_model():
201
  )
202
  if GEMINI_API_KEY:
203
  return LiteLLMModel(
204
- model_id=f"gemini/{GEMINI_MODEL}",
205
  api_key=GEMINI_API_KEY,
206
- timeout=60,
207
- num_retries=2,
208
  )
209
- '''
210
  return InferenceClientModel(
211
  model_id=os.getenv("MODEL_ID", "Qwen/Qwen2.5-72B-Instruct"), token=HF_TOKEN
212
  )
@@ -227,7 +279,7 @@ class BasicAgent:
227
  self.agent = CodeAgent(
228
  tools=[
229
  DuckDuckGoSearchTool(),
230
- VisitWebpageTool(),
231
  download_task_file,
232
  read_file,
233
  transcribe_audio,
@@ -300,7 +352,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None = None):
300
  print(f"Task {task_id} failed: {err}")
301
  answers.append({"task_id": task_id, "submitted_answer": ans})
302
  log.append({"Task ID": task_id, "Question": question[:80], "Submitted Answer": ans, "Error": err})
303
- time.sleep(4) # stay under free-tier rate limits
304
 
305
  if not any(a["submitted_answer"] for a in answers):
306
  first_err = next((l["Error"] for l in log if l["Error"]), "unknown")
@@ -334,7 +386,7 @@ def test_one():
334
  try:
335
  agent = BasicAgent(max_steps=3)
336
  q = ".rewsna eht sa \"tfel\" drow eht fo etisoppo eht etirw ,ecnetnes siht dnatsrednu uoy fI"
337
- used = pick_groq_model() if GROQ_API_KEY else "n/a"
338
  return f"Model: {used}\nAnswer: {agent(q, 'test', '')} (expected: right)"
339
  except Exception as e:
340
  return f"FAILED: {type(e).__name__}: {e}"
 
33
  GROQ_MODEL = os.getenv("GROQ_MODEL") # optional override
34
  OPENROUTER_API_KEY = os.getenv("OPENROUTER_API_KEY")
35
  OPENROUTER_MODEL = os.getenv("OPENROUTER_MODEL", "meta-llama/llama-3.3-70b-instruct:free")
36
+ CEREBRAS_API_KEY = os.getenv("CEREBRAS_API_KEY")
37
+ MISTRAL_API_KEY = os.getenv("MISTRAL_API_KEY")
38
+ ANY_KEY = bool(
39
+ CEREBRAS_API_KEY or MISTRAL_API_KEY or GROQ_API_KEY
40
+ or OPENROUTER_API_KEY or GEMINI_API_KEY or HF_TOKEN
41
+ )
42
 
43
 
44
  # ----------------------------------------------------------------- TOOLS
 
165
 
166
 
167
  # ----------------------------------------------------------------- AGENT
168
+ def pick_compat_model(url: str, key: str, prefs: tuple, extra_skip: tuple = ()) -> str:
169
+ """List models from an OpenAI-compatible provider and pick the best chat model."""
170
+ r = requests.get(url, headers={"Authorization": f"Bearer {key}"}, timeout=30)
171
+ r.raise_for_status()
172
+ ids = [m["id"] for m in r.json().get("data", [])]
173
+ skip = ("whisper", "guard", "tts", "embed", "moderation", "ocr", "voxtral", "transcribe") + tuple(extra_skip)
174
+ chat = [i for i in ids if not any(s in i.lower() for s in skip)]
175
+ for p in prefs:
176
+ for i in chat:
177
+ if p in i.lower():
178
+ return i
179
+ if chat:
180
+ return chat[0]
181
+ raise RuntimeError(f"No usable chat model found. Models seen: {ids}")
182
+
183
+
184
+ def gemini_model_name() -> str:
185
+ """Use GEMINI_MODEL if set, else ask Google which Flash model this key can use."""
186
+ if os.getenv("GEMINI_MODEL"):
187
+ return os.getenv("GEMINI_MODEL")
188
+ try:
189
+ m = pick_compat_model(
190
+ "https://generativelanguage.googleapis.com/v1beta/openai/models",
191
+ GEMINI_API_KEY,
192
+ ("3.8-flash", "3.5-flash", "flash"),
193
+ extra_skip=("lite", "image", "live", "audio", "robotics", "computer", "imagen", "veo"),
194
+ )
195
+ return m.replace("models/", "")
196
+ except Exception as e:
197
+ print(f"Gemini model auto-pick failed, using default: {e}")
198
+ return GEMINI_MODEL
199
+
200
+
201
  def pick_groq_model() -> str:
202
  """Ask Groq which models this key can use and pick the strongest chat model."""
203
  if GROQ_MODEL:
 
222
 
223
 
224
  def build_model():
225
+ if CEREBRAS_API_KEY:
226
+ m = pick_compat_model(
227
+ "https://api.cerebras.ai/v1/models", CEREBRAS_API_KEY,
228
+ ("gpt-oss-120b", "llama-3.3-70b", "qwen-3-235b", "qwen", "llama"),
229
+ )
230
+ return LiteLLMModel(
231
+ model_id=f"cerebras/{m}", api_key=CEREBRAS_API_KEY, timeout=90, num_retries=4
232
+ )
233
+ if MISTRAL_API_KEY:
234
+ m = pick_compat_model(
235
+ "https://api.mistral.ai/v1/models", MISTRAL_API_KEY,
236
+ ("mistral-large-latest", "mistral-large", "mistral-medium-latest", "mistral-medium", "mistral-small-latest", "mistral-small"),
237
+ )
238
+ return LiteLLMModel(
239
+ model_id=f"mistral/{m}", api_key=MISTRAL_API_KEY, timeout=90, num_retries=4
240
+ )
241
  if GROQ_API_KEY:
242
  return LiteLLMModel(
243
  model_id=f"groq/{pick_groq_model()}",
 
254
  )
255
  if GEMINI_API_KEY:
256
  return LiteLLMModel(
257
+ model_id=f"gemini/{gemini_model_name()}",
258
  api_key=GEMINI_API_KEY,
259
+ timeout=120,
260
+ num_retries=6,
261
  )
 
262
  return InferenceClientModel(
263
  model_id=os.getenv("MODEL_ID", "Qwen/Qwen2.5-72B-Instruct"), token=HF_TOKEN
264
  )
 
279
  self.agent = CodeAgent(
280
  tools=[
281
  DuckDuckGoSearchTool(),
282
+ VisitWebpageTool(max_output_length=6000),
283
  download_task_file,
284
  read_file,
285
  transcribe_audio,
 
352
  print(f"Task {task_id} failed: {err}")
353
  answers.append({"task_id": task_id, "submitted_answer": ans})
354
  log.append({"Task ID": task_id, "Question": question[:80], "Submitted Answer": ans, "Error": err})
355
+ time.sleep(12) # stay under free-tier rate limits
356
 
357
  if not any(a["submitted_answer"] for a in answers):
358
  first_err = next((l["Error"] for l in log if l["Error"]), "unknown")
 
386
  try:
387
  agent = BasicAgent(max_steps=3)
388
  q = ".rewsna eht sa \"tfel\" drow eht fo etisoppo eht etirw ,ecnetnes siht dnatsrednu uoy fI"
389
+ used = agent.agent.model.model_id
390
  return f"Model: {used}\nAnswer: {agent(q, 'test', '')} (expected: right)"
391
  except Exception as e:
392
  return f"FAILED: {type(e).__name__}: {e}"