Spaces:
Runtime error
Runtime error
gaspar-avit
commited on
Commit
·
91fa72d
1
Parent(s):
6f7d10f
Upload app.py
Browse files
app.py
CHANGED
@@ -14,6 +14,9 @@ from datetime import time, datetime
|
|
14 |
from zipfile import ZipFile
|
15 |
from sentence_transformers import SentenceTransformer
|
16 |
from diffusers import DiffusionPipeline
|
|
|
|
|
|
|
17 |
|
18 |
|
19 |
###############################
|
@@ -39,13 +42,104 @@ from kaggle.api.kaggle_api_extended import KaggleApi
|
|
39 |
## ------- FUNCTIONS ------- ##
|
40 |
###############################
|
41 |
|
42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
@st.experimental_memo(persist=True, show_spinner=False, suppress_st_warning=True)
|
44 |
def load_dataset():
|
45 |
"""
|
46 |
Load Dataset from Kaggle
|
47 |
-return: dataframe containing dataset
|
48 |
"""
|
|
|
|
|
|
|
|
|
49 |
# Downloading Movies dataset
|
50 |
api.dataset_download_file('rounakbanik/the-movies-dataset', 'movies_metadata.csv')
|
51 |
|
@@ -105,69 +199,57 @@ def generate_poster(movie_data):
|
|
105 |
return None #poster_image
|
106 |
|
107 |
|
108 |
-
###############################
|
109 |
-
## --- CONNECT TO KAGGLE --- ##
|
110 |
-
###############################
|
111 |
-
|
112 |
|
113 |
-
if not os.path.exists(PATH_JSON):
|
114 |
-
api_token = {"username":st.secrets['username'],"key":st.secrets['key']}
|
115 |
-
with open(PATH_JSON, 'w') as file:
|
116 |
-
json.dump(api_token, file)
|
117 |
-
|
118 |
-
|
119 |
-
# Activate Kaggle API
|
120 |
-
api = KaggleApi()
|
121 |
-
api.authenticate()
|
122 |
|
|
|
|
|
|
|
123 |
|
124 |
-
try:
|
125 |
-
api.authenticate()
|
126 |
-
except:
|
127 |
-
with open('/home/appuser/.kaggle/kaggle.json', 'w') as file:
|
128 |
-
json.dump(api_token, file)
|
129 |
-
api.authenticate()
|
130 |
|
|
|
131 |
|
132 |
|
133 |
-
|
134 |
-
|
135 |
-
###############################
|
136 |
|
137 |
-
|
|
|
138 |
|
139 |
-
# Create dataset
|
140 |
-
data = load_dataset()
|
141 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
142 |
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
Just select the title of a movie to generate an alternative poster.
|
147 |
-
""")
|
148 |
|
149 |
-
st.text("")
|
150 |
-
st.text("")
|
151 |
-
st.text("")
|
152 |
-
st.text("")
|
153 |
|
154 |
-
session.selected_movie = st.selectbox(label="Select a movie to generate alternative poster", options=data.title)
|
155 |
|
156 |
-
st.text("")
|
157 |
-
st.text("")
|
158 |
|
159 |
-
buffer1, col1, buffer2 = st.columns([1.3, 1, 1])
|
160 |
|
161 |
-
is_clicked = col1.button(label="Generate poster!")
|
162 |
|
163 |
|
164 |
-
if is_clicked:
|
165 |
-
|
166 |
|
167 |
-
st.text("")
|
168 |
-
st.text("")
|
169 |
-
st.text("")
|
170 |
-
st.text("")
|
171 |
|
172 |
-
#if
|
173 |
-
# st.
|
|
|
14 |
from zipfile import ZipFile
|
15 |
from sentence_transformers import SentenceTransformer
|
16 |
from diffusers import DiffusionPipeline
|
17 |
+
from htbuilder import HtmlElement, div, ul, li, br, hr, a, p, img, styles, classes, fonts
|
18 |
+
from htbuilder.units import percent, px
|
19 |
+
from htbuilder.funcs import rgba, rgb
|
20 |
|
21 |
|
22 |
###############################
|
|
|
42 |
## ------- FUNCTIONS ------- ##
|
43 |
###############################
|
44 |
|
45 |
+
def link(link, text, **style):
|
46 |
+
return a(_href=link, _target="_blank", style=styles(**style))(text)
|
47 |
+
|
48 |
+
def image(src_as_string, **style):
|
49 |
+
return img(src=src_as_string, style=styles(**style))
|
50 |
+
|
51 |
+
def layout(*args):
|
52 |
+
|
53 |
+
style = """
|
54 |
+
<style>
|
55 |
+
# MainMenu {visibility: hidden;}
|
56 |
+
footer {visibility: hidden;}
|
57 |
+
.stApp { bottom: 105px; }
|
58 |
+
</style>
|
59 |
+
"""
|
60 |
+
|
61 |
+
style_div = styles(
|
62 |
+
position="fixed",
|
63 |
+
left=0,
|
64 |
+
bottom=0,
|
65 |
+
margin=px(0, 0, 0, 0),
|
66 |
+
width=percent(100),
|
67 |
+
color="black",
|
68 |
+
text_align="center",
|
69 |
+
height="auto",
|
70 |
+
opacity=1
|
71 |
+
)
|
72 |
+
|
73 |
+
style_hr = styles(
|
74 |
+
display="block",
|
75 |
+
margin=px(8, 8, "auto", "auto"),
|
76 |
+
border_style="inset",
|
77 |
+
border_width=px(2)
|
78 |
+
)
|
79 |
+
|
80 |
+
body = p()
|
81 |
+
foot = div(
|
82 |
+
style=style_div
|
83 |
+
)(
|
84 |
+
hr(
|
85 |
+
style=style_hr
|
86 |
+
),
|
87 |
+
body
|
88 |
+
)
|
89 |
+
|
90 |
+
st.markdown(style, unsafe_allow_html=True)
|
91 |
+
|
92 |
+
for arg in args:
|
93 |
+
if isinstance(arg, str):
|
94 |
+
body(arg)
|
95 |
+
|
96 |
+
elif isinstance(arg, HtmlElement):
|
97 |
+
body(arg)
|
98 |
+
|
99 |
+
st.markdown(str(foot), unsafe_allow_html=True)
|
100 |
+
|
101 |
+
def footer():
|
102 |
+
myargs = [
|
103 |
+
"Made in ",
|
104 |
+
image('https://avatars3.githubusercontent.com/u/45109972?s=400&v=4',
|
105 |
+
width=px(25), height=px(25)),
|
106 |
+
" with ❤️ by ",
|
107 |
+
link("https://www.linkedin.com/in/gaspar-avit/", "Gaspar Avit"),
|
108 |
+
]
|
109 |
+
layout(*myargs)
|
110 |
+
|
111 |
+
def authenticate_kaggle():
|
112 |
+
# Connect to kaggle API
|
113 |
+
|
114 |
+
if not os.path.exists(PATH_JSON):
|
115 |
+
api_token = {"username":st.secrets['username'],"key":st.secrets['key']}
|
116 |
+
with open(PATH_JSON, 'w') as file:
|
117 |
+
json.dump(api_token, file)
|
118 |
+
|
119 |
+
|
120 |
+
# Activate Kaggle API
|
121 |
+
api = KaggleApi()
|
122 |
+
api.authenticate()
|
123 |
+
|
124 |
+
|
125 |
+
try:
|
126 |
+
api.authenticate()
|
127 |
+
except:
|
128 |
+
with open('/home/appuser/.kaggle/kaggle.json', 'w') as file:
|
129 |
+
json.dump(api_token, file)
|
130 |
+
api.authenticate()
|
131 |
+
|
132 |
+
|
133 |
@st.experimental_memo(persist=True, show_spinner=False, suppress_st_warning=True)
|
134 |
def load_dataset():
|
135 |
"""
|
136 |
Load Dataset from Kaggle
|
137 |
-return: dataframe containing dataset
|
138 |
"""
|
139 |
+
|
140 |
+
# Connect to kaggle API
|
141 |
+
authenticate_kaggle()
|
142 |
+
|
143 |
# Downloading Movies dataset
|
144 |
api.dataset_download_file('rounakbanik/the-movies-dataset', 'movies_metadata.csv')
|
145 |
|
|
|
199 |
return None #poster_image
|
200 |
|
201 |
|
|
|
|
|
|
|
|
|
202 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
203 |
|
204 |
+
###############################
|
205 |
+
## --------- MAIN ---------- ##
|
206 |
+
###############################
|
207 |
|
|
|
|
|
|
|
|
|
|
|
|
|
208 |
|
209 |
+
if __name__ == "__main__":
|
210 |
|
211 |
|
212 |
+
# Initialize image variable
|
213 |
+
image = None
|
|
|
214 |
|
215 |
+
## Create dataset
|
216 |
+
data = load_dataset()
|
217 |
|
|
|
|
|
218 |
|
219 |
+
## --- Page config --- ##
|
220 |
+
# Set page title
|
221 |
+
st.title("""
|
222 |
+
Alternative Movie Poster Generator :film_frames:
|
223 |
+
This is a movie poster generator based on movie's synopsis :sunglasses:
|
224 |
+
Just select the title of a movie to generate an alternative poster.
|
225 |
+
""")
|
226 |
|
227 |
+
## Set page footer
|
228 |
+
footer()
|
229 |
+
## ------------------- ##
|
|
|
|
|
230 |
|
231 |
+
st.text("")
|
232 |
+
st.text("")
|
233 |
+
st.text("")
|
234 |
+
st.text("")
|
235 |
|
236 |
+
session.selected_movie = st.selectbox(label="Select a movie to generate alternative poster", options=data.title)
|
237 |
|
238 |
+
st.text("")
|
239 |
+
st.text("")
|
240 |
|
241 |
+
buffer1, col1, buffer2 = st.columns([1.3, 1, 1])
|
242 |
|
243 |
+
is_clicked = col1.button(label="Generate poster!")
|
244 |
|
245 |
|
246 |
+
if is_clicked:
|
247 |
+
image = generate_poster(data[data.title==session.selected_movie])
|
248 |
|
249 |
+
st.text("")
|
250 |
+
st.text("")
|
251 |
+
st.text("")
|
252 |
+
st.text("")
|
253 |
|
254 |
+
#if image is not None:
|
255 |
+
# st.image(image, caption=session.selected_movie.title.values[0])
|