Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
|
@@ -5,6 +5,8 @@ import faiss
|
|
| 5 |
import numpy as np
|
| 6 |
from transformers import pipeline
|
| 7 |
import time
|
|
|
|
|
|
|
| 8 |
|
| 9 |
# --- 1. DATA LOADING AND INITIALIZATION ---
|
| 10 |
print("===== Application Startup =====")
|
|
@@ -34,6 +36,54 @@ except RuntimeError:
|
|
| 34 |
|
| 35 |
|
| 36 |
# --- 3. SYNTHETIC GENERATION ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
def get_recommendations_and_generate(query_text, k=3):
|
| 39 |
# 1. Get Recommendations from existing data
|
|
@@ -87,9 +137,10 @@ def get_recommendations_and_generate(query_text, k=3):
|
|
| 87 |
# --- 4. GRADIO USER INTERFACE ---
|
| 88 |
def format_trip_plan(trip):
|
| 89 |
# Formats the recommended trips with markdown
|
| 90 |
-
if not trip:
|
| 91 |
return "### No similar trip plan found."
|
| 92 |
-
|
|
|
|
| 93 |
|
| 94 |
def format_generated_trip(trip_text):
|
| 95 |
return trip_text
|
|
|
|
| 5 |
import numpy as np
|
| 6 |
from transformers import pipeline
|
| 7 |
import time
|
| 8 |
+
import ast
|
| 9 |
+
import re
|
| 10 |
|
| 11 |
# --- 1. DATA LOADING AND INITIALIZATION ---
|
| 12 |
print("===== Application Startup =====")
|
|
|
|
| 36 |
|
| 37 |
|
| 38 |
# --- 3. SYNTHETIC GENERATION ---
|
| 39 |
+
def format_plan_details(plan_string):
|
| 40 |
+
"""
|
| 41 |
+
Parses and formats the raw plan string from the dataset into readable Markdown.
|
| 42 |
+
"""
|
| 43 |
+
# If the plan is not in the expected dictionary format, return it as is.
|
| 44 |
+
if not plan_string or not plan_string.strip().startswith('['):
|
| 45 |
+
return plan_string
|
| 46 |
+
|
| 47 |
+
try:
|
| 48 |
+
# Safely parse the string representation of a list of dictionaries
|
| 49 |
+
plan_list = ast.literal_eval(plan_string)
|
| 50 |
+
except (ValueError, SyntaxError):
|
| 51 |
+
# If parsing fails, return the original string to avoid crashing
|
| 52 |
+
return plan_string
|
| 53 |
+
|
| 54 |
+
formatted_sections = []
|
| 55 |
+
for section in plan_list:
|
| 56 |
+
description = section.get('Description', 'Details')
|
| 57 |
+
content = section.get('Content', '').strip()
|
| 58 |
+
|
| 59 |
+
# Add a bold title for each section
|
| 60 |
+
formatted_sections.append(f"#### {description}")
|
| 61 |
+
|
| 62 |
+
# Use specific formatting based on the section's description
|
| 63 |
+
if any(keyword in description for keyword in ['Attractions', 'Restaurants', 'Accommodations', 'Flight']):
|
| 64 |
+
lines = content.split('\n')
|
| 65 |
+
if lines:
|
| 66 |
+
# Make the header bold
|
| 67 |
+
formatted_sections.append(f"**{lines[0]}**")
|
| 68 |
+
# Format the rest of the lines as a clean, bulleted list
|
| 69 |
+
for item in lines[1:]:
|
| 70 |
+
clean_item = ' '.join(item.split()) # Remove extra whitespace
|
| 71 |
+
if clean_item:
|
| 72 |
+
formatted_sections.append(f"- {clean_item}")
|
| 73 |
+
|
| 74 |
+
elif 'Self-driving' in description or 'Taxi' in description:
|
| 75 |
+
# Make simple travel descriptions more readable
|
| 76 |
+
mode_emoji = "🚗" if 'Self-driving' in description else "🚕"
|
| 77 |
+
formatted_sections.append(f"- {mode_emoji} {content.replace(', ', ', ')}")
|
| 78 |
+
|
| 79 |
+
else:
|
| 80 |
+
# Default formatting for any other type of content
|
| 81 |
+
formatted_sections.append(content)
|
| 82 |
+
|
| 83 |
+
# Add a newline for spacing between sections
|
| 84 |
+
formatted_sections.append("")
|
| 85 |
+
|
| 86 |
+
return "\n".join(formatted_sections)
|
| 87 |
|
| 88 |
def get_recommendations_and_generate(query_text, k=3):
|
| 89 |
# 1. Get Recommendations from existing data
|
|
|
|
| 137 |
# --- 4. GRADIO USER INTERFACE ---
|
| 138 |
def format_trip_plan(trip):
|
| 139 |
# Formats the recommended trips with markdown
|
| 140 |
+
if not trip or 'reference_information' not in trip:
|
| 141 |
return "### No similar trip plan found."
|
| 142 |
+
formatted_plan = format_plan_details(trip['reference_information'])
|
| 143 |
+
return f"### {trip['days']}-days trip to {trip['dest'].upper()}\n**Suggested Plan:**\n{formatted_plan}"
|
| 144 |
|
| 145 |
def format_generated_trip(trip_text):
|
| 146 |
return trip_text
|