Isaac454 commited on
Commit
5bcea4d
·
verified ·
1 Parent(s): 14ae1e1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -10
app.py CHANGED
@@ -1,22 +1,75 @@
1
  import os
 
 
 
 
2
 
 
3
  HF_TOKEN = os.getenv("HF_TOKEN")
4
 
5
- import os
6
- import datetime
7
- from tools.final_answer import FinalAnswerTool
8
- from Gradio_UI import GradioUI
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
- from smolagents import tool, LiteLLMModel, ToolCallingAgent, DuckDuckGoSearchTool # or langchain.tools import tool
11
 
12
- final_answer = FinalAnswerTool()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  model = LiteLLMModel(
14
- model_id="huggingface/google/gemma-2-2b-it"
 
15
  )
16
 
 
17
  agent = ToolCallingAgent(
18
- tools=[DuckDuckGoSearchTool()],
19
  model=model,
20
- max_steps=10
 
21
  )
22
- GradioUI(agent).launch()
 
 
 
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(
62
+ model_id="huggingface/google/gemma-2-2b-it",
63
+ hf_token=HF_TOKEN
64
  )
65
 
66
+ # --- Initialize Tool-Calling Agent ---
67
  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()