Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,11 @@
|
|
| 1 |
from smolagents import CodeAgent, DuckDuckGoSearchTool, load_tool, tool
|
| 2 |
-
from smolagents.models import
|
| 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
|
|
@@ -25,12 +24,10 @@ def hourly_temperature(latitude: float, longitude: float) -> str:
|
|
| 25 |
longitude: Longitude of the location.
|
| 26 |
"""
|
| 27 |
try:
|
| 28 |
-
# Setup API client with cache and retries
|
| 29 |
cache_session = requests_cache.CachedSession('.cache', expire_after=3600)
|
| 30 |
retry_session = retry(cache_session, retries=5, backoff_factor=0.2)
|
| 31 |
openmeteo = openmeteo_requests.Client(session=retry_session)
|
| 32 |
|
| 33 |
-
# Request weather data
|
| 34 |
url = "https://api.open-meteo.com/v1/forecast"
|
| 35 |
params = {
|
| 36 |
"latitude": latitude,
|
|
@@ -40,12 +37,10 @@ def hourly_temperature(latitude: float, longitude: float) -> str:
|
|
| 40 |
responses = openmeteo.weather_api(url, params=params)
|
| 41 |
response = responses[0]
|
| 42 |
|
| 43 |
-
# Process hourly data
|
| 44 |
hourly = response.Hourly()
|
| 45 |
hourly_temperature_2m = hourly.Variables(0).ValuesAsNumpy()
|
| 46 |
timestamps = pd.to_datetime(hourly.Time(), unit="s")
|
| 47 |
|
| 48 |
-
# Prepare summary
|
| 49 |
data = pd.DataFrame({
|
| 50 |
"time": timestamps,
|
| 51 |
"temperature_2m": hourly_temperature_2m
|
|
@@ -60,10 +55,7 @@ def hourly_temperature(latitude: float, longitude: float) -> str:
|
|
| 60 |
|
| 61 |
@tool
|
| 62 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 63 |
-
"""Fetch the current local time in a specified timezone.
|
| 64 |
-
Args:
|
| 65 |
-
timezone: A string representing a valid timezone (e.g., 'Africa/Kigali').
|
| 66 |
-
"""
|
| 67 |
try:
|
| 68 |
tz = pytz.timezone(timezone)
|
| 69 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
|
@@ -76,16 +68,14 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
| 76 |
|
| 77 |
final_answer = FinalAnswerTool()
|
| 78 |
|
| 79 |
-
model =
|
| 80 |
model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
|
| 81 |
temperature=0.5,
|
| 82 |
-
|
| 83 |
)
|
| 84 |
|
| 85 |
-
# Import remote tool example (text-to-image)
|
| 86 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 87 |
|
| 88 |
-
# Load prompt templates
|
| 89 |
with open("prompts.yaml", "r") as stream:
|
| 90 |
prompt_templates = yaml.safe_load(stream)
|
| 91 |
|
|
@@ -98,10 +88,8 @@ agent = CodeAgent(
|
|
| 98 |
)
|
| 99 |
|
| 100 |
|
| 101 |
-
# =====
|
| 102 |
if __name__ == "__main__":
|
| 103 |
-
print(hourly_temperature(-1.95, 30.05))
|
| 104 |
print(get_current_time_in_timezone("Africa/Kigali"))
|
| 105 |
-
|
| 106 |
-
# Launch Gradio UI
|
| 107 |
GradioUI(agent).launch()
|
|
|
|
| 1 |
from smolagents import CodeAgent, DuckDuckGoSearchTool, load_tool, tool
|
| 2 |
+
from smolagents.models import HuggingFaceLLM # ✅ unified model class in latest versions
|
| 3 |
import datetime
|
| 4 |
import requests
|
| 5 |
import pytz
|
| 6 |
import yaml
|
| 7 |
from tools.final_answer import FinalAnswerTool
|
| 8 |
|
|
|
|
| 9 |
import openmeteo_requests
|
| 10 |
import pandas as pd
|
| 11 |
import requests_cache
|
|
|
|
| 24 |
longitude: Longitude of the location.
|
| 25 |
"""
|
| 26 |
try:
|
|
|
|
| 27 |
cache_session = requests_cache.CachedSession('.cache', expire_after=3600)
|
| 28 |
retry_session = retry(cache_session, retries=5, backoff_factor=0.2)
|
| 29 |
openmeteo = openmeteo_requests.Client(session=retry_session)
|
| 30 |
|
|
|
|
| 31 |
url = "https://api.open-meteo.com/v1/forecast"
|
| 32 |
params = {
|
| 33 |
"latitude": latitude,
|
|
|
|
| 37 |
responses = openmeteo.weather_api(url, params=params)
|
| 38 |
response = responses[0]
|
| 39 |
|
|
|
|
| 40 |
hourly = response.Hourly()
|
| 41 |
hourly_temperature_2m = hourly.Variables(0).ValuesAsNumpy()
|
| 42 |
timestamps = pd.to_datetime(hourly.Time(), unit="s")
|
| 43 |
|
|
|
|
| 44 |
data = pd.DataFrame({
|
| 45 |
"time": timestamps,
|
| 46 |
"temperature_2m": hourly_temperature_2m
|
|
|
|
| 55 |
|
| 56 |
@tool
|
| 57 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 58 |
+
"""Fetch the current local time in a specified timezone."""
|
|
|
|
|
|
|
|
|
|
| 59 |
try:
|
| 60 |
tz = pytz.timezone(timezone)
|
| 61 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
|
|
|
| 68 |
|
| 69 |
final_answer = FinalAnswerTool()
|
| 70 |
|
| 71 |
+
model = HuggingFaceLLM(
|
| 72 |
model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
|
| 73 |
temperature=0.5,
|
| 74 |
+
max_new_tokens=2048
|
| 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 |
|
|
|
|
| 88 |
)
|
| 89 |
|
| 90 |
|
| 91 |
+
# ===== Run the Agent =====
|
| 92 |
if __name__ == "__main__":
|
| 93 |
+
print(hourly_temperature(-1.95, 30.05))
|
| 94 |
print(get_current_time_in_timezone("Africa/Kigali"))
|
|
|
|
|
|
|
| 95 |
GradioUI(agent).launch()
|