Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
| 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 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 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=
|
| 167 |
max_steps=15,
|
| 168 |
)
|
| 169 |
|
| 170 |
|
| 171 |
class BasicAgent:
|
| 172 |
def __init__(self):
|
| 173 |
-
|
| 174 |
-
if not
|
| 175 |
raise RuntimeError(
|
| 176 |
-
"
|
| 177 |
-
"Go to Space Settings β Variables and secrets β add
|
| 178 |
)
|
| 179 |
-
print(
|
| 180 |
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 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(
|
| 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]}...")
|