avi080704 commited on
Commit
71d2fb4
Β·
verified Β·
1 Parent(s): 6768e25

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -52
app.py CHANGED
@@ -1,18 +1,14 @@
1
  import os
2
  import gradio as gr
3
  import requests
4
- import inspect
5
  import pandas as pd
 
 
6
 
7
  # --- Constants ---
8
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
9
 
10
- # --- Basic Agent Definition ---
11
- # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
12
-
13
- import re
14
- import traceback
15
- from smolagents import CodeAgent, InferenceClientModel, tool, VisitWebpageTool, DuckDuckGoSearchTool
16
 
17
 
18
  @tool
@@ -145,16 +141,11 @@ def search_wikipedia(query: str) -> str:
145
  return f"Wikipedia search error: {e}"
146
 
147
 
148
- # --- Models in priority order: smallest/free first ---
149
- CANDIDATE_MODELS = [
150
- "HuggingFaceTB/SmolLM2-1.7B-Instruct", # tiny, always free
151
- "mistralai/Mistral-7B-Instruct-v0.3", # free tier
152
- "Qwen/Qwen2.5-7B-Instruct", # free tier
153
- "Qwen/Qwen2.5-72B-Instruct", # Pro only, last resort
154
- ]
155
-
156
-
157
- def _make_agent(model_id: str, hf_token: str) -> CodeAgent:
158
  return CodeAgent(
159
  tools=[
160
  DuckDuckGoSearchTool(),
@@ -163,51 +154,31 @@ def _make_agent(model_id: str, hf_token: str) -> CodeAgent:
163
  calculate,
164
  search_wikipedia,
165
  ],
166
- model=InferenceClientModel(model_id=model_id, token=hf_token),
167
  max_steps=15,
168
  )
169
 
170
 
171
  class BasicAgent:
172
  def __init__(self):
173
- hf_token = os.environ.get("HF_TOKEN") or os.environ.get("HUGGINGFACE_TOKEN")
174
- if not hf_token:
175
  raise RuntimeError(
176
- "HF_TOKEN environment variable is not set. "
177
- "Go to Space Settings β†’ Variables and secrets β†’ add HF_TOKEN."
178
  )
179
- print(f"HF_TOKEN present: {bool(hf_token)}")
180
 
181
- self.agent = None
182
- self.model_id = None
183
- errors = []
184
-
185
- for model_id in CANDIDATE_MODELS:
186
- try:
187
- print(f"Trying model: {model_id}")
188
- agent = _make_agent(model_id, hf_token)
189
- # Quick smoke test
190
- test = agent.run("Reply with just the number 42.")
191
- print(f"βœ… Model OK: {model_id} β†’ {str(test)[:30]}")
192
- self.agent = agent
193
- self.model_id = model_id
194
- break
195
- except Exception as e:
196
- msg = str(e)
197
- errors.append(f"{model_id}: {msg[:120]}")
198
- if "402" in msg or "Payment" in msg:
199
- print(f"β›” {model_id} requires payment, trying next...")
200
- else:
201
- print(f"⚠️ {model_id} failed ({msg[:80]}), trying next...")
202
-
203
- if self.agent is None:
204
- raise RuntimeError(
205
- "No working model found.\n"
206
- + "\n".join(errors)
207
- + "\n\nCheck that HF_TOKEN is set correctly in Space Secrets and your account tier."
208
- )
209
 
210
- print(f"BasicAgent initialized with model: {self.model_id}")
211
 
212
  def __call__(self, question: str) -> str:
213
  print(f"Agent received question (first 50 chars): {question[:50]}...")
 
1
  import os
2
  import gradio as gr
3
  import requests
 
4
  import pandas as pd
5
+ import re
6
+ import traceback
7
 
8
  # --- Constants ---
9
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
10
 
11
+ from smolagents import CodeAgent, tool, VisitWebpageTool, DuckDuckGoSearchTool, LiteLLMModel
 
 
 
 
 
12
 
13
 
14
  @tool
 
141
  return f"Wikipedia search error: {e}"
142
 
143
 
144
+ def _make_agent(gemini_api_key: str) -> CodeAgent:
145
+ model = LiteLLMModel(
146
+ model_id="gemini/gemini-2.5-flash",
147
+ api_key=gemini_api_key,
148
+ )
 
 
 
 
 
149
  return CodeAgent(
150
  tools=[
151
  DuckDuckGoSearchTool(),
 
154
  calculate,
155
  search_wikipedia,
156
  ],
157
+ model=model,
158
  max_steps=15,
159
  )
160
 
161
 
162
  class BasicAgent:
163
  def __init__(self):
164
+ gemini_api_key = os.environ.get("GEMINI_API_KEY")
165
+ if not gemini_api_key:
166
  raise RuntimeError(
167
+ "GEMINI_API_KEY environment variable is not set. "
168
+ "Go to Space Settings β†’ Variables and secrets β†’ add GEMINI_API_KEY."
169
  )
170
+ print("GEMINI_API_KEY present: True")
171
 
172
+ try:
173
+ print("Initializing agent with gemini-2.5-flash...")
174
+ self.agent = _make_agent(gemini_api_key)
175
+ # Quick smoke test
176
+ test = self.agent.run("Reply with just the number 42.")
177
+ print(f"βœ… Gemini agent OK β†’ {str(test)[:30]}")
178
+ except Exception as e:
179
+ raise RuntimeError(f"Failed to initialize Gemini agent: {e}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
180
 
181
+ print("BasicAgent initialized with model: gemini/gemini-2.5-flash")
182
 
183
  def __call__(self, question: str) -> str:
184
  print(f"Agent received question (first 50 chars): {question[:50]}...")