Spaces:
Sleeping
Sleeping
Update app.py
Browse filesfixed agent setup
app.py
CHANGED
|
@@ -3,12 +3,13 @@
|
|
| 3 |
import os
|
| 4 |
import re
|
| 5 |
import requests
|
|
|
|
| 6 |
from typing import Optional
|
| 7 |
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, tool
|
| 8 |
from Gradio_UI import GradioUI
|
| 9 |
|
| 10 |
# ======================
|
| 11 |
-
# TRAVEL TOOLS (SMOLAGENTS-COMPLIANT)
|
| 12 |
# ======================
|
| 13 |
|
| 14 |
@tool
|
|
@@ -226,18 +227,11 @@ def generate_travel_images(destination: str) -> str:
|
|
| 226 |
JSON-formatted string containing two image URLs with keys "landmark_image" and "street_scene_image"
|
| 227 |
"""
|
| 228 |
try:
|
| 229 |
-
# Clean destination name for URL (remove special chars, keep spaces for Unsplash)
|
| 230 |
clean_dest = re.sub(r"[^a-zA-Z\s]", "", destination).strip()
|
| 231 |
-
|
| 232 |
-
# Landmark image - use landmark/architecture keywords
|
| 233 |
landmark_url = f"https://source.unsplash.com/800x600/?{clean_dest},landmark,architecture,travel"
|
| 234 |
-
|
| 235 |
-
# Street scene image - use street/people/culture keywords
|
| 236 |
street_url = f"https://source.unsplash.com/800x600/?{clean_dest},street,people,culture,travel"
|
| 237 |
-
|
| 238 |
return f'{{"landmark_image": "{landmark_url}", "street_scene_image": "{street_url}"}}'
|
| 239 |
except Exception:
|
| 240 |
-
# Fallback generic travel images with correct JSON keys
|
| 241 |
return '{"landmark_image": "https://source.unsplash.com/800x600/?europe,landmark,travel", "street_scene_image": "https://source.unsplash.com/800x600/?europe,street,people"}'
|
| 242 |
|
| 243 |
@tool
|
|
@@ -278,7 +272,6 @@ def assemble_catalogue(
|
|
| 278 |
img1 = "https://source.unsplash.com/800x600/?travel,landmark"
|
| 279 |
img2 = "https://source.unsplash.com/800x600/?travel,street"
|
| 280 |
|
| 281 |
-
# Count days from itinerary for header
|
| 282 |
import re
|
| 283 |
day_count = len(re.findall(r'DAY\s+\d+', itinerary))
|
| 284 |
|
|
@@ -324,19 +317,22 @@ def assemble_catalogue(
|
|
| 324 |
|
| 325 |
> 💡 **Travel Pro Tips**
|
| 326 |
> • Download offline Google Maps before departure
|
| 327 |
-
> • Learn 5 basic phrases in the local language
|
| 328 |
-
> • Keep digital copies of passport/insurance in cloud storage
|
| 329 |
> • Budget 10-15% extra for spontaneous experiences
|
| 330 |
-
> • Public transport passes often save 30%+ vs single tickets
|
| 331 |
-
> • Always carry small local currency for markets/tips
|
| 332 |
"""
|
| 333 |
|
| 334 |
# ======================
|
| 335 |
-
# AGENT SETUP
|
| 336 |
# ======================
|
| 337 |
if __name__ == "__main__":
|
| 338 |
print("🚀 Initializing Travel Catalogue Creator...")
|
| 339 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 340 |
# Load tools
|
| 341 |
web_search = DuckDuckGoSearchTool()
|
| 342 |
|
|
@@ -346,53 +342,6 @@ if __name__ == "__main__":
|
|
| 346 |
model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
|
| 347 |
)
|
| 348 |
|
| 349 |
-
# ENHANCED SYSTEM PROMPT: Prevent code generation, enforce pure tool usage
|
| 350 |
-
system_prompt = """
|
| 351 |
-
You are TravelCatalogueCreator, an expert travel planner. You MUST follow this EXACT 8-step workflow using ONLY tool calls - NEVER write Python code:
|
| 352 |
-
|
| 353 |
-
1. RESEARCH: Use DuckDuckGoSearchTool to find:
|
| 354 |
-
- Top 4-5 attractions in {destination}
|
| 355 |
-
- Local customs and etiquette tips
|
| 356 |
-
- Safety considerations
|
| 357 |
-
- Typical food/transport prices
|
| 358 |
-
|
| 359 |
-
2. WEATHER: Use get_weather_forecast with {destination} and {travel_dates}
|
| 360 |
-
→ Capture temperature range and precipitation likelihood
|
| 361 |
-
|
| 362 |
-
3. CURRENCY: Use convert_currency to convert {budget_amount} {home_currency} to {local_currency}
|
| 363 |
-
→ Note the exchange rate for daily budgeting
|
| 364 |
-
|
| 365 |
-
4. TIMEZONE: Use get_time_difference with {origin_city} and {destination}
|
| 366 |
-
→ Calculate jet lag impact
|
| 367 |
-
|
| 368 |
-
5. ITINERARY: Use build_itinerary with:
|
| 369 |
-
- destination={destination}
|
| 370 |
-
- attractions=[top attractions from Step 1]
|
| 371 |
-
- budget_local=[daily budget from Step 3]
|
| 372 |
-
- days=[trip duration]
|
| 373 |
-
|
| 374 |
-
6. PACKING: Use generate_packing_list with:
|
| 375 |
-
- destination={destination}
|
| 376 |
-
- weather_summary=[output from Step 2]
|
| 377 |
-
- trip_days=[duration]
|
| 378 |
-
- trip_type="city" (default unless beach/mountain specified)
|
| 379 |
-
|
| 380 |
-
7. IMAGES: CALL generate_travel_images EXACTLY ONCE with {destination}
|
| 381 |
-
→ Returns JSON with "landmark_image" and "street_scene_image" keys
|
| 382 |
-
→ CRITICAL: You MUST use this tool - do not skip image generation
|
| 383 |
-
|
| 384 |
-
8. ASSEMBLE: Use assemble_catalogue with ALL previous outputs including image URLs JSON
|
| 385 |
-
|
| 386 |
-
RULES YOU MUST FOLLOW:
|
| 387 |
-
• NEVER write Python code blocks - ONLY use tool calls
|
| 388 |
-
• NEVER call final_answer directly – always use assemble_catalogue first
|
| 389 |
-
• ALWAYS extract concrete values (temps, rates, times) from tool outputs before next step
|
| 390 |
-
• Budget must be converted to local currency BEFORE itinerary planning
|
| 391 |
-
• You MUST complete all 8 steps before finishing
|
| 392 |
-
• If a tool fails, retry ONCE with simplified parameters before proceeding
|
| 393 |
-
• Keep responses concise and focused on travel planning
|
| 394 |
-
"""
|
| 395 |
-
|
| 396 |
agent = CodeAgent(
|
| 397 |
model=model,
|
| 398 |
tools=[
|
|
@@ -402,14 +351,14 @@ RULES YOU MUST FOLLOW:
|
|
| 402 |
get_time_difference,
|
| 403 |
generate_packing_list,
|
| 404 |
build_itinerary,
|
| 405 |
-
generate_travel_images,
|
| 406 |
assemble_catalogue,
|
| 407 |
],
|
| 408 |
max_steps=25,
|
| 409 |
verbosity_level=1,
|
| 410 |
name="TravelCatalogueCreator",
|
| 411 |
description="Creates comprehensive, personalized travel catalogues with images",
|
| 412 |
-
prompt_templates=
|
| 413 |
)
|
| 414 |
|
| 415 |
# Launch UI
|
|
|
|
| 3 |
import os
|
| 4 |
import re
|
| 5 |
import requests
|
| 6 |
+
import yaml
|
| 7 |
from typing import Optional
|
| 8 |
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, tool
|
| 9 |
from Gradio_UI import GradioUI
|
| 10 |
|
| 11 |
# ======================
|
| 12 |
+
# TRAVEL TOOLS (SMOLAGENTS-COMPLIANT DOCSTRINGS)
|
| 13 |
# ======================
|
| 14 |
|
| 15 |
@tool
|
|
|
|
| 227 |
JSON-formatted string containing two image URLs with keys "landmark_image" and "street_scene_image"
|
| 228 |
"""
|
| 229 |
try:
|
|
|
|
| 230 |
clean_dest = re.sub(r"[^a-zA-Z\s]", "", destination).strip()
|
|
|
|
|
|
|
| 231 |
landmark_url = f"https://source.unsplash.com/800x600/?{clean_dest},landmark,architecture,travel"
|
|
|
|
|
|
|
| 232 |
street_url = f"https://source.unsplash.com/800x600/?{clean_dest},street,people,culture,travel"
|
|
|
|
| 233 |
return f'{{"landmark_image": "{landmark_url}", "street_scene_image": "{street_url}"}}'
|
| 234 |
except Exception:
|
|
|
|
| 235 |
return '{"landmark_image": "https://source.unsplash.com/800x600/?europe,landmark,travel", "street_scene_image": "https://source.unsplash.com/800x600/?europe,street,people"}'
|
| 236 |
|
| 237 |
@tool
|
|
|
|
| 272 |
img1 = "https://source.unsplash.com/800x600/?travel,landmark"
|
| 273 |
img2 = "https://source.unsplash.com/800x600/?travel,street"
|
| 274 |
|
|
|
|
| 275 |
import re
|
| 276 |
day_count = len(re.findall(r'DAY\s+\d+', itinerary))
|
| 277 |
|
|
|
|
| 317 |
|
| 318 |
> 💡 **Travel Pro Tips**
|
| 319 |
> • Download offline Google Maps before departure
|
| 320 |
+
> • Learn 5 basic phrases in the local language
|
| 321 |
+
> • Keep digital copies of passport/insurance in cloud storage
|
| 322 |
> • Budget 10-15% extra for spontaneous experiences
|
| 323 |
+
> • Public transport passes often save 30%+ vs single tickets
|
|
|
|
| 324 |
"""
|
| 325 |
|
| 326 |
# ======================
|
| 327 |
+
# AGENT SETUP (LOAD PROMPT FROM YAML)
|
| 328 |
# ======================
|
| 329 |
if __name__ == "__main__":
|
| 330 |
print("🚀 Initializing Travel Catalogue Creator...")
|
| 331 |
|
| 332 |
+
# Load system prompt from YAML
|
| 333 |
+
with open("prompts.yaml", "r") as f:
|
| 334 |
+
prompt_config = yaml.safe_load(f)
|
| 335 |
+
|
| 336 |
# Load tools
|
| 337 |
web_search = DuckDuckGoSearchTool()
|
| 338 |
|
|
|
|
| 342 |
model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
|
| 343 |
)
|
| 344 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 345 |
agent = CodeAgent(
|
| 346 |
model=model,
|
| 347 |
tools=[
|
|
|
|
| 351 |
get_time_difference,
|
| 352 |
generate_packing_list,
|
| 353 |
build_itinerary,
|
| 354 |
+
generate_travel_images,
|
| 355 |
assemble_catalogue,
|
| 356 |
],
|
| 357 |
max_steps=25,
|
| 358 |
verbosity_level=1,
|
| 359 |
name="TravelCatalogueCreator",
|
| 360 |
description="Creates comprehensive, personalized travel catalogues with images",
|
| 361 |
+
prompt_templates=prompt_config,
|
| 362 |
)
|
| 363 |
|
| 364 |
# Launch UI
|