sagarnildass commited on
Commit
12e7fda
·
verified ·
1 Parent(s): 6d9a52c

update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -14
app.py CHANGED
@@ -9,29 +9,64 @@ from Gradio_UI import GradioUI
9
 
10
 
11
  # Define a new tool: Weather Information Fetcher
 
 
 
 
12
  @tool
13
  def get_weather(city: str) -> str:
14
- """A tool that fetches current weather information for a given city.
 
 
15
  Args:
16
  city: The name of the city (e.g., 'New York').
 
 
 
17
  """
18
  try:
19
- api_key = os.getenv("OPENWEATHERMAP_API_KEY")
 
20
  if not api_key:
21
- return "Error: Missing API key. Set OPENWEATHERMAP_API_KEY as an environment variable."
22
 
 
23
  url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}&units=metric"
24
- response = requests.get(url).json()
25
 
26
- if response.get("cod") != 200:
27
- return f"Error fetching weather: {response.get('message', 'Unknown error')}"
 
 
 
 
28
 
29
- weather = response["weather"][0]["description"].capitalize()
30
- temp = response["main"]["temp"]
31
- humidity = response["main"]["humidity"]
32
- return f"Current weather in {city}: {weather}, Temp: {temp}°C, Humidity: {humidity}%"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  except Exception as e:
34
- return f"Error fetching weather for '{city}': {str(e)}"
 
35
 
36
 
37
  # Define a custom tool
@@ -67,7 +102,8 @@ def web_search(query: str) -> str:
67
  Args:
68
  query: The search query.
69
  """
70
- return DuckDuckGoSearchTool().search(query)
 
71
 
72
 
73
  # Load final answer tool
@@ -77,7 +113,7 @@ final_answer = FinalAnswerTool()
77
  model = HfApiModel(
78
  max_tokens=2096,
79
  temperature=0.5,
80
- model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
81
  custom_role_conversions=None,
82
  )
83
 
@@ -108,4 +144,4 @@ agent = CodeAgent(
108
  )
109
 
110
  # Launch Gradio UI
111
- GradioUI(agent).launch()
 
9
 
10
 
11
  # Define a new tool: Weather Information Fetcher
12
+ import os
13
+ import requests
14
+ from smolagents import tool
15
+
16
  @tool
17
  def get_weather(city: str) -> str:
18
+ """
19
+ Fetches the current weather information for a given city.
20
+
21
  Args:
22
  city: The name of the city (e.g., 'New York').
23
+
24
+ Returns:
25
+ str: A formatted weather report including temperature, humidity, and conditions.
26
  """
27
  try:
28
+ # 🔒 Get API key from environment variable (NEVER hardcode it)
29
+ api_key = "39ed1534532b15efbf0fb9dfffc55298" #os.getenv("OPENWEATHERMAP_API_KEY")
30
  if not api_key:
31
+ return "Error: API key is missing. Please set OPENWEATHERMAP_API_KEY as an environment variable."
32
 
33
+ # 🌍 Build the API request URL
34
  url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}&units=metric"
 
35
 
36
+ # 📡 Fetch data from OpenWeather API
37
+ response = requests.get(url)
38
+
39
+ # ❌ Handle HTTP request failure
40
+ if response.status_code != 200:
41
+ return f"Error: Unable to fetch weather data. HTTP Status: {response.status_code}"
42
 
43
+ # 📜 Parse JSON response
44
+ data = response.json()
45
+
46
+ # Handle API-level error (e.g., city not found)
47
+ if data.get("cod") != 200:
48
+ return f"Error fetching weather: {data.get('message', 'Unknown error')}"
49
+
50
+ # 🌡️ Extract weather details
51
+ weather_desc = data["weather"][0]["description"].capitalize()
52
+ temp = data["main"]["temp"]
53
+ pressure = data["main"]["pressure"]
54
+ humidity = data["main"]["humidity"]
55
+ wind_speed = data["wind"]["speed"]
56
+
57
+ # 📝 Format the response
58
+ return (
59
+ f"🌍 **Weather in {city}**:\n"
60
+ f"- 🌤 **Condition:** {weather_desc}\n"
61
+ f"- 🌡 **Temperature:** {temp}°C\n"
62
+ f"- 💨 **Wind Speed:** {wind_speed} m/s\n"
63
+ f"- 💧 **Humidity:** {humidity}%\n"
64
+ f"- 🏋 **Pressure:** {pressure} hPa"
65
+ )
66
+
67
  except Exception as e:
68
+ return f"⚠️ Error fetching weather for '{city}': {str(e)}"
69
+
70
 
71
 
72
  # Define a custom tool
 
102
  Args:
103
  query: The search query.
104
  """
105
+ return DuckDuckGoSearchTool().forward(query) # ✅ Correct: Calls `.forward(query)`
106
+
107
 
108
 
109
  # Load final answer tool
 
113
  model = HfApiModel(
114
  max_tokens=2096,
115
  temperature=0.5,
116
+ model_id='deepseek-ai/DeepSeek-R1-Distill-Qwen-32B',
117
  custom_role_conversions=None,
118
  )
119
 
 
144
  )
145
 
146
  # Launch Gradio UI
147
+ GradioUI(agent).launch()