irenewhll commited on
Commit
ce05fe5
Β·
verified Β·
1 Parent(s): 0c91bb9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +105 -58
app.py CHANGED
@@ -1,58 +1,105 @@
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()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ import numpy as np
4
+ import plotly.express as px
5
+ from transformers import (
6
+ AutoTokenizer,
7
+ AutoModel,
8
+ AutoModelForSequenceClassification
9
+ )
10
+ import torch
11
+
12
+ # Initialize pipelines and tokenizers
13
+ @st.cache_resource
14
+ def load_components():
15
+ # Pipeline 1: Director analysis
16
+ director_tokenizer = AutoTokenizer.from_pretrained("huggingface-course/distilbert-base-uncased-finetuned-imdb")
17
+ director_model = AutoModelForSequenceClassification.from_pretrained("huggingface-course/distilbert-base-uncased-finetuned-imdb")
18
+
19
+ # Pipeline 2: Semantic similarity for movie recommendation
20
+ sim_tokenizer = AutoTokenizer.from_pretrained("sentence-transformers/all-mpnet-base-v2")
21
+ sim_model = AutoModel.from_pretrained("sentence-transformers/all-mpnet-base-v2")
22
+
23
+ return {
24
+ "director": (director_tokenizer, director_model),
25
+ "similarity": (sim_tokenizer, sim_model)
26
+ }
27
+
28
+ components = load_components()
29
+
30
+ # Unpack components
31
+ director_tokenizer, director_model = components["director"]
32
+ sim_tokenizer, sim_model = components["similarity"]
33
+
34
+ # Genre mapping (translated)
35
+ genre_mapping = {"Action": 0, "Comedy": 1, "Sci-Fi": 2, "Adventure": 3}
36
+
37
+ # Sample database
38
+ movie_db = pd.DataFrame({
39
+ 'Title': ['Avatar', 'Interstellar', 'Jurassic Park', 'Fast & Furious 7', 'Hi, Mom'],
40
+ 'Genre': ['Sci-Fi', 'Sci-Fi', 'Adventure', 'Action', 'Comedy'],
41
+ 'Budget (Billion USD)': [2.37, 1.65, 0.63, 1.9, 0.15],
42
+ 'Box Office (Billion USD)': [2.92, 0.71, 1.10, 1.51, 0.83]
43
+ })
44
+
45
+ # Pipeline: Director quality analysis
46
+ def analyze_director(director):
47
+ inputs = director_tokenizer(director, return_tensors="pt")
48
+ with torch.no_grad():
49
+ outputs = director_model(**inputs)
50
+ scores = torch.sigmoid(outputs.logits)
51
+ return {
52
+ "Commercial Value": scores[0][0].item() * 10,
53
+ "Artistic Quality": scores[0][1].item() * 10
54
+ }
55
+
56
+ # Pipeline: Movie recommendation
57
+ def find_similar_movies(title, genre):
58
+ inputs = sim_tokenizer(title, padding=True, truncation=True, return_tensors="pt")
59
+ with torch.no_grad():
60
+ title_embed = sim_model(**inputs).last_hidden_state.mean(dim=1)
61
+
62
+ similarities = []
63
+ for _, row in movie_db.iterrows():
64
+ movie_inputs = sim_tokenizer(row['Title'], padding=True, truncation=True, return_tensors="pt")
65
+ with torch.no_grad():
66
+ movie_embed = sim_model(**movie_inputs).last_hidden_state.mean(dim=1)
67
+ sim = torch.cosine_similarity(title_embed, movie_embed)
68
+ similarities.append(sim.item())
69
+
70
+ movie_db['Similarity'] = similarities
71
+ return movie_db[movie_db['Genre'] == genre].sort_values('Similarity', ascending=False)
72
+
73
+ # Streamlit Interface
74
+ st.title("🎬 Movie Intelligence Dashboard")
75
+
76
+ with st.sidebar:
77
+ director = st.text_input("Director Name", "Christopher Nolan")
78
+ title = st.text_input("Movie Title", "Inception 2")
79
+ genre = st.selectbox("Genre", list(genre_mapping.keys()))
80
+
81
+ if st.button("Analyze"):
82
+ # Director analysis
83
+ st.header("πŸ§‘β€πŸ’Ό Director Profile")
84
+ director_scores = analyze_director(director)
85
+ fig = px.bar(
86
+ x=list(director_scores.keys()),
87
+ y=list(director_scores.values()),
88
+ range_y=[0, 10]
89
+ )
90
+ st.plotly_chart(fig)
91
+
92
+ # Movie recommendation
93
+ st.header("🎞 Recommended Movies")
94
+ similar_movies = find_similar_movies(title, genre)
95
+ st.dataframe(
96
+ similar_movies[['Title', 'Genre', 'Budget (Billion USD)', 'Box Office (Billion USD)', 'Similarity']],
97
+ column_config={
98
+ "Similarity": st.column_config.ProgressColumn(
99
+ format="%.2f",
100
+ min_value=0,
101
+ max_value=1
102
+ )
103
+ }
104
+ )
105
+