File size: 4,300 Bytes
5840396
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# -*- coding: utf-8 -*-
"""JourneyGenius.ipynb

Automatically generated by Colab.

Original file is located at
    https://colab.research.google.com/drive/1TX_o_0MEaHKPF8J0-L3FEqfqawGLP30J
"""

!pip install sentence-transformers
from sentence_transformers import SentenceTransformer, util

import ast
import pandas as pd
import seaborn as sns
!pip install geopy
!pip install streamlit

import pandas as pd
from sentence_transformers import SentenceTransformer

# Load the dataset
file_path = '/content/ML_proj_dataset_updated (1).csv'
df = pd.read_csv(file_path)

# Extract relevant columns in the order to be returned
relevant_columns = [
    'Primary',
    'per_person_price',
    'Topography',
    'Temprature',
    'Weather',
    'Mood',
    'package_name',
    'itinerary',
    'sightseeing_places_covered'
]
df_relevant = df[relevant_columns].dropna()

# Preprocess data
def preprocess_data(df):
    df['description'] = df.apply(lambda row: f"{row['Primary']} {row['Topography']} {row['Temprature']} {row['Weather']} {row['Mood']} {row['per_person_price']}", axis=1)
    return df

df_relevant = preprocess_data(df_relevant)

# Encode data
model = SentenceTransformer('all-MiniLM-L6-v2')
df_relevant['embedding'] = df_relevant['description'].apply(lambda x: model.encode(x, convert_to_tensor=True))

# Save embeddings to file
df_relevant.to_pickle('/content/df_with_embeddings.pkl')

import pandas as pd
from sentence_transformers import SentenceTransformer, util

# Load precomputed embeddings
df_with_embeddings = pd.read_pickle('/content/df_with_embeddings.pkl')

# User input function
def get_user_input():
    companions = input("Who are you traveling with (solo, couple, family): ").strip().lower()

    if companions == "solo":
        num_people = 1
    elif companions == "couple":
        num_people = 2
    elif companions == "family":
        num_people = int(input("Enter the number of people: "))
    else:
        print("Invalid input for companions. Please enter 'solo', 'couple', or 'family'.")
        return get_user_input()  # Recursively ask for input again

    budget = float(input("Enter your budget per person: "))
    days_of_lodging = int(input("Enter the number of days of lodging: "))
    preferred_weather = input("Enter preferred weather (Sunny, Rainy, Snowy): ").strip().capitalize()

    return budget, num_people, companions, days_of_lodging, preferred_weather

# Encode user input
model = SentenceTransformer('all-MiniLM-L6-v2')

def encode_user_input(user_input):
    user_description = f"budget {user_input[0]} companions {user_input[2]} days {user_input[3]} weather {user_input[4]}"
    return model.encode(user_description, convert_to_tensor=True)

# Recommend destinations
def recommend_destinations(user_input, df):
    user_embedding = encode_user_input(user_input)
    df['similarity'] = df['embedding'].apply(lambda x: util.pytorch_cos_sim(user_embedding, x).item())

    # Sort by similarity and drop duplicates based on 'Primary' column
    recommendations = df.sort_values(by='similarity', ascending=False).drop_duplicates(subset='Primary').head(5)

    return recommendations[['Primary', 'per_person_price', 'Topography', 'Temprature', 'Weather', 'Mood']]

# Display selected package details
def display_package_details(selection, df):
    selected_row = df.loc[df['Primary'] == selection]
    if not selected_row.empty:
        print("\nSelected Package Details:")
        print(f"Package Name: {selected_row['package_name'].values[0]}")
        print(f"Itinerary: {selected_row['itinerary'].values[0]}")
        print(f"Sightseeing Places Covered: {selected_row['sightseeing_places_covered'].values[0]}")
    else:
        print("Invalid selection. No package found.")

# Main function to run the recommendation system
def main():
    user_input = get_user_input()
    recommendations = recommend_destinations(user_input, df_with_embeddings)
    print("Top recommended destinations for you:")
    print(recommendations)

    # Let the user select a recommendation
    selected_primary = input("\nEnter the Primary name of the package you want to view details for: ").strip()
    display_package_details(selected_primary, df_with_embeddings)

# Run the main function
if __name__ == "__main__":
    main()

if __name__ == "__main__":
    main()