Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
|
| 7 |
+
# 讟注谉 讗转 讛诪讜讚诇
|
| 8 |
+
model = SentenceTransformer("all-MiniLM-L6-v2")
|
| 9 |
+
|
| 10 |
+
# 讟注谉 讗转 讛讚讗讟讛住讟 诪讛诇讬谞拽
|
| 11 |
+
url = "https://huggingface.co/datasets/Pablinho/movies-dataset/resolve/main/9000plus.csv"
|
| 12 |
+
print("Loading dataset...")
|
| 13 |
+
dataset = pd.read_csv(url)
|
| 14 |
+
|
| 15 |
+
# 讜讚讗 砖讛注诪讜讚讜转 拽讬讬诪讜转
|
| 16 |
+
assert "Title" in dataset.columns
|
| 17 |
+
assert "Overview" in dataset.columns
|
| 18 |
+
|
| 19 |
+
# 谞拽讛 砖讜专讜转 注诐 Overview 讞住专 讗讜 诇讗 诪讞专讜讝转
|
| 20 |
+
dataset = dataset.dropna(subset=["Overview"])
|
| 21 |
+
dataset = dataset[dataset["Overview"].apply(lambda x: isinstance(x, str))]
|
| 22 |
+
|
| 23 |
+
# 讛讙讘诇 诇志500 住专讟讬诐
|
| 24 |
+
MAX_MOVIES = 500
|
| 25 |
+
dataset = dataset.head(MAX_MOVIES)
|
| 26 |
+
|
| 27 |
+
print(f"Encoding {len(dataset)} movie descriptions...")
|
| 28 |
+
dataset["embeddings"] = dataset["Overview"].apply(lambda x: model.encode(x).tolist())
|
| 29 |
+
print("Done encoding!")
|
| 30 |
+
|
| 31 |
+
def recommend_similar_movies(input_text, top_n=5):
|
| 32 |
+
input_embedding = model.encode([input_text])
|
| 33 |
+
similarities = cosine_similarity(input_embedding, np.vstack(dataset['embeddings'].to_numpy()))[0]
|
| 34 |
+
top_indices = similarities.argsort()[::-1][:top_n]
|
| 35 |
+
results = dataset.iloc[top_indices][['Title', 'Overview']]
|
| 36 |
+
return "\n\n".join(f"馃幀 **{row['Title']}**\n{row['Overview']}" for _, row in results.iterrows())
|
| 37 |
+
|
| 38 |
+
demo = gr.Interface(
|
| 39 |
+
fn=recommend_similar_movies,
|
| 40 |
+
inputs=gr.Textbox(lines=2, placeholder="Describe a movie..."),
|
| 41 |
+
outputs="text",
|
| 42 |
+
title="Movie Recommender",
|
| 43 |
+
description="Get movie recommendations based on your description. Powered by sentence-transformers and cosine similarity."
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
if __name__ == "__main__":
|
| 47 |
+
demo.launch()
|