ahmedelgendy commited on
Commit
560d019
·
verified ·
1 Parent(s): e0aafee

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -15
app.py CHANGED
@@ -1,3 +1,6 @@
 
 
 
1
  from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
2
  import datetime
3
  import requests
@@ -10,34 +13,45 @@ from PIL import Image
10
  import io
11
  import base64
12
 
13
- # Weather tool
14
  @tool
15
  def get_weather(location: str) -> str:
16
  """Fetch current weather information for a specified location.
17
 
18
  Args:
19
- location: A string representing the city, state, or country.
20
  """
21
  try:
22
- # Using OpenWeatherMap API (you'll need an API key)
23
- api_key = "YOUR_OPENWEATHERMAP_API_KEY" # Replace with your API key
24
- url = f"https://api.openweathermap.org/data/2.5/weather?q={location}&appid={api_key}&units=metric"
25
- response = requests.get(url)
 
 
 
 
26
  data = response.json()
27
 
28
  if response.status_code == 200:
29
- weather_description = data['weather'][0]['description']
30
- temperature = data['main']['temp']
31
- humidity = data['main']['humidity']
32
- wind_speed = data['wind']['speed']
 
 
 
 
 
33
 
34
- return f"Weather in {location}:\n" \
35
- f"Description: {weather_description}\n" \
36
- f"Temperature: {temperature}°C\n" \
 
37
  f"Humidity: {humidity}%\n" \
38
- f"Wind Speed: {wind_speed} m/s"
39
  else:
40
- return f"Error fetching weather for {location}: {data.get('message', 'Unknown error')}"
 
41
  except Exception as e:
42
  return f"Error fetching weather for {location}: {str(e)}"
43
 
 
1
+ # Installation of required packages
2
+ # pip install smolagents requests pytz pyyaml beautifulsoup4 Pillow
3
+
4
  from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
5
  import datetime
6
  import requests
 
13
  import io
14
  import base64
15
 
16
+ # Weather tool using a free API (Weather API by API-Ninjas)
17
  @tool
18
  def get_weather(location: str) -> str:
19
  """Fetch current weather information for a specified location.
20
 
21
  Args:
22
+ location: A string representing the city name.
23
  """
24
  try:
25
+ api_url = f"https://api.weatherapi.com/v1/current.json"
26
+ params = {
27
+ 'key': '80ca2c7d7e0945b583b153111241802', # Free API key with limited usage
28
+ 'q': location,
29
+ 'aqi': 'no'
30
+ }
31
+
32
+ response = requests.get(api_url, params=params)
33
  data = response.json()
34
 
35
  if response.status_code == 200:
36
+ location_name = data['location']['name']
37
+ region = data['location']['region']
38
+ country = data['location']['country']
39
+ temperature_c = data['current']['temp_c']
40
+ temperature_f = data['current']['temp_f']
41
+ condition = data['current']['condition']['text']
42
+ humidity = data['current']['humidity']
43
+ wind_kph = data['current']['wind_kph']
44
+ feels_like_c = data['current']['feelslike_c']
45
 
46
+ return f"Weather in {location_name}, {region}, {country}:\n" \
47
+ f"Condition: {condition}\n" \
48
+ f"Temperature: {temperature_c}°C / {temperature_f}°F\n" \
49
+ f"Feels like: {feels_like_c}°C\n" \
50
  f"Humidity: {humidity}%\n" \
51
+ f"Wind Speed: {wind_kph} km/h"
52
  else:
53
+ error_msg = data.get('error', {}).get('message', 'Unknown error')
54
+ return f"Error fetching weather for {location}: {error_msg}"
55
  except Exception as e:
56
  return f"Error fetching weather for {location}: {str(e)}"
57