Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,24 +1,14 @@
|
|
| 1 |
-
from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
|
| 2 |
import datetime
|
| 3 |
import requests
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
-
|
| 7 |
-
from tools.weather import WeatherForecast
|
| 8 |
|
|
|
|
|
|
|
|
|
|
| 9 |
from Gradio_UI import GradioUI
|
| 10 |
|
| 11 |
-
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
| 12 |
-
@tool
|
| 13 |
-
def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
|
| 14 |
-
#Keep this format for the description / args / args description but feel free to modify the tool
|
| 15 |
-
"""A tool that does nothing yet
|
| 16 |
-
Args:
|
| 17 |
-
arg1: the first argument
|
| 18 |
-
arg2: the second argument
|
| 19 |
-
"""
|
| 20 |
-
return "What magic will you build ?"
|
| 21 |
-
|
| 22 |
@tool
|
| 23 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 24 |
"""A tool that fetches the current local time in a specified timezone.
|
|
@@ -41,13 +31,13 @@ def get_weather_forecast(city_name: str) -> str:
|
|
| 41 |
city_name: A string representing a valid city (e.g., 'Bangalore').
|
| 42 |
"""
|
| 43 |
|
| 44 |
-
latitude, longitude =
|
| 45 |
|
| 46 |
base_url = "https://api.open-meteo.com/v1/forecast" # No API key needed for this version
|
| 47 |
params = {
|
| 48 |
"latitude": latitude,
|
| 49 |
"longitude": longitude,
|
| 50 |
-
"hourly": "temperature_2m
|
| 51 |
"daily": "temperature_2m_max,temperature_2m_min,precipitation_sum",
|
| 52 |
"forecast_days": 7,
|
| 53 |
"timezone": "auto"
|
|
@@ -70,12 +60,14 @@ def get_weather_forecast(city_name: str) -> str:
|
|
| 70 |
print(f"Error decoding JSON response: {e}")
|
| 71 |
return None
|
| 72 |
|
| 73 |
-
def
|
| 74 |
"""Gets coordinates using OpenStreetMap's Nominatim (no API key, but with limitations)."""
|
| 75 |
|
| 76 |
# This approach is less reliable and might have rate limits.
|
| 77 |
# It's suitable for basic use cases but not for production.
|
| 78 |
-
|
|
|
|
|
|
|
| 79 |
geocoding_url = "https://nominatim.openstreetmap.org/search" # No API Key needed
|
| 80 |
params = {
|
| 81 |
"q": city_name,
|
|
@@ -103,32 +95,17 @@ def get_coordinates_no_api_key(city_name: str) -> [float, float]:
|
|
| 103 |
print(f"Error parsing geocoding response: {e}")
|
| 104 |
return None, None
|
| 105 |
|
| 106 |
-
# def get_weather(city: str) -> str:
|
| 107 |
-
# """A tool that fetches the current weather of a specified city.
|
| 108 |
-
# Args:
|
| 109 |
-
# city: A string representing a valid city (e.g., 'Bangalore').
|
| 110 |
-
# """
|
| 111 |
-
# try:
|
| 112 |
-
# response = requests.get('https://api.open-meteo.com/v1/forecast?latitude=52.52&longitude=13.41&hourly=temperature_2m&models=bom_access_global')
|
| 113 |
-
# weatherJSON = response.json()
|
| 114 |
-
# cityName = weatherJSON.get('location').get('name')
|
| 115 |
-
# cityTemp = weatherJSON.get('current').get('temp_c')
|
| 116 |
-
# cityCondition = weatherJSON.get('current').get('condition').get('text')
|
| 117 |
-
# return f"The current Temperature in {cityName} is {cityTemp} and conditions are {cityCondition}"
|
| 118 |
-
# except Exception as e:
|
| 119 |
-
# return f"Error fetching weather conditions for {city}"
|
| 120 |
-
|
| 121 |
final_answer = FinalAnswerTool()
|
| 122 |
weather_forecast = WeatherForecast()
|
| 123 |
|
| 124 |
-
# If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
|
| 125 |
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
|
|
|
| 126 |
|
| 127 |
model = HfApiModel(
|
| 128 |
-
max_tokens=2096,
|
| 129 |
-
temperature=0.5,
|
| 130 |
-
model_id=
|
| 131 |
-
custom_role_conversions=None,
|
| 132 |
)
|
| 133 |
|
| 134 |
|
|
|
|
|
|
|
| 1 |
import datetime
|
| 2 |
import requests
|
| 3 |
import pytz
|
| 4 |
import yaml
|
| 5 |
+
import json
|
|
|
|
| 6 |
|
| 7 |
+
from tools.weather import WeatherForecast
|
| 8 |
+
from tools.final_answer import FinalAnswerTool
|
| 9 |
+
from smolagents import CodeAgent, HfApiModel, load_tool, tool
|
| 10 |
from Gradio_UI import GradioUI
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
@tool
|
| 13 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 14 |
"""A tool that fetches the current local time in a specified timezone.
|
|
|
|
| 31 |
city_name: A string representing a valid city (e.g., 'Bangalore').
|
| 32 |
"""
|
| 33 |
|
| 34 |
+
latitude, longitude = get_coordinates(city_name)
|
| 35 |
|
| 36 |
base_url = "https://api.open-meteo.com/v1/forecast" # No API key needed for this version
|
| 37 |
params = {
|
| 38 |
"latitude": latitude,
|
| 39 |
"longitude": longitude,
|
| 40 |
+
"hourly": "temperature_2m",
|
| 41 |
"daily": "temperature_2m_max,temperature_2m_min,precipitation_sum",
|
| 42 |
"forecast_days": 7,
|
| 43 |
"timezone": "auto"
|
|
|
|
| 60 |
print(f"Error decoding JSON response: {e}")
|
| 61 |
return None
|
| 62 |
|
| 63 |
+
def get_coordinates(city_name: str) -> [float, float]:
|
| 64 |
"""Gets coordinates using OpenStreetMap's Nominatim (no API key, but with limitations)."""
|
| 65 |
|
| 66 |
# This approach is less reliable and might have rate limits.
|
| 67 |
# It's suitable for basic use cases but not for production.
|
| 68 |
+
headers = {
|
| 69 |
+
'User-Agent': 'MyGeocodingApp/1.0 (youremail@example.com)' # Replace with your actual email
|
| 70 |
+
}
|
| 71 |
geocoding_url = "https://nominatim.openstreetmap.org/search" # No API Key needed
|
| 72 |
params = {
|
| 73 |
"q": city_name,
|
|
|
|
| 95 |
print(f"Error parsing geocoding response: {e}")
|
| 96 |
return None, None
|
| 97 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
final_answer = FinalAnswerTool()
|
| 99 |
weather_forecast = WeatherForecast()
|
| 100 |
|
|
|
|
| 101 |
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
| 102 |
+
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
|
| 103 |
|
| 104 |
model = HfApiModel(
|
| 105 |
+
max_tokens=2096,
|
| 106 |
+
temperature=0.5,
|
| 107 |
+
model_id=model_id,
|
| 108 |
+
custom_role_conversions=None,
|
| 109 |
)
|
| 110 |
|
| 111 |
|