TheHuriShow commited on
Commit
e00641a
·
verified ·
1 Parent(s): 9deea4e

Upload 4 files

Browse files
Files changed (5) hide show
  1. .gitattributes +1 -0
  2. README.md +7 -7
  3. app.py +144 -0
  4. requirements.txt +8 -0
  5. trip_index.faiss +3 -0
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ trip_index.faiss filter=lfs diff=lfs merge=lfs -text
README.md CHANGED
@@ -1,14 +1,14 @@
1
  ---
2
- title: TravelAdviser
3
- emoji: 😻
4
- colorFrom: purple
5
- colorTo: purple
6
  sdk: gradio
7
- sdk_version: 5.42.0
8
  app_file: app.py
9
  pinned: false
10
  license: mit
11
- short_description: help in travel planning
12
  ---
13
 
14
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: TripPlanner
3
+ emoji: ✈️
4
+ colorFrom: blue
5
+ colorTo: green
6
  sdk: gradio
7
+ sdk_version: 4.8.0
8
  app_file: app.py
9
  pinned: false
10
  license: mit
11
+ short_description: 'Your personal AI trip planner! ✈️'
12
  ---
13
 
14
+ An example trip recommendation chatbot using [Gradio](https://gradio.app), the `datasets` and `sentence-transformers` libraries, and FAISS for similarity search.
app.py ADDED
@@ -0,0 +1,144 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from datasets import load_dataset
3
+ from sentence_transformers import SentenceTransformer, util
4
+ 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 =====")
11
+ start_time = time.time()
12
+
13
+ # Load the travel dataset and limit to the first 20,000 rows (same as index)
14
+ print("Loading TravelPlanner dataset...")
15
+ dataset = load_dataset("osunlp/TravelPlanner", "test")
16
+ print("Dataset ready.")
17
+
18
+ # --- 2. EMBEDDING AND RECOMMENDATION ENGINE ---
19
+ print("Loading embedding model...")
20
+ model_name = "all-mpnet-base-v2"
21
+ embedding_model = SentenceTransformer(f"sentence-transformers/{model_name}")
22
+
23
+ index_file = "trip_index.faiss"
24
+
25
+ print(f"Loading FAISS index from {index_file}...")
26
+
27
+ try:
28
+ index = faiss.read_index(index_file)
29
+ print(f"Index is ready. Total vectors in index: {index.ntotal}")
30
+ except RuntimeError:
31
+ print(f"Error: FAISS index file '{index_file}' not found.")
32
+ print("Please run the `build_index.py` script first to create the index.")
33
+ exit()
34
+
35
+
36
+ # --- 3. SYNTHETIC GENERATION ---
37
+ print("Loading generative model...")
38
+ generator = pipeline('text-generation', model='gpt2')
39
+
40
+ def get_recommendations_and_generate(query_text, k=3):
41
+ # 1. Get Recommendations from existing data
42
+ query_vector = embedding_model.encode([query_text])
43
+ query_vector = np.array(query_vector, dtype=np.float32)
44
+ distances, indices = index.search(query_vector, k)
45
+
46
+ results = []
47
+ for idx_numpy in indices[0]:
48
+ idx = int(idx_numpy)
49
+ trip_plan = {
50
+ "title": dataset[idx]['title'],
51
+ "plan": dataset[idx]['plan']
52
+ }
53
+ results.append(trip_plan)
54
+
55
+ while len(results) < 3:
56
+ results.append({"title": "No trip plan found", "plan": ""})
57
+
58
+ # 2. Create a prompt for the generative model
59
+ prompt = f"Write a complete travel plan that includes a title and a day-by-day itinerary. The trip must be about: {query_text}."
60
+
61
+ # 3. Generate 10 new, creative trip ideas
62
+ print("Generating 10 synthetic trip ideas...")
63
+ generated_outputs = generator(
64
+ prompt,
65
+ max_new_tokens=250, # Increased tokens for more detailed plans
66
+ num_return_sequences=10,
67
+ pad_token_id=50256
68
+ )
69
+
70
+ # 4. Find the best trip out of the 10 generated
71
+ print("Finding the most relevant generated trip...")
72
+ generated_texts = [output['generated_text'].replace(prompt, "").strip() for output in generated_outputs]
73
+
74
+ # Embed all 10 generated texts
75
+ generated_embeddings = embedding_model.encode(generated_texts)
76
+
77
+ # Calculate cosine similarity between the user's query and each generated text
78
+ similarities = util.cos_sim(query_vector, generated_embeddings)
79
+
80
+ # Find the index of the most similar generated trip
81
+ best_recipe_index = np.argmax(similarities)
82
+ best_generated_trip = generated_texts[best_recipe_index]
83
+
84
+ return results[0], results[1], results[2], best_generated_trip
85
+
86
+ # --- 4. GRADIO USER INTERFACE ---
87
+ def format_trip_plan(trip):
88
+ # Formats the recommended trips with markdown
89
+ if not trip:
90
+ return "### No similar trip plan found."
91
+ return f"### {trip['day']}-days trip to {trip['dest'].upper()}\n**Suggested Plan:**\n{trip['reference_information']}"
92
+
93
+ def format_generated_trip(trip_text):
94
+ return trip_text
95
+
96
+ def trip_planner_wizard(destination, days):
97
+ # Combine user inputs into a single query for processing
98
+ days = int(days) # Ensure days is an integer for the f-string
99
+ query_text = f"a {days}-day trip to {destination}"
100
+ rec1, rec2, rec3, gen_rec_text = get_recommendations_and_generate(query_text)
101
+ return format_trip_plan(rec1), format_trip_plan(rec2), format_trip_plan(rec3), format_generated_trip(gen_rec_text)
102
+
103
+ end_time = time.time()
104
+ print(f"Models and data loaded in {end_time - start_time:.2f} seconds.")
105
+
106
+ # Gradio Interface
107
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
108
+ gr.Markdown("# ✈️ TripPlanner AI")
109
+ gr.Markdown("Enter your destination and desired trip length, and get plan recommendations plus a new AI-generated idea!")
110
+
111
+ with gr.Row():
112
+ destination_input = gr.Textbox(label="Destination", placeholder="e.g., Paris")
113
+ days_input = gr.Number(label="Number of Days", value=3)
114
+
115
+ with gr.Row():
116
+ submit_btn = gr.Button("Get Trip Plans", variant="primary")
117
+
118
+ with gr.Row():
119
+ with gr.Column(scale=2):
120
+ gr.Markdown("### Recommended Trip Plans from Dataset")
121
+ output_rec1 = gr.Markdown()
122
+ output_rec2 = gr.Markdown()
123
+ output_rec3 = gr.Markdown()
124
+ with gr.Column(scale=1):
125
+ gr.Markdown("### ✨ New AI-Generated Idea")
126
+ output_gen = gr.Textbox(label="AI Generated Trip Plan", lines=20, interactive=False)
127
+
128
+ submit_btn.click(
129
+ fn=trip_planner_wizard,
130
+ inputs=[destination_input, days_input],
131
+ outputs=[output_rec1, output_rec2, output_rec3, output_gen]
132
+ )
133
+
134
+ gr.Examples(
135
+ examples=[
136
+ ["Paris", 3],
137
+ ["Orlando", 7],
138
+ ["Tokyo", 5],
139
+ ["the Greek Islands", 10]
140
+ ],
141
+ inputs=[destination_input, days_input]
142
+ )
143
+
144
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ gradio==4.31.5
2
+ datasets==2.19.1
3
+ sentence-transformers==2.7.0
4
+ faiss-cpu==1.8.0
5
+ transformers==4.41.2
6
+ torch==2.3.1
7
+ pyarrow==16.1.0
8
+ huggingface-hub==0.23.3
trip_index.faiss ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:2c8d2a2eb720f78a9be02358e0d87e287766a257179553b784831ba7b207c875
3
+ size 3072045