Spaces:
Sleeping
Sleeping
Update app.py
Browse filesadded model back in ... forgot last time
app.py
CHANGED
@@ -10,7 +10,6 @@ from typing import Dict, Tuple, Optional
|
|
10 |
def get_coordinates(city: str) -> Optional[Tuple[float, float]]:
|
11 |
"""Get coordinates for any city using Open-Meteo Geocoding API."""
|
12 |
try:
|
13 |
-
# Using Open-Meteo's free geocoding API
|
14 |
geocoding_url = f"https://geocoding-api.open-meteo.com/v1/search?name={city}&count=1&language=en&format=json"
|
15 |
response = requests.get(geocoding_url, timeout=10)
|
16 |
|
@@ -34,14 +33,12 @@ def get_weather(city: str) -> str:
|
|
34 |
str: Weather information including temperature and conditions
|
35 |
"""
|
36 |
try:
|
37 |
-
# First get the coordinates for the city
|
38 |
coords = get_coordinates(city)
|
39 |
if not coords:
|
40 |
return f"Sorry, couldn't find coordinates for {city}. Please check the city name and try again."
|
41 |
|
42 |
lat, lon = coords
|
43 |
|
44 |
-
# Get weather using Open-Meteo API
|
45 |
api_url = "https://api.open-meteo.com/v1/forecast"
|
46 |
params = {
|
47 |
"latitude": lat,
|
@@ -71,124 +68,29 @@ def get_weather(city: str) -> str:
|
|
71 |
except Exception as e:
|
72 |
return f"Error fetching weather data: {str(e)}"
|
73 |
|
74 |
-
|
75 |
-
def check_biking_conditions(city: str) -> str:
|
76 |
-
"""Check if current weather conditions are good for biking in specified city.
|
77 |
-
|
78 |
-
Args:
|
79 |
-
city: Name of any city in the world
|
80 |
-
|
81 |
-
Returns:
|
82 |
-
str: Assessment of biking conditions based on weather
|
83 |
-
"""
|
84 |
-
try:
|
85 |
-
coords = get_coordinates(city)
|
86 |
-
if not coords:
|
87 |
-
return f"Sorry, couldn't find coordinates for {city}."
|
88 |
-
|
89 |
-
lat, lon = coords
|
90 |
-
|
91 |
-
api_url = "https://api.open-meteo.com/v1/forecast"
|
92 |
-
params = {
|
93 |
-
"latitude": lat,
|
94 |
-
"longitude": lon,
|
95 |
-
"current": "temperature_2m,wind_speed_10m",
|
96 |
-
"hourly": "temperature_2m,relative_humidity_2m,wind_speed_10m"
|
97 |
-
}
|
98 |
-
|
99 |
-
response = requests.get(api_url, params=params, timeout=10)
|
100 |
-
|
101 |
-
if response.status_code == 200:
|
102 |
-
data = response.json()
|
103 |
-
current = data["current"]
|
104 |
-
temp = current['temperature_2m']
|
105 |
-
wind_speed = current['wind_speed_10m']
|
106 |
-
|
107 |
-
# Assess conditions for biking
|
108 |
-
conditions = []
|
109 |
-
|
110 |
-
if wind_speed > 20:
|
111 |
-
conditions.append(f"⚠️ Wind speeds are high ({wind_speed} km/h)")
|
112 |
-
if temp < 10:
|
113 |
-
conditions.append(f"🌡️ It's cold ({temp}°C) - dress warmly")
|
114 |
-
elif temp > 30:
|
115 |
-
conditions.append(f"🌡️ It's hot ({temp}°C) - stay hydrated")
|
116 |
-
|
117 |
-
if conditions:
|
118 |
-
return f"Biking conditions in {city}:\n" + "\n".join(conditions)
|
119 |
-
else:
|
120 |
-
return f"👍 Great conditions for biking in {city}! {temp}°C with moderate wind speeds."
|
121 |
-
|
122 |
-
else:
|
123 |
-
return f"Error checking conditions. Status code: {response.status_code}"
|
124 |
-
|
125 |
-
except Exception as e:
|
126 |
-
return f"Error checking biking conditions: {str(e)}"
|
127 |
|
128 |
-
#
|
129 |
-
|
130 |
-
# Dynamic trail generation based on city characteristics
|
131 |
-
"default_trails": [
|
132 |
-
{
|
133 |
-
"name": "{city} Riverside Path",
|
134 |
-
"difficulty": "Easy",
|
135 |
-
"length": "5.5 miles",
|
136 |
-
"description": "Scenic waterfront trail perfect for casual rides",
|
137 |
-
"features": ["Waterfront views", "Paved path", "Family-friendly"]
|
138 |
-
},
|
139 |
-
{
|
140 |
-
"name": "{city} Urban Loop",
|
141 |
-
"difficulty": "Moderate",
|
142 |
-
"length": "8.2 miles",
|
143 |
-
"description": "Popular city loop with diverse urban scenery",
|
144 |
-
"features": ["City views", "Mixed terrain", "Cultural spots"]
|
145 |
-
}
|
146 |
-
]
|
147 |
-
}
|
148 |
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
Returns:
|
157 |
-
str: Information about available bike trails
|
158 |
-
"""
|
159 |
-
# Generate dynamic trails for any city
|
160 |
-
trails = []
|
161 |
-
for trail_template in MOCK_TRAILS["default_trails"]:
|
162 |
-
trail = {
|
163 |
-
"name": trail_template["name"].format(city=city),
|
164 |
-
"difficulty": trail_template["difficulty"],
|
165 |
-
"length": trail_template["length"],
|
166 |
-
"description": trail_template["description"],
|
167 |
-
"features": trail_template["features"]
|
168 |
-
}
|
169 |
-
trails.append(trail)
|
170 |
-
|
171 |
-
response = f"🚲 Suggested Bike Trails in {city}:\n\n"
|
172 |
-
|
173 |
-
for trail in trails:
|
174 |
-
response += (
|
175 |
-
f"📍 {trail['name']}\n"
|
176 |
-
f" • Difficulty: {trail['difficulty']}\n"
|
177 |
-
f" • Length: {trail['length']}\n"
|
178 |
-
f" • Description: {trail['description']}\n"
|
179 |
-
f" • Features: {', '.join(trail['features'])}\n\n"
|
180 |
-
)
|
181 |
-
|
182 |
-
response += "(Note: These are suggested routes based on typical city features. Always verify local trails and conditions.)"
|
183 |
-
|
184 |
-
return response
|
185 |
|
186 |
-
#
|
|
|
|
|
|
|
187 |
with open("prompts.yaml", 'r') as stream:
|
188 |
prompt_templates = yaml.safe_load(stream)
|
189 |
|
|
|
190 |
agent = CodeAgent(
|
191 |
-
model=model,
|
192 |
tools=[
|
193 |
final_answer,
|
194 |
get_weather,
|
|
|
10 |
def get_coordinates(city: str) -> Optional[Tuple[float, float]]:
|
11 |
"""Get coordinates for any city using Open-Meteo Geocoding API."""
|
12 |
try:
|
|
|
13 |
geocoding_url = f"https://geocoding-api.open-meteo.com/v1/search?name={city}&count=1&language=en&format=json"
|
14 |
response = requests.get(geocoding_url, timeout=10)
|
15 |
|
|
|
33 |
str: Weather information including temperature and conditions
|
34 |
"""
|
35 |
try:
|
|
|
36 |
coords = get_coordinates(city)
|
37 |
if not coords:
|
38 |
return f"Sorry, couldn't find coordinates for {city}. Please check the city name and try again."
|
39 |
|
40 |
lat, lon = coords
|
41 |
|
|
|
42 |
api_url = "https://api.open-meteo.com/v1/forecast"
|
43 |
params = {
|
44 |
"latitude": lat,
|
|
|
68 |
except Exception as e:
|
69 |
return f"Error fetching weather data: {str(e)}"
|
70 |
|
71 |
+
[... previous tool definitions remain the same ...]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
|
73 |
+
# Initialize final answer tool
|
74 |
+
final_answer = FinalAnswerTool()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
|
76 |
+
# Initialize the model properly
|
77 |
+
model = HfApiModel(
|
78 |
+
max_tokens=2096,
|
79 |
+
temperature=0.5,
|
80 |
+
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
|
81 |
+
custom_role_conversions=None,
|
82 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
|
84 |
+
# Import tool from Hub if needed
|
85 |
+
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
86 |
+
|
87 |
+
# Load prompt templates
|
88 |
with open("prompts.yaml", 'r') as stream:
|
89 |
prompt_templates = yaml.safe_load(stream)
|
90 |
|
91 |
+
# Initialize agent with all tools
|
92 |
agent = CodeAgent(
|
93 |
+
model=model, # Now model is properly defined
|
94 |
tools=[
|
95 |
final_answer,
|
96 |
get_weather,
|