import gradio as gr from transformers import pipeline # pipeline 1: zero-shot to identify movie genres genre_classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli") # pipeline 2: text2text-generation for movie descriptions desc_pipeline = pipeline("text2text-generation", model="google/flan-t5-base") # fine-tuned model (pipeline 3): local movie recommender model rec_pipeline = pipeline("text-classification", model="model") candidate_movies = [ "Inception", "The Matrix", "Interstellar", "Titanic", "The Dark Knight", "The Godfather", "Pulp Fiction", "The Shawshank Redemption", "Forrest Gump", "Avengers" ] movie_genres = ["sci-fi", "drama", "romance", "action", "crime", "thriller", "adventure"] def recommend_movies(input_movies): if not input_movies.strip(): return "⚠️ Please enter at least one movie.", [] genres = genre_classifier(input_movies, candidate_labels=movie_genres, multi_label=True) top_genres = genres["labels"][:2] # Use fine-tuned model to recommend a movie label (simplified) try: rec_results = rec_pipeline(f"{input_movies} | genres: {', '.join(top_genres)}") except Exception as e: return f"❌ Recommendation model error: {e}", [] top_rec = rec_results[:5] gallery = [] for item in top_rec: title = item["label"].replace("LABEL_", "").strip() score = item["score"] try: desc = desc_pipeline(f"Describe the movie {title} in one sentence.", max_length=40)[0]["generated_text"] except: desc = "No description available." img_url = f"https://via.placeholder.com/200x300?text={title.replace(' ', '+')}" label = f"🎞️ **{title}**\n\n{desc}\n\nConfidence: {score:.2f}" gallery.append((img_url, label)) summary = f"🎬 Based on your input and detected genres ({', '.join(top_genres)}), we recommend:" return summary, gallery with gr.Blocks(title="🎥 Movie Recommender") as demo: gr.Markdown("# 🎬 Personalized Movie Recommendation\n_Using Hugging Face pipelines + fine-tuned model_") with gr.Row(): input_box = gr.Textbox(label="Enter up to 3 movies you like", placeholder="e.g. Inception, Titanic") btn = gr.Button("🎯 Recommend") output_text = gr.Markdown() output_gallery = gr.Gallery(columns=2) btn.click(fn=recommend_movies, inputs=input_box, outputs=[output_text, output_gallery]) demo.launch()