Download app.py from quyetnx/rcm: direct link, hf CLI and curl.
- Browser
- Download file 1.51 kB
-
https://huggingface.co/spaces/quyetnx/rcm/resolve/main/app.py
- Command line
-
hf download hf://spaces/quyetnx/rcm/app.py
-
curl -L -o app.py https://huggingface.co/spaces/quyetnx/rcm/resolve/main/app.py
1.51 kB
| import pandas as pd | |
| from sklearn.feature_extraction.text import TfidfVectorizer | |
| from sklearn.metrics.pairwise import cosine_similarity | |
| import gradio as gr | |
| # Sample movie dataset | |
| data = { | |
| 'title': [ | |
| 'The Matrix', | |
| 'John Wick', | |
| 'Inception', | |
| 'Interstellar', | |
| 'The Notebook', | |
| 'The Godfather' | |
| ], | |
| 'genre': [ | |
| 'Action Sci-Fi', | |
| 'Action Thriller', | |
| 'Action Sci-Fi Thriller', | |
| 'Adventure Drama Sci-Fi', | |
| 'Romance Drama', | |
| 'Crime Drama' | |
| ] | |
| } | |
| df = pd.DataFrame(data) | |
| # Vectorize genres using TF-IDF | |
| tfidf = TfidfVectorizer(stop_words='english') | |
| tfidf_matrix = tfidf.fit_transform(df['genre']) | |
| # Compute cosine similarity between all movies | |
| cosine_sim = cosine_similarity(tfidf_matrix, tfidf_matrix) | |
| # Map movie titles to index | |
| indices = pd.Series(df.index, index=df['title']).drop_duplicates() | |
| def recommend(movie_title, top_n=3): | |
| if movie_title not in indices: | |
| return ["Movie not found. Try one from:"] + list(df['title']) | |
| idx = indices[movie_title] | |
| sim_scores = list(enumerate(cosine_sim[idx])) | |
| sim_scores = sorted(sim_scores, key=lambda x: x[1], reverse=True)[1:top_n+1] | |
| movie_indices = [i[0] for i in sim_scores] | |
| return df['title'].iloc[movie_indices].tolist() | |
| # Gradio UI | |
| gr.Interface( | |
| fn=recommend, | |
| inputs="text", | |
| outputs="text", | |
| title="🎬 Content-Based Movie Recommender", | |
| description="Enter a movie title to get similar recommendations." | |
| ).launch() |