File size: 1,895 Bytes
87af1bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import pickle

def load_model(filename):
    with open(filename, 'rb') as f:
        return pickle.load(f)

# Load the model
loaded_model = load_model('recommendation_model.pkl')

def get_recommendations(input_categories):
    tfidf_vectorizer = loaded_model['tfidf_vectorizer']
    knn_classifier = loaded_model['knn_classifier']
    df = loaded_model['df']

    all_predictions = []

    for category in input_categories:
        category_tfidf = tfidf_vectorizer.transform([category])
        top_20_indices = knn_classifier.kneighbors(category_tfidf, n_neighbors=20, return_distance=False)[0]
        top_20_places = df.iloc[top_20_indices]
        best_2_places = top_20_places.sort_values('normalized_score', ascending=False).head(2)
        
        all_predictions.append({
            'category': category,
            'predictions': best_2_places[['name', 'rating', 'user_ratings_total', 'normalized_score']].to_dict('records')
        })

    return all_predictions

# Streamlit app
st.title("Place Recommendation System")

st.write("Enter the categories you are interested in (comma-separated):")
input_categories = st.text_input("Categories", "historical monuments, history tours, wildlife")

if st.button("Get Recommendations"):
    categories_list = [category.strip() for category in input_categories.split(',')]
    recommendations = get_recommendations(categories_list)

    for prediction in recommendations:
        st.subheader(f"Top 2 recommended places for '{prediction['category']}':")
        for place in prediction['predictions']:
            st.write(f"**Name:** {place['name']}")
            st.write(f"**Rating:** {place['rating']:.2f}")
            st.write(f"**Review Count:** {place['user_ratings_total']}")
            st.write(f"**Score:** {place['normalized_score']:.4f}")
            st.write("")  # Add a blank line for better spacing