Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
# pipeline 1: zero-shot to identify movie genres
|
| 5 |
+
genre_classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
|
| 6 |
+
|
| 7 |
+
# pipeline 2: text2text-generation for movie descriptions
|
| 8 |
+
desc_pipeline = pipeline("text2text-generation", model="google/flan-t5-base")
|
| 9 |
+
|
| 10 |
+
# fine-tuned model (pipeline 3): local movie recommender model
|
| 11 |
+
rec_pipeline = pipeline("text-classification", model="model")
|
| 12 |
+
|
| 13 |
+
candidate_movies = [
|
| 14 |
+
"Inception", "The Matrix", "Interstellar", "Titanic", "The Dark Knight",
|
| 15 |
+
"The Godfather", "Pulp Fiction", "The Shawshank Redemption", "Forrest Gump", "Avengers"
|
| 16 |
+
]
|
| 17 |
+
|
| 18 |
+
movie_genres = ["sci-fi", "drama", "romance", "action", "crime", "thriller", "adventure"]
|
| 19 |
+
|
| 20 |
+
def recommend_movies(input_movies):
|
| 21 |
+
if not input_movies.strip():
|
| 22 |
+
return "β οΈ Please enter at least one movie.", []
|
| 23 |
+
|
| 24 |
+
genres = genre_classifier(input_movies, candidate_labels=movie_genres, multi_label=True)
|
| 25 |
+
top_genres = genres["labels"][:2]
|
| 26 |
+
|
| 27 |
+
# Use fine-tuned model to recommend a movie label (simplified)
|
| 28 |
+
try:
|
| 29 |
+
rec_results = rec_pipeline(f"{input_movies} | genres: {', '.join(top_genres)}")
|
| 30 |
+
except Exception as e:
|
| 31 |
+
return f"β Recommendation model error: {e}", []
|
| 32 |
+
|
| 33 |
+
top_rec = rec_results[:5]
|
| 34 |
+
gallery = []
|
| 35 |
+
for item in top_rec:
|
| 36 |
+
title = item["label"].replace("LABEL_", "").strip()
|
| 37 |
+
score = item["score"]
|
| 38 |
+
try:
|
| 39 |
+
desc = desc_pipeline(f"Describe the movie {title} in one sentence.", max_length=40)[0]["generated_text"]
|
| 40 |
+
except:
|
| 41 |
+
desc = "No description available."
|
| 42 |
+
img_url = f"https://via.placeholder.com/200x300?text={title.replace(' ', '+')}"
|
| 43 |
+
label = f"ποΈ **{title}**\n\n{desc}\n\nConfidence: {score:.2f}"
|
| 44 |
+
gallery.append((img_url, label))
|
| 45 |
+
|
| 46 |
+
summary = f"π¬ Based on your input and detected genres ({', '.join(top_genres)}), we recommend:"
|
| 47 |
+
return summary, gallery
|
| 48 |
+
|
| 49 |
+
with gr.Blocks(title="π₯ Movie Recommender") as demo:
|
| 50 |
+
gr.Markdown("# π¬ Personalized Movie Recommendation\n_Using Hugging Face pipelines + fine-tuned model_")
|
| 51 |
+
with gr.Row():
|
| 52 |
+
input_box = gr.Textbox(label="Enter up to 3 movies you like", placeholder="e.g. Inception, Titanic")
|
| 53 |
+
btn = gr.Button("π― Recommend")
|
| 54 |
+
output_text = gr.Markdown()
|
| 55 |
+
output_gallery = gr.Gallery(columns=2)
|
| 56 |
+
btn.click(fn=recommend_movies, inputs=input_box, outputs=[output_text, output_gallery])
|
| 57 |
+
|
| 58 |
+
demo.launch()
|