Download app.py from Golyass/Recomender-System-Clustering-Based: direct link, hf CLI and curl.
- Browser
- Download file 3.52 kB
-
https://huggingface.co/spaces/Golyass/Recomender-System-Clustering-Based/resolve/main/app.py
- Command line
-
hf download hf://spaces/Golyass/Recomender-System-Clustering-Based/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Golyass/Recomender-System-Clustering-Based/resolve/main/app.py
3.52 kB
| import pandas as pd | |
| import numpy as np | |
| import random | |
| import sklearn | |
| from sklearn.cluster import KMeans | |
| from sklearn.preprocessing import scale, StandardScaler, normalize | |
| df2=pd.read_csv('movies_metadata.csv') | |
| data=df2[['revenue','runtime','vote_average','vote_count','title']] | |
| data=data.fillna(0) | |
| scaler = StandardScaler() | |
| data_scaled = scaler.fit_transform(data.iloc[:, :-1]) | |
| data=data.fillna(0) | |
| kmeans_mod = KMeans(n_clusters=5, n_init=10) | |
| kmeans_mod.fit(data_scaled) | |
| labels =kmeans_mod.fit_predict(data_scaled) | |
| data['cluster']=labels | |
| data=data[['revenue','runtime','vote_average','vote_count','cluster','title']] | |
| cdata=data[['cluster','title']] | |
| cluster0=[] | |
| cluster1=[] | |
| cluster2=[] | |
| cluster3=[] | |
| cluster4=[] | |
| filmname='' | |
| for i in cdata.iloc(): | |
| if i[0]==0: | |
| filmname='' | |
| filmname=i[1] | |
| #print(filmname) | |
| cluster0.append(filmname) | |
| elif i[0]==1: | |
| filmname='' | |
| filmname=i[1] | |
| cluster1.append(filmname) | |
| elif i[0]==2: | |
| filmname='' | |
| filmname=i[1] | |
| cluster2.append(filmname) | |
| if i[0]==3: | |
| filmname='' | |
| filmname=i[1] | |
| cluster3.append(filmname) | |
| if i[0]==4: | |
| filmname='' | |
| filmname=i[1] | |
| cluster4.append(filmname) | |
| data=data.iloc[:, :-1] | |
| cluster=[cluster0,cluster1,cluster2,cluster3,cluster4] | |
| def recomender(input1,input2,input3): | |
| encod_input1=[] | |
| encod_input2=[] | |
| encod_input3=[] | |
| encod_input1.append(cluster0.count(input1)) | |
| encod_input1.append(cluster1.count(input1)) | |
| encod_input1.append(cluster2.count(input1)) | |
| encod_input1.append(cluster3.count(input1)) | |
| encod_input1.append(cluster4.count(input1)) | |
| encod_input2.append(cluster0.count(input2)) | |
| encod_input2.append(cluster1.count(input2)) | |
| encod_input2.append(cluster2.count(input2)) | |
| encod_input2.append(cluster3.count(input2)) | |
| encod_input2.append(cluster4.count(input2)) | |
| encod_input3.append(cluster0.count(input3)) | |
| encod_input3.append(cluster1.count(input3)) | |
| encod_input3.append(cluster2.count(input3)) | |
| encod_input3.append(cluster3.count(input3)) | |
| encod_input3.append(cluster4.count(input3)) | |
| return encod_input1,encod_input2,encod_input3 | |
| def recomender2(a): | |
| s1=0 | |
| s2=0 | |
| s3=0 | |
| for i in range(0,5): | |
| if a[0][i]!=0: | |
| s1=i | |
| #print(f'The first film is in cluster {i+1}') | |
| if a[1][i]!=0: | |
| s2=i | |
| #print(f'The second film is in cluster {i+1}') | |
| if a[2][i]!=0: | |
| s3=i | |
| #print(f'The third film is in cluster {i+1}') | |
| return s1, s2, s3 | |
| def recom(b): | |
| if b[0]==b[1]==b[2]: | |
| return random.choice(cluster[b[1]]) | |
| if b[0]==b[1]!=b[2]: | |
| return random.choice(cluster[b[1]]) | |
| if b[0]!=b[1]==b[2]: | |
| return random.choice(cluster[b[1]]) | |
| if b[0]==b[2]!=b[1]: | |
| return random.choice(cluster[b[2]]) | |
| if b[0]!=b[2]!=b[1]: | |
| return random.choice(cluster[b[0]]) | |
| def original_recom(input1,input2,input3): | |
| return recom(recomender2(recomender(input1,input2,input3))) | |
| import gradio as gr | |
| def original_recom(input1,input2,input3): | |
| return recom(recomender2(recomender(input1,input2,input3))) | |
| with gr.Blocks() as demo: | |
| gr.Markdown("Flip text or image files using this demo.") | |
| input1 = gr.Textbox(placeholder='first film name...') | |
| input2 = gr.Textbox(placeholder='second film name...') | |
| input3 = gr.Textbox(placeholder='third film name...') | |
| text_output=gr.Textbox( ) | |
| text_button = gr.Button("Submit") | |
| text_button.click(original_recom, inputs=[input1,input2,input3], outputs=text_output) | |
| demo.launch() | |