Spaces:
Sleeping
Sleeping
Abdulla Fahem
commited on
Commit
Β·
9304981
1
Parent(s):
961d2c0
Update application file
Browse files
app.py
CHANGED
@@ -77,23 +77,23 @@ def create_sample_data():
|
|
77 |
# Activity templates for different interests
|
78 |
activities = {
|
79 |
'Culture': ['Visit historical sites', 'Explore local traditions', 'Attend cultural events',
|
80 |
-
|
81 |
'History': ['Tour ancient ruins', 'Visit museums', 'Explore historic districts',
|
82 |
-
|
83 |
'Food': ['Try local cuisine', 'Join cooking classes', 'Visit food markets',
|
84 |
-
|
85 |
'Shopping': ['Browse local markets', 'Visit shopping districts', 'Shop at boutiques',
|
86 |
-
|
87 |
'Art': ['Visit art galleries', 'Attend art exhibitions', 'Join art workshops',
|
88 |
'Visit artist studios', 'Explore street art'],
|
89 |
'Museums': ['Tour famous museums', 'Visit specialty museums', 'Join museum tours',
|
90 |
-
|
91 |
'Nature': ['Visit parks', 'Nature walks', 'Explore gardens', 'Visit natural landmarks',
|
92 |
-
|
93 |
'Adventure': ['Join adventure tours', 'Try outdoor sports', 'Explore hidden spots',
|
94 |
-
|
95 |
'Relaxation': ['Spa treatments', 'Visit peaceful gardens', 'Leisure activities',
|
96 |
-
|
97 |
}
|
98 |
|
99 |
def generate_daily_plan(day, total_days, interests, budget_level, destination):
|
@@ -140,21 +140,23 @@ def create_sample_data():
|
|
140 |
|
141 |
@st.cache_resource
|
142 |
def load_or_train_model():
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
|
|
|
|
158 |
|
159 |
def train_model():
|
160 |
"""Train the T5 model on travel planning data"""
|
@@ -356,20 +358,23 @@ def format_travel_plan(plan, days):
|
|
356 |
|
357 |
# Extract day number
|
358 |
try:
|
359 |
-
|
|
|
360 |
current_day = day_num
|
361 |
current_activities = []
|
362 |
-
|
363 |
-
if
|
364 |
-
|
365 |
-
|
366 |
-
|
|
|
|
|
367 |
except ValueError:
|
368 |
continue
|
369 |
-
|
370 |
-
#
|
371 |
-
|
372 |
-
|
373 |
|
374 |
# Add the last day if there are pending activities
|
375 |
if current_day is not None and current_activities:
|
@@ -386,15 +391,24 @@ def format_travel_plan(plan, days):
|
|
386 |
# Find the matching day's activities or use a placeholder
|
387 |
day_content = next(
|
388 |
(section for section in day_sections if section['day'] == day),
|
389 |
-
{'day': day, 'activities': [
|
390 |
)
|
391 |
|
392 |
# Format the day's content with markdown
|
393 |
formatted_plan.append(f"### Day {day}\n")
|
394 |
|
395 |
-
#
|
396 |
-
|
397 |
-
formatted_plan.append(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
398 |
|
399 |
# Add spacing between days
|
400 |
formatted_plan.append("\n")
|
@@ -436,13 +450,13 @@ def main():
|
|
436 |
|
437 |
# Load or train model
|
438 |
if 'model' not in st.session_state:
|
439 |
-
|
440 |
-
|
441 |
-
|
442 |
-
|
443 |
-
|
444 |
-
|
445 |
-
|
446 |
|
447 |
# Create two columns for input form
|
448 |
col1, col2 = st.columns([2, 1])
|
@@ -483,7 +497,7 @@ def main():
|
|
483 |
interests = st.multiselect(
|
484 |
"π― Interests",
|
485 |
["Culture", "History", "Food", "Nature", "Shopping",
|
486 |
-
|
487 |
["Culture", "Food"],
|
488 |
help="Select up to three interests to personalize your plan"
|
489 |
)
|
|
|
77 |
# Activity templates for different interests
|
78 |
activities = {
|
79 |
'Culture': ['Visit historical sites', 'Explore local traditions', 'Attend cultural events',
|
80 |
+
'Visit ancient monuments', 'Experience local festivals'],
|
81 |
'History': ['Tour ancient ruins', 'Visit museums', 'Explore historic districts',
|
82 |
+
'Join guided history walks', 'Visit heritage sites'],
|
83 |
'Food': ['Try local cuisine', 'Join cooking classes', 'Visit food markets',
|
84 |
+
'Dine at famous restaurants', 'Food tasting tours'],
|
85 |
'Shopping': ['Browse local markets', 'Visit shopping districts', 'Shop at boutiques',
|
86 |
+
'Explore artisan shops', 'Visit shopping centers'],
|
87 |
'Art': ['Visit art galleries', 'Attend art exhibitions', 'Join art workshops',
|
88 |
'Visit artist studios', 'Explore street art'],
|
89 |
'Museums': ['Tour famous museums', 'Visit specialty museums', 'Join museum tours',
|
90 |
+
'Explore art collections', 'Visit cultural institutes'],
|
91 |
'Nature': ['Visit parks', 'Nature walks', 'Explore gardens', 'Visit natural landmarks',
|
92 |
+
'Outdoor activities'],
|
93 |
'Adventure': ['Join adventure tours', 'Try outdoor sports', 'Explore hidden spots',
|
94 |
+
'Take scenic hikes', 'Adventure activities'],
|
95 |
'Relaxation': ['Spa treatments', 'Visit peaceful gardens', 'Leisure activities',
|
96 |
+
'Relaxing sightseeing', 'Peaceful excursions']
|
97 |
}
|
98 |
|
99 |
def generate_daily_plan(day, total_days, interests, budget_level, destination):
|
|
|
140 |
|
141 |
@st.cache_resource
|
142 |
def load_or_train_model():
|
143 |
+
"""Load trained model or train new one"""
|
144 |
+
model_path = "trained_travel_planner"
|
145 |
+
|
146 |
+
if os.path.exists(model_path):
|
147 |
+
try:
|
148 |
+
model = T5ForConditionalGeneration.from_pretrained(model_path)
|
149 |
+
tokenizer = T5Tokenizer.from_pretrained(model_path)
|
150 |
+
if torch.cuda.is_available():
|
151 |
+
model = model.cuda()
|
152 |
+
st.success("β Loaded existing model")
|
153 |
+
return model, tokenizer
|
154 |
+
except Exception as e:
|
155 |
+
st.warning("Could not load existing model, will train new one")
|
156 |
+
st.error(f"Error loading trained model: {str(e)}")
|
157 |
+
|
158 |
+
# If no trained model exists or loading fails, train new model
|
159 |
+
return train_model()
|
160 |
|
161 |
def train_model():
|
162 |
"""Train the T5 model on travel planning data"""
|
|
|
358 |
|
359 |
# Extract day number
|
360 |
try:
|
361 |
+
day_part = line.split(':', 1)
|
362 |
+
day_num = int(''.join(filter(str.isdigit, day_part[0])))
|
363 |
current_day = day_num
|
364 |
current_activities = []
|
365 |
+
|
366 |
+
# Process content after the day header if it exists
|
367 |
+
if len(day_part) > 1:
|
368 |
+
# Split activities by periods and filter out empty ones
|
369 |
+
activities = [act.strip() for act in day_part[1].split('.') if act.strip()]
|
370 |
+
current_activities.extend(activities)
|
371 |
+
|
372 |
except ValueError:
|
373 |
continue
|
374 |
+
elif current_day is not None:
|
375 |
+
# Split the line by periods to separate activities
|
376 |
+
activities = [act.strip() for act in line.split('.') if act.strip()]
|
377 |
+
current_activities.extend(activities)
|
378 |
|
379 |
# Add the last day if there are pending activities
|
380 |
if current_day is not None and current_activities:
|
|
|
391 |
# Find the matching day's activities or use a placeholder
|
392 |
day_content = next(
|
393 |
(section for section in day_sections if section['day'] == day),
|
394 |
+
{'day': day, 'activities': []}
|
395 |
)
|
396 |
|
397 |
# Format the day's content with markdown
|
398 |
formatted_plan.append(f"### Day {day}\n")
|
399 |
|
400 |
+
# If no activities found for this day, add default activity
|
401 |
+
if not day_content['activities']:
|
402 |
+
formatted_plan.append("- Explore local attractions and sights")
|
403 |
+
else:
|
404 |
+
# Format each activity with bullet points
|
405 |
+
for activity in day_content['activities']:
|
406 |
+
# Clean up the activity text
|
407 |
+
activity = activity.strip()
|
408 |
+
if activity.lower().startswith('optional'):
|
409 |
+
formatted_plan.append(f"- *{activity}*")
|
410 |
+
else:
|
411 |
+
formatted_plan.append(f"- {activity}")
|
412 |
|
413 |
# Add spacing between days
|
414 |
formatted_plan.append("\n")
|
|
|
450 |
|
451 |
# Load or train model
|
452 |
if 'model' not in st.session_state:
|
453 |
+
with st.spinner("Loading AI model... Please wait..."):
|
454 |
+
model, tokenizer = load_or_train_model()
|
455 |
+
if model is None or tokenizer is None:
|
456 |
+
st.error("Failed to load/train the AI model. Please try again.")
|
457 |
+
return
|
458 |
+
st.session_state.model = model
|
459 |
+
st.session_state.tokenizer = tokenizer
|
460 |
|
461 |
# Create two columns for input form
|
462 |
col1, col2 = st.columns([2, 1])
|
|
|
497 |
interests = st.multiselect(
|
498 |
"π― Interests",
|
499 |
["Culture", "History", "Food", "Nature", "Shopping",
|
500 |
+
"Adventure", "Relaxation", "Art", "Museums"],
|
501 |
["Culture", "Food"],
|
502 |
help="Select up to three interests to personalize your plan"
|
503 |
)
|