File size: 3,115 Bytes
d9dec00
 
 
 
 
 
 
 
 
7ea1eed
16824b4
 
 
 
 
 
d9dec00
9b46852
d9dec00
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16824b4
d9dec00
 
 
b9336f7
d9dec00
 
b9336f7
 
 
d9dec00
 
b9336f7
afb1e96
b9336f7
 
afb1e96
a372eb9
9b46852
b9336f7
d9dec00
 
ea8b22d
afb1e96
9b46852
a90dc61
ae482b3
aaa1cf1
 
ae482b3
d04bbe9
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
from PIL import Image
from utils import pipe_image
import streamlit as st
from streamlit_drawable_canvas import st_canvas
import gettext
import numpy as np

_ = gettext.gettext

#language = st.selectbox(_('Choose your language'), ['en', 'it'])
# try:
#   localizator = gettext.translation('base', localedir='locales', languages=[language])
#   localizator.install()
#   _ = localizator.gettext 
# except:
#     pass

st.markdown("<h1 style='text-align:center;'> Magic Board - CPU version</h1>", unsafe_allow_html=True)
# Specify canvas parameters in application
drawing_mode = st.sidebar.selectbox(
    _("Drawing tool:"), ("freedraw", "line", "rect", "circle", "transform")
)

stroke_width = st.sidebar.slider(_("Stroke width: "), 1, 25, 3)
stroke_color = st.sidebar.color_picker(_("Stroke color hex: "))
bg_color = st.sidebar.color_picker(_("Background color hex: "), "#eee")
bg_image = st.sidebar.file_uploader(_("Background image:"), type=["png", "jpg"])
strength = st.sidebar.slider(_("Strength value: "), 0.0, 1.0, 0.1)

# Create a canvas component
canvas_result = st_canvas(
    fill_color="rgba(255, 165, 0, 0.3)",  # Fixed fill color with some opacity
    stroke_width=stroke_width,
    stroke_color=stroke_color,
    background_color=bg_color,
    background_image=Image.open(bg_image) if bg_image else None,
    update_streamlit=True,
    height=450,
    width=680,
    drawing_mode=drawing_mode,
    #point_display_radius=point_display_radius if drawing_mode == 'point' else 0,
    key="canvas",
)

prompt = st.text_input(_('Prompt to generate your cool image'), 
               placeholder=_('write you text here to improve the image with your favourite style'))

if st.button('Submit'):
    img_size = (512,512)
    if bg_image:
        img_back=Image.open(bg_image).convert('RGB').resize(img_size)
        comp_img = img_back
    elif np.any(canvas_result.image_data):
        img_draw=Image.fromarray(canvas_result.image_data).convert('RGB').resize(img_size)
        comp_img = img_draw
    elif bg_image and np.any(canvas_result.image_data):
        img_back=Image.open(bg_image).convert('RGB').resize(img_size)
        img_draw=Image.fromarray(canvas_result.image_data).convert('RGB').resize(img_size)
        comp_img = Image.new("RGB", img_size)
        comp_img = Image.blend(img_draw, img_back, alpha=0.5)
    else:
        st.write("Upload an image or draw something")
    
    if prompt is None:
        st.write("No prompt, no magic! :(")

    with st.spinner('Wait for it... ~ 5 minutes '):
        images = pipe_image(prompt=prompt,
                       init_image=comp_img,
                       strength=strength)
    if images["nsfw_content_detected"]:
        st.write("NSFW content detected, retry with another prompt or image")
    else:
        st.image(images.images)

st.warning("If you don't see any image could be the NSFW content checker or a Memory error. "
            "Please, re-run the submission and modify the prompt. "
            "Taking care that, each word in the prompt result in much more token for the model"
            "which means much more memory usage.")