File size: 6,459 Bytes
dde4b87
 
 
 
 
 
 
 
 
9d0fc91
dde4b87
 
 
 
 
 
91fa72d
 
 
dde4b87
 
6f7d10f
 
 
 
 
 
 
 
 
 
 
9d0fc91
 
6f7d10f
 
9d0fc91
 
 
 
dde4b87
 
 
 
91fa72d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dde4b87
b886d0a
dde4b87
 
 
 
91fa72d
 
 
 
dde4b87
 
 
 
 
 
 
 
 
 
 
 
 
6f7d10f
dde4b87
6f7d10f
 
 
 
 
dde4b87
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65ccb73
 
 
dde4b87
 
6f7d10f
 
 
 
 
dde4b87
65ccb73
 
dde4b87
65ccb73
 
dde4b87
 
91fa72d
 
 
6f7d10f
 
91fa72d
dde4b87
 
91fa72d
 
dde4b87
91fa72d
 
dde4b87
 
91fa72d
 
 
 
 
 
 
dde4b87
91fa72d
 
 
dde4b87
91fa72d
 
 
 
dde4b87
91fa72d
dde4b87
91fa72d
 
dde4b87
91fa72d
dde4b87
91fa72d
dde4b87
 
91fa72d
 
dde4b87
91fa72d
 
 
 
dde4b87
65ccb73
 
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
## Alternative movie poster generator



import streamlit as st
import pandas as pd
import numpy as np
import json
import requests
import os

from streamlit import session_state as session
from datetime import time, datetime
from zipfile import ZipFile
from sentence_transformers import SentenceTransformer
from diffusers import DiffusionPipeline
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


###############################
## --- GLOBAL VARIABLES ---- ##
###############################


IS_MODEL_LOADED = False
PATH_JSON = '/home/user/app/.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 image(src_as_string, **style):
    return img(src=src_as_string, style=styles(**style))

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(8, 8, "auto", "auto"),
        border_style="inset",
        border_width=px(2)
    )

    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 in ",
        image('https://avatars3.githubusercontent.com/u/45109972?s=400&v=4',
              width=px(25), height=px(25)),
        " with ❤️ by ",
        link("https://www.linkedin.com/in/gaspar-avit/", "Gaspar Avit"),
    ]
    layout(*myargs)

def authenticate_kaggle():
    # Connect to kaggle API

    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
    api = KaggleApi()
    api.authenticate()


    try:
        api.authenticate()
    except:
        with open('/home/appuser/.kaggle/kaggle.json', 'w') as file:
            json.dump(api_token, file)
        api.authenticate() 


@st.experimental_memo(persist=True, show_spinner=False, suppress_st_warning=True)
def load_dataset():
    """
    Load Dataset from Kaggle
    -return: dataframe containing dataset
    """

    # Connect to kaggle API
    authenticate_kaggle()

    # 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)

    return data

@st.cache(persist=True, show_spinner=False, allow_output_mutation=True, suppress_st_warning=True)
def load_model():
    model = DiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5")
    IS_MODEL_LOADED = True
    return model
    #return DiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4")
    #return DiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-2")


def query_summarization(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()
    return response[0].get('summary_text')

def generate_poster(movie_data):
    """
    Function for recommending movies
    -param movie_data: metadata of movie selected by user
    -return: image of generated alternative poster
    """

    # Get summarization of movie synopsis
    with st.spinner("Please wait while the synopsis is being summarized..."):
        synopsis_sum = query_summarization(movie_data.overview.values[0])

    st.text("")
    st.text("")
    st.text(synopsis_sum)

    # Load text-to-image model
    if not IS_MODEL_LOADED:
        with st.spinner("Loading Text to Image model..."):
            pipeline = load_model()
    
    # Get image based on synopsis
    poster_image = pipeline(synopsis_sum).images[0]
    st.image(poster_image, caption=movie_data.title)

    return poster_image
# ------------------------------------------------------- #


###############################
## --------- MAIN ---------- ##
###############################


if __name__ == "__main__":


    # Initialize image variable
    image = None

    ## Create dataset
    data = load_dataset()


    ## --- Page config --- ##
    # Set page title
    st.title("""
    Alternative 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()
    ## ------------------- ##

    st.text("")
    st.text("")
    st.text("")
    st.text("")

    session.selected_movie = st.selectbox(label="Select a movie to generate alternative poster", options=data.title)

    st.text("")
    st.text("")

    buffer1, col1, buffer2 = st.columns([1.3, 1, 1])

    is_clicked = col1.button(label="Generate poster!")


    if is_clicked:
        image = generate_poster(data[data.title==session.selected_movie])

    st.text("")
    st.text("")
    st.text("")
    st.text("")

    if image is not None:
        st.image(image, caption=session.selected_movie.title.values[0])