Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,100 +1,83 @@
|
|
| 1 |
-
from smolagents import CodeAgent,DuckDuckGoSearchTool,
|
| 2 |
import datetime
|
| 3 |
-
import requests
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
-
from tools.final_answer import FinalAnswerTool
|
| 7 |
-
#These are waether specific imports
|
| 8 |
import openmeteo_requests
|
| 9 |
import pandas as pd
|
| 10 |
import requests_cache
|
| 11 |
from retry_requests import retry
|
| 12 |
-
|
| 13 |
from Gradio_UI import GradioUI
|
| 14 |
|
| 15 |
-
|
|
|
|
| 16 |
@tool
|
| 17 |
-
def
|
| 18 |
-
|
| 19 |
-
"""A tool that does nothing yet
|
| 20 |
Args:
|
| 21 |
-
|
| 22 |
-
|
| 23 |
"""
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
| 46 |
|
|
|
|
|
|
|
| 47 |
@tool
|
| 48 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 49 |
-
"""
|
| 50 |
-
Args:
|
| 51 |
-
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
| 52 |
-
"""
|
| 53 |
try:
|
| 54 |
-
# Create timezone object
|
| 55 |
tz = pytz.timezone(timezone)
|
| 56 |
-
# Get current time in that timezone
|
| 57 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 58 |
return f"The current local time in {timezone} is: {local_time}"
|
| 59 |
except Exception as e:
|
| 60 |
-
return f"Error fetching time
|
| 61 |
|
| 62 |
|
|
|
|
| 63 |
final_answer = FinalAnswerTool()
|
| 64 |
|
| 65 |
-
#
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
model = HfApiModel(
|
| 69 |
-
max_tokens=2096,
|
| 70 |
-
temperature=0.5,
|
| 71 |
-
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
|
| 72 |
-
custom_role_conversions=None,
|
| 73 |
-
)
|
| 74 |
-
|
| 75 |
|
| 76 |
-
#
|
| 77 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 78 |
|
|
|
|
| 79 |
with open("prompts.yaml", 'r') as stream:
|
| 80 |
prompt_templates = yaml.safe_load(stream)
|
| 81 |
-
|
|
|
|
| 82 |
agent = CodeAgent(
|
| 83 |
model=model,
|
| 84 |
-
tools=[final_answer
|
| 85 |
max_steps=6,
|
| 86 |
verbosity_level=1,
|
| 87 |
-
grammar=None,
|
| 88 |
-
planning_interval=None,
|
| 89 |
-
name=None,
|
| 90 |
-
description=None,
|
| 91 |
prompt_templates=prompt_templates
|
| 92 |
)
|
| 93 |
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
GradioUI(agent).launch()
|
|
|
|
| 1 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfAgent, load_tool, tool
|
| 2 |
import datetime
|
|
|
|
| 3 |
import pytz
|
| 4 |
import yaml
|
|
|
|
|
|
|
| 5 |
import openmeteo_requests
|
| 6 |
import pandas as pd
|
| 7 |
import requests_cache
|
| 8 |
from retry_requests import retry
|
| 9 |
+
from tools.final_answer import FinalAnswerTool
|
| 10 |
from Gradio_UI import GradioUI
|
| 11 |
|
| 12 |
+
|
| 13 |
+
# Weather tool
|
| 14 |
@tool
|
| 15 |
+
def hourly_temperature(latitude: float, longitude: float) -> str:
|
| 16 |
+
"""Fetch hourly temperature data using Open-Meteo API.
|
|
|
|
| 17 |
Args:
|
| 18 |
+
latitude: Latitude of the location
|
| 19 |
+
longitude: Longitude of the location
|
| 20 |
"""
|
| 21 |
+
try:
|
| 22 |
+
cache_session = requests_cache.CachedSession('.cache', expire_after=3600)
|
| 23 |
+
retry_session = retry(cache_session, retries=5, backoff_factor=0.2)
|
| 24 |
+
openmeteo = openmeteo_requests.Client(session=retry_session)
|
| 25 |
+
|
| 26 |
+
url = "https://api.open-meteo.com/v1/forecast"
|
| 27 |
+
params = {
|
| 28 |
+
"latitude": latitude,
|
| 29 |
+
"longitude": longitude,
|
| 30 |
+
"hourly": "temperature_2m",
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
responses = openmeteo.weather_api(url, params=params)
|
| 34 |
+
response = responses[0] # Get first response object
|
| 35 |
+
|
| 36 |
+
hourly = response.Hourly()
|
| 37 |
+
hourly_temperature_2m = hourly.Variables(0).ValuesAsNumpy()
|
| 38 |
+
|
| 39 |
+
# Prepare readable output
|
| 40 |
+
temps = hourly_temperature_2m[:5] # Show first 5 hourly values
|
| 41 |
+
return f"Next hours' temperatures (°C): {temps.tolist()}"
|
| 42 |
+
|
| 43 |
+
except Exception as e:
|
| 44 |
+
return f"Error fetching weather: {str(e)}"
|
| 45 |
|
| 46 |
+
|
| 47 |
+
# Time tool
|
| 48 |
@tool
|
| 49 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 50 |
+
"""Fetch the current local time in a specified timezone."""
|
|
|
|
|
|
|
|
|
|
| 51 |
try:
|
|
|
|
| 52 |
tz = pytz.timezone(timezone)
|
|
|
|
| 53 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 54 |
return f"The current local time in {timezone} is: {local_time}"
|
| 55 |
except Exception as e:
|
| 56 |
+
return f"Error fetching time: {str(e)}"
|
| 57 |
|
| 58 |
|
| 59 |
+
# Final answer tool
|
| 60 |
final_answer = FinalAnswerTool()
|
| 61 |
|
| 62 |
+
# Initialize model (new API)
|
| 63 |
+
model = HfAgent("Qwen/Qwen2.5-Coder-32B-Instruct")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
+
# Optional: load text-to-image tool from Hugging Face Hub
|
| 66 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 67 |
|
| 68 |
+
# Load custom prompts if you have them
|
| 69 |
with open("prompts.yaml", 'r') as stream:
|
| 70 |
prompt_templates = yaml.safe_load(stream)
|
| 71 |
+
|
| 72 |
+
# Define your agent
|
| 73 |
agent = CodeAgent(
|
| 74 |
model=model,
|
| 75 |
+
tools=[final_answer, hourly_temperature, get_current_time_in_timezone],
|
| 76 |
max_steps=6,
|
| 77 |
verbosity_level=1,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
prompt_templates=prompt_templates
|
| 79 |
)
|
| 80 |
|
| 81 |
+
# Run Gradio UI
|
| 82 |
+
if __name__ == "__main__":
|
| 83 |
+
GradioUI(agent).launch()
|
|
|
|
|
|
|
|
|
|
|
|