Abdulla Fahem commited on
Commit
5da5110
Β·
1 Parent(s): dc8d270

Update application file

Browse files
Files changed (1) hide show
  1. app.py +91 -22
app.py CHANGED
@@ -275,7 +275,7 @@ def generate_travel_plan(destination, days, interests, budget, model, tokenizer)
275
  raise ValueError("Generated plan is empty")
276
 
277
  # Format the plan using the new formatting function
278
- formatted_plan = format_travel_plan(travel_plan, destination, days)
279
 
280
  return formatted_plan
281
 
@@ -284,10 +284,10 @@ def generate_travel_plan(destination, days, interests, budget, model, tokenizer)
284
  print(error_msg) # Log the error
285
 
286
  # Generate a basic fallback plan
287
- fallback_plan = generate_fallback_plan(destination, days, interests, budget)
288
  return fallback_plan
289
 
290
- def generate_fallback_plan(destination, days, interests, budget):
291
  """
292
  Generate a basic fallback travel plan if the model fails
293
 
@@ -318,8 +318,8 @@ def generate_fallback_plan(destination, days, interests, budget):
318
  f"# Travel Plan Details",
319
  f"## Destination: {destination}",
320
  f"## Duration: {days} days",
321
- f"## Interests: {', '.join(interests)}",
322
  f"## Budget: {budget}\n",
 
323
  "## Itinerary:\n"
324
  ]
325
 
@@ -350,42 +350,111 @@ def generate_fallback_plan(destination, days, interests, budget):
350
  # Create a travel plan string
351
  return "\n".join(travel_plan_lines)
352
 
353
- def format_travel_plan(travel_plan, destination, days, interests=None, budget=None):
354
  """
355
- Format travel plan string
356
 
357
  Parameters:
358
  - travel_plan: Generated travel plan string
359
  - destination: Target destination
360
  - days: Number of trip days
361
- - interests: Optional interests (default: None)
362
- - budget: Optional budget (default: None)
363
 
364
  Returns:
365
- Formatted travel plan as a string
366
  """
367
- # If the input is already a properly formatted string, return it
368
- if isinstance(travel_plan, str):
369
- return travel_plan
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
370
 
371
- # If it's a DataFrame or Series, extract the travel plan
372
  try:
373
- # Attempt to get the travel plan from DataFrame or Series
374
  if hasattr(travel_plan, 'iloc'):
375
- # If it's a DataFrame or Series, get the first row's travel plan
376
  travel_plan = travel_plan.iloc[0]['travel_plan']
377
  elif hasattr(travel_plan, 'get'):
378
- # If it's a dictionary-like object
379
  travel_plan = travel_plan.get('travel_plan', '')
380
  except Exception:
381
- # Fallback to generating a new plan if extraction fails
382
- return generate_fallback_plan(destination, days, interests or [], budget or 'Moderate')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
383
 
384
- # Ensure travel_plan is a string
385
- if not isinstance(travel_plan, str):
386
- return generate_fallback_plan(destination, days, interests or [], budget or 'Moderate')
387
 
388
- return travel_plan
389
 
390
 
391
  def main():
 
275
  raise ValueError("Generated plan is empty")
276
 
277
  # Format the plan using the new formatting function
278
+ formatted_plan = format_travel_plan(travel_plan, destination, days, budget, interests)
279
 
280
  return formatted_plan
281
 
 
284
  print(error_msg) # Log the error
285
 
286
  # Generate a basic fallback plan
287
+ fallback_plan = generate_fallback_plan(destination, days, budget, interests)
288
  return fallback_plan
289
 
290
+ def generate_fallback_plan(destination, days, budget, interests):
291
  """
292
  Generate a basic fallback travel plan if the model fails
293
 
 
318
  f"# Travel Plan Details",
319
  f"## Destination: {destination}",
320
  f"## Duration: {days} days",
 
321
  f"## Budget: {budget}\n",
322
+ f"## Interests: {', '.join(interests)}",
323
  "## Itinerary:\n"
324
  ]
325
 
 
350
  # Create a travel plan string
351
  return "\n".join(travel_plan_lines)
352
 
353
+ def format_travel_plan(travel_plan, destination, days, interests, budget):
354
  """
355
+ Comprehensively format travel plan with enhanced customization
356
 
357
  Parameters:
358
  - travel_plan: Generated travel plan string
359
  - destination: Target destination
360
  - days: Number of trip days
361
+ - interests: List of user's interests
362
+ - budget: Budget level for the trip
363
 
364
  Returns:
365
+ Formatted and customized travel plan
366
  """
367
+ # Validate inputs
368
+ if not destination or not isinstance(destination, str):
369
+ raise ValueError("Invalid destination")
370
+
371
+ if not interests or not isinstance(interests, list):
372
+ raise ValueError("Interests must be a non-empty list")
373
+
374
+ if not budget or budget not in ["Budget", "Moderate", "Luxury"]:
375
+ budget = "Moderate" # Default to moderate if invalid
376
+
377
+ # Ensure days is within reasonable range
378
+ days = max(1, min(14, int(days)))
379
+
380
+ # Prepare the header with key trip details
381
+ formatted_lines = [
382
+ f"# 🌍 Travel Plan: {destination}",
383
+ f"## Trip Overview",
384
+ f"- **Destination:** {destination}",
385
+ f"- **Duration:** {days} {'day' if days == 1 else 'days'}",
386
+ f"- **Budget Level:** {budget}",
387
+ f"- **Interests:** {', '.join(interests)}",
388
+ "\n## Detailed Itinerary\n"
389
+ ]
390
 
391
+ # Try to parse the original travel plan
392
  try:
393
+ # If it's a DataFrame or Series, extract travel plan
394
  if hasattr(travel_plan, 'iloc'):
 
395
  travel_plan = travel_plan.iloc[0]['travel_plan']
396
  elif hasattr(travel_plan, 'get'):
 
397
  travel_plan = travel_plan.get('travel_plan', '')
398
  except Exception:
399
+ travel_plan = ""
400
+
401
+ # If no travel plan or empty, return a simple message
402
+ if not travel_plan or not isinstance(travel_plan, str):
403
+ return "\n".join(formatted_lines + [
404
+ "### 🚧 Plan Generation Unavailable",
405
+ "",
406
+ "Unfortunately, we couldn't generate a detailed travel plan at this moment. ",
407
+ "This could be due to temporary model limitations or processing issues.",
408
+ "",
409
+ "**Recommendations:**",
410
+ "- Try regenerating the plan",
411
+ "- Check your internet connection",
412
+ "- Verify the destination and interests",
413
+ "- Contact support if the issue persists"
414
+ ])
415
+
416
+ # Combine header and travel plan
417
+ formatted_lines.append(travel_plan)
418
+
419
+ # Final formatting and return
420
+ return "\n".join(formatted_lines)
421
+
422
+ # def format_travel_plan(travel_plan, destination, days, interests, budget):
423
+ # """
424
+ # Format travel plan string
425
+
426
+ # Parameters:
427
+ # - travel_plan: Generated travel plan string
428
+ # - destination: Target destination
429
+ # - days: Number of trip days
430
+ # - interests: Optional interests (default: None)
431
+ # - budget: Optional budget (default: None)
432
+
433
+ # Returns:
434
+ # Formatted travel plan as a string
435
+ # """
436
+ # # If the input is already a properly formatted string, return it
437
+ # if isinstance(travel_plan, str):
438
+ # return travel_plan
439
+
440
+ # # If it's a DataFrame or Series, extract the travel plan
441
+ # try:
442
+ # # Attempt to get the travel plan from DataFrame or Series
443
+ # if hasattr(travel_plan, 'iloc'):
444
+ # # If it's a DataFrame or Series, get the first row's travel plan
445
+ # travel_plan = travel_plan.iloc[0]['travel_plan']
446
+ # elif hasattr(travel_plan, 'get'):
447
+ # # If it's a dictionary-like object
448
+ # travel_plan = travel_plan.get('travel_plan', '')
449
+ # except Exception:
450
+ # # Fallback to generating a new plan if extraction fails
451
+ # return generate_fallback_plan(destination, days, interests or [], budget or 'Moderate')
452
 
453
+ # # Ensure travel_plan is a string
454
+ # if not isinstance(travel_plan, str):
455
+ # return generate_fallback_plan(destination, days, interests or [], budget or 'Moderate')
456
 
457
+ # return travel_plan
458
 
459
 
460
  def main():