agarwalamit081 commited on
Commit
55fa624
·
verified ·
1 Parent(s): f3c126a

Update app.py

Browse files

Fixed DuckDuckGo import warning

Files changed (1) hide show
  1. app.py +48 -40
app.py CHANGED
@@ -3,13 +3,12 @@
3
  import os
4
  import re
5
  import requests
6
- from datetime import datetime
7
  from typing import Optional
8
- from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
9
  from Gradio_UI import GradioUI
10
 
11
  # ======================
12
- # TRAVEL TOOLS (SMOLAGENTS-COMPLIANT DOCSTRINGS)
13
  # ======================
14
 
15
  @tool
@@ -101,21 +100,6 @@ def get_time_difference(origin_city: str, destination_city: str) -> str:
101
  "shanghai": 8, "hong kong": 8, "manila": 8, "kuala lumpur": 8,
102
  "jakarta": 7, "delhi": 5.5, "rio de janeiro": -3, "sao paulo": -3,
103
  "buenos aires": -3, "mexico city": -6, "toronto": -5, "vancouver": -8,
104
- "montreal": -5, "miami": -5, "las vegas": -8, "san francisco": -8,
105
- "seattle": -8, "denver": -7, "dallas": -6, "houston": -6, "atlanta": -5,
106
- "boston": -5, "philadelphia": -5, "washington dc": -5, "orlando": -5,
107
- "phoenix": -7, "minneapolis": -6, "detroit": -5, "pittsburgh": -5,
108
- "cleveland": -5, "cincinnati": -5, "st louis": -6, "kansas city": -6,
109
- "new orleans": -6, "memphis": -6, "nashville": -6, "charlotte": -5,
110
- "raleigh": -5, "baltimore": -5, "milwaukee": -6, "indianapolis": -5,
111
- "louisville": -5, "portland": -8, "salt lake city": -7, "albuquerque": -7,
112
- "tucson": -7, "fresno": -8, "sacramento": -8, "oakland": -8, "san jose": -8,
113
- "las palmas": 0, "tenerife": 0, "lisbon": 0, "madrid": 1, "barcelona": 1,
114
- "valencia": 1, "seville": 1, "malaga": 1, "bilbao": 1, "zaragoza": 1,
115
- "granada": 1, "alicante": 1, "murcia": 1, "palma": 1, "ibiza": 1,
116
- "menorca": 1, "fuerteventura": 0, "lanzarote": 0, "tenerife": 0,
117
- "gran canaria": 0, "la palma": 0, "el hierro": 0, "la gomera": 0,
118
- "santa cruz de tenerife": 0, "las palmas de gran canaria": 0,
119
  }
120
 
121
  origin_key = origin_city.lower().strip()
@@ -224,6 +208,32 @@ def build_itinerary(destination: str, attractions: str, budget_local: float, day
224
  lines.append("\n💡 Pro tips: Book major attractions online in advance. Use public transport day passes for 30%+ savings.")
225
  return "\n".join(lines)
226
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
227
  @tool
228
  def assemble_catalogue(
229
  destination: str,
@@ -234,8 +244,7 @@ def assemble_catalogue(
234
  timezone_info: str,
235
  itinerary: str,
236
  packing_list: str,
237
- image_url_1: str,
238
- image_url_2: str
239
  ) -> str:
240
  """
241
  Compile all travel research into a beautiful, structured Markdown travel catalogue.
@@ -249,12 +258,20 @@ def assemble_catalogue(
249
  timezone_info: Time difference and jet lag guidance
250
  itinerary: Day-by-day schedule with time allocations
251
  packing_list: Complete customized packing checklist
252
- image_url_1: URL of first generated destination image (landmark)
253
- image_url_2: URL of second generated destination image (street scene)
254
 
255
  Returns:
256
  Complete Markdown-formatted travel catalogue with embedded images
257
  """
 
 
 
 
 
 
 
 
 
258
  return f"""# 🌍 {destination} Travel Catalogue
259
  *Planned from {origin} • {dates} • {budget_summary}*
260
 
@@ -284,11 +301,11 @@ def assemble_catalogue(
284
 
285
  <div style="display: flex; gap: 20px; margin: 20px 0;">
286
  <div style="flex: 1; text-align: center;">
287
- <img src="{image_url_1}" alt="{destination} landmark" style="max-width: 100%; border-radius: 8px; box-shadow: 0 4px 8px rgba(0,0,0,0.1);">
288
  <p><em>Iconic {destination} landmark</em></p>
289
  </div>
290
  <div style="flex: 1; text-align: center;">
291
- <img src="{image_url_2}" alt="{destination} street scene" style="max-width: 100%; border-radius: 8px; box-shadow: 0 4px 8px rgba(0,0,0,0.1);">
292
  <p><em>Vibrant local atmosphere</em></p>
293
  </div>
294
  </div>
@@ -310,18 +327,9 @@ def assemble_catalogue(
310
  if __name__ == "__main__":
311
  print("🚀 Initializing Travel Catalogue Creator...")
312
 
313
- # Load tools
314
  web_search = DuckDuckGoSearchTool()
315
 
316
- # Try to load image generation tool (fallback if unavailable)
317
- try:
318
- image_gen = load_tool("black-forest-labs/FLUX.1-schnell", trust_remote_code=True)
319
- except Exception:
320
- try:
321
- image_gen = load_tool("stabilityai/stable-diffusion-3-medium", trust_remote_code=True)
322
- except Exception as e:
323
- raise RuntimeError(f"Failed to load image generation tool: {e}")
324
-
325
  model = HfApiModel(
326
  max_tokens=2048,
327
  temperature=0.3,
@@ -359,14 +367,14 @@ You are TravelCatalogueCreator, an expert travel planner. ALWAYS follow this EXA
359
  - trip_days=[duration]
360
  - trip_type="city" (default unless beach/mountain specified)
361
 
362
- 7. IMAGES: CALL image_generation_tool EXACTLY TWICE:
363
- First call: "{destination} iconic landmark photorealistic travel photo, golden hour, 4k"
364
- Second call: "{destination} vibrant street scene with locals, authentic atmosphere, travel photography"
365
 
366
- 8. ASSEMBLE: Use assemble_catalogue with ALL previous outputs including BOTH image URLs
367
 
368
  CRITICAL RULES:
369
- • NEVER skip image generation (Step 7 is mandatory - 2 calls required)
370
  • NEVER call final_answer directly – always use assemble_catalogue first
371
  • If a tool fails, retry ONCE with simplified parameters before proceeding
372
  • Always extract concrete values (temps, rates, times) from tool outputs
@@ -378,12 +386,12 @@ CRITICAL RULES:
378
  model=model,
379
  tools=[
380
  web_search,
381
- image_gen,
382
  get_weather_forecast,
383
  convert_currency,
384
  get_time_difference,
385
  generate_packing_list,
386
  build_itinerary,
 
387
  assemble_catalogue,
388
  ],
389
  max_steps=25,
 
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
 
100
  "shanghai": 8, "hong kong": 8, "manila": 8, "kuala lumpur": 8,
101
  "jakarta": 7, "delhi": 5.5, "rio de janeiro": -3, "sao paulo": -3,
102
  "buenos aires": -3, "mexico city": -6, "toronto": -5, "vancouver": -8,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
103
  }
104
 
105
  origin_key = origin_city.lower().strip()
 
208
  lines.append("\n💡 Pro tips: Book major attractions online in advance. Use public transport day passes for 30%+ savings.")
209
  return "\n".join(lines)
210
 
211
+ @tool
212
+ def generate_travel_images(destination: str) -> str:
213
+ """
214
+ Generate two realistic placeholder image URLs for the destination using Unsplash API (no key required).
215
+
216
+ Args:
217
+ destination: Destination city name (e.g., "Barcelona")
218
+
219
+ Returns:
220
+ JSON-formatted string containing two image URLs: landmark and street scene
221
+ """
222
+ try:
223
+ # Clean destination name for URL
224
+ clean_dest = re.sub(r"[^a-zA-Z\s]", "", destination).strip().replace(" ", "%20")
225
+
226
+ # Landmark image
227
+ landmark_url = f"https://source.unsplash.com/800x600/?{clean_dest},landmark,architecture"
228
+
229
+ # Street scene image
230
+ street_url = f"https://source.unsplash.com/800x600/?{clean_dest},street,people,culture"
231
+
232
+ return f'{{"landmark_image": "{landmark_url}", "street_scene_image": "{street_url}"}}'
233
+ except Exception as e:
234
+ # Fallback generic travel images
235
+ return '{"landmark_image": "https://source.unsplash.com/800x600/?travel,landmark", "street_scene_image": "https://source.unsplash.com/800x600/?travel,street"}'
236
+
237
  @tool
238
  def assemble_catalogue(
239
  destination: str,
 
244
  timezone_info: str,
245
  itinerary: str,
246
  packing_list: str,
247
+ image_urls_json: str
 
248
  ) -> str:
249
  """
250
  Compile all travel research into a beautiful, structured Markdown travel catalogue.
 
258
  timezone_info: Time difference and jet lag guidance
259
  itinerary: Day-by-day schedule with time allocations
260
  packing_list: Complete customized packing checklist
261
+ image_urls_json: JSON string with "landmark_image" and "street_scene_image" URLs
 
262
 
263
  Returns:
264
  Complete Markdown-formatted travel catalogue with embedded images
265
  """
266
+ import json
267
+ try:
268
+ images = json.loads(image_urls_json)
269
+ img1 = images.get("landmark_image", "https://source.unsplash.com/800x600/?travel")
270
+ img2 = images.get("street_scene_image", "https://source.unsplash.com/800x600/?street")
271
+ except:
272
+ img1 = "https://source.unsplash.com/800x600/?travel,landmark"
273
+ img2 = "https://source.unsplash.com/800x600/?travel,street"
274
+
275
  return f"""# 🌍 {destination} Travel Catalogue
276
  *Planned from {origin} • {dates} • {budget_summary}*
277
 
 
301
 
302
  <div style="display: flex; gap: 20px; margin: 20px 0;">
303
  <div style="flex: 1; text-align: center;">
304
+ <img src="{img1}" alt="{destination} landmark" style="max-width: 100%; border-radius: 8px; box-shadow: 0 4px 8px rgba(0,0,0,0.1);">
305
  <p><em>Iconic {destination} landmark</em></p>
306
  </div>
307
  <div style="flex: 1; text-align: center;">
308
+ <img src="{img2}" alt="{destination} street scene" style="max-width: 100%; border-radius: 8px; box-shadow: 0 4px 8px rgba(0,0,0,0.1);">
309
  <p><em>Vibrant local atmosphere</em></p>
310
  </div>
311
  </div>
 
327
  if __name__ == "__main__":
328
  print("🚀 Initializing Travel Catalogue Creator...")
329
 
330
+ # Load tools (no external image gen tool needed - using custom generate_travel_images)
331
  web_search = DuckDuckGoSearchTool()
332
 
 
 
 
 
 
 
 
 
 
333
  model = HfApiModel(
334
  max_tokens=2048,
335
  temperature=0.3,
 
367
  - trip_days=[duration]
368
  - trip_type="city" (default unless beach/mountain specified)
369
 
370
+ 7. IMAGES: CALL generate_travel_images EXACTLY ONCE with {destination}
371
+ Returns JSON with two image URLs (landmark + street scene)
372
+ CRITICAL: You MUST use this tool - do not skip image generation
373
 
374
+ 8. ASSEMBLE: Use assemble_catalogue with ALL previous outputs including image URLs JSON
375
 
376
  CRITICAL RULES:
377
+ • NEVER skip image generation (Step 7 is mandatory)
378
  • NEVER call final_answer directly – always use assemble_catalogue first
379
  • If a tool fails, retry ONCE with simplified parameters before proceeding
380
  • Always extract concrete values (temps, rates, times) from tool outputs
 
386
  model=model,
387
  tools=[
388
  web_search,
 
389
  get_weather_forecast,
390
  convert_currency,
391
  get_time_difference,
392
  generate_packing_list,
393
  build_itinerary,
394
+ generate_travel_images, # Custom reliable image generator
395
  assemble_catalogue,
396
  ],
397
  max_steps=25,