Spaces:
Runtime error
Runtime error
File size: 9,994 Bytes
ac9d9ab 233b641 ac9d9ab 9ebedb6 ac9d9ab 9fda244 ac9d9ab a063e30 ac9d9ab a063e30 ac9d9ab b6084ac ac9d9ab a063e30 ac9d9ab a063e30 ac9d9ab d3c9591 ac9d9ab d3c9591 ac9d9ab d3c9591 ac9d9ab 233b641 ac9d9ab f7716a6 ac9d9ab f7716a6 ac9d9ab f7716a6 ac9d9ab 4a8f50c ac9d9ab 4a8f50c ac9d9ab e4d0f4c 4a8f50c ac9d9ab 3eaf0d9 0984e5d 8741317 ac9d9ab e0d3b95 657d4a2 95a5558 ac9d9ab 233b641 4a8f50c ac9d9ab d3bc4c5 f7716a6 d3bc4c5 3eaf0d9 d3bc4c5 57728b1 ac9d9ab ad562e0 6d82a23 ac9d9ab af93059 ac9d9ab a7ea406 af93059 2074242 f243b69 ac9d9ab b6084ac ac9d9ab 6d82a23 4a8f50c ac9d9ab f243b69 ac9d9ab 4e1e792 79ef047 af93059 ac9d9ab d3bc4c5 acf9b17 18e2def a063e30 ac9d9ab 2074242 f243b69 ac9d9ab f243b69 ac9d9ab 5c3c7cb ac9d9ab 073cf07 f7716a6 ac9d9ab 073cf07 3d9c6f6 4a8f50c 3d9c6f6 4a8f50c 8b234db acf9b17 c19700b fb76caa c19700b 073cf07 8b234db 4a8f50c f1b4eb9 0984e5d 073cf07 acf9b17 18e2def a063e30 |
1 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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 |
## Alternative movie poster generator
import streamlit as st
import pandas as pd
import numpy as np
import json
import requests
import os
import io
import string
import random
from streamlit import session_state as session
from datetime import time, datetime
from zipfile import ZipFile
from htbuilder import HtmlElement, div, ul, li, br, hr, a, p, img, styles, classes, fonts
from htbuilder.units import percent, px
from htbuilder.funcs import rgba, rgb
from PIL import Image
###############################
## --- GLOBAL VARIABLES ---- ##
###############################
PATH_JSON = '/home/user/.kaggle/kaggle.json'
# Environment variables to authenticate Kaggle account
os.environ['KAGGLE_USERNAME'] = st.secrets['username']
os.environ['KAGGLE_KEY'] = st.secrets['key']
os.environ['KAGGLE_CONFIG_DIR'] = PATH_JSON
from kaggle.api.kaggle_api_extended import KaggleApi
###############################
## ------- FUNCTIONS ------- ##
###############################
def link(link, text, **style):
return a(_href=link, _target="_blank", style=styles(**style))(text)
def layout(*args):
style = """
<style>
# MainMenu {visibility: hidden;}
footer {visibility: hidden;}
.stApp { bottom: 105px; }
</style>
"""
style_div = styles(
position="fixed",
left=0,
bottom=0,
margin=px(0, 0, 0, 0),
width=percent(100),
color="black",
text_align="center",
height="auto",
opacity=1
)
style_hr = styles(
display="block",
margin=px(4, 4, "auto", "auto"),
border_style="inset",
border_width=px(0)
)
body = p()
foot = div(
style=style_div
)(
hr(
style=style_hr
),
body
)
st.markdown(style, unsafe_allow_html=True)
for arg in args:
if isinstance(arg, str):
body(arg)
elif isinstance(arg, HtmlElement):
body(arg)
st.markdown(str(foot), unsafe_allow_html=True)
def footer():
myargs = [
"Made with ❤️ by ",
link("https://www.linkedin.com/in/gaspar-avit/?locale=en_US", "Gaspar Avit"),
]
layout(*myargs)
def authenticate_kaggle():
# Connect to kaggle API
# Save credentials to json file
if not os.path.exists(PATH_JSON):
api_token = {"username":st.secrets['username'],"key":st.secrets['key']}
with open(PATH_JSON, 'w') as file:
json.dump(api_token, file)
# Activate Kaggle API
global api
api = KaggleApi()
api.authenticate()
@st.experimental_memo(persist=True, show_spinner=False, suppress_st_warning=True, max_entries=1)
def load_dataset():
"""
Load Dataset from Kaggle
-return: dataframe containing dataset
"""
## --- Connect to kaggle API --- ##
# Save credentials to json file
if not os.path.exists(PATH_JSON):
api_token = {"username":st.secrets['username'],"key":st.secrets['key']}
with open(PATH_JSON, 'w') as file:
json.dump(api_token, file)
# Activate Kaggle API
global api
api = KaggleApi()
api.authenticate()
## ----------------------------- ##
# Downloading Movies dataset
api.dataset_download_file('rounakbanik/the-movies-dataset', 'movies_metadata.csv')
# Extract data
zf = ZipFile('movies_metadata.csv.zip')
zf.extractall()
zf.close()
# Create dataframe
data = pd.read_csv('movies_metadata.csv', low_memory=False)
data['year'] = data["release_date"].map(lambda x: x.split('-')[0] if isinstance(x, str) else '0')
data['title_year'] = data['title'] + ' (' + data['year'] + ')'
return data
def query_summary(text):
"""
Get summarization from HuggingFace Inference API
-param text: text to be summarized
-return: summarized text
"""
API_URL = "https://api-inference.huggingface.co/models/facebook/bart-large-cnn"
headers = {"Authorization": f"Bearer {st.secrets['hf_token']}"}
payload = {"inputs": f"{text}",}
response = requests.request("POST", API_URL, headers=headers, json=payload).json()
try:
text = response[0].get('summary_text')
except:
text = response[0]
return text
def query_generate(text, title, genres, year, selected_model='Stable Diffusion v1.5'):
"""
Get image from HuggingFace Inference API
-param text: text to generate image
-param title: title of the movie
-param genres: genres of the movie
-param year: year of the movie
-return: generated image
"""
if selected_model=='Stable Diffusion XL':
API_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-xl-base-1.0"
elif selected_model=='Stable Diffusion v2.1':
API_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-2-1"
elif selected_model=='Stable Diffusion v1.5':
API_URL = "https://api-inference.huggingface.co/models/runwayml/stable-diffusion-v1-5"
else:
raise ValueError("Value not valid for argument 'selected_model'.")
headers = {"Authorization": f"Bearer {st.secrets['hf_token']}"}
text = 'A Poster for the movie ' + title.split('(')[0] + 'in portrait mode based on the following synopsis: \"' + text + '\". Style: ' + genres + '. Year ' + year + \
'. Ignore ' + ''.join(random.choices(string.ascii_letters, k=10))
payload = {"inputs": f"{text}", "options": {"use_cache": "false"},}
response = requests.post(API_URL, headers=headers, json=payload)
try:
response_str = response.content.decode("utf-8")
if 'error' in response_str:
payload = {"inputs": f"{text}",
"options": {"wait_for_model": True},
}
response = requests.post(API_URL, headers=headers, json=payload)
except:
pass
return response.content
@st.experimental_memo(persist=False, show_spinner=False, suppress_st_warning=True)
def generate_poster(movie_data, selected_model):
"""
Function for recommending movies
-param movie_data: metadata of movie selected by user
-return: image of generated alternative poster
"""
# Get movie metadata
genres = [i['name'] for i in eval(movie_data['genres'].values[0])]
genres_string = ', '.join(genres)
year = movie_data['year'].values[0]
title = movie_data['title'].values[0]
# Get summarization of movie synopsis
st.text("")
with st.spinner("Summarizing synopsis..."):
synopsis_sum = query_summary(movie_data.overview.values[0])
# Print summarized synopsis
st.text("")
synopsis_expander = st.expander("Show synopsis", expanded=False)
with synopsis_expander:
st.subheader("Summarized synopsis:")
col1, col2 = st.columns([5, 1])
with col1:
st.write(synopsis_sum)
st.text("")
st.text("")
st.text("")
st.text("")
# Get image based on synopsis
with st.spinner("Generating poster..."):
response_content = query_generate(synopsis_sum, title, genres_string, year, selected_model)
# Show image
try:
image = Image.open(io.BytesIO(response_content))
st.text("")
st.text("")
st.subheader("Resulting poster:")
st.text("")
col1, col2, col3 = st.columns([1, 5, 1])
with col2:
st.image(image, caption="Movie: \"" + movie_data.title.values[0] + "\"")
del image
st.text("")
st.text("")
st.text("")
st.text("")
except:
col1, col2 = st.columns([5, 1])
with col1:
st.write(response_content)
return response_content
# ------------------------------------------------------- #
###############################
## --------- MAIN ---------- ##
###############################
if __name__ == "__main__":
# Initialize image variable
poster = None
## --- Page config ------------ ##
# Set page title
st.title("""
Movie Poster Generator :film_frames:
#### This is a movie poster generator based on movie's synopsis :sunglasses:
#### Just select the title of a movie to generate an alternative poster.
""")
# Set page footer
footer()
# Set sidebar with info
st.sidebar.markdown("## Generating movie posters using Stable Diffusion")
st.sidebar.markdown("This streamlit space aims to generate movie posters based on synopsis.")
st.sidebar.markdown("Firstly, the synopsis of the selected movie is extracted from the dataset and then summarized using Facebook's BART model.")
st.sidebar.markdown("Once the movie's summary is ready, it is passed to the Stable Diffusion v1.5 model using HF's Inference API, with some prompt tuning.")
## ---------------------------- ##
## Create dataset
data = load_dataset()
st.text("")
st.text("")
st.text("")
st.text("")
## Select box with all the movies as choices
session.selected_movie = st.selectbox(label="Select a movie to generate alternative poster", options=data.title_year)
st.text("")
st.text("")
## Create button to trigger poster generation
sd_options = ['Stable Diffusion v1.5', 'Stable Diffusion v2.1', 'Stable Diffusion XL']
buffer1, col1, col2, buffer2 = st.columns([0.3, 1, 1, 1])
session.selected_model = col1.selectbox(label="Select SD model version", options=sd_options, label_visibility="collapsed")
is_clicked = col2.button(label="Generate poster!")
st.text("")
st.text("")
## Clear cache between runs
st.runtime.legacy_caching.clear_cache()
generate_poster.clear()
## Generate poster
if is_clicked:
poster = generate_poster(data[data.title_year==session.selected_movie], session.selected_model)
generate_poster.clear()
st.runtime.legacy_caching.clear_cache()
st.text("")
st.text("")
st.text("")
st.text("")
|