Spaces:
Sleeping
Sleeping
Abdulla Fahem
commited on
Commit
·
ce5a0ef
1
Parent(s):
e6f05dd
Add application file
Browse files
app.py
CHANGED
@@ -262,8 +262,7 @@ def generate_travel_plan(destination, days, interests, budget, model, tokenizer)
|
|
262 |
top_k=50,
|
263 |
top_p=0.9,
|
264 |
do_sample=True,
|
265 |
-
repetition_penalty=1.2
|
266 |
-
num_return_sequences=1
|
267 |
)
|
268 |
|
269 |
# Decode output
|
@@ -272,76 +271,11 @@ def generate_travel_plan(destination, days, interests, budget, model, tokenizer)
|
|
272 |
# Handle empty output
|
273 |
if not travel_plan.strip():
|
274 |
raise ValueError("Generated plan is empty")
|
275 |
-
|
276 |
-
# Ensure the plan has the correct number of days
|
277 |
-
plan_lines = travel_plan.split('\n')
|
278 |
-
formatted_lines = []
|
279 |
-
activity_templates = {
|
280 |
-
'Budget': [
|
281 |
-
"Explore local free attractions",
|
282 |
-
"Visit public parks and gardens",
|
283 |
-
"Take self-guided walking tours",
|
284 |
-
"Visit markets and street food venues",
|
285 |
-
"Use public transportation"
|
286 |
-
],
|
287 |
-
'Moderate': [
|
288 |
-
"Join group tours and activities",
|
289 |
-
"Visit popular attractions",
|
290 |
-
"Try local restaurants",
|
291 |
-
"Use mix of public and private transport",
|
292 |
-
"Book mid-range accommodations"
|
293 |
-
],
|
294 |
-
'Luxury': [
|
295 |
-
"Book private guided tours",
|
296 |
-
"Experience fine dining",
|
297 |
-
"Visit exclusive attractions",
|
298 |
-
"Use private transportation",
|
299 |
-
"Stay at luxury accommodations"
|
300 |
-
]
|
301 |
-
}
|
302 |
-
|
303 |
-
# Ensure we have enough content for each day
|
304 |
-
for day in range(1, days + 1):
|
305 |
-
day_content = next(
|
306 |
-
(line for line in plan_lines if f"Day {day}:" in line),
|
307 |
-
None
|
308 |
-
)
|
309 |
|
310 |
-
|
311 |
-
|
312 |
-
else:
|
313 |
-
# Generate fallback content based on budget and interests
|
314 |
-
budget_activities = activity_templates[budget]
|
315 |
-
interests_activities = [
|
316 |
-
f"Explore {destination}'s {interest.lower()} attractions"
|
317 |
-
for interest in interests
|
318 |
-
]
|
319 |
-
activities = budget_activities + interests_activities
|
320 |
-
|
321 |
-
fallback_content = (
|
322 |
-
f"Day {day}: {random.choice(activities)}. "
|
323 |
-
f"Also {random.choice(activities).lower()}."
|
324 |
-
)
|
325 |
-
formatted_lines.append(fallback_content)
|
326 |
-
|
327 |
-
# Join the lines back together
|
328 |
-
final_plan = '\n'.join(formatted_lines)
|
329 |
-
|
330 |
-
# Add a trip overview at the beginning
|
331 |
-
overview = (
|
332 |
-
f"Trip Overview:\n"
|
333 |
-
f"Destination: {destination}\n"
|
334 |
-
f"Duration: {days} days\n"
|
335 |
-
f"Budget Level: {budget}\n"
|
336 |
-
f"Interests: {interests_str}\n\n"
|
337 |
-
)
|
338 |
-
|
339 |
-
final_plan = overview + final_plan
|
340 |
-
|
341 |
-
# Log successful generation
|
342 |
-
print(f"Successfully generated plan for {destination} ({days} days)")
|
343 |
|
344 |
-
return
|
345 |
|
346 |
except Exception as e:
|
347 |
error_msg = f"Error generating travel plan: {str(e)}"
|
@@ -353,6 +287,7 @@ def generate_travel_plan(destination, days, interests, budget, model, tokenizer)
|
|
353 |
|
354 |
def generate_fallback_plan(destination, days, interests, budget):
|
355 |
"""Generate a basic fallback plan if the model fails"""
|
|
|
356 |
fallback_plan = f"# Emergency Travel Plan for {destination}\n\n"
|
357 |
|
358 |
# Basic activity templates
|
@@ -368,51 +303,87 @@ def generate_fallback_plan(destination, days, interests, budget):
|
|
368 |
'Museums': ['Visit main museums', 'Join guided tours', 'See special exhibits']
|
369 |
}
|
370 |
|
|
|
371 |
for day in range(1, days + 1):
|
372 |
-
fallback_plan += f"\
|
|
|
373 |
# Select activities based on interests
|
374 |
day_activities = []
|
375 |
-
|
|
|
|
|
376 |
if interest in basic_activities:
|
377 |
activity = random.choice(basic_activities[interest])
|
378 |
day_activities.append(activity)
|
379 |
|
380 |
# Add budget-appropriate text
|
381 |
budget_text = {
|
382 |
-
'Budget': 'Focus on free and affordable activities
|
383 |
-
'Moderate': 'Mix of affordable and premium experiences
|
384 |
-
'Luxury': 'Premium experiences and exclusive access
|
385 |
}.get(budget, '')
|
386 |
|
|
|
387 |
fallback_plan += f"Morning: {day_activities[0] if day_activities else 'Explore the area'}\n"
|
388 |
if len(day_activities) > 1:
|
389 |
fallback_plan += f"Afternoon/Evening: {day_activities[1]}\n"
|
390 |
fallback_plan += f"Note: {budget_text}\n"
|
391 |
|
392 |
-
|
|
|
393 |
|
394 |
def format_travel_plan(plan, days):
|
395 |
-
"""Format the
|
396 |
-
formatted_plan =
|
|
|
|
|
397 |
|
398 |
-
#
|
399 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
400 |
|
401 |
-
#
|
402 |
-
|
|
|
403 |
|
404 |
-
#
|
405 |
-
|
406 |
-
|
407 |
-
|
408 |
-
|
409 |
-
|
410 |
-
|
411 |
-
|
412 |
-
|
413 |
-
|
414 |
|
415 |
-
|
|
|
416 |
|
417 |
def main():
|
418 |
st.set_page_config(
|
|
|
262 |
top_k=50,
|
263 |
top_p=0.9,
|
264 |
do_sample=True,
|
265 |
+
repetition_penalty=1.2 # Additional repetition avoidance
|
|
|
266 |
)
|
267 |
|
268 |
# Decode output
|
|
|
271 |
# Handle empty output
|
272 |
if not travel_plan.strip():
|
273 |
raise ValueError("Generated plan is empty")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
274 |
|
275 |
+
# Format the plan using the new formatting function
|
276 |
+
formatted_plan = format_travel_plan(travel_plan, days)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
277 |
|
278 |
+
return formatted_plan
|
279 |
|
280 |
except Exception as e:
|
281 |
error_msg = f"Error generating travel plan: {str(e)}"
|
|
|
287 |
|
288 |
def generate_fallback_plan(destination, days, interests, budget):
|
289 |
"""Generate a basic fallback plan if the model fails"""
|
290 |
+
# Start with the overview section
|
291 |
fallback_plan = f"# Emergency Travel Plan for {destination}\n\n"
|
292 |
|
293 |
# Basic activity templates
|
|
|
303 |
'Museums': ['Visit main museums', 'Join guided tours', 'See special exhibits']
|
304 |
}
|
305 |
|
306 |
+
# Generate exactly the requested number of days
|
307 |
for day in range(1, days + 1):
|
308 |
+
fallback_plan += f"\nDay {day}:\n"
|
309 |
+
|
310 |
# Select activities based on interests
|
311 |
day_activities = []
|
312 |
+
available_interests = interests[:2] # Use up to 2 interests per day
|
313 |
+
|
314 |
+
for interest in available_interests:
|
315 |
if interest in basic_activities:
|
316 |
activity = random.choice(basic_activities[interest])
|
317 |
day_activities.append(activity)
|
318 |
|
319 |
# Add budget-appropriate text
|
320 |
budget_text = {
|
321 |
+
'Budget': 'Focus on free and affordable activities',
|
322 |
+
'Moderate': 'Mix of affordable and premium experiences',
|
323 |
+
'Luxury': 'Premium experiences and exclusive access'
|
324 |
}.get(budget, '')
|
325 |
|
326 |
+
# Format the day's activities
|
327 |
fallback_plan += f"Morning: {day_activities[0] if day_activities else 'Explore the area'}\n"
|
328 |
if len(day_activities) > 1:
|
329 |
fallback_plan += f"Afternoon/Evening: {day_activities[1]}\n"
|
330 |
fallback_plan += f"Note: {budget_text}\n"
|
331 |
|
332 |
+
# Format the fallback plan using the same formatter
|
333 |
+
return format_travel_plan(fallback_plan, days)
|
334 |
|
335 |
def format_travel_plan(plan, days):
|
336 |
+
"""Format the travel plan to ensure exact number of days and proper formatting"""
|
337 |
+
formatted_plan = []
|
338 |
+
current_day = None
|
339 |
+
current_activities = []
|
340 |
|
341 |
+
# Process the plan line by line
|
342 |
+
for line in plan.split('\n'):
|
343 |
+
line = line.strip()
|
344 |
+
if not line:
|
345 |
+
continue
|
346 |
+
|
347 |
+
# Check for day headers
|
348 |
+
if line.lower().startswith('day'):
|
349 |
+
# Save previous day's activities if they exist
|
350 |
+
if current_day is not None and current_activities:
|
351 |
+
formatted_plan.append((current_day, '\n'.join(current_activities)))
|
352 |
+
|
353 |
+
# Extract day number
|
354 |
+
try:
|
355 |
+
day_num = int(''.join(filter(str.isdigit, line.split(':')[0])))
|
356 |
+
current_day = day_num
|
357 |
+
current_activities = []
|
358 |
+
# Add any activities that appear on the same line after the colon
|
359 |
+
if ':' in line:
|
360 |
+
activity = line.split(':', 1)[1].strip()
|
361 |
+
if activity:
|
362 |
+
current_activities.append(activity)
|
363 |
+
except ValueError:
|
364 |
+
continue
|
365 |
+
else:
|
366 |
+
# Add activity to current day
|
367 |
+
if current_day is not None:
|
368 |
+
current_activities.append(line)
|
369 |
|
370 |
+
# Add the last day if there are pending activities
|
371 |
+
if current_day is not None and current_activities:
|
372 |
+
formatted_plan.append((current_day, '\n'.join(current_activities)))
|
373 |
|
374 |
+
# Sort days and ensure we have exactly the requested number of days
|
375 |
+
formatted_plan.sort(key=lambda x: x[0])
|
376 |
+
final_plan = []
|
377 |
+
|
378 |
+
# Generate the final formatted plan with exactly the requested number of days
|
379 |
+
for day in range(1, days + 1):
|
380 |
+
# Find the matching day's activities or use a placeholder
|
381 |
+
day_content = next((content for d, content in formatted_plan if d == day),
|
382 |
+
"Explore local attractions and sights")
|
383 |
+
final_plan.append(f"Day {day}:\n{day_content}\n")
|
384 |
|
385 |
+
# Join all days with proper spacing
|
386 |
+
return '\n'.join(final_plan)
|
387 |
|
388 |
def main():
|
389 |
st.set_page_config(
|