Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -98,100 +98,27 @@ def get_open_meteo_weather(city: str) -> str:
|
|
| 98 |
return f"Error fetching weather data: {str(e)}"
|
| 99 |
|
| 100 |
@tool
|
| 101 |
-
def
|
| 102 |
"""
|
| 103 |
-
|
| 104 |
-
sunrise, sunset, and moon phase and the nighttime (21:00) hourly cloud cover using Open-Meteo's APIs.
|
| 105 |
|
| 106 |
-
Args:
|
| 107 |
-
city: The name of the city (e.g., "Erding").
|
| 108 |
-
|
| 109 |
Returns:
|
| 110 |
-
A string
|
| 111 |
"""
|
| 112 |
try:
|
| 113 |
-
#
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
results = geo_data.get("results")
|
| 119 |
-
if not results:
|
| 120 |
-
return f"Could not find coordinates for city: {city}."
|
| 121 |
-
first_result = results[0]
|
| 122 |
-
latitude = first_result.get("latitude")
|
| 123 |
-
longitude = first_result.get("longitude")
|
| 124 |
-
resolved_city = first_result.get("name", city)
|
| 125 |
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
f"&daily=sunrise,sunset,moon_phase"
|
| 132 |
-
f"&forecast_days=2"
|
| 133 |
-
f"&timezone=auto"
|
| 134 |
-
)
|
| 135 |
-
daily_resp = requests.get(daily_url)
|
| 136 |
-
daily_resp.raise_for_status()
|
| 137 |
-
daily_json = daily_resp.json()
|
| 138 |
-
daily_data = daily_json.get("daily", {})
|
| 139 |
-
times = daily_data.get("time", [])
|
| 140 |
-
if not times:
|
| 141 |
-
return "No daily forecast data available."
|
| 142 |
-
|
| 143 |
-
# Use the first day's forecast.
|
| 144 |
-
forecast_date = times[0] # e.g., "YYYY-MM-DD"
|
| 145 |
-
moon_phase = daily_data.get("moon_phase", [])[0]
|
| 146 |
-
sunrise = daily_data.get("sunrise", [])[0]
|
| 147 |
-
sunset = daily_data.get("sunset", [])[0]
|
| 148 |
-
|
| 149 |
-
# Step 3: Get hourly forecast data for cloud cover.
|
| 150 |
-
hourly_url = (
|
| 151 |
-
f"https://api.open-meteo.com/v1/forecast?"
|
| 152 |
-
f"latitude={latitude}&longitude={longitude}"
|
| 153 |
-
f"&hourly=cloud_cover"
|
| 154 |
-
f"&timezone=auto"
|
| 155 |
-
)
|
| 156 |
-
hourly_resp = requests.get(hourly_url)
|
| 157 |
-
hourly_resp.raise_for_status()
|
| 158 |
-
hourly_data = hourly_resp.json().get("hourly", {})
|
| 159 |
-
hourly_times = hourly_data.get("time", [])
|
| 160 |
-
hourly_clouds = hourly_data.get("cloud_cover", [])
|
| 161 |
-
if not hourly_times or not hourly_clouds:
|
| 162 |
-
return "No hourly cloud cover data available."
|
| 163 |
-
|
| 164 |
-
# Step 4: Find the cloud cover value near 21:00 local time on the forecast day.
|
| 165 |
-
target_time_str = f"{forecast_date}T21:00"
|
| 166 |
-
target_dt = datetime.datetime.fromisoformat(target_time_str)
|
| 167 |
-
time_objs = [datetime.datetime.fromisoformat(t) for t in hourly_times]
|
| 168 |
-
cloud_value = None
|
| 169 |
-
for t_obj, cloud in zip(time_objs, hourly_clouds):
|
| 170 |
-
if abs((t_obj - target_dt).total_seconds()) < 1800: # within 30 minutes
|
| 171 |
-
cloud_value = cloud
|
| 172 |
-
break
|
| 173 |
-
if cloud_value is None:
|
| 174 |
-
closest_idx = min(range(len(time_objs)), key=lambda i: abs((time_objs[i] - target_dt).total_seconds()))
|
| 175 |
-
cloud_value = hourly_clouds[closest_idx]
|
| 176 |
-
|
| 177 |
-
# Step 5: Evaluate stargazing conditions.
|
| 178 |
-
# Ideal conditions: cloud cover < 20% and a moon phase near new moon (<=0.2 or >=0.8, with 0.5 being full moon).
|
| 179 |
-
if cloud_value < 20 and (moon_phase <= 0.2 or moon_phase >= 0.8):
|
| 180 |
-
recommendation = "excellent"
|
| 181 |
-
elif cloud_value < 40 and (moon_phase <= 0.3 or moon_phase >= 0.7):
|
| 182 |
-
recommendation = "good"
|
| 183 |
-
else:
|
| 184 |
-
recommendation = "not ideal"
|
| 185 |
-
|
| 186 |
-
return (
|
| 187 |
-
f"Stargazing Advisor for {resolved_city} on {forecast_date}:\n"
|
| 188 |
-
f"Sunrise: {sunrise}, Sunset: {sunset}\n"
|
| 189 |
-
f"Moon Phase: {moon_phase:.2f} (0=new, 0.5=full)\n"
|
| 190 |
-
f"Cloud Cover around 21:00: {cloud_value}%\n"
|
| 191 |
-
f"Tonight's stargazing conditions are {recommendation}."
|
| 192 |
-
)
|
| 193 |
except Exception as e:
|
| 194 |
-
return f"Error fetching
|
| 195 |
|
| 196 |
final_answer = FinalAnswerTool()
|
| 197 |
|
|
@@ -214,7 +141,8 @@ with open("prompts.yaml", 'r') as stream:
|
|
| 214 |
|
| 215 |
agent = CodeAgent(
|
| 216 |
model=model,
|
| 217 |
-
tools=[final_answer, get_current_time_in_timezone, image_generation_tool, search, get_open_meteo_weather,
|
|
|
|
| 218 |
max_steps=6,
|
| 219 |
verbosity_level=1,
|
| 220 |
grammar=None,
|
|
|
|
| 98 |
return f"Error fetching weather data: {str(e)}"
|
| 99 |
|
| 100 |
@tool
|
| 101 |
+
def get_random_joke() -> str:
|
| 102 |
"""
|
| 103 |
+
Fetches a random joke from the Official Joke API.
|
|
|
|
| 104 |
|
|
|
|
|
|
|
|
|
|
| 105 |
Returns:
|
| 106 |
+
A string containing the joke's setup and punchline, or an error message.
|
| 107 |
"""
|
| 108 |
try:
|
| 109 |
+
# The API endpoint for a random joke:
|
| 110 |
+
url = "https://official-joke-api.appspot.com/random_joke"
|
| 111 |
+
response = requests.get(url)
|
| 112 |
+
response.raise_for_status()
|
| 113 |
+
joke_data = response.json()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
|
| 115 |
+
setup = joke_data.get("setup")
|
| 116 |
+
punchline = joke_data.get("punchline")
|
| 117 |
+
if not setup or not punchline:
|
| 118 |
+
return "Couldn't find a complete joke."
|
| 119 |
+
return f"{setup} - {punchline}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
except Exception as e:
|
| 121 |
+
return f"Error fetching joke: {str(e)}"
|
| 122 |
|
| 123 |
final_answer = FinalAnswerTool()
|
| 124 |
|
|
|
|
| 141 |
|
| 142 |
agent = CodeAgent(
|
| 143 |
model=model,
|
| 144 |
+
tools=[final_answer, get_current_time_in_timezone, image_generation_tool, search, get_open_meteo_weather,
|
| 145 |
+
get_random_joke], ## add your tools here (don't remove final answer)
|
| 146 |
max_steps=6,
|
| 147 |
verbosity_level=1,
|
| 148 |
grammar=None,
|