Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
from smolagents import CodeAgent,DuckDuckGoSearchTool, WebSearchTool, HfApiModel,load_tool,tool
|
| 2 |
import datetime
|
| 3 |
import requests
|
| 4 |
import pytz
|
|
@@ -16,87 +16,71 @@ from Gradio_UI import GradioUI
|
|
| 16 |
|
| 17 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
| 18 |
# @tool
|
| 19 |
-
# def my_custom_tool(arg1:str, arg2:int): str #it's
|
| 20 |
-
|
| 21 |
-
"""A tool that does nothing yet
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
"""
|
| 26 |
-
# return
|
| 27 |
|
| 28 |
@tool
|
| 29 |
-
def get_location_tool(ip: str):
|
|
|
|
| 30 |
|
| 31 |
-
return {
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
|
| 39 |
-
"""
|
|
|
|
| 40 |
Args:
|
| 41 |
ip: A string representing a valid ip address (e.g., '192.168.10.1')
|
| 42 |
-
|
| 43 |
-
Args:
|
| 44 |
-
city: A string representing a valid city (e.g., 'Dubai, Singapore')
|
| 45 |
-
|
| 46 |
-
Args:
|
| 47 |
-
region: A string representing a valid region (e.g, 'Africa, Asia')
|
| 48 |
-
|
| 49 |
-
Args:
|
| 50 |
-
country: A string representing a valid country (e.g., 'Ethiopia, United Arab Emirates')
|
| 51 |
-
|
| 52 |
-
Args:
|
| 53 |
-
latitude: A string representing valid latitude coordinates (e.g., '40.741895')
|
| 54 |
-
|
| 55 |
-
Args:
|
| 56 |
-
longitude: A string representing valid longitude coordinates (e.g, '73.989308')
|
| 57 |
-
|
| 58 |
Returns: ip address, city, region, country, and geo coordinates
|
| 59 |
-
"""
|
| 60 |
|
| 61 |
-
response = DbIpCity.get(
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
ip_address = 'YOUR_IP_ADDRESS' # Replace with the actual IP address
|
| 65 |
-
location = get_location(ip_address)
|
| 66 |
print(location)
|
| 67 |
|
| 68 |
|
| 69 |
@tool
|
| 70 |
-
def get_current_time_in_timezone(timezone: str):
|
| 71 |
|
| 72 |
-
"""A tool that fetches the current local time in a specified timezone.
|
| 73 |
Args:
|
| 74 |
timezone: A string representing a valid timezone (e.g., 'America/New_York')
|
| 75 |
-
|
| 76 |
Returns: The current local time in the following format ("%Y-%m-%d %H:%M:%S")
|
| 77 |
-
"""
|
| 78 |
-
try:
|
| 79 |
# Create timezone object
|
| 80 |
tz = pytz.timezone(timezone)
|
| 81 |
-
|
| 82 |
-
|
| 83 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 84 |
return f"The current local time in {timezone} is: {local_time}"
|
| 85 |
-
except Exception as e:
|
| 86 |
|
| 87 |
-
|
|
|
|
| 88 |
|
| 89 |
|
| 90 |
final_answer = FinalAnswerTool()
|
| 91 |
|
| 92 |
# 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:
|
| 93 |
-
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
| 94 |
|
| 95 |
model = HfApiModel(
|
| 96 |
-
max_tokens=2096,
|
| 97 |
-
temperature=0.5,
|
| 98 |
-
model_id='Qwen/Qwen2.5-Coder-32B-Instruct'
|
| 99 |
-
custom_role_conversions=None,
|
| 100 |
)
|
| 101 |
|
| 102 |
@tool
|
|
@@ -110,17 +94,23 @@ def get_weather_data(city: str):
|
|
| 110 |
A dictionary containing weather data
|
| 111 |
"""
|
| 112 |
# Get weather for multiple cities
|
| 113 |
-
cities = ["Tokyo", "New York", "London"]
|
| 114 |
-
data = {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
-
for city in cities:
|
| 117 |
-
data[city] = get_weather_data(city)
|
| 118 |
-
# Calculate average temperatures
|
| 119 |
-
avg_temps = {city: sum(info["temperatures"])/len(info["temperatures"])
|
| 120 |
-
for city, info in data.items()}
|
| 121 |
-
# Find the warmest city
|
| 122 |
-
warmest_city = max(avg_temps, key=avg_temps.get)
|
| 123 |
-
print(f"The warmest city is {warmest_city} with an average of {avg_temps[warmest_city]:.1f}°C")
|
| 124 |
|
| 125 |
@tool
|
| 126 |
def hotel_recommendation_tool(city: str,price: str): str
|
|
|
|
| 1 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, WebSearchTool, HfApiModel, load_tool, tool
|
| 2 |
import datetime
|
| 3 |
import requests
|
| 4 |
import pytz
|
|
|
|
| 16 |
|
| 17 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
| 18 |
# @tool
|
| 19 |
+
# def my_custom_tool(arg1:str, arg2:int): str #it's important to specify the return type
|
| 20 |
+
# #Keep this format for the description / args / args description but feel free to modify the tool
|
| 21 |
+
# """A tool that does nothing yet
|
| 22 |
+
# Args:
|
| 23 |
+
# arg1: the first argument
|
| 24 |
+
# arg2: the second argument
|
| 25 |
+
# """
|
| 26 |
+
# # return
|
| 27 |
|
| 28 |
@tool
|
| 29 |
+
def get_location_tool(ip: str):
|
| 30 |
+
response = DbIpCity.get(ip, api_key='free')
|
| 31 |
|
| 32 |
+
return {
|
| 33 |
+
'city': response.city,
|
| 34 |
+
'region': response.region,
|
| 35 |
+
'country': response.country,
|
| 36 |
+
'latitude': response.latitude,
|
| 37 |
+
'longitude': response.longitude
|
| 38 |
+
}
|
| 39 |
|
| 40 |
+
"""
|
| 41 |
+
This is a tool that gets the exact location of a user including city, region, country, latitude, and longitude.
|
| 42 |
Args:
|
| 43 |
ip: A string representing a valid ip address (e.g., '192.168.10.1')
|
| 44 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
Returns: ip address, city, region, country, and geo coordinates
|
| 46 |
+
"""
|
| 47 |
|
| 48 |
+
response = DbIpCity.get('YOUR_IP_ADDRESS') # Replace with the actual IP address
|
| 49 |
+
location = get_location('ip_address')
|
|
|
|
|
|
|
|
|
|
| 50 |
print(location)
|
| 51 |
|
| 52 |
|
| 53 |
@tool
|
| 54 |
+
def get_current_time_in_timezone(timezone: str):
|
| 55 |
|
| 56 |
+
"""A tool that fetches the current local time in a specified timezone.
|
| 57 |
Args:
|
| 58 |
timezone: A string representing a valid timezone (e.g., 'America/New_York')
|
| 59 |
+
|
| 60 |
Returns: The current local time in the following format ("%Y-%m-%d %H:%M:%S")
|
| 61 |
+
"""
|
| 62 |
+
try:
|
| 63 |
# Create timezone object
|
| 64 |
tz = pytz.timezone(timezone)
|
| 65 |
+
|
| 66 |
+
# Get current time in that timezone
|
| 67 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 68 |
return f"The current local time in {timezone} is: {local_time}"
|
|
|
|
| 69 |
|
| 70 |
+
except Exception as e:
|
| 71 |
+
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 72 |
|
| 73 |
|
| 74 |
final_answer = FinalAnswerTool()
|
| 75 |
|
| 76 |
# 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:
|
| 77 |
+
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
| 78 |
|
| 79 |
model = HfApiModel(
|
| 80 |
+
max_tokens=2096,
|
| 81 |
+
temperature=0.5,
|
| 82 |
+
model_id='Qwen/Qwen2.5-Coder-32B-Instruct', # it is possible that this model may be overloaded
|
| 83 |
+
custom_role_conversions=None,
|
| 84 |
)
|
| 85 |
|
| 86 |
@tool
|
|
|
|
| 94 |
A dictionary containing weather data
|
| 95 |
"""
|
| 96 |
# Get weather for multiple cities
|
| 97 |
+
cities = ["Tokyo", "New York", "London"]
|
| 98 |
+
data = {}
|
| 99 |
+
|
| 100 |
+
for city in cities:
|
| 101 |
+
data[city] = get_weather_data(city)
|
| 102 |
+
|
| 103 |
+
# Calculate average temperatures
|
| 104 |
+
avg_temps = {city: sum(info["temperatures"])/len(info["temperatures"])
|
| 105 |
+
for city, info in data.items()}
|
| 106 |
+
|
| 107 |
+
# Find the warmest city
|
| 108 |
+
warmest_city = max(avg_temps, key=avg_temps.get)
|
| 109 |
+
print(f"The warmest city is {warmest_city} with an average of {avg_temps[warmest_city]:.1f}°C")
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
|
| 113 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
|
| 115 |
@tool
|
| 116 |
def hotel_recommendation_tool(city: str,price: str): str
|