Spaces:
Sleeping
Sleeping
zasharepw77 commited on
Commit ·
5e23b87
1
Parent(s): 7a84380
add real tool get_current_weather_by_city
Browse files12345630 unmodified lines37383940414216 unmodified lines59606162636465from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel, load_tool, tool, TransformersModelimport datetimeimport requestsimport pytzimport yamlimport os30 unmodified linesexcept Exception as e:return f"Error fetching time for timezone '{timezone}': {str(e)}"final_answer = FinalAnswerTool()# Local: mcedit ~/.bashrc16 unmodified linesagent = CodeAgent(model=model,tools=[final_answer, get_current_time_in_timezone], ## add your tools here (don't remove final answer)max_steps=6,verbosity_level=1,grammar=None,123456730 unmodified lines3839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949516 unmodified lines112113114115116117118119120121122from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel, load_tool, tool, TransformersModelimport datetimeimport requestsimport jsonimport pytzimport yamlimport os30 unmodified linesexcept Exception as e:return f"Error fetching time for timezone '{timezone}': {str(e)}"@tooldef get_current_weather_by_city(city: str) -> str:'''Retrieves the current and forecasted weather for a specified city.Args:city: The name of the city (e.g., "New York")Return:A string with weather information'''# API Geocoding от Open-Meteogeocoding_url = f"https://geocoding-api.open-meteo.com/v1/search?name={city}&count=1&language=en&format=json"response_geocoding = requests.get(geocoding_url)if response_geocoding.status_code == 200:data_geocoding = response_geocoding.json()if 'results' in data_geocoding and len(data_geocoding['results']) > 0:result = data_geocoding['results'][0]lat = result.get('latitude', None)lon = result.get('longitude', None)if lat is not None and lon is not None:# Получаем погоду по координатамweather_url = f"https://api.open-meteo.com/v1/forecast?latitude={lat}&longitude={lon}¤t_weather=true&hourly=temperature_2m"response_weather = requests.get(weather_url)if response_weather.status_code == 200:data_weather = response_weather.json()current_temp = data_weather['current_weather']['temperature'] if 'current_weather' in data_weather else Nonehourly_temps = data_weather['hourly']['temperature_2m'] if 'hourly' in data_weather and 'temperature_2m' in data_weather['hourly'] else []weather_info = f"Current temperature: {current_temp}°C\n"weather_info += "Hourly temperatures:\n"for temp in hourly_temps:weather_info += f"{temp}°C\n"return weather_infoelse:return f"Failed to retrieve weather data. Status code: {response_weather.status_code}"else:return "Latitude and Longitude not found."else:return "No results found for the specified city."else:return f"Geocoding failed. Status code: {response_geocoding.status_code}"# Execute the function and prepare the final answer#result = get_current_weather_by_city("New York")#final_answer = f"The current weather in New York is:\n{result}"#print(final_answer)final_answer = FinalAnswerTool()# Local: mcedit ~/.bashrc16 unmodified linesagent = CodeAgent(model=model,tools=[final_answer,get_current_time_in_timezone,get_current_weather_by_city,], ## add your tools here (don't remove final answer)max_steps=6,verbosity_level=1,grammar=None,
app.py+58-1