File size: 1,056 Bytes
f52ee09
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import pandas as pd
import pickle
import joblib
kmeans = joblib.load('kmeans_model.joblib')

movies_pred = pd.read_csv("movies_nonnull.csv")
with open("movies_df.pkl", "rb") as f:
    movies_df = pickle.load(f)

with open("cosine.pkl", "rb") as f:
    cosine_sim = pickle.load(f)

def recommend_movies(name):

    try:
        
        idx = movies_pred[movies_pred['title'] == name].index[0]
    
        prediction = kmeans.predict(movies_df.iloc[idx,:-1].to_numpy().reshape(1,-1))
        ans = list(movies_pred[movies_df['KmeansCluster']==prediction[0]].index)
        scores=[]

        for i in ans:
            scores.append((i,cosine_sim.at[idx,i]))
        scores.sort(key = lambda x: x[1],reverse=True)
        final_ans = []
        for i in scores[:20]:
            final_ans.append(movies_pred.iloc[i[0]]['title'])
    
        return final_ans
    except Exception as e:
            return "Sorry Movie does not exist in the database"
iface = gr.Interface(fn=recommend_movies, inputs="text", outputs="text")
iface.launch()