Spaces:
Sleeping
Sleeping
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() |