Spaces:
Running
on
Zero
Running
on
Zero
SunderAli17
commited on
Commit
•
3d54858
1
Parent(s):
903c8f1
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,208 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import spaces
|
2 |
+
import gradio as gr
|
3 |
+
import numpy as np
|
4 |
+
import torch
|
5 |
+
|
6 |
+
from toonmage import attention_processor as attention
|
7 |
+
from toonmage.pipeline import ToonMagePipeline
|
8 |
+
from toonmage.utils import resize_numpy_image_long, seed_everything
|
9 |
+
|
10 |
+
torch.set_grad_enabled(False)
|
11 |
+
|
12 |
+
pipeline = PuLIDPipeline()
|
13 |
+
|
14 |
+
# other params
|
15 |
+
DEFAULT_NEGATIVE_PROMPT = (
|
16 |
+
'cross-eyed, blurry, deformed eyeballs, deformed, deformed or partially rendered eyes, partially rendered objects, low resolution, disfigured hands, ugly, mutated, glitch,'
|
17 |
+
'watermark, text, artifacts noise, worst quality, low quality, non-HDRi, lowres, flaws, flaws in the face, flaws in the eyes, extra limbs, signature'
|
18 |
+
)
|
19 |
+
|
20 |
+
|
21 |
+
@spaces.GPU
|
22 |
+
def run(*args):
|
23 |
+
id_image = args[0]
|
24 |
+
supp_images = args[1:4]
|
25 |
+
prompt, neg_prompt, scale, n_samples, seed, steps, H, W, id_scale, mode, id_mix = args[4:]
|
26 |
+
|
27 |
+
pipeline.debug_img_list = []
|
28 |
+
if mode == 'fidelity':
|
29 |
+
attention.NUM_ZERO = 8
|
30 |
+
attention.ORTHO = False
|
31 |
+
attention.ORTHO_v2 = True
|
32 |
+
elif mode == 'extremely style':
|
33 |
+
attention.NUM_ZERO = 16
|
34 |
+
attention.ORTHO = True
|
35 |
+
attention.ORTHO_v2 = False
|
36 |
+
else:
|
37 |
+
raise ValueError
|
38 |
+
|
39 |
+
if id_image is not None:
|
40 |
+
id_image = resize_numpy_image_long(id_image, 1024)
|
41 |
+
id_embeddings = pipeline.get_id_embedding(id_image)
|
42 |
+
for supp_id_image in supp_images:
|
43 |
+
if supp_id_image is not None:
|
44 |
+
supp_id_image = resize_numpy_image_long(supp_id_image, 1024)
|
45 |
+
supp_id_embeddings = pipeline.get_id_embedding(supp_id_image)
|
46 |
+
id_embeddings = torch.cat(
|
47 |
+
(id_embeddings, supp_id_embeddings if id_mix else supp_id_embeddings[:, :5]), dim=1
|
48 |
+
)
|
49 |
+
else:
|
50 |
+
id_embeddings = None
|
51 |
+
|
52 |
+
seed_everything(seed)
|
53 |
+
ims = []
|
54 |
+
for _ in range(n_samples):
|
55 |
+
img = pipeline.inference(prompt, (1, H, W), neg_prompt, id_embeddings, id_scale, scale, steps)[0]
|
56 |
+
ims.append(np.array(img))
|
57 |
+
|
58 |
+
return ims, pipeline.debug_img_list
|
59 |
+
|
60 |
+
|
61 |
+
_MARKDOWN_ = """
|
62 |
+
This demo utilizes <a href="https://huggingface.co/black-forest-labs/FLUX.1-dev">FLUX Pipeline</a> for Image to Image Translation
|
63 |
+
**Tips**
|
64 |
+
- Smaller value of timestep to start inserting ID would lead to higher fidelity, however, it will reduce the editability; and vice versa.
|
65 |
+
Its value range is from 0 - 4. If you want to generate a stylized scene; use the value of 0 - 1. If you want to generate a photorealistic image; use the value of 4.
|
66 |
+
-It is recommended to use fake CFG by setting the true CFG scale value to 1 while you can vary the guidance scale. However, in a few cases, utilizing a true CFG can yield better results.
|
67 |
+
Try out with different prompts using your image and do provide your feedback.
|
68 |
+
**Demo by [Sunder Ali Khowaja](https://sander-ali.github.io) - [X](https://x.com/SunderAKhowaja) -[Github](https://github.com/sander-ali) -[Hugging Face](https://huggingface.co/SunderAli17)**
|
69 |
+
"""
|
70 |
+
|
71 |
+
theme = gr.themes.Soft(
|
72 |
+
font=[gr.themes.GoogleFont('Source Code Pro'), gr.themes.GoogleFont('Public Sans'), 'system-ui', 'sans-serif'],
|
73 |
+
)
|
74 |
+
js_func = """
|
75 |
+
function refresh() {
|
76 |
+
const url = new URL(window.location);
|
77 |
+
if (url.searchParams.get('__theme') !== 'dark') {
|
78 |
+
url.searchParams.set('__theme', 'dark');
|
79 |
+
window.location.href = url.href;
|
80 |
+
}
|
81 |
+
}
|
82 |
+
"""
|
83 |
+
|
84 |
+
|
85 |
+
with gr.Blocks(title="ToonMagev2", js = js_func, theme = theme") as SAK:
|
86 |
+
gr.Markdown(_MARKDOWN_)
|
87 |
+
with gr.Row():
|
88 |
+
with gr.Column():
|
89 |
+
with gr.Row():
|
90 |
+
face_image = gr.Image(label="ID image (main)", sources="upload", type="numpy", height=256)
|
91 |
+
supp_image1 = gr.Image(
|
92 |
+
label="Additional ID image (auxiliary)", sources="upload", type="numpy", height=256
|
93 |
+
)
|
94 |
+
supp_image2 = gr.Image(
|
95 |
+
label="Additional ID image (auxiliary)", sources="upload", type="numpy", height=256
|
96 |
+
)
|
97 |
+
supp_image3 = gr.Image(
|
98 |
+
label="Additional ID image (auxiliary)", sources="upload", type="numpy", height=256
|
99 |
+
)
|
100 |
+
prompt = gr.Textbox(label="Prompt", value='portrait,cinematic,wolf ears,white hair')
|
101 |
+
submit = gr.Button("Generate")
|
102 |
+
neg_prompt = gr.Textbox(label="Negative Prompt", value=DEFAULT_NEGATIVE_PROMPT)
|
103 |
+
scale = gr.Slider(
|
104 |
+
label="CFG, recommend value range [1, 1.5], 1 will be faster ",
|
105 |
+
value=1.2,
|
106 |
+
minimum=1,
|
107 |
+
maximum=1.5,
|
108 |
+
step=0.1,
|
109 |
+
)
|
110 |
+
n_samples = gr.Slider(label="Num samples", value=4, minimum=1, maximum=4, step=1)
|
111 |
+
seed = gr.Slider(
|
112 |
+
label="Seed", value=42, minimum=np.iinfo(np.uint32).min, maximum=np.iinfo(np.uint32).max, step=1
|
113 |
+
)
|
114 |
+
steps = gr.Slider(label="Steps", value=4, minimum=1, maximum=8, step=1)
|
115 |
+
with gr.Row():
|
116 |
+
H = gr.Slider(label="Height", value=1024, minimum=512, maximum=1280, step=64)
|
117 |
+
W = gr.Slider(label="Width", value=768, minimum=512, maximum=1280, step=64)
|
118 |
+
with gr.Row():
|
119 |
+
id_scale = gr.Slider(label="ID scale", minimum=0, maximum=5, step=0.05, value=0.8, interactive=True)
|
120 |
+
mode = gr.Dropdown(label="mode", choices=['fidelity', 'extremely style'], value='fidelity')
|
121 |
+
id_mix = gr.Checkbox(
|
122 |
+
label="ID Mix (if you want to mix two ID image, please turn this on, otherwise, turn this off)",
|
123 |
+
value=False,
|
124 |
+
)
|
125 |
+
|
126 |
+
gr.Markdown("## Examples")
|
127 |
+
example_inps = [
|
128 |
+
[
|
129 |
+
'portrait,cinematic,wolf ears,white hair',
|
130 |
+
'sample_img/sample_img_test24.jpg',
|
131 |
+
'fidelity',
|
132 |
+
]
|
133 |
+
]
|
134 |
+
gr.Examples(examples=example_inps, inputs=[prompt, face_image, mode], label='realistic')
|
135 |
+
|
136 |
+
example_inps = [
|
137 |
+
[
|
138 |
+
'portrait, impressionist painting, loose brushwork, vibrant color, light and shadow play',
|
139 |
+
'sample_img/sample_img_test1.jpg',
|
140 |
+
'fidelity',
|
141 |
+
]
|
142 |
+
]
|
143 |
+
gr.Examples(examples=example_inps, inputs=[prompt, face_image, mode], label='painting style')
|
144 |
+
|
145 |
+
example_inps = [
|
146 |
+
[
|
147 |
+
'portrait, flat papercut style, silhouette, clean cuts, paper, sharp edges, minimalist,color block,man',
|
148 |
+
'sample_img/lecun.jpg',
|
149 |
+
'fidelity',
|
150 |
+
]
|
151 |
+
]
|
152 |
+
gr.Examples(examples=example_inps, inputs=[prompt, face_image, mode], label='papercut style')
|
153 |
+
|
154 |
+
example_inps = [
|
155 |
+
[
|
156 |
+
'woman,cartoon,solo,Popmart Blind Box, Super Mario, 3d',
|
157 |
+
'sample_img/sample_img_test24.jpg',
|
158 |
+
'fidelity',
|
159 |
+
]
|
160 |
+
]
|
161 |
+
gr.Examples(examples=example_inps, inputs=[prompt, face_image, mode], label='3d style')
|
162 |
+
|
163 |
+
example_inps = [
|
164 |
+
[
|
165 |
+
'portrait, the legend of zelda, anime',
|
166 |
+
'sample_img/image1.png',
|
167 |
+
'extremely style',
|
168 |
+
]
|
169 |
+
]
|
170 |
+
gr.Examples(examples=example_inps, inputs=[prompt, face_image, mode], label='anime style')
|
171 |
+
|
172 |
+
example_inps = [
|
173 |
+
[
|
174 |
+
'portrait, superman',
|
175 |
+
'sample_img/lecun.jpg',
|
176 |
+
'sample_img/sample_img_test1.jpg',
|
177 |
+
'fidelity',
|
178 |
+
True,
|
179 |
+
]
|
180 |
+
]
|
181 |
+
gr.Examples(examples=example_inps, inputs=[prompt, face_image, supp_image1, mode, id_mix], label='id mix')
|
182 |
+
|
183 |
+
with gr.Column():
|
184 |
+
output = gr.Gallery(label='Output', elem_id="gallery")
|
185 |
+
intermediate_output = gr.Gallery(label='DebugImage', elem_id="gallery", visible=False)
|
186 |
+
gr.Markdown(_CITE_)
|
187 |
+
|
188 |
+
inps = [
|
189 |
+
face_image,
|
190 |
+
supp_image1,
|
191 |
+
supp_image2,
|
192 |
+
supp_image3,
|
193 |
+
prompt,
|
194 |
+
neg_prompt,
|
195 |
+
scale,
|
196 |
+
n_samples,
|
197 |
+
seed,
|
198 |
+
steps,
|
199 |
+
H,
|
200 |
+
W,
|
201 |
+
id_scale,
|
202 |
+
mode,
|
203 |
+
id_mix,
|
204 |
+
]
|
205 |
+
submit.click(fn=run, inputs=inps, outputs=[output, intermediate_output])
|
206 |
+
|
207 |
+
|
208 |
+
demo.launch()
|