Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,61 +1,60 @@
|
|
| 1 |
import os
|
| 2 |
import requests
|
| 3 |
from smolagents import LiteLLMModel, ToolCallingAgent, Tool
|
| 4 |
-
from smolagents.webui import GradioUI
|
| 5 |
import wikipedia
|
|
|
|
| 6 |
|
| 7 |
# Optional: Hugging Face token (for private models)
|
| 8 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 9 |
|
| 10 |
# --- Tools ---
|
| 11 |
-
|
| 12 |
class WebSearchTool(Tool):
|
| 13 |
"""Search the web using DuckDuckGo"""
|
| 14 |
name = "web_search"
|
| 15 |
description = "Search the web and return concise results. Input: search query string."
|
| 16 |
-
|
| 17 |
-
def
|
| 18 |
from smolagents import DuckDuckGoSearchTool
|
| 19 |
tool = DuckDuckGoSearchTool()
|
| 20 |
-
return tool.
|
| 21 |
-
|
| 22 |
|
| 23 |
class WikipediaTool(Tool):
|
| 24 |
"""Fetch summaries from Wikipedia"""
|
| 25 |
name = "wikipedia_search"
|
| 26 |
description = "Fetch Wikipedia summary for a topic. Input: topic string."
|
| 27 |
-
|
| 28 |
-
def
|
| 29 |
try:
|
| 30 |
summary = wikipedia.summary(topic, sentences=3)
|
| 31 |
return summary
|
| 32 |
except Exception as e:
|
| 33 |
return f"Wikipedia lookup failed: {e}"
|
| 34 |
|
| 35 |
-
|
| 36 |
class WeatherTool(Tool):
|
| 37 |
"""Get weather using Open-Meteo API"""
|
| 38 |
name = "weather"
|
| 39 |
description = "Get current weather. Input: city name."
|
| 40 |
-
|
| 41 |
-
def
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
| 59 |
|
| 60 |
# --- Initialize LLM Model ---
|
| 61 |
model = LiteLLMModel(
|
|
@@ -68,8 +67,34 @@ agent = ToolCallingAgent(
|
|
| 68 |
tools=[WebSearchTool(), WikipediaTool(), WeatherTool()],
|
| 69 |
model=model,
|
| 70 |
max_steps=10,
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
)
|
| 73 |
|
| 74 |
# --- Launch Gradio Web UI ---
|
| 75 |
-
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import requests
|
| 3 |
from smolagents import LiteLLMModel, ToolCallingAgent, Tool
|
|
|
|
| 4 |
import wikipedia
|
| 5 |
+
import gradio as gr
|
| 6 |
|
| 7 |
# Optional: Hugging Face token (for private models)
|
| 8 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 9 |
|
| 10 |
# --- Tools ---
|
|
|
|
| 11 |
class WebSearchTool(Tool):
|
| 12 |
"""Search the web using DuckDuckGo"""
|
| 13 |
name = "web_search"
|
| 14 |
description = "Search the web and return concise results. Input: search query string."
|
| 15 |
+
|
| 16 |
+
def forward(self, query: str):
|
| 17 |
from smolagents import DuckDuckGoSearchTool
|
| 18 |
tool = DuckDuckGoSearchTool()
|
| 19 |
+
return tool.forward(query)
|
|
|
|
| 20 |
|
| 21 |
class WikipediaTool(Tool):
|
| 22 |
"""Fetch summaries from Wikipedia"""
|
| 23 |
name = "wikipedia_search"
|
| 24 |
description = "Fetch Wikipedia summary for a topic. Input: topic string."
|
| 25 |
+
|
| 26 |
+
def forward(self, topic: str):
|
| 27 |
try:
|
| 28 |
summary = wikipedia.summary(topic, sentences=3)
|
| 29 |
return summary
|
| 30 |
except Exception as e:
|
| 31 |
return f"Wikipedia lookup failed: {e}"
|
| 32 |
|
|
|
|
| 33 |
class WeatherTool(Tool):
|
| 34 |
"""Get weather using Open-Meteo API"""
|
| 35 |
name = "weather"
|
| 36 |
description = "Get current weather. Input: city name."
|
| 37 |
+
|
| 38 |
+
def forward(self, city: str):
|
| 39 |
+
try:
|
| 40 |
+
# Geocoding to get coordinates
|
| 41 |
+
geocode_url = f"https://geocoding-api.open-meteo.com/v1/search?name={city}"
|
| 42 |
+
geo_resp = requests.get(geocode_url, timeout=10).json()
|
| 43 |
+
results = geo_resp.get("results")
|
| 44 |
+
if not results:
|
| 45 |
+
return f"Could not find coordinates for {city}."
|
| 46 |
+
lat = results[0]["latitude"]
|
| 47 |
+
lon = results[0]["longitude"]
|
| 48 |
+
|
| 49 |
+
# Get current weather
|
| 50 |
+
weather_url = f"https://api.open-meteo.com/v1/forecast?latitude={lat}&longitude={lon}¤t_weather=true"
|
| 51 |
+
weather_resp = requests.get(weather_url, timeout=10).json()
|
| 52 |
+
weather = weather_resp.get("current_weather")
|
| 53 |
+
if not weather:
|
| 54 |
+
return f"Could not retrieve weather for {city}."
|
| 55 |
+
return f"Weather in {city}: {weather['temperature']}°C, wind {weather['windspeed']} km/h, weather code {weather['weathercode']}."
|
| 56 |
+
except Exception as e:
|
| 57 |
+
return f"Weather lookup failed: {e}"
|
| 58 |
|
| 59 |
# --- Initialize LLM Model ---
|
| 60 |
model = LiteLLMModel(
|
|
|
|
| 67 |
tools=[WebSearchTool(), WikipediaTool(), WeatherTool()],
|
| 68 |
model=model,
|
| 69 |
max_steps=10,
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
# --- Custom Gradio Interface ---
|
| 73 |
+
def chat_with_agent(message, history):
|
| 74 |
+
"""Process user message and return agent response"""
|
| 75 |
+
try:
|
| 76 |
+
result = agent.run(message)
|
| 77 |
+
return str(result)
|
| 78 |
+
except Exception as e:
|
| 79 |
+
return f"Error: {str(e)}"
|
| 80 |
+
|
| 81 |
+
# Create Gradio ChatInterface
|
| 82 |
+
demo = gr.ChatInterface(
|
| 83 |
+
fn=chat_with_agent,
|
| 84 |
+
title="🤖 Internet Agent",
|
| 85 |
+
description="An AI agent with web search, Wikipedia, and weather tools powered by Gemma-2-2b",
|
| 86 |
+
examples=[
|
| 87 |
+
"What's the weather in Paris?",
|
| 88 |
+
"Search for recent news about AI",
|
| 89 |
+
"Tell me about Albert Einstein from Wikipedia",
|
| 90 |
+
"What's the current temperature in Tokyo?"
|
| 91 |
+
],
|
| 92 |
+
theme=gr.themes.Soft(),
|
| 93 |
+
retry_btn=None,
|
| 94 |
+
undo_btn=None,
|
| 95 |
+
clear_btn="Clear Chat",
|
| 96 |
)
|
| 97 |
|
| 98 |
# --- Launch Gradio Web UI ---
|
| 99 |
+
if __name__ == "__main__":
|
| 100 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|