goingnow / app.py
pleabargain's picture
Update app.py
a90761c verified
raw
history blame
8.13 kB
import datetime
from typing import Dict, List, Tuple
import random
class TravelPlanner:
def __init__(self):
self.accommodation_types = {
'luxury': (200, 500),
'mid_range': (100, 200),
'budget': (50, 100),
'hostel': (20, 50)
}
self.activity_costs = {
'sightseeing': (20, 50),
'museum': (15, 25),
'adventure_sport': (50, 200),
'local_tour': (30, 80),
'cultural_experience': (25, 60)
}
self.meal_costs = {
'luxury': (50, 100),
'mid_range': (20, 50),
'budget': (10, 20),
'street_food': (5, 10)
}
def generate_itinerary(self,
destination: str,
num_days: int,
budget_level: str) -> List[Dict]:
"""
Generate a daily itinerary based on destination and budget level
Args:
destination: Name of the destination
num_days: Number of days for the trip
budget_level: luxury/mid_range/budget/hostel
Returns:
List of daily activities and estimated times
"""
itinerary = []
activities = [
'Morning sightseeing',
'Museum visit',
'Local market tour',
'Cultural workshop',
'Historical site visit',
'Nature walk',
'Local neighborhood exploration',
'Evening entertainment'
]
for day in range(1, num_days + 1):
daily_schedule = {
'day': day,
'morning': random.choice(activities),
'afternoon': random.choice(activities),
'evening': random.choice(activities),
'accommodation': f"{budget_level} accommodation"
}
itinerary.append(daily_schedule)
return itinerary
def calculate_budget(self,
num_days: int,
budget_level: str,
num_people: int) -> Dict:
"""
Calculate estimated budget based on duration and comfort level
Args:
num_days: Number of days for the trip
budget_level: luxury/mid_range/budget/hostel
num_people: Number of travelers
Returns:
Dictionary with budget breakdown
"""
# Get cost ranges based on budget level
accommodation_range = self.accommodation_types[budget_level]
meal_range = self.meal_costs[budget_level]
# Calculate daily costs
daily_accommodation = random.uniform(*accommodation_range)
daily_meals = random.uniform(*meal_range) * 3 # 3 meals per day
daily_activities = random.uniform(30, 100) # Average activity cost
daily_transport = random.uniform(10, 50) # Local transport
# Calculate total costs
total_accommodation = daily_accommodation * num_days
total_meals = daily_meals * num_days
total_activities = daily_activities * num_days
total_transport = daily_transport * num_days
# Multiply by number of people (except accommodation which is per room)
total_meals *= num_people
total_activities *= num_people
total_transport *= num_people
# Add 10% buffer for miscellaneous expenses
buffer = (total_accommodation + total_meals + total_activities + total_transport) * 0.1
total_cost = total_accommodation + total_meals + total_activities + total_transport + buffer
return {
'accommodation': round(total_accommodation, 2),
'meals': round(total_meals, 2),
'activities': round(total_activities, 2),
'transport': round(total_transport, 2),
'buffer': round(buffer, 2),
'total': round(total_cost, 2)
}
def format_output(self,
destination: str,
itinerary: List[Dict],
budget: Dict) -> str:
"""
Format the itinerary and budget into a readable string
Args:
destination: Name of the destination
itinerary: List of daily activities
budget: Dictionary of budget breakdown
Returns:
Formatted string with itinerary and budget
"""
output = f"Travel Plan for {destination}\n\n"
output += "=== ITINERARY ===\n\n"
for day in itinerary:
output += f"Day {day['day']}:\n"
output += f"Morning: {day['morning']}\n"
output += f"Afternoon: {day['afternoon']}\n"
output += f"Evening: {day['evening']}\n"
output += f"Accommodation: {day['accommodation']}\n\n"
output += "=== BUDGET BREAKDOWN ===\n\n"
output += f"Accommodation: ${budget['accommodation']}\n"
output += f"Meals: ${budget['meals']}\n"
output += f"Activities: ${budget['activities']}\n"
output += f"Local Transport: ${budget['transport']}\n"
output += f"Buffer (10%): ${budget['buffer']}\n"
output += f"Total Estimated Cost: ${budget['total']}\n"
return output
def generate_travel_plan(destination: str,
num_days: int,
budget_level: str,
num_people: int) -> str:
"""
Main function to generate complete travel plan
Args:
destination: Name of the destination
num_days: Number of days for the trip
budget_level: luxury/mid_range/budget/hostel
num_people: Number of travelers
Returns:
Formatted string with complete travel plan
"""
planner = TravelPlanner()
itinerary = planner.generate_itinerary(destination, num_days, budget_level)
budget = planner.calculate_budget(num_days, budget_level, num_people)
return planner.format_output(destination, itinerary, budget)
# Integration with your Gradio interface
def respond_with_travel_plan(message: str,
history: List[Tuple[str, str]],
system_message: str,
max_tokens: int,
temperature: float,
top_p: float) -> str:
"""
Modified respond function to handle travel planning requests
"""
# Parse the input message for travel parameters
# This is a simple example - you might want to add more sophisticated parsing
try:
# Example message format: "Plan a trip to Paris for 5 days, mid_range budget, 2 people"
parts = message.lower().split()
destination_idx = parts.index("to") + 1
days_idx = parts.index("days") - 1
budget_idx = parts.index("budget") - 1
people_idx = parts.index("people") - 1
destination = parts[destination_idx].capitalize()
num_days = int(parts[days_idx])
budget_level = parts[budget_idx]
num_people = int(parts[people_idx])
# Generate travel plan
response = generate_travel_plan(destination, num_days, budget_level, num_people)
return response
except (ValueError, IndexError):
return "I couldn't understand your travel request. Please use the format: 'Plan a trip to [destination] for [X] days, [budget_level] budget, [X] people'"
# Update your main Gradio interface
demo = gr.ChatInterface(
respond_with_travel_plan,
additional_inputs=[
gr.Textbox(value="You are a travel planning assistant.", label="System message"),
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
],
)