Spaces:
Runtime error
Runtime error
| # -*- 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() | |