Isaac454 commited on
Commit
f072c2b
·
verified ·
1 Parent(s): 8e5878d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -30
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 run(self, query: str):
18
  from smolagents import DuckDuckGoSearchTool
19
  tool = DuckDuckGoSearchTool()
20
- return tool.run(query)
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 run(self, topic: str):
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 run(self, city: str):
42
- # Geocoding to get coordinates
43
- geocode_url = f"https://geocoding-api.open-meteo.com/v1/search?name={city}"
44
- geo_resp = requests.get(geocode_url, timeout=10).json()
45
- results = geo_resp.get("results")
46
- if not results:
47
- return f"Could not find coordinates for {city}."
48
- lat = results[0]["latitude"]
49
- lon = results[0]["longitude"]
50
-
51
- # Get current weather
52
- weather_url = f"https://api.open-meteo.com/v1/forecast?latitude={lat}&longitude={lon}&current_weather=true"
53
- weather_resp = requests.get(weather_url, timeout=10).json()
54
- weather = weather_resp.get("current_weather")
55
- if not weather:
56
- return f"Could not retrieve weather for {city}."
57
- return f"Weather in {city}: {weather['temperature']}°C, wind {weather['windspeed']} km/h, weather code {weather['weathercode']}."
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
- name="Internet Agent"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
  )
73
 
74
  # --- Launch Gradio Web UI ---
75
- GradioUI(agent).launch()
 
 
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}&current_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)