zasharepw77 commited on
Commit
5e23b87
·
1 Parent(s): 7a84380

add real tool get_current_weather_by_city

Browse files
Files changed (1) hide show
  1. app.py +58 -1
app.py CHANGED
@@ -1,6 +1,7 @@
1
  from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel, load_tool, tool, TransformersModel
2
  import datetime
3
  import requests
 
4
  import pytz
5
  import yaml
6
  import os
@@ -37,6 +38,58 @@ def get_current_time_in_timezone(timezone: str) -> str:
37
  except Exception as e:
38
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
  final_answer = FinalAnswerTool()
41
 
42
  # Local: mcedit ~/.bashrc
@@ -59,7 +112,11 @@ with open("prompts.yaml", 'r') as stream:
59
 
60
  agent = CodeAgent(
61
  model=model,
62
- tools=[final_answer, get_current_time_in_timezone], ## add your tools here (don't remove final answer)
 
 
 
 
63
  max_steps=6,
64
  verbosity_level=1,
65
  grammar=None,
 
1
  from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel, load_tool, tool, TransformersModel
2
  import datetime
3
  import requests
4
+ import json
5
  import pytz
6
  import yaml
7
  import os
 
38
  except Exception as e:
39
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
40
 
41
+ @tool
42
+ def get_current_weather_by_city(city: str) -> str:
43
+ '''Retrieves the current and forecasted weather for a specified city.
44
+ Args:
45
+ city: The name of the city (e.g., "New York")
46
+ Return:
47
+ A string with weather information
48
+ '''
49
+ # API Geocoding от Open-Meteo
50
+ geocoding_url = f"https://geocoding-api.open-meteo.com/v1/search?name={city}&count=1&language=en&format=json"
51
+ response_geocoding = requests.get(geocoding_url)
52
+
53
+ if response_geocoding.status_code == 200:
54
+ data_geocoding = response_geocoding.json()
55
+
56
+ if 'results' in data_geocoding and len(data_geocoding['results']) > 0:
57
+ result = data_geocoding['results'][0]
58
+ lat = result.get('latitude', None)
59
+ lon = result.get('longitude', None)
60
+
61
+ if lat is not None and lon is not None:
62
+ # Получаем погоду по координатам
63
+ weather_url = f"https://api.open-meteo.com/v1/forecast?latitude={lat}&longitude={lon}&current_weather=true&hourly=temperature_2m"
64
+ response_weather = requests.get(weather_url)
65
+
66
+ if response_weather.status_code == 200:
67
+ data_weather = response_weather.json()
68
+
69
+ current_temp = data_weather['current_weather']['temperature'] if 'current_weather' in data_weather else None
70
+ hourly_temps = data_weather['hourly']['temperature_2m'] if 'hourly' in data_weather and 'temperature_2m' in data_weather['hourly'] else []
71
+
72
+ weather_info = f"Current temperature: {current_temp}°C\n"
73
+ weather_info += "Hourly temperatures:\n"
74
+
75
+ for temp in hourly_temps:
76
+ weather_info += f"{temp}°C\n"
77
+
78
+ return weather_info
79
+ else:
80
+ return f"Failed to retrieve weather data. Status code: {response_weather.status_code}"
81
+ else:
82
+ return "Latitude and Longitude not found."
83
+ else:
84
+ return "No results found for the specified city."
85
+ else:
86
+ return f"Geocoding failed. Status code: {response_geocoding.status_code}"
87
+
88
+ # Execute the function and prepare the final answer
89
+ #result = get_current_weather_by_city("New York")
90
+ #final_answer = f"The current weather in New York is:\n{result}"
91
+ #print(final_answer)
92
+
93
  final_answer = FinalAnswerTool()
94
 
95
  # Local: mcedit ~/.bashrc
 
112
 
113
  agent = CodeAgent(
114
  model=model,
115
+ tools=[
116
+ final_answer,
117
+ get_current_time_in_timezone,
118
+ get_current_weather_by_city,
119
+ ], ## add your tools here (don't remove final answer)
120
  max_steps=6,
121
  verbosity_level=1,
122
  grammar=None,