Spaces:
Sleeping
Sleeping
Usar Groq como modelo para no depender de creditos de HF
Browse files- app.py +49 -12
- requirements.txt +2 -3
app.py
CHANGED
|
@@ -15,8 +15,7 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
| 15 |
|
| 16 |
# --- Basic Agent Definition ---
|
| 17 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 18 |
-
from smolagents import CodeAgent, InferenceClientModel, DuckDuckGoSearchTool, VisitWebpageTool, Tool
|
| 19 |
-
import wikipedia
|
| 20 |
|
| 21 |
|
| 22 |
class WikipediaSearchTool(Tool):
|
|
@@ -34,17 +33,50 @@ class WikipediaSearchTool(Tool):
|
|
| 34 |
}
|
| 35 |
output_type = "string"
|
| 36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
def forward(self, query: str) -> str:
|
| 38 |
try:
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
except Exception as e:
|
| 49 |
return f"Error buscando en Wikipedia: {e}"
|
| 50 |
|
|
@@ -62,7 +94,12 @@ No incluyas explicaciones en esa linea final, solo la respuesta."""
|
|
| 62 |
class BasicAgent:
|
| 63 |
def __init__(self):
|
| 64 |
print("BasicAgent initialized.")
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
self.agent = CodeAgent(
|
| 67 |
tools=[DuckDuckGoSearchTool(), VisitWebpageTool(), WikipediaSearchTool()],
|
| 68 |
model=model,
|
|
|
|
| 15 |
|
| 16 |
# --- Basic Agent Definition ---
|
| 17 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 18 |
+
from smolagents import CodeAgent, InferenceClientModel, LiteLLMModel, DuckDuckGoSearchTool, VisitWebpageTool, Tool
|
|
|
|
| 19 |
|
| 20 |
|
| 21 |
class WikipediaSearchTool(Tool):
|
|
|
|
| 33 |
}
|
| 34 |
output_type = "string"
|
| 35 |
|
| 36 |
+
HEADERS = {
|
| 37 |
+
"User-Agent": "final-assignment-agent/1.0 (https://huggingface.co/spaces/mariajsl/final-assignment)"
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
def _search_title(self, query: str) -> str | None:
|
| 41 |
+
"""Usa la API de busqueda de Wikipedia para encontrar el titulo del articulo mas relevante."""
|
| 42 |
+
resp = requests.get(
|
| 43 |
+
"https://en.wikipedia.org/w/api.php",
|
| 44 |
+
params={
|
| 45 |
+
"action": "query",
|
| 46 |
+
"list": "search",
|
| 47 |
+
"srsearch": query,
|
| 48 |
+
"format": "json",
|
| 49 |
+
"srlimit": 1,
|
| 50 |
+
},
|
| 51 |
+
headers=self.HEADERS,
|
| 52 |
+
timeout=15,
|
| 53 |
+
)
|
| 54 |
+
resp.raise_for_status()
|
| 55 |
+
data = resp.json()
|
| 56 |
+
results = data.get("query", {}).get("search", [])
|
| 57 |
+
if not results:
|
| 58 |
+
return None
|
| 59 |
+
return results[0]["title"]
|
| 60 |
+
|
| 61 |
def forward(self, query: str) -> str:
|
| 62 |
try:
|
| 63 |
+
title = self._search_title(query)
|
| 64 |
+
if not title:
|
| 65 |
+
return f"No se encontro pagina de Wikipedia para: {query}"
|
| 66 |
+
|
| 67 |
+
resp = requests.get(
|
| 68 |
+
f"https://en.wikipedia.org/api/rest_v1/page/summary/{title.replace(' ', '_')}",
|
| 69 |
+
headers=self.HEADERS,
|
| 70 |
+
timeout=15,
|
| 71 |
+
)
|
| 72 |
+
resp.raise_for_status()
|
| 73 |
+
data = resp.json()
|
| 74 |
+
extract = data.get("extract", "")
|
| 75 |
+
if not extract:
|
| 76 |
+
return f"No se encontro resumen para: {title}"
|
| 77 |
+
return f"Titulo: {title}\n\n{extract}"
|
| 78 |
+
except requests.exceptions.RequestException as e:
|
| 79 |
+
return f"Error de red buscando en Wikipedia: {e}"
|
| 80 |
except Exception as e:
|
| 81 |
return f"Error buscando en Wikipedia: {e}"
|
| 82 |
|
|
|
|
| 94 |
class BasicAgent:
|
| 95 |
def __init__(self):
|
| 96 |
print("BasicAgent initialized.")
|
| 97 |
+
if os.getenv("GROQ_API_KEY"):
|
| 98 |
+
# Groq: gratis, rapido, y no consume tu cuota de HF Inference Providers.
|
| 99 |
+
model = LiteLLMModel(model_id="groq/llama-3.3-70b-versatile")
|
| 100 |
+
else:
|
| 101 |
+
# Fallback: HF Inference Providers (requiere creditos disponibles).
|
| 102 |
+
model = InferenceClientModel(model_id="Qwen/Qwen2.5-72B-Instruct")
|
| 103 |
self.agent = CodeAgent(
|
| 104 |
tools=[DuckDuckGoSearchTool(), VisitWebpageTool(), WikipediaSearchTool()],
|
| 105 |
model=model,
|
requirements.txt
CHANGED
|
@@ -1,7 +1,6 @@
|
|
| 1 |
gradio
|
| 2 |
requests
|
| 3 |
pandas
|
| 4 |
-
smolagents
|
| 5 |
ddgs
|
| 6 |
-
markdownify
|
| 7 |
-
wikipedia
|
|
|
|
| 1 |
gradio
|
| 2 |
requests
|
| 3 |
pandas
|
| 4 |
+
smolagents[litellm]
|
| 5 |
ddgs
|
| 6 |
+
markdownify
|
|
|