Upload 3 files
Browse files- README.md +30 -13
- app.py +42 -0
- requirements.txt +5 -0
README.md
CHANGED
|
@@ -1,13 +1,30 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Movie Recommender
|
| 2 |
+
|
| 3 |
+
A simple web app to recommend movies based on your text description, using embeddings and cosine similarity.
|
| 4 |
+
|
| 5 |
+
## How it works
|
| 6 |
+
|
| 7 |
+
- The app uses [SentenceTransformers](https://www.sbert.net/) to encode your text and the movie descriptions into embeddings.
|
| 8 |
+
- Then it calculates the cosine similarity between your input and all movies in the dataset.
|
| 9 |
+
- It shows the top 5 most similar movies.
|
| 10 |
+
|
| 11 |
+
## How to run
|
| 12 |
+
|
| 13 |
+
1. Make sure you have `movies.csv` in the same folder, with columns: `Title`, `Overview`, `embeddings` (as list of floats).
|
| 14 |
+
2. Install dependencies:
|
| 15 |
+
|
| 16 |
+
```bash
|
| 17 |
+
pip install -r requirements.txt
|
| 18 |
+
```
|
| 19 |
+
|
| 20 |
+
3. Run the app:
|
| 21 |
+
|
| 22 |
+
```bash
|
| 23 |
+
python app.py
|
| 24 |
+
```
|
| 25 |
+
|
| 26 |
+
Then open the URL shown in your browser.
|
| 27 |
+
|
| 28 |
+
## HuggingFace Spaces
|
| 29 |
+
|
| 30 |
+
You can also upload this folder to [HuggingFace Spaces](https://huggingface.co/spaces) as a Gradio app.
|
app.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
| 2 |
+
import numpy as np
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import pandas as pd
|
| 5 |
+
from sentence_transformers import SentenceTransformer
|
| 6 |
+
import ast
|
| 7 |
+
|
| 8 |
+
# 讟注谉 讗转 讛诪讜讚诇
|
| 9 |
+
model = SentenceTransformer("all-MiniLM-L6-v2")
|
| 10 |
+
|
| 11 |
+
# 讟注谉 讗转 讛讚讗讟讛住讟 诪讛诇讬谞拽
|
| 12 |
+
url = "https://huggingface.co/datasets/Pablinho/movies-dataset/resolve/main/9000plus.csv"
|
| 13 |
+
print("Loading dataset...")
|
| 14 |
+
dataset = pd.read_csv(url)
|
| 15 |
+
|
| 16 |
+
# 讜讚讗 砖讛注诪讜讚讜转 拽讬讬诪讜转
|
| 17 |
+
assert "Title" in dataset.columns
|
| 18 |
+
assert "Overview" in dataset.columns
|
| 19 |
+
assert "embeddings" in dataset.columns
|
| 20 |
+
|
| 21 |
+
# 讛驻讜讱 讗转 讛讟拽住讟 砖诇 embeddings 诇专砖讬诪讜转 砖诇 诪住驻专讬诐
|
| 22 |
+
dataset['embeddings'] = dataset['embeddings'].apply(ast.literal_eval)
|
| 23 |
+
|
| 24 |
+
print("Dataset loaded!")
|
| 25 |
+
|
| 26 |
+
def recommend_similar_movies(input_text, top_n=5):
|
| 27 |
+
input_embedding = model.encode([input_text])
|
| 28 |
+
similarities = cosine_similarity(input_embedding, np.vstack(dataset['embeddings'].to_numpy()))[0]
|
| 29 |
+
top_indices = similarities.argsort()[::-1][:top_n]
|
| 30 |
+
results = dataset.iloc[top_indices][['Title', 'Overview']]
|
| 31 |
+
return "\n\n".join(f"馃幀 **{row['Title']}**\n{row['Overview']}" for _, row in results.iterrows())
|
| 32 |
+
|
| 33 |
+
demo = gr.Interface(
|
| 34 |
+
fn=recommend_similar_movies,
|
| 35 |
+
inputs=gr.Textbox(lines=2, placeholder="Describe a movie..."),
|
| 36 |
+
outputs="text",
|
| 37 |
+
title="Movie Recommender",
|
| 38 |
+
description="Get movie recommendations based on your description. Powered by sentence-transformers and cosine similarity."
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
if __name__ == "__main__":
|
| 42 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
numpy
|
| 2 |
+
pandas
|
| 3 |
+
scikit-learn
|
| 4 |
+
gradio
|
| 5 |
+
sentence-transformers
|