Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,32 +1,54 @@
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
-
|
3 |
-
|
4 |
-
def
|
5 |
-
#
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
# Add
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
#
|
31 |
-
|
32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import base64
|
2 |
+
import graphviz as gv
|
3 |
import streamlit as st
|
4 |
+
|
5 |
+
# Define a function that renders the card using Graphviz
|
6 |
+
def render_card(image_path, title, subtitle):
|
7 |
+
# Define the Graphviz graph
|
8 |
+
g = gv.Graph(format='png')
|
9 |
+
g.attr(bgcolor='white', margin='0')
|
10 |
+
|
11 |
+
# Add the background image to the graph
|
12 |
+
with open(image_path, 'rb') as f:
|
13 |
+
data = f.read()
|
14 |
+
b64_image = base64.b64encode(data).decode()
|
15 |
+
g.node('background', image=f'data:image/png;base64,{b64_image}', shape='box', width='3.5', height='5')
|
16 |
+
|
17 |
+
# Add the title and subtitle to the graph
|
18 |
+
g.node('title', label=title, fontname='Arial', fontsize='16', shape='none', width='2')
|
19 |
+
g.node('subtitle', label=subtitle, fontname='Arial', fontsize='12', shape='none', width='2')
|
20 |
+
|
21 |
+
# Add edges between the nodes
|
22 |
+
g.edge('background', 'title', color='white', style='invis')
|
23 |
+
g.edge('background', 'subtitle', color='white', style='invis')
|
24 |
+
|
25 |
+
# Render the graph and return the PNG data
|
26 |
+
return g.pipe().decode('utf-8')
|
27 |
+
|
28 |
+
# Define the Streamlit app
|
29 |
+
def app():
|
30 |
+
st.title('Card Builder Evolver')
|
31 |
+
|
32 |
+
# Define the file uploader
|
33 |
+
uploaded_file = st.file_uploader('Upload an image', type=['jpg', 'jpeg', 'png'])
|
34 |
+
|
35 |
+
# Define the card title and subtitle inputs
|
36 |
+
title = st.text_input('Title')
|
37 |
+
subtitle = st.text_input('Subtitle')
|
38 |
+
|
39 |
+
# Render the card if an image has been uploaded and a title has been provided
|
40 |
+
if uploaded_file and title:
|
41 |
+
image_path = f'tmp/{uploaded_file.name}'
|
42 |
+
with open(image_path, 'wb') as f:
|
43 |
+
f.write(uploaded_file.getbuffer())
|
44 |
+
card = render_card(image_path, title, subtitle)
|
45 |
+
st.image(card, use_column_width=True)
|
46 |
+
|
47 |
+
# Define the download button
|
48 |
+
b64_card = base64.b64encode(card.encode()).decode()
|
49 |
+
href = f'<a href="data:image/png;base64,{b64_card}" download="card.png">Download card</a>'
|
50 |
+
st.markdown(href, unsafe_allow_html=True)
|
51 |
+
|
52 |
+
# Run the Streamlit app
|
53 |
+
if __name__ == '__main__':
|
54 |
+
app()
|