Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,45 +1,48 @@
|
|
| 1 |
# app.py
|
| 2 |
-
from smolagents import CodeAgent, DuckDuckGoSearchTool, load_tool, tool
|
| 3 |
-
from smolagents.models import HuggingFaceLLM # unified model class in latest versions
|
| 4 |
import datetime
|
|
|
|
| 5 |
import pytz
|
| 6 |
import yaml
|
|
|
|
| 7 |
|
| 8 |
-
# Weather-
|
| 9 |
import openmeteo_requests
|
| 10 |
import pandas as pd
|
| 11 |
import requests_cache
|
| 12 |
from retry_requests import retry
|
| 13 |
|
| 14 |
-
from tools.final_answer import FinalAnswerTool
|
| 15 |
from Gradio_UI import GradioUI
|
| 16 |
|
| 17 |
-
|
| 18 |
-
# Example tool: fetch hourly temperature from Open-Meteo API
|
| 19 |
@tool
|
| 20 |
-
def hourly_temperature(
|
| 21 |
-
"""Fetch hourly temperature
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
cache_session = requests_cache.CachedSession('.cache', expire_after=3600)
|
| 23 |
retry_session = retry(cache_session, retries=5, backoff_factor=0.2)
|
| 24 |
-
|
| 25 |
|
| 26 |
url = "https://api.open-meteo.com/v1/forecast"
|
| 27 |
params = {
|
| 28 |
-
"latitude":
|
| 29 |
-
"longitude":
|
| 30 |
"hourly": "temperature_2m",
|
| 31 |
}
|
| 32 |
-
response =
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
return pd.Series(temp_series, name="temperature_2m")
|
| 37 |
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
# Tool: get current time in timezone
|
| 40 |
@tool
|
| 41 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 42 |
-
"""
|
| 43 |
try:
|
| 44 |
tz = pytz.timezone(timezone)
|
| 45 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
|
@@ -51,43 +54,36 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
| 51 |
# Final answer tool
|
| 52 |
final_answer = FinalAnswerTool()
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
max_tokens=2096,
|
| 58 |
temperature=0.5,
|
| 59 |
-
|
| 60 |
-
custom_role_conversions=None,
|
| 61 |
)
|
| 62 |
|
| 63 |
-
|
| 64 |
-
# Load a remote tool from HuggingFace Hub
|
| 65 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 66 |
|
| 67 |
-
|
| 68 |
# Load prompt templates
|
| 69 |
-
with open("prompts.yaml",
|
| 70 |
prompt_templates = yaml.safe_load(stream)
|
| 71 |
|
| 72 |
-
|
| 73 |
-
# Create the agent
|
| 74 |
agent = CodeAgent(
|
| 75 |
model=model,
|
| 76 |
-
tools=[final_answer, hourly_temperature, get_current_time_in_timezone],
|
| 77 |
max_steps=6,
|
| 78 |
verbosity_level=1,
|
| 79 |
grammar=None,
|
| 80 |
planning_interval=None,
|
| 81 |
-
name=
|
| 82 |
-
description=
|
| 83 |
prompt_templates=prompt_templates
|
| 84 |
)
|
| 85 |
|
| 86 |
-
|
| 87 |
-
# Example calls (optional, can remove in prod)
|
| 88 |
hourly_temperature(32.9, 15)
|
| 89 |
-
get_current_time_in_timezone(
|
| 90 |
-
|
| 91 |
|
| 92 |
-
# Launch
|
| 93 |
GradioUI(agent).launch()
|
|
|
|
| 1 |
# app.py
|
| 2 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, load_tool, tool, InferenceClientModel
|
|
|
|
| 3 |
import datetime
|
| 4 |
+
import requests
|
| 5 |
import pytz
|
| 6 |
import yaml
|
| 7 |
+
from tools.final_answer import FinalAnswerTool
|
| 8 |
|
| 9 |
+
# Weather-specific imports
|
| 10 |
import openmeteo_requests
|
| 11 |
import pandas as pd
|
| 12 |
import requests_cache
|
| 13 |
from retry_requests import retry
|
| 14 |
|
|
|
|
| 15 |
from Gradio_UI import GradioUI
|
| 16 |
|
| 17 |
+
# Example tool
|
|
|
|
| 18 |
@tool
|
| 19 |
+
def hourly_temperature(latitude: float, longitude: float) -> str:
|
| 20 |
+
"""Fetch hourly temperature from Open-Meteo API.
|
| 21 |
+
|
| 22 |
+
Args:
|
| 23 |
+
latitude: Latitude of the location
|
| 24 |
+
longitude: Longitude of the location
|
| 25 |
+
"""
|
| 26 |
cache_session = requests_cache.CachedSession('.cache', expire_after=3600)
|
| 27 |
retry_session = retry(cache_session, retries=5, backoff_factor=0.2)
|
| 28 |
+
openmeteo = openmeteo_requests.Client(session=retry_session)
|
| 29 |
|
| 30 |
url = "https://api.open-meteo.com/v1/forecast"
|
| 31 |
params = {
|
| 32 |
+
"latitude": latitude,
|
| 33 |
+
"longitude": longitude,
|
| 34 |
"hourly": "temperature_2m",
|
| 35 |
}
|
| 36 |
+
response = openmeteo.weather_api(url, params=params)
|
| 37 |
+
hourly = response.Hourly()
|
| 38 |
+
hourly_temperature_2m = hourly.Variables(0).ValuesAsNumpy()
|
|
|
|
|
|
|
| 39 |
|
| 40 |
+
data = {"temperature_2m": hourly_temperature_2m}
|
| 41 |
+
return data["temperature_2m"]
|
| 42 |
|
|
|
|
| 43 |
@tool
|
| 44 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 45 |
+
"""Fetch current local time for a given timezone."""
|
| 46 |
try:
|
| 47 |
tz = pytz.timezone(timezone)
|
| 48 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
|
|
|
| 54 |
# Final answer tool
|
| 55 |
final_answer = FinalAnswerTool()
|
| 56 |
|
| 57 |
+
# Create model using latest smolagents class
|
| 58 |
+
model = InferenceClientModel(
|
| 59 |
+
model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
|
|
|
|
| 60 |
temperature=0.5,
|
| 61 |
+
max_new_tokens=2048
|
|
|
|
| 62 |
)
|
| 63 |
|
| 64 |
+
# Load additional tool from Hub
|
|
|
|
| 65 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 66 |
|
|
|
|
| 67 |
# Load prompt templates
|
| 68 |
+
with open("prompts.yaml", 'r') as stream:
|
| 69 |
prompt_templates = yaml.safe_load(stream)
|
| 70 |
|
| 71 |
+
# Create agent
|
|
|
|
| 72 |
agent = CodeAgent(
|
| 73 |
model=model,
|
| 74 |
+
tools=[final_answer, hourly_temperature, get_current_time_in_timezone, image_generation_tool],
|
| 75 |
max_steps=6,
|
| 76 |
verbosity_level=1,
|
| 77 |
grammar=None,
|
| 78 |
planning_interval=None,
|
| 79 |
+
name="DormAI",
|
| 80 |
+
description="AI agent for dorm management",
|
| 81 |
prompt_templates=prompt_templates
|
| 82 |
)
|
| 83 |
|
| 84 |
+
# Test the tools
|
|
|
|
| 85 |
hourly_temperature(32.9, 15)
|
| 86 |
+
get_current_time_in_timezone('Africa/Kigali')
|
|
|
|
| 87 |
|
| 88 |
+
# Launch UI
|
| 89 |
GradioUI(agent).launch()
|