Spaces:
Runtime error
Runtime error
gaspar-avit
commited on
Commit
·
dde4b87
1
Parent(s):
b2605fa
Upload 2 files
Browse files- app.py +152 -0
- requirements.txt +6 -0
app.py
ADDED
@@ -0,0 +1,152 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
## Alternative movie poster generator
|
2 |
+
|
3 |
+
|
4 |
+
|
5 |
+
import streamlit as st
|
6 |
+
import pandas as pd
|
7 |
+
import numpy as np
|
8 |
+
import json
|
9 |
+
import requests
|
10 |
+
import os
|
11 |
+
|
12 |
+
from streamlit import session_state as session
|
13 |
+
from datetime import time, datetime
|
14 |
+
from zipfile import ZipFile
|
15 |
+
from kaggle.api.kaggle_api_extended import KaggleApi
|
16 |
+
from sentence_transformers import SentenceTransformer
|
17 |
+
from diffusers import DiffusionPipeline
|
18 |
+
|
19 |
+
|
20 |
+
###############################
|
21 |
+
## ------- FUNCTIONS ------- ##
|
22 |
+
###############################
|
23 |
+
|
24 |
+
#@st.cache(persist=True, show_spinner=False, suppress_st_warning=True)
|
25 |
+
@st.experimental_memo(persist=True, show_spinner=False, suppress_st_warning=True)
|
26 |
+
def load_dataset():
|
27 |
+
"""
|
28 |
+
Load Dataset from Kaggle
|
29 |
+
-return: dataframe containing dataset
|
30 |
+
"""
|
31 |
+
# Downloading Movies dataset
|
32 |
+
api.dataset_download_file('rounakbanik/the-movies-dataset', 'movies_metadata.csv')
|
33 |
+
|
34 |
+
# Extract data
|
35 |
+
zf = ZipFile('movies_metadata.csv.zip')
|
36 |
+
zf.extractall()
|
37 |
+
zf.close()
|
38 |
+
|
39 |
+
# Create dataframe
|
40 |
+
data = pd.read_csv('movies_metadata.csv', low_memory=False)
|
41 |
+
|
42 |
+
return data
|
43 |
+
|
44 |
+
@st.cache
|
45 |
+
def load_model():
|
46 |
+
#return DiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5")
|
47 |
+
return DiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4")
|
48 |
+
|
49 |
+
|
50 |
+
def query_summarization(text):
|
51 |
+
"""
|
52 |
+
Get summarization from HuggingFace Inference API
|
53 |
+
-param text: text to be summarized
|
54 |
+
-return: summarized text
|
55 |
+
"""
|
56 |
+
API_URL = "https://api-inference.huggingface.co/models/facebook/bart-large-cnn"
|
57 |
+
headers = {"Authorization": f"Bearer {st.secrets['hf_token']}"}
|
58 |
+
payload = {"inputs": f"{text}",}
|
59 |
+
|
60 |
+
response = requests.request("POST", API_URL, headers=headers, json=payload).json()
|
61 |
+
return response[0].get('summary_text')
|
62 |
+
|
63 |
+
def generate_poster(movie_data):
|
64 |
+
"""
|
65 |
+
Function for recommending movies
|
66 |
+
-param movie_data: metadata of movie selected by user
|
67 |
+
-return: image of generated alternative poster
|
68 |
+
"""
|
69 |
+
|
70 |
+
# Get summarization of movie synopsis
|
71 |
+
with st.spinner("Please wait while the synopsis is being summarized..."):
|
72 |
+
synopsis_sum = query_summarization(movie_data.overview.values[0])
|
73 |
+
st.text(synopsis_sum)
|
74 |
+
|
75 |
+
# Get image based on synopsis
|
76 |
+
pipeline = load_model()
|
77 |
+
#pipeline = DiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-2")
|
78 |
+
|
79 |
+
#image = pipe(prompt).images[0]
|
80 |
+
#st.image(image, caption=movie_data.title)
|
81 |
+
|
82 |
+
return None #poster_image
|
83 |
+
|
84 |
+
|
85 |
+
###############################
|
86 |
+
## --- CONNECT TO KAGGLE --- ##
|
87 |
+
###############################
|
88 |
+
|
89 |
+
# Authenticate Kaggle account
|
90 |
+
os.environ['KAGGLE_USERNAME'] = st.secrets['username']
|
91 |
+
os.environ['KAGGLE_KEY'] = st.secrets['key']
|
92 |
+
|
93 |
+
|
94 |
+
api_token = {"username":st.secrets['username'],"key":st.secrets['key']}
|
95 |
+
with open('/home/appuser/.kaggle/kaggle.json', 'w') as file:
|
96 |
+
json.dump(api_token, file)
|
97 |
+
|
98 |
+
|
99 |
+
# Activate Kaggle API
|
100 |
+
|
101 |
+
try:
|
102 |
+
api = KaggleApi()
|
103 |
+
api.authenticate()
|
104 |
+
except:
|
105 |
+
with open('/home/appuser/.kaggle/kaggle.json', 'w') as file:
|
106 |
+
json.dump(api_token, file)
|
107 |
+
api = KaggleApi()
|
108 |
+
api.authenticate()
|
109 |
+
|
110 |
+
|
111 |
+
|
112 |
+
###############################
|
113 |
+
## --------- MAIN ---------- ##
|
114 |
+
###############################
|
115 |
+
|
116 |
+
image = None
|
117 |
+
|
118 |
+
# Create dataset
|
119 |
+
data = load_dataset()
|
120 |
+
|
121 |
+
|
122 |
+
st.title("""
|
123 |
+
Alternative Movie Poster Generator :film_frames:
|
124 |
+
This is a movie poster generator based on movie's synopsis :sunglasses:
|
125 |
+
Just select the title of a movie to generate an alternative poster.
|
126 |
+
""")
|
127 |
+
|
128 |
+
st.text("")
|
129 |
+
st.text("")
|
130 |
+
st.text("")
|
131 |
+
st.text("")
|
132 |
+
|
133 |
+
session.selected_movie = st.selectbox(label="Select a movie to generate alternative poster", options=data.title)
|
134 |
+
|
135 |
+
st.text("")
|
136 |
+
st.text("")
|
137 |
+
|
138 |
+
buffer1, col1, buffer2 = st.columns([1.3, 1, 1])
|
139 |
+
|
140 |
+
is_clicked = col1.button(label="Generate poster!")
|
141 |
+
|
142 |
+
|
143 |
+
if is_clicked:
|
144 |
+
image = generate_poster(data[data.title==session.selected_movie])
|
145 |
+
|
146 |
+
st.text("")
|
147 |
+
st.text("")
|
148 |
+
st.text("")
|
149 |
+
st.text("")
|
150 |
+
|
151 |
+
#if data is not None:
|
152 |
+
# st.table(data)
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
kaggle
|
3 |
+
zipfile39
|
4 |
+
sentence-transformers
|
5 |
+
diffusers
|
6 |
+
accelerate
|