Upload 5 files
Browse files- LICENSE +21 -0
- README.md +10 -6
- app.py +189 -0
- requirements.txt +13 -0
- style.css +16 -0
LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
MIT License
|
2 |
+
|
3 |
+
Copyright (c) 2023 hysts
|
4 |
+
|
5 |
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6 |
+
of this software and associated documentation files (the "Software"), to deal
|
7 |
+
in the Software without restriction, including without limitation the rights
|
8 |
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9 |
+
copies of the Software, and to permit persons to whom the Software is
|
10 |
+
furnished to do so, subject to the following conditions:
|
11 |
+
|
12 |
+
The above copyright notice and this permission notice shall be included in all
|
13 |
+
copies or substantial portions of the Software.
|
14 |
+
|
15 |
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16 |
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17 |
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18 |
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19 |
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20 |
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21 |
+
SOFTWARE.
|
README.md
CHANGED
@@ -1,12 +1,16 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
sdk: gradio
|
7 |
-
sdk_version:
|
8 |
app_file: app.py
|
9 |
-
|
|
|
|
|
|
|
|
|
10 |
---
|
11 |
|
12 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
---
|
2 |
+
title: Latent Consistency Models OpenVino CPU
|
3 |
+
emoji: 🥶
|
4 |
+
colorFrom: blue
|
5 |
+
colorTo: purple
|
6 |
sdk: gradio
|
7 |
+
sdk_version: 3.48.0
|
8 |
app_file: app.py
|
9 |
+
license: mit
|
10 |
+
pinned: true
|
11 |
+
suggested_hardware: cpu-basic
|
12 |
+
suggested_storage: small
|
13 |
+
hf_oauth: true
|
14 |
---
|
15 |
|
16 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
@@ -0,0 +1,189 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python
|
2 |
+
from __future__ import annotations
|
3 |
+
|
4 |
+
import os
|
5 |
+
import random
|
6 |
+
import time
|
7 |
+
|
8 |
+
import gradio as gr
|
9 |
+
import numpy as np
|
10 |
+
import PIL.Image
|
11 |
+
|
12 |
+
from huggingface_hub import snapshot_download
|
13 |
+
from diffusers import DiffusionPipeline
|
14 |
+
|
15 |
+
from optimum.intel.openvino.modeling_diffusion import OVModelVaeDecoder, OVBaseModel, OVStableDiffusionPipeline
|
16 |
+
|
17 |
+
import os
|
18 |
+
from tqdm import tqdm
|
19 |
+
import gradio_user_history as gr_user_history
|
20 |
+
|
21 |
+
from concurrent.futures import ThreadPoolExecutor
|
22 |
+
import uuid
|
23 |
+
|
24 |
+
DESCRIPTION = '''# Latent Consistency Model OpenVino CPU
|
25 |
+
Based on [Latency Consistency Model OpenVINO CPU](deinferno/Latent_Consistency_Model_OpenVino_CPU) HF space
|
26 |
+
|
27 |
+
Converted from [SoteMix](https://huggingface.co/Disty0/SoteMix) [Project page](https://latent-consistency-models.github.io)
|
28 |
+
|
29 |
+
<p>Running on CPU 🥶.</p>
|
30 |
+
'''
|
31 |
+
|
32 |
+
MAX_SEED = np.iinfo(np.int32).max
|
33 |
+
CACHE_EXAMPLES = os.getenv("CACHE_EXAMPLES") == "1"
|
34 |
+
|
35 |
+
model_id = "Disty0/LCM_SoteMix"
|
36 |
+
batch_size = 1
|
37 |
+
width = int(os.getenv("IMAGE_WIDTH", "512"))
|
38 |
+
height = int(os.getenv("IMAGE_HEIGHT", "512"))
|
39 |
+
num_images = int(os.getenv("NUM_IMAGES", "1"))
|
40 |
+
|
41 |
+
class CustomOVModelVaeDecoder(OVModelVaeDecoder):
|
42 |
+
def __init__(
|
43 |
+
self, model: openvino.runtime.Model, parent_model: OVBaseModel, ov_config: Optional[Dict[str, str]] = None, model_dir: str = None,
|
44 |
+
):
|
45 |
+
super(OVModelVaeDecoder, self).__init__(model, parent_model, ov_config, "vae_decoder", model_dir)
|
46 |
+
|
47 |
+
pipe = OVStableDiffusionPipeline.from_pretrained(model_id, scheduler = scheduler, compile = False, ov_config = {"CACHE_DIR":""})
|
48 |
+
|
49 |
+
# Inject TAESD
|
50 |
+
|
51 |
+
taesd_dir = snapshot_download(repo_id="deinferno/taesd-openvino")
|
52 |
+
pipe.vae_decoder = CustomOVModelVaeDecoder(model = OVBaseModel.load_model(f"{taesd_dir}/vae_decoder/openvino_model.xml"), parent_model = pipe, model_dir = taesd_dir)
|
53 |
+
|
54 |
+
pipe.reshape(batch_size=batch_size, height=height, width=width, num_images_per_prompt=num_images)
|
55 |
+
pipe.compile()
|
56 |
+
|
57 |
+
def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
58 |
+
if randomize_seed:
|
59 |
+
seed = random.randint(0, MAX_SEED)
|
60 |
+
return seed
|
61 |
+
|
62 |
+
def save_image(img, profile: gr.OAuthProfile | None, metadata: dict):
|
63 |
+
unique_name = str(uuid.uuid4()) + '.png'
|
64 |
+
img.save(unique_name)
|
65 |
+
gr_user_history.save_image(label=metadata["prompt"], image=img, profile=profile, metadata=metadata)
|
66 |
+
return unique_name
|
67 |
+
|
68 |
+
def save_images(image_array, profile: gr.OAuthProfile | None, metadata: dict):
|
69 |
+
paths = []
|
70 |
+
with ThreadPoolExecutor() as executor:
|
71 |
+
paths = list(executor.map(save_image, image_array, [profile]*len(image_array), [metadata]*len(image_array)))
|
72 |
+
return paths
|
73 |
+
|
74 |
+
def generate(
|
75 |
+
prompt: str,
|
76 |
+
seed: int = 0,
|
77 |
+
guidance_scale: float = 1.0,
|
78 |
+
num_inference_steps: int = 4,
|
79 |
+
randomize_seed: bool = False,
|
80 |
+
progress = gr.Progress(track_tqdm=True),
|
81 |
+
profile: gr.OAuthProfile | None = None,
|
82 |
+
) -> PIL.Image.Image:
|
83 |
+
global batch_size
|
84 |
+
global width
|
85 |
+
global height
|
86 |
+
global num_images
|
87 |
+
|
88 |
+
seed = randomize_seed_fn(seed, randomize_seed)
|
89 |
+
np.random.seed(seed)
|
90 |
+
start_time = time.time()
|
91 |
+
result = pipe(
|
92 |
+
prompt=prompt,
|
93 |
+
width=width,
|
94 |
+
height=height,
|
95 |
+
guidance_scale=guidance_scale,
|
96 |
+
num_inference_steps=num_inference_steps,
|
97 |
+
num_images_per_prompt=num_images,
|
98 |
+
output_type="pil",
|
99 |
+
).images
|
100 |
+
paths = save_images(result, profile, metadata={"prompt": prompt, "seed": seed, "width": width, "height": height, "guidance_scale": guidance_scale, "num_inference_steps": num_inference_steps})
|
101 |
+
print(time.time() - start_time)
|
102 |
+
return paths, seed
|
103 |
+
|
104 |
+
examples = [
|
105 |
+
"(masterpiece, best quality, highres), anime art style, pixiv, 1girl, solo",
|
106 |
+
"(masterpiece, best quality, highres), anime art style, pixiv, 1girl, solo, dark red hair, blue eyes, long hair, straight hair, medium breasts, mature female, sweater",
|
107 |
+
"(masterpiece, best quality, highres), anime art style, pixiv, 1girl, solo, pov, scenery, flowers, shrine, dark red hair, blue eyes, long hair, straight hair, medium breasts, mature female, sweater",
|
108 |
+
"(masterpiece, best quality, highres), anime art style, pixiv, 1girl, solo, abstract, abstract background, (bloom, swirling lights, light particles), floating, romanticized, blush, emotional, cat ears, fire, galaxy, dark red hair, blue eyes, long hair, straight hair, medium breasts, mature female, sweater",
|
109 |
+
]
|
110 |
+
|
111 |
+
|
112 |
+
with gr.Blocks(css="style.css") as demo:
|
113 |
+
gr.Markdown(DESCRIPTION)
|
114 |
+
gr.DuplicateButton(
|
115 |
+
value="Duplicate Space for private use",
|
116 |
+
elem_id="duplicate-button",
|
117 |
+
visible=os.getenv("SHOW_DUPLICATE_BUTTON") == "1",
|
118 |
+
)
|
119 |
+
with gr.Group():
|
120 |
+
with gr.Row():
|
121 |
+
prompt = gr.Text(
|
122 |
+
label="Prompt",
|
123 |
+
show_label=False,
|
124 |
+
max_lines=1,
|
125 |
+
placeholder="Enter your prompt",
|
126 |
+
container=False,
|
127 |
+
)
|
128 |
+
run_button = gr.Button("Run", scale=0)
|
129 |
+
result = gr.Gallery(
|
130 |
+
label="Generated images", show_label=False, elem_id="gallery", grid=[2]
|
131 |
+
)
|
132 |
+
with gr.Accordion("Advanced options", open=False):
|
133 |
+
seed = gr.Slider(
|
134 |
+
label="Seed",
|
135 |
+
minimum=0,
|
136 |
+
maximum=MAX_SEED,
|
137 |
+
step=1,
|
138 |
+
value=0,
|
139 |
+
randomize=True
|
140 |
+
)
|
141 |
+
randomize_seed = gr.Checkbox(label="Randomize seed across runs", value=True)
|
142 |
+
with gr.Row():
|
143 |
+
guidance_scale = gr.Slider(
|
144 |
+
label="Guidance scale for base",
|
145 |
+
minimum=2,
|
146 |
+
maximum=14,
|
147 |
+
step=0.1,
|
148 |
+
value=8.0,
|
149 |
+
)
|
150 |
+
num_inference_steps = gr.Slider(
|
151 |
+
label="Number of inference steps for base",
|
152 |
+
minimum=1,
|
153 |
+
maximum=8,
|
154 |
+
step=1,
|
155 |
+
value=4,
|
156 |
+
)
|
157 |
+
|
158 |
+
with gr.Accordion("Past generations", open=False):
|
159 |
+
gr_user_history.render()
|
160 |
+
|
161 |
+
gr.Examples(
|
162 |
+
examples=examples,
|
163 |
+
inputs=prompt,
|
164 |
+
outputs=result,
|
165 |
+
fn=generate,
|
166 |
+
cache_examples=CACHE_EXAMPLES,
|
167 |
+
)
|
168 |
+
|
169 |
+
gr.on(
|
170 |
+
triggers=[
|
171 |
+
prompt.submit,
|
172 |
+
run_button.click,
|
173 |
+
],
|
174 |
+
fn=generate,
|
175 |
+
inputs=[
|
176 |
+
prompt,
|
177 |
+
seed,
|
178 |
+
guidance_scale,
|
179 |
+
num_inference_steps,
|
180 |
+
randomize_seed
|
181 |
+
],
|
182 |
+
outputs=[result, seed],
|
183 |
+
api_name="run",
|
184 |
+
)
|
185 |
+
|
186 |
+
if __name__ == "__main__":
|
187 |
+
demo.queue(api_open=False)
|
188 |
+
# demo.queue(max_size=20).launch()
|
189 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
accelerate
|
2 |
+
diffusers==0.23.0
|
3 |
+
gradio==3.48.0
|
4 |
+
Pillow
|
5 |
+
--extra-index-url https://download.pytorch.org/whl/cpu
|
6 |
+
torch==2.1.0+cpu
|
7 |
+
openvino-nightly==2023.3.0.dev20231109
|
8 |
+
optimum==1.13.2
|
9 |
+
optimum-intel==1.12.1
|
10 |
+
onnx==1.16.2
|
11 |
+
transformers==4.35.0
|
12 |
+
|
13 |
+
git+https://huggingface.co/spaces/Wauplin/gradio-user-history
|
style.css
ADDED
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
h1 {
|
2 |
+
text-align: center;
|
3 |
+
}
|
4 |
+
|
5 |
+
#duplicate-button {
|
6 |
+
margin: auto;
|
7 |
+
color: #fff;
|
8 |
+
background: #1565c0;
|
9 |
+
border-radius: 100vh;
|
10 |
+
}
|
11 |
+
|
12 |
+
#component-0 {
|
13 |
+
max-width: 830px;
|
14 |
+
margin: auto;
|
15 |
+
padding-top: 1.5rem;
|
16 |
+
}
|