Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,69 +1,186 @@
|
|
| 1 |
-
from smolagents import CodeAgent,
|
| 2 |
import datetime
|
| 3 |
import requests
|
| 4 |
import pytz
|
| 5 |
import yaml
|
|
|
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
| 7 |
-
|
| 8 |
from Gradio_UI import GradioUI
|
| 9 |
|
| 10 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
@tool
|
| 12 |
-
def
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
@tool
|
| 22 |
-
def
|
| 23 |
-
"""
|
| 24 |
-
Args:
|
| 25 |
-
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
| 26 |
-
"""
|
| 27 |
try:
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
tz = pytz.timezone(timezone)
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
except Exception as e:
|
| 34 |
-
return
|
| 35 |
|
| 36 |
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
|
| 40 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
model = HfApiModel(
|
| 43 |
-
max_tokens=2096,
|
| 44 |
-
temperature=0.5,
|
| 45 |
-
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
|
| 46 |
-
custom_role_conversions=None,
|
| 47 |
)
|
| 48 |
|
| 49 |
-
|
| 50 |
-
# Import tool from Hub
|
| 51 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 52 |
|
| 53 |
with open("prompts.yaml", 'r') as stream:
|
| 54 |
prompt_templates = yaml.safe_load(stream)
|
| 55 |
-
|
| 56 |
agent = CodeAgent(
|
| 57 |
model=model,
|
| 58 |
-
tools=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
max_steps=6,
|
| 60 |
verbosity_level=1,
|
| 61 |
-
grammar=None,
|
| 62 |
-
planning_interval=None,
|
| 63 |
-
name=None,
|
| 64 |
-
description=None,
|
| 65 |
prompt_templates=prompt_templates
|
| 66 |
)
|
| 67 |
|
| 68 |
-
|
| 69 |
-
GradioUI(agent).launch()
|
|
|
|
| 1 |
+
from smolagents import CodeAgent, HfApiModel, load_tool, tool
|
| 2 |
import datetime
|
| 3 |
import requests
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
+
import matplotlib.pyplot as plt
|
| 7 |
from tools.final_answer import FinalAnswerTool
|
|
|
|
| 8 |
from Gradio_UI import GradioUI
|
| 9 |
|
| 10 |
+
# -----------------------------
|
| 11 |
+
# LOCATION + TIMEZONE TOOLS
|
| 12 |
+
# -----------------------------
|
| 13 |
+
|
| 14 |
+
@tool
|
| 15 |
+
def get_location() -> dict:
|
| 16 |
+
"""Get user's approximate latitude and longitude."""
|
| 17 |
+
try:
|
| 18 |
+
res = requests.get("http://ip-api.com/json/")
|
| 19 |
+
data = res.json()
|
| 20 |
+
return {
|
| 21 |
+
"latitude": data["lat"],
|
| 22 |
+
"longitude": data["lon"]
|
| 23 |
+
}
|
| 24 |
+
except Exception as e:
|
| 25 |
+
return {"error": str(e)}
|
| 26 |
+
|
| 27 |
@tool
|
| 28 |
+
def get_local_timezone() -> str:
|
| 29 |
+
"""Detects the user's local timezone based on IP."""
|
| 30 |
+
try:
|
| 31 |
+
res = requests.get("http://ip-api.com/json/")
|
| 32 |
+
data = res.json()
|
| 33 |
+
return data.get("timezone", "UTC")
|
| 34 |
+
except Exception as e:
|
| 35 |
+
return f"Error detecting timezone: {str(e)}"
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
# -----------------------------
|
| 39 |
+
# WEATHER TOOL (24H + UNCERTAINTY)
|
| 40 |
+
# -----------------------------
|
| 41 |
|
| 42 |
@tool
|
| 43 |
+
def get_hourly_weather(latitude: float, longitude: float, timezone: str) -> list:
|
| 44 |
+
"""Fetch hourly weather forecast for next 24 hours including uncertainty bands."""
|
|
|
|
|
|
|
|
|
|
| 45 |
try:
|
| 46 |
+
url = "https://api.open-meteo.com/v1/forecast"
|
| 47 |
+
|
| 48 |
+
params = {
|
| 49 |
+
"latitude": latitude,
|
| 50 |
+
"longitude": longitude,
|
| 51 |
+
"hourly": "temperature_2m,precipitation,weathercode",
|
| 52 |
+
"forecast_days": 2,
|
| 53 |
+
"timezone": timezone
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
res = requests.get(url, params=params)
|
| 57 |
+
data = res.json()
|
| 58 |
+
|
| 59 |
+
hourly = data["hourly"]
|
| 60 |
tz = pytz.timezone(timezone)
|
| 61 |
+
now = datetime.datetime.now(tz)
|
| 62 |
+
|
| 63 |
+
result = []
|
| 64 |
+
|
| 65 |
+
for t, temp, precip, code in zip(
|
| 66 |
+
hourly["time"],
|
| 67 |
+
hourly["temperature_2m"],
|
| 68 |
+
hourly["precipitation"],
|
| 69 |
+
hourly["weathercode"]
|
| 70 |
+
):
|
| 71 |
+
dt = datetime.datetime.fromisoformat(t)
|
| 72 |
+
|
| 73 |
+
if dt >= now and len(result) < 24:
|
| 74 |
+
# simple synthetic uncertainty band (±2°C)
|
| 75 |
+
result.append({
|
| 76 |
+
"time": t,
|
| 77 |
+
"temperature": temp,
|
| 78 |
+
"temp_min": temp - 2,
|
| 79 |
+
"temp_max": temp + 2,
|
| 80 |
+
"precipitation": precip,
|
| 81 |
+
"weathercode": code
|
| 82 |
+
})
|
| 83 |
+
|
| 84 |
+
return result
|
| 85 |
+
|
| 86 |
except Exception as e:
|
| 87 |
+
return [{"error": str(e)}]
|
| 88 |
|
| 89 |
|
| 90 |
+
# -----------------------------
|
| 91 |
+
# WEATHER CODE → ICON
|
| 92 |
+
# -----------------------------
|
| 93 |
+
|
| 94 |
+
def weather_icon(code):
|
| 95 |
+
if code == 0:
|
| 96 |
+
return "☀️"
|
| 97 |
+
elif code in [1, 2, 3]:
|
| 98 |
+
return "⛅"
|
| 99 |
+
elif code in [45, 48]:
|
| 100 |
+
return "🌫"
|
| 101 |
+
elif code in [51, 53, 55, 61, 63, 65]:
|
| 102 |
+
return "🌧"
|
| 103 |
+
elif code in [71, 73, 75]:
|
| 104 |
+
return "❄️"
|
| 105 |
+
elif code in [95]:
|
| 106 |
+
return "⛈"
|
| 107 |
+
else:
|
| 108 |
+
return "🌈"
|
| 109 |
|
| 110 |
+
|
| 111 |
+
# -----------------------------
|
| 112 |
+
# PLOT TOOL (ICONS + BANDS)
|
| 113 |
+
# -----------------------------
|
| 114 |
+
|
| 115 |
+
@tool
|
| 116 |
+
def plot_weather(weather_data: list) -> str:
|
| 117 |
+
"""Generate stylized weather visualization with icons + uncertainty bands."""
|
| 118 |
+
|
| 119 |
+
times = [entry["time"][11:16] for entry in weather_data]
|
| 120 |
+
temps = [entry["temperature"] for entry in weather_data]
|
| 121 |
+
temp_min = [entry["temp_min"] for entry in weather_data]
|
| 122 |
+
temp_max = [entry["temp_max"] for entry in weather_data]
|
| 123 |
+
precip = [entry["precipitation"] for entry in weather_data]
|
| 124 |
+
codes = [entry["weathercode"] for entry in weather_data]
|
| 125 |
+
|
| 126 |
+
icons = [weather_icon(c) for c in codes]
|
| 127 |
+
|
| 128 |
+
plt.figure(figsize=(12, 6))
|
| 129 |
+
|
| 130 |
+
# Temperature line
|
| 131 |
+
plt.plot(times, temps, label="Temperature")
|
| 132 |
+
|
| 133 |
+
# Uncertainty band
|
| 134 |
+
plt.fill_between(times, temp_min, temp_max, alpha=0.2)
|
| 135 |
+
|
| 136 |
+
# Precip bars
|
| 137 |
+
plt.bar(times, precip, alpha=0.3, label="Precipitation")
|
| 138 |
+
|
| 139 |
+
# Add icons above line
|
| 140 |
+
for i, icon in enumerate(icons):
|
| 141 |
+
plt.text(i, temps[i] + 1.5, icon, ha='center')
|
| 142 |
+
|
| 143 |
+
plt.xticks(rotation=45)
|
| 144 |
+
plt.title("24-Hour Weather Forecast")
|
| 145 |
+
plt.legend()
|
| 146 |
+
plt.tight_layout()
|
| 147 |
+
|
| 148 |
+
file_path = "/mnt/data/weather_visual.png"
|
| 149 |
+
plt.savefig(file_path)
|
| 150 |
+
plt.close()
|
| 151 |
+
|
| 152 |
+
return file_path
|
| 153 |
+
|
| 154 |
+
|
| 155 |
+
# -----------------------------
|
| 156 |
+
# AGENT SETUP
|
| 157 |
+
# -----------------------------
|
| 158 |
+
|
| 159 |
+
final_answer = FinalAnswerTool()
|
| 160 |
|
| 161 |
model = HfApiModel(
|
| 162 |
+
max_tokens=2096,
|
| 163 |
+
temperature=0.5,
|
| 164 |
+
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
|
|
|
|
| 165 |
)
|
| 166 |
|
|
|
|
|
|
|
| 167 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 168 |
|
| 169 |
with open("prompts.yaml", 'r') as stream:
|
| 170 |
prompt_templates = yaml.safe_load(stream)
|
| 171 |
+
|
| 172 |
agent = CodeAgent(
|
| 173 |
model=model,
|
| 174 |
+
tools=[
|
| 175 |
+
final_answer,
|
| 176 |
+
get_location,
|
| 177 |
+
get_local_timezone,
|
| 178 |
+
get_hourly_weather,
|
| 179 |
+
plot_weather
|
| 180 |
+
],
|
| 181 |
max_steps=6,
|
| 182 |
verbosity_level=1,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
prompt_templates=prompt_templates
|
| 184 |
)
|
| 185 |
|
| 186 |
+
GradioUI(agent).launch
|
|
|