Commit
•
c5ba729
1
Parent(s):
370092f
app.py: novita api request added.
Browse files- .gitignore +1 -0
- app.py +234 -103
- test.ipynb +0 -0
.gitignore
CHANGED
@@ -1,4 +1,5 @@
|
|
1 |
|
2 |
*.pyc
|
|
|
3 |
|
4 |
gradio_cached_examples/
|
|
|
1 |
|
2 |
*.pyc
|
3 |
+
*.local
|
4 |
|
5 |
gradio_cached_examples/
|
app.py
CHANGED
@@ -1,27 +1,53 @@
|
|
1 |
|
|
|
2 |
import random
|
3 |
import numpy as np
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
from style_template import styles
|
6 |
|
7 |
-
import gradio as gr
|
8 |
-
|
9 |
|
10 |
|
11 |
# global variable
|
12 |
MAX_SEED = np.iinfo(np.int32).max
|
13 |
STYLE_NAMES = list(styles.keys())
|
14 |
-
DEFAULT_STYLE_NAME =
|
15 |
enable_lcm_arg = False
|
16 |
|
17 |
# Path to InstantID models
|
18 |
-
face_adapter = f
|
19 |
-
controlnet_path = f
|
20 |
|
21 |
# controlnet-pose/canny/depth
|
22 |
-
controlnet_pose_model =
|
23 |
-
controlnet_canny_model =
|
24 |
-
controlnet_depth_model =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
|
27 |
def toggle_lcm_ui (value):
|
@@ -47,70 +73,81 @@ def remove_tips ():
|
|
47 |
return gr.update(visible=False)
|
48 |
|
49 |
|
|
|
|
|
|
|
|
|
|
|
50 |
def get_example ():
|
51 |
case = [
|
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 |
],
|
80 |
[
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
],
|
87 |
]
|
88 |
return case
|
89 |
|
90 |
|
91 |
-
def
|
92 |
-
|
93 |
-
face_file
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
|
|
|
|
|
|
|
|
|
|
111 |
|
112 |
|
113 |
def generate_image (
|
|
|
114 |
face_image_path,
|
115 |
pose_image_path,
|
116 |
prompt,
|
@@ -130,15 +167,78 @@ def generate_image (
|
|
130 |
enhance_face_region,
|
131 |
progress=gr.Progress(track_tqdm=True),
|
132 |
):
|
133 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
134 |
|
135 |
|
136 |
# Description
|
137 |
-
title = r
|
138 |
<h1 align="center">InstantID: Zero-shot Identity-Preserving Generation in Seconds</h1>
|
139 |
-
|
140 |
|
141 |
-
description = r
|
142 |
<b>Official 🤗 Gradio demo</b> for <a href='https://github.com/InstantID/InstantID' target='_blank'><b>InstantID: Zero-shot Identity-Preserving Generation in Seconds</b></a>.<br>
|
143 |
|
144 |
We are organizing a Spring Festival event with HuggingFace from 2.7 to 2.25, and you can now generate pictures of Spring Festival costumes. Happy Dragon Year 🐲 ! Share the joy with your family.<br>
|
@@ -149,148 +249,157 @@ How to use:<br>
|
|
149 |
3. (Optional) You can select multiple ControlNet models to control the generation process. The default is to use the IdentityNet only. The ControlNet models include pose skeleton, canny, and depth. You can adjust the strength of each ControlNet model to control the generation process.
|
150 |
4. Enter a text prompt, as done in normal text-to-image models.
|
151 |
5. Click the <b>Submit</b> button to begin customization.
|
152 |
-
6. Share your customized photo with your friends and enjoy! 😊
|
153 |
|
154 |
-
article = r
|
155 |
---
|
156 |
-
|
157 |
|
158 |
-
tips = r
|
159 |
### Usage tips of InstantID
|
160 |
1. If you're not satisfied with the similarity, try increasing the weight of "IdentityNet Strength" and "Adapter Strength."
|
161 |
2. If you feel that the saturation is too high, first decrease the Adapter strength. If it remains too high, then decrease the IdentityNet strength.
|
162 |
3. If you find that text control is not as expected, decrease Adapter strength.
|
163 |
4. If you find that realistic style is not good enough, go for our Github repo and use a more realistic base model.
|
164 |
-
|
165 |
|
166 |
-
css =
|
167 |
.gradio-container {width: 85% !important}
|
168 |
-
|
169 |
with gr.Blocks(css=css) as demo:
|
170 |
# description
|
171 |
gr.Markdown(title)
|
172 |
gr.Markdown(description)
|
173 |
|
|
|
|
|
|
|
|
|
|
|
|
|
174 |
with gr.Row():
|
175 |
with gr.Column():
|
176 |
with gr.Row(equal_height=True):
|
177 |
# upload face image
|
178 |
face_file = gr.Image(
|
179 |
-
label=
|
180 |
)
|
181 |
# optional: upload a reference pose image
|
182 |
pose_file = gr.Image(
|
183 |
-
label=
|
184 |
-
type=
|
185 |
)
|
186 |
|
187 |
# prompt
|
188 |
prompt = gr.Textbox(
|
189 |
-
label=
|
190 |
-
info=
|
191 |
-
placeholder=
|
192 |
-
value=
|
193 |
)
|
194 |
|
195 |
-
submit = gr.Button(
|
196 |
enable_LCM = gr.Checkbox(
|
197 |
-
label=
|
198 |
-
info=
|
199 |
)
|
200 |
style = gr.Dropdown(
|
201 |
-
label=
|
202 |
choices=STYLE_NAMES,
|
203 |
value=DEFAULT_STYLE_NAME,
|
204 |
)
|
205 |
|
206 |
# strength
|
207 |
identitynet_strength_ratio = gr.Slider(
|
208 |
-
label=
|
209 |
minimum=0,
|
210 |
maximum=1.5,
|
211 |
step=0.05,
|
212 |
value=0.80,
|
213 |
)
|
214 |
adapter_strength_ratio = gr.Slider(
|
215 |
-
label=
|
216 |
minimum=0,
|
217 |
maximum=1.5,
|
218 |
step=0.05,
|
219 |
value=0.80,
|
220 |
)
|
221 |
-
with gr.Accordion(
|
222 |
controlnet_selection = gr.CheckboxGroup(
|
223 |
-
[
|
224 |
-
info=
|
225 |
)
|
226 |
pose_strength = gr.Slider(
|
227 |
-
label=
|
228 |
minimum=0,
|
229 |
maximum=1.5,
|
230 |
step=0.05,
|
231 |
value=0.40,
|
232 |
)
|
233 |
canny_strength = gr.Slider(
|
234 |
-
label=
|
235 |
minimum=0,
|
236 |
maximum=1.5,
|
237 |
step=0.05,
|
238 |
value=0.40,
|
239 |
)
|
240 |
depth_strength = gr.Slider(
|
241 |
-
label=
|
242 |
minimum=0,
|
243 |
maximum=1.5,
|
244 |
step=0.05,
|
245 |
value=0.40,
|
246 |
)
|
247 |
-
with gr.Accordion(open=False, label=
|
248 |
negative_prompt = gr.Textbox(
|
249 |
-
label=
|
250 |
-
placeholder=
|
251 |
-
value=
|
252 |
)
|
253 |
num_steps = gr.Slider(
|
254 |
-
label=
|
255 |
minimum=1,
|
256 |
maximum=100,
|
257 |
step=1,
|
258 |
value=5 if enable_lcm_arg else 30,
|
259 |
)
|
260 |
guidance_scale = gr.Slider(
|
261 |
-
label=
|
262 |
minimum=0.1,
|
263 |
maximum=20.0,
|
264 |
step=0.1,
|
265 |
value=0.0 if enable_lcm_arg else 5.0,
|
266 |
)
|
267 |
seed = gr.Slider(
|
268 |
-
label=
|
269 |
minimum=0,
|
270 |
maximum=MAX_SEED,
|
271 |
step=1,
|
272 |
value=42,
|
273 |
)
|
274 |
schedulers = [
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
|
|
|
|
|
|
|
281 |
]
|
282 |
scheduler = gr.Dropdown(
|
283 |
-
label=
|
284 |
choices=schedulers,
|
285 |
-
value=
|
286 |
)
|
287 |
-
randomize_seed = gr.Checkbox(label=
|
288 |
-
enhance_face_region = gr.Checkbox(label=
|
289 |
|
290 |
with gr.Column(scale=1):
|
291 |
-
gallery = gr.Image(label=
|
292 |
usage_tips = gr.Markdown(
|
293 |
-
label=
|
294 |
)
|
295 |
|
296 |
submit.click(
|
@@ -305,6 +414,7 @@ with gr.Blocks(css=css) as demo:
|
|
305 |
).then(
|
306 |
fn=generate_image,
|
307 |
inputs=[
|
|
|
308 |
face_file,
|
309 |
pose_file,
|
310 |
prompt,
|
@@ -336,12 +446,33 @@ with gr.Blocks(css=css) as demo:
|
|
336 |
gr.Examples(
|
337 |
examples=get_example(),
|
338 |
inputs=[face_file, pose_file, prompt, style, negative_prompt],
|
339 |
-
fn=
|
|
|
340 |
outputs=[gallery, usage_tips],
|
341 |
cache_examples=True,
|
342 |
)
|
343 |
|
344 |
gr.Markdown(article)
|
345 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
346 |
demo.queue(api_open=False)
|
347 |
demo.launch()
|
|
|
1 |
|
2 |
+
import os
|
3 |
import random
|
4 |
import numpy as np
|
5 |
+
import gradio as gr
|
6 |
+
import base64
|
7 |
+
from io import BytesIO
|
8 |
+
import PIL.Image
|
9 |
+
from typing import Tuple
|
10 |
+
from novita_client import NovitaClient, V3TaskResponseStatus
|
11 |
+
from time import sleep
|
12 |
|
13 |
from style_template import styles
|
14 |
|
|
|
|
|
15 |
|
16 |
|
17 |
# global variable
|
18 |
MAX_SEED = np.iinfo(np.int32).max
|
19 |
STYLE_NAMES = list(styles.keys())
|
20 |
+
DEFAULT_STYLE_NAME = 'Spring Festival'
|
21 |
enable_lcm_arg = False
|
22 |
|
23 |
# Path to InstantID models
|
24 |
+
face_adapter = f'./checkpoints/ip-adapter.bin'
|
25 |
+
controlnet_path = f'./checkpoints/ControlNetModel'
|
26 |
|
27 |
# controlnet-pose/canny/depth
|
28 |
+
controlnet_pose_model = 'thibaud/controlnet-openpose-sdxl-1.0'
|
29 |
+
controlnet_canny_model = 'diffusers/controlnet-canny-sdxl-1.0'
|
30 |
+
controlnet_depth_model = 'diffusers/controlnet-depth-sdxl-1.0-small'
|
31 |
+
|
32 |
+
|
33 |
+
def get_novita_client (novita_key):
|
34 |
+
client = NovitaClient(novita_key, os.getenv('NOVITA_API_URI', None))
|
35 |
+
return client
|
36 |
+
|
37 |
+
|
38 |
+
get_local_storage = '''
|
39 |
+
function () {
|
40 |
+
globalThis.setStorage = (key, value)=>{
|
41 |
+
localStorage.setItem(key, JSON.stringify(value))
|
42 |
+
}
|
43 |
+
globalThis.getStorage = (key, value)=>{
|
44 |
+
return JSON.parse(localStorage.getItem(key))
|
45 |
+
}
|
46 |
+
|
47 |
+
const novita_key = getStorage("novita_key")
|
48 |
+
return [novita_key];
|
49 |
+
}
|
50 |
+
'''
|
51 |
|
52 |
|
53 |
def toggle_lcm_ui (value):
|
|
|
73 |
return gr.update(visible=False)
|
74 |
|
75 |
|
76 |
+
def apply_style (style_name: str, positive: str, negative: str = "") -> Tuple[str, str]:
|
77 |
+
p, n = styles.get(style_name, styles[DEFAULT_STYLE_NAME])
|
78 |
+
return p.replace("{prompt}", positive), n + " " + negative
|
79 |
+
|
80 |
+
|
81 |
def get_example ():
|
82 |
case = [
|
83 |
[
|
84 |
+
'./examples/yann-lecun_resize.jpg',
|
85 |
+
'./examples/poses/pose.jpg',
|
86 |
+
'a man',
|
87 |
+
'Spring Festival',
|
88 |
+
'(lowres, low quality, worst quality:1.2), (text:1.2), watermark, (frame:1.2), deformed, ugly, deformed eyes, blur, out of focus, blurry, deformed cat, deformed, photo, anthropomorphic cat, monochrome, photo, pet collar, gun, weapon, blue, 3d, drones, drone, buildings in background, green',
|
89 |
],
|
90 |
[
|
91 |
+
'./examples/musk_resize.jpeg',
|
92 |
+
'./examples/poses/pose2.jpg',
|
93 |
+
'a man flying in the sky in Mars',
|
94 |
+
'Mars',
|
95 |
+
'(lowres, low quality, worst quality:1.2), (text:1.2), watermark, (frame:1.2), deformed, ugly, deformed eyes, blur, out of focus, blurry, deformed cat, deformed, photo, anthropomorphic cat, monochrome, photo, pet collar, gun, weapon, blue, 3d, drones, drone, buildings in background, green',
|
96 |
],
|
97 |
[
|
98 |
+
'./examples/sam_resize.png',
|
99 |
+
'./examples/poses/pose4.jpg',
|
100 |
+
'a man doing a silly pose wearing a suite',
|
101 |
+
'Jungle',
|
102 |
+
'(lowres, low quality, worst quality:1.2), (text:1.2), watermark, (frame:1.2), deformed, ugly, deformed eyes, blur, out of focus, blurry, deformed cat, deformed, photo, anthropomorphic cat, monochrome, photo, pet collar, gun, weapon, blue, 3d, drones, drone, buildings in background, gree',
|
103 |
],
|
104 |
[
|
105 |
+
'./examples/schmidhuber_resize.png',
|
106 |
+
'./examples/poses/pose3.jpg',
|
107 |
+
'a man sit on a chair',
|
108 |
+
'Neon',
|
109 |
+
'(lowres, low quality, worst quality:1.2), (text:1.2), watermark, (frame:1.2), deformed, ugly, deformed eyes, blur, out of focus, blurry, deformed cat, deformed, photo, anthropomorphic cat, monochrome, photo, pet collar, gun, weapon, blue, 3d, drones, drone, buildings in background, green',
|
110 |
],
|
111 |
[
|
112 |
+
'./examples/kaifu_resize.png',
|
113 |
+
'./examples/poses/pose.jpg',
|
114 |
+
'a man',
|
115 |
+
'Vibrant Color',
|
116 |
+
'(lowres, low quality, worst quality:1.2), (text:1.2), watermark, (frame:1.2), deformed, ugly, deformed eyes, blur, out of focus, blurry, deformed cat, deformed, photo, anthropomorphic cat, monochrome, photo, pet collar, gun, weapon, blue, 3d, drones, drone, buildings in background, green',
|
117 |
],
|
118 |
]
|
119 |
return case
|
120 |
|
121 |
|
122 |
+
def run_for_examples_with_key (novita_key):
|
123 |
+
def run_for_examples (face_file, pose_file, prompt, style, negative_prompt):
|
124 |
+
print('run_for_examples:', face_file)
|
125 |
+
return generate_image(
|
126 |
+
novita_key,
|
127 |
+
face_file,
|
128 |
+
pose_file,
|
129 |
+
prompt,
|
130 |
+
negative_prompt,
|
131 |
+
style,
|
132 |
+
20, # num_steps
|
133 |
+
0.8, # identitynet_strength_ratio
|
134 |
+
0.8, # adapter_strength_ratio
|
135 |
+
0.4, # pose_strength
|
136 |
+
0.3, # canny_strength
|
137 |
+
0.5, # depth_strength
|
138 |
+
['pose', 'canny'], # controlnet_selection
|
139 |
+
5.0, # guidance_scale
|
140 |
+
42, # seed
|
141 |
+
'EulerDiscreteScheduler', # scheduler
|
142 |
+
False, # enable_LCM
|
143 |
+
True, # enable_Face_Region
|
144 |
+
)
|
145 |
+
#return None, gr.update(visible=True)
|
146 |
+
return run_for_examples
|
147 |
|
148 |
|
149 |
def generate_image (
|
150 |
+
novita_key1,
|
151 |
face_image_path,
|
152 |
pose_image_path,
|
153 |
prompt,
|
|
|
167 |
enhance_face_region,
|
168 |
progress=gr.Progress(track_tqdm=True),
|
169 |
):
|
170 |
+
if face_image_path is None:
|
171 |
+
raise gr.Error(f'Cannot find any input face image! Please refer to step 1️⃣')
|
172 |
+
|
173 |
+
#print('novita_key:', novita_key1)
|
174 |
+
print('face_image_path:', face_image_path)
|
175 |
+
if not novita_key1:
|
176 |
+
raise gr.Error(f'Please input your Novita Key!')
|
177 |
+
try:
|
178 |
+
client = get_novita_client(novita_key1)
|
179 |
+
prompt, negative_prompt = apply_style(style_name, prompt, negative_prompt)
|
180 |
+
#print('prompt:', prompt)
|
181 |
+
#print('negative_prompt:', negative_prompt)
|
182 |
+
#print('seed:', seed)
|
183 |
+
#print('identitynet_strength_ratio:', identitynet_strength_ratio)
|
184 |
+
#print('adapter_strength_ratio:', adapter_strength_ratio)
|
185 |
+
#print('scheduler:', scheduler)
|
186 |
+
#print('guidance_scale:', guidance_scale)
|
187 |
+
#print('num_steps:', num_steps)
|
188 |
+
|
189 |
+
ref_image_path = pose_image_path if pose_image_path else face_image_path
|
190 |
+
ref_image = PIL.Image.open(ref_image_path)
|
191 |
+
width, height = ref_image.size
|
192 |
+
|
193 |
+
res = client._post('/v3/async/instant-id', {
|
194 |
+
'extra': {
|
195 |
+
'response_image_type': 'jpeg',
|
196 |
+
},
|
197 |
+
'model_name': 'sd_xl_base_1.0.safetensors',
|
198 |
+
'face_image_assets_ids': client.upload_assets([face_image_path]),
|
199 |
+
'ref_image_assets_ids': client.upload_assets([pose_image_path]) if pose_image_path else [],
|
200 |
+
'prompt': prompt,
|
201 |
+
'negative_prompt': negative_prompt,
|
202 |
+
'controlnet': {
|
203 |
+
'units': [], # TODO
|
204 |
+
},
|
205 |
+
'image_num': 1,
|
206 |
+
'steps': num_steps,
|
207 |
+
'seed': seed,
|
208 |
+
'guidance_scale': guidance_scale,
|
209 |
+
'sampler_name': scheduler,
|
210 |
+
'id_strength': identitynet_strength_ratio,
|
211 |
+
'adapter_strength': adapter_strength_ratio,
|
212 |
+
'width': width,
|
213 |
+
'height': height,
|
214 |
+
})
|
215 |
+
|
216 |
+
print('task_id:', res['task_id'])
|
217 |
+
def progress (x):
|
218 |
+
print('progress:', x.task.status)
|
219 |
+
final_res = client.wait_for_task_v3(res['task_id'], callback=progress)
|
220 |
+
print('status:', final_res.task.status)
|
221 |
+
if final_res.task.status == V3TaskResponseStatus.TASK_STATUS_FAILED:
|
222 |
+
raise RuntimeError(f'Novita task failed: {final_res.task.status}')
|
223 |
+
|
224 |
+
final_res.download_images()
|
225 |
+
except Exception as e:
|
226 |
+
raise gr.Error(f'Error: {e}')
|
227 |
+
|
228 |
+
#print('final_res:', final_res)
|
229 |
+
#print('final_res.images_encoded:', final_res.images_encoded)
|
230 |
+
|
231 |
+
image = PIL.Image.open(BytesIO(base64.b64decode(final_res.images_encoded[0])))
|
232 |
+
|
233 |
+
return image, gr.update(visible=True)
|
234 |
|
235 |
|
236 |
# Description
|
237 |
+
title = r'''
|
238 |
<h1 align="center">InstantID: Zero-shot Identity-Preserving Generation in Seconds</h1>
|
239 |
+
'''
|
240 |
|
241 |
+
description = r'''
|
242 |
<b>Official 🤗 Gradio demo</b> for <a href='https://github.com/InstantID/InstantID' target='_blank'><b>InstantID: Zero-shot Identity-Preserving Generation in Seconds</b></a>.<br>
|
243 |
|
244 |
We are organizing a Spring Festival event with HuggingFace from 2.7 to 2.25, and you can now generate pictures of Spring Festival costumes. Happy Dragon Year 🐲 ! Share the joy with your family.<br>
|
|
|
249 |
3. (Optional) You can select multiple ControlNet models to control the generation process. The default is to use the IdentityNet only. The ControlNet models include pose skeleton, canny, and depth. You can adjust the strength of each ControlNet model to control the generation process.
|
250 |
4. Enter a text prompt, as done in normal text-to-image models.
|
251 |
5. Click the <b>Submit</b> button to begin customization.
|
252 |
+
6. Share your customized photo with your friends and enjoy! 😊'''
|
253 |
|
254 |
+
article = r'''
|
255 |
---
|
256 |
+
'''
|
257 |
|
258 |
+
tips = r'''
|
259 |
### Usage tips of InstantID
|
260 |
1. If you're not satisfied with the similarity, try increasing the weight of "IdentityNet Strength" and "Adapter Strength."
|
261 |
2. If you feel that the saturation is too high, first decrease the Adapter strength. If it remains too high, then decrease the IdentityNet strength.
|
262 |
3. If you find that text control is not as expected, decrease Adapter strength.
|
263 |
4. If you find that realistic style is not good enough, go for our Github repo and use a more realistic base model.
|
264 |
+
'''
|
265 |
|
266 |
+
css = '''
|
267 |
.gradio-container {width: 85% !important}
|
268 |
+
'''
|
269 |
with gr.Blocks(css=css) as demo:
|
270 |
# description
|
271 |
gr.Markdown(title)
|
272 |
gr.Markdown(description)
|
273 |
|
274 |
+
with gr.Row():
|
275 |
+
with gr.Column(scale=1):
|
276 |
+
novita_key = gr.Textbox(value='', label='Novita.AI API KEY (store in broweser)', placeholder='novita.ai api key', type='password')
|
277 |
+
with gr.Column(scale=1):
|
278 |
+
user_balance = gr.Textbox(label='User Balance', value='0.0')
|
279 |
+
|
280 |
with gr.Row():
|
281 |
with gr.Column():
|
282 |
with gr.Row(equal_height=True):
|
283 |
# upload face image
|
284 |
face_file = gr.Image(
|
285 |
+
label='Upload a photo of your face', type='filepath'
|
286 |
)
|
287 |
# optional: upload a reference pose image
|
288 |
pose_file = gr.Image(
|
289 |
+
label='Upload a reference pose image (Optional)',
|
290 |
+
type='filepath',
|
291 |
)
|
292 |
|
293 |
# prompt
|
294 |
prompt = gr.Textbox(
|
295 |
+
label='Prompt',
|
296 |
+
info='Give simple prompt is enough to achieve good face fidelity',
|
297 |
+
placeholder='A photo of a person',
|
298 |
+
value='',
|
299 |
)
|
300 |
|
301 |
+
submit = gr.Button('Submit', variant='primary')
|
302 |
enable_LCM = gr.Checkbox(
|
303 |
+
label='Enable Fast Inference with LCM', value=enable_lcm_arg,
|
304 |
+
info='LCM speeds up the inference step, the trade-off is the quality of the generated image. It performs better with portrait face images rather than distant faces',
|
305 |
)
|
306 |
style = gr.Dropdown(
|
307 |
+
label='Style template',
|
308 |
choices=STYLE_NAMES,
|
309 |
value=DEFAULT_STYLE_NAME,
|
310 |
)
|
311 |
|
312 |
# strength
|
313 |
identitynet_strength_ratio = gr.Slider(
|
314 |
+
label='IdentityNet strength (for fidelity)',
|
315 |
minimum=0,
|
316 |
maximum=1.5,
|
317 |
step=0.05,
|
318 |
value=0.80,
|
319 |
)
|
320 |
adapter_strength_ratio = gr.Slider(
|
321 |
+
label='Image adapter strength (for detail)',
|
322 |
minimum=0,
|
323 |
maximum=1.5,
|
324 |
step=0.05,
|
325 |
value=0.80,
|
326 |
)
|
327 |
+
with gr.Accordion('Controlnet'):
|
328 |
controlnet_selection = gr.CheckboxGroup(
|
329 |
+
['pose', 'canny', 'depth'], label='Controlnet', value=['pose'],
|
330 |
+
info='Use pose for skeleton inference, canny for edge detection, and depth for depth map estimation. You can try all three to control the generation process'
|
331 |
)
|
332 |
pose_strength = gr.Slider(
|
333 |
+
label='Pose strength',
|
334 |
minimum=0,
|
335 |
maximum=1.5,
|
336 |
step=0.05,
|
337 |
value=0.40,
|
338 |
)
|
339 |
canny_strength = gr.Slider(
|
340 |
+
label='Canny strength',
|
341 |
minimum=0,
|
342 |
maximum=1.5,
|
343 |
step=0.05,
|
344 |
value=0.40,
|
345 |
)
|
346 |
depth_strength = gr.Slider(
|
347 |
+
label='Depth strength',
|
348 |
minimum=0,
|
349 |
maximum=1.5,
|
350 |
step=0.05,
|
351 |
value=0.40,
|
352 |
)
|
353 |
+
with gr.Accordion(open=False, label='Advanced Options'):
|
354 |
negative_prompt = gr.Textbox(
|
355 |
+
label='Negative Prompt',
|
356 |
+
placeholder='low quality',
|
357 |
+
value='(lowres, low quality, worst quality:1.2), (text:1.2), watermark, (frame:1.2), deformed, ugly, deformed eyes, blur, out of focus, blurry, deformed cat, deformed, photo, anthropomorphic cat, monochrome, pet collar, gun, weapon, blue, 3d, drones, drone, buildings in background, green',
|
358 |
)
|
359 |
num_steps = gr.Slider(
|
360 |
+
label='Number of sample steps',
|
361 |
minimum=1,
|
362 |
maximum=100,
|
363 |
step=1,
|
364 |
value=5 if enable_lcm_arg else 30,
|
365 |
)
|
366 |
guidance_scale = gr.Slider(
|
367 |
+
label='Guidance scale',
|
368 |
minimum=0.1,
|
369 |
maximum=20.0,
|
370 |
step=0.1,
|
371 |
value=0.0 if enable_lcm_arg else 5.0,
|
372 |
)
|
373 |
seed = gr.Slider(
|
374 |
+
label='Seed',
|
375 |
minimum=0,
|
376 |
maximum=MAX_SEED,
|
377 |
step=1,
|
378 |
value=42,
|
379 |
)
|
380 |
schedulers = [
|
381 |
+
'Euler',
|
382 |
+
'Euler a',
|
383 |
+
'Heun',
|
384 |
+
'DPM++ SDE',
|
385 |
+
'DPM++ SDE Karras',
|
386 |
+
'DPM2',
|
387 |
+
'DPM2 Karras',
|
388 |
+
'DPM2 a',
|
389 |
+
'DPM2 a Karras',
|
390 |
]
|
391 |
scheduler = gr.Dropdown(
|
392 |
+
label='Schedulers',
|
393 |
choices=schedulers,
|
394 |
+
value='Euler a',
|
395 |
)
|
396 |
+
randomize_seed = gr.Checkbox(label='Randomize seed', value=True)
|
397 |
+
enhance_face_region = gr.Checkbox(label='Enhance non-face region', value=True)
|
398 |
|
399 |
with gr.Column(scale=1):
|
400 |
+
gallery = gr.Image(label='Generated Images')
|
401 |
usage_tips = gr.Markdown(
|
402 |
+
label='InstantID Usage Tips', value=tips, visible=False
|
403 |
)
|
404 |
|
405 |
submit.click(
|
|
|
414 |
).then(
|
415 |
fn=generate_image,
|
416 |
inputs=[
|
417 |
+
novita_key,
|
418 |
face_file,
|
419 |
pose_file,
|
420 |
prompt,
|
|
|
446 |
gr.Examples(
|
447 |
examples=get_example(),
|
448 |
inputs=[face_file, pose_file, prompt, style, negative_prompt],
|
449 |
+
fn=run_for_examples_with_key(novita_key),
|
450 |
+
run_on_click=True,
|
451 |
outputs=[gallery, usage_tips],
|
452 |
cache_examples=True,
|
453 |
)
|
454 |
|
455 |
gr.Markdown(article)
|
456 |
|
457 |
+
def onload(novita_key):
|
458 |
+
if novita_key is None or novita_key == '':
|
459 |
+
return novita_key, f'$ UNKNOWN', gr.update(visible=False)
|
460 |
+
try:
|
461 |
+
user_info_json = get_novita_client(novita_key).user_info()
|
462 |
+
except Exception as e:
|
463 |
+
return novita_key, f'$ UNKNOWN'
|
464 |
+
|
465 |
+
return novita_key, f'$ {user_info_json.credit_balance / 100 / 100:.2f}'
|
466 |
+
|
467 |
+
novita_key.change(onload, inputs=novita_key, outputs=[novita_key, user_balance], js='v=>{ setStorage("novita_key", v); return [v]; }')
|
468 |
+
|
469 |
+
demo.load(
|
470 |
+
inputs=[novita_key],
|
471 |
+
outputs=[novita_key, user_balance],
|
472 |
+
fn=onload,
|
473 |
+
js=get_local_storage,
|
474 |
+
)
|
475 |
+
|
476 |
+
|
477 |
demo.queue(api_open=False)
|
478 |
demo.launch()
|
test.ipynb
CHANGED
The diff for this file is too large to render.
See raw diff
|
|