Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,58 +1,105 @@
|
|
| 1 |
-
import
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
|