Upload hf_demo.py
Browse files- frontend/webui/hf_demo.py +161 -0
frontend/webui/hf_demo.py
ADDED
@@ -0,0 +1,161 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from backend.lcm_text_to_image import LCMTextToImage
|
3 |
+
from backend.models.lcmdiffusion_setting import LCMLora, LCMDiffusionSetting
|
4 |
+
from constants import DEVICE, LCM_DEFAULT_MODEL_OPENVINO
|
5 |
+
from time import perf_counter
|
6 |
+
import numpy as np
|
7 |
+
from cv2 import imencode
|
8 |
+
import base64
|
9 |
+
from backend.device import get_device_name
|
10 |
+
from constants import APP_VERSION
|
11 |
+
from backend.device import is_openvino_device
|
12 |
+
import PIL
|
13 |
+
from backend.models.lcmdiffusion_setting import DiffusionTask
|
14 |
+
from pprint import pprint
|
15 |
+
|
16 |
+
lcm_text_to_image = LCMTextToImage()
|
17 |
+
lcm_lora = LCMLora(
|
18 |
+
base_model_id="Lykon/dreamshaper-7",
|
19 |
+
lcm_lora_id="latent-consistency/lcm-lora-sdv1-5",
|
20 |
+
)
|
21 |
+
|
22 |
+
|
23 |
+
# https://github.com/gradio-app/gradio/issues/2635#issuecomment-1423531319
|
24 |
+
def encode_pil_to_base64_new(pil_image):
|
25 |
+
image_arr = np.asarray(pil_image)[:, :, ::-1]
|
26 |
+
_, byte_data = imencode(".png", image_arr)
|
27 |
+
base64_data = base64.b64encode(byte_data)
|
28 |
+
base64_string_opencv = base64_data.decode("utf-8")
|
29 |
+
return "data:image/png;base64," + base64_string_opencv
|
30 |
+
|
31 |
+
|
32 |
+
# monkey patching encode pil
|
33 |
+
gr.processing_utils.encode_pil_to_base64 = encode_pil_to_base64_new
|
34 |
+
|
35 |
+
|
36 |
+
def predict(
|
37 |
+
prompt,
|
38 |
+
steps,
|
39 |
+
seed,
|
40 |
+
use_seed,
|
41 |
+
):
|
42 |
+
print(f"prompt - {prompt}")
|
43 |
+
lcm_diffusion_setting = LCMDiffusionSetting()
|
44 |
+
lcm_diffusion_setting.diffusion_task = DiffusionTask.text_to_image.value
|
45 |
+
lcm_diffusion_setting.openvino_lcm_model_id = "rupeshs/sd-turbo-openvino"
|
46 |
+
lcm_diffusion_setting.use_lcm_lora = False
|
47 |
+
lcm_diffusion_setting.prompt = prompt
|
48 |
+
lcm_diffusion_setting.guidance_scale = 1.0
|
49 |
+
lcm_diffusion_setting.inference_steps = steps
|
50 |
+
lcm_diffusion_setting.seed = seed
|
51 |
+
lcm_diffusion_setting.use_seed = use_seed
|
52 |
+
lcm_diffusion_setting.use_safety_checker = True
|
53 |
+
lcm_diffusion_setting.use_tiny_auto_encoder = True
|
54 |
+
# lcm_diffusion_setting.image_width = 320 if is_openvino_device() else 512
|
55 |
+
# lcm_diffusion_setting.image_height = 320 if is_openvino_device() else 512
|
56 |
+
lcm_diffusion_setting.image_width = 512
|
57 |
+
lcm_diffusion_setting.image_height = 512
|
58 |
+
lcm_diffusion_setting.use_openvino = True
|
59 |
+
lcm_diffusion_setting.use_tiny_auto_encoder = True
|
60 |
+
pprint(lcm_diffusion_setting.model_dump())
|
61 |
+
lcm_text_to_image.init(lcm_diffusion_setting=lcm_diffusion_setting)
|
62 |
+
start = perf_counter()
|
63 |
+
images = lcm_text_to_image.generate(lcm_diffusion_setting)
|
64 |
+
latency = perf_counter() - start
|
65 |
+
print(f"Latency: {latency:.2f} seconds")
|
66 |
+
return images[0] # .resize([512, 512], PIL.Image.ANTIALIAS)
|
67 |
+
|
68 |
+
|
69 |
+
css = """
|
70 |
+
#container{
|
71 |
+
margin: 0 auto;
|
72 |
+
max-width: 40rem;
|
73 |
+
}
|
74 |
+
#intro{
|
75 |
+
max-width: 100%;
|
76 |
+
text-align: center;
|
77 |
+
margin: 0 auto;
|
78 |
+
}
|
79 |
+
#generate_button {
|
80 |
+
color: white;
|
81 |
+
border-color: #007bff;
|
82 |
+
background: #007bff;
|
83 |
+
width: 200px;
|
84 |
+
height: 50px;
|
85 |
+
}
|
86 |
+
footer {
|
87 |
+
visibility: hidden
|
88 |
+
}
|
89 |
+
"""
|
90 |
+
|
91 |
+
|
92 |
+
def _get_footer_message() -> str:
|
93 |
+
version = f"<center><p> {APP_VERSION} "
|
94 |
+
footer_msg = version + (
|
95 |
+
' © 2023 <a href="https://github.com/rupeshs">'
|
96 |
+
" Rupesh Sreeraman</a></p></center>"
|
97 |
+
)
|
98 |
+
warning_msg = "<p><b> Please note that this is a minimal demo app.</b> </p><br>"
|
99 |
+
return warning_msg + footer_msg
|
100 |
+
|
101 |
+
|
102 |
+
with gr.Blocks(css=css) as demo:
|
103 |
+
with gr.Column(elem_id="container"):
|
104 |
+
use_openvino = "" if is_openvino_device() else ""
|
105 |
+
gr.Markdown(
|
106 |
+
f"""# FastSD CPU demo {use_openvino}
|
107 |
+
**Device : {DEVICE.upper()} , {get_device_name()}**
|
108 |
+
""",
|
109 |
+
elem_id="intro",
|
110 |
+
)
|
111 |
+
gr.HTML(
|
112 |
+
f"""
|
113 |
+
<p id="project-links" align="center">
|
114 |
+
<a href='https://github.com/rupeshs/fastsdcpu'><img src='https://img.shields.io/badge/Project-Page-Green'></a>
|
115 |
+
</p>
|
116 |
+
"""
|
117 |
+
)
|
118 |
+
|
119 |
+
with gr.Row():
|
120 |
+
with gr.Row():
|
121 |
+
prompt = gr.Textbox(
|
122 |
+
placeholder="Describe the image you'd like to see",
|
123 |
+
scale=5,
|
124 |
+
container=False,
|
125 |
+
)
|
126 |
+
generate_btn = gr.Button(
|
127 |
+
"Generate",
|
128 |
+
scale=1,
|
129 |
+
elem_id="generate_button",
|
130 |
+
)
|
131 |
+
|
132 |
+
image = gr.Image(type="filepath")
|
133 |
+
with gr.Accordion("Advanced options", open=False):
|
134 |
+
steps = gr.Slider(
|
135 |
+
label="Steps",
|
136 |
+
value=1,
|
137 |
+
minimum=1,
|
138 |
+
maximum=3,
|
139 |
+
step=1,
|
140 |
+
)
|
141 |
+
seed = gr.Slider(
|
142 |
+
randomize=True,
|
143 |
+
minimum=0,
|
144 |
+
maximum=999999999,
|
145 |
+
label="Seed",
|
146 |
+
step=1,
|
147 |
+
)
|
148 |
+
seed_checkbox = gr.Checkbox(
|
149 |
+
label="Use seed",
|
150 |
+
value=False,
|
151 |
+
interactive=True,
|
152 |
+
)
|
153 |
+
gr.HTML(_get_footer_message())
|
154 |
+
|
155 |
+
inputs = [prompt, steps, seed, seed_checkbox]
|
156 |
+
generate_btn.click(fn=predict, inputs=inputs, outputs=image)
|
157 |
+
|
158 |
+
|
159 |
+
def start_demo_text_to_image(share=False):
|
160 |
+
demo.queue()
|
161 |
+
demo.launch(share=share)
|