Spaces:
Runtime error
Runtime error
ClaireOzzz
commited on
Commit
•
847b4f9
1
Parent(s):
48462d5
Update app_inference.py
Browse files- app_inference.py +131 -132
app_inference.py
CHANGED
@@ -226,138 +226,137 @@ button#load_model_btn{
|
|
226 |
font-size: 0.9em;
|
227 |
}
|
228 |
"""
|
229 |
-
def create_inference_demo() -> gr.Blocks:
|
230 |
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
|
282 |
-
|
283 |
-
|
284 |
-
|
285 |
-
|
286 |
-
|
287 |
-
|
288 |
-
|
289 |
-
|
290 |
-
|
291 |
-
|
292 |
-
|
293 |
-
|
294 |
-
|
295 |
-
|
296 |
-
|
297 |
-
|
298 |
-
|
299 |
-
|
300 |
-
|
301 |
-
|
302 |
-
|
303 |
-
|
304 |
-
|
305 |
-
|
306 |
-
|
307 |
-
|
308 |
-
|
309 |
-
|
310 |
-
|
311 |
-
|
312 |
-
|
313 |
-
|
314 |
-
|
315 |
-
|
316 |
-
|
317 |
-
|
318 |
-
|
319 |
-
|
320 |
-
|
321 |
-
|
322 |
-
|
323 |
-
|
324 |
-
|
325 |
-
|
326 |
-
|
327 |
-
|
328 |
-
|
329 |
-
|
330 |
-
|
331 |
-
|
332 |
-
|
333 |
-
|
334 |
-
|
335 |
-
|
336 |
-
|
337 |
-
|
338 |
-
|
339 |
-
|
340 |
-
|
341 |
-
|
342 |
-
|
343 |
-
|
344 |
-
|
345 |
-
|
346 |
-
|
347 |
-
|
348 |
-
|
349 |
-
|
350 |
-
|
351 |
-
|
352 |
-
|
353 |
-
|
354 |
-
|
355 |
-
|
356 |
-
|
357 |
-
|
358 |
-
|
359 |
-
|
360 |
-
return demo
|
361 |
|
362 |
|
363 |
-
|
|
|
226 |
font-size: 0.9em;
|
227 |
}
|
228 |
"""
|
229 |
+
# def create_inference_demo() -> gr.Blocks:
|
230 |
|
231 |
+
with gr.Blocks(css=css) as demo:
|
232 |
+
with gr.Column(elem_id="col-container"):
|
233 |
+
if is_shared_ui:
|
234 |
+
top_description = gr.HTML(f'''
|
235 |
+
<div class="gr-prose">
|
236 |
+
<h2><svg xmlns="http://www.w3.org/2000/svg" width="18px" height="18px" style="margin-right: 0px;display: inline-block;"fill="none"><path fill="#fff" d="M7 13.2a6.3 6.3 0 0 0 4.4-10.7A6.3 6.3 0 0 0 .6 6.9 6.3 6.3 0 0 0 7 13.2Z"/><path fill="#fff" fill-rule="evenodd" d="M7 0a6.9 6.9 0 0 1 4.8 11.8A6.9 6.9 0 0 1 0 7 6.9 6.9 0 0 1 7 0Zm0 0v.7V0ZM0 7h.6H0Zm7 6.8v-.6.6ZM13.7 7h-.6.6ZM9.1 1.7c-.7-.3-1.4-.4-2.2-.4a5.6 5.6 0 0 0-4 1.6 5.6 5.6 0 0 0-1.6 4 5.6 5.6 0 0 0 1.6 4 5.6 5.6 0 0 0 4 1.7 5.6 5.6 0 0 0 4-1.7 5.6 5.6 0 0 0 1.7-4 5.6 5.6 0 0 0-1.7-4c-.5-.5-1.1-.9-1.8-1.2Z" clip-rule="evenodd"/><path fill="#000" fill-rule="evenodd" d="M7 2.9a.8.8 0 1 1 0 1.5A.8.8 0 0 1 7 3ZM5.8 5.7c0-.4.3-.6.6-.6h.7c.3 0 .6.2.6.6v3.7h.5a.6.6 0 0 1 0 1.3H6a.6.6 0 0 1 0-1.3h.4v-3a.6.6 0 0 1-.6-.7Z" clip-rule="evenodd"/></svg>
|
237 |
+
Note: you might want to use a <strong>private</strong> custom LoRa model</h2>
|
238 |
+
<p class="main-message">
|
239 |
+
To do so, <strong>duplicate the Space</strong> and run it on your own profile using <strong>your own access token</strong> and eventually a GPU (T4-small or A10G-small) for faster inference without waiting in the queue.<br />
|
240 |
+
</p>
|
241 |
+
<p class="actions">
|
242 |
+
<a href="https://huggingface.co/spaces/{os.environ['SPACE_ID']}?duplicate=true">
|
243 |
+
<img src="https://huggingface.co/datasets/huggingface/badges/resolve/main/duplicate-this-space-lg-dark.svg" alt="Duplicate this Space" />
|
244 |
+
</a>
|
245 |
+
to start using private models and skip the queue
|
246 |
+
</p>
|
247 |
+
</div>
|
248 |
+
''', elem_id="warning-duplicate")
|
249 |
+
gr.HTML("""
|
250 |
+
<h2 style="text-align: center;">SD-XL Control LoRas</h2>
|
251 |
+
<p style="text-align: center;">Use StableDiffusion XL with <a href="https://huggingface.co/collections/diffusers/sdxl-controlnets-64f9c35846f3f06f5abe351f">Diffusers' SDXL ControlNets</a></p>
|
252 |
+
""")
|
253 |
+
|
254 |
+
use_custom_model = gr.Checkbox(label="Use a custom pre-trained LoRa model ? (optional)", value=False, info="To use a private model, you'll need to duplicate the space with your own access token.")
|
255 |
+
|
256 |
+
with gr.Blocks(visible=False) as custom_model_box:
|
257 |
+
with gr.Row():
|
258 |
+
with gr.Column():
|
259 |
+
if not is_shared_ui:
|
260 |
+
your_username = api.whoami()["name"]
|
261 |
+
my_models = api.list_models(author=your_username, filter=["diffusers", "stable-diffusion-xl", 'lora'])
|
262 |
+
model_names = [item.modelId for item in my_models]
|
263 |
+
|
264 |
+
if not is_shared_ui:
|
265 |
+
custom_model = gr.Dropdown(
|
266 |
+
label = "Your custom model ID",
|
267 |
+
info="You can pick one of your private models",
|
268 |
+
choices = model_names,
|
269 |
+
allow_custom_value = True
|
270 |
+
#placeholder = "username/model_id"
|
271 |
+
)
|
272 |
+
else:
|
273 |
+
custom_model = gr.Textbox(
|
274 |
+
label="Your custom model ID",
|
275 |
+
placeholder="your_username/your_trained_model_name",
|
276 |
+
info="Make sure your model is set to PUBLIC"
|
277 |
+
)
|
278 |
+
|
279 |
+
weight_name = gr.Dropdown(
|
280 |
+
label="Safetensors file",
|
281 |
+
#value="pytorch_lora_weights.safetensors",
|
282 |
+
info="specify which one if model has several .safetensors files",
|
283 |
+
allow_custom_value=True,
|
284 |
+
visible = False
|
285 |
+
)
|
286 |
+
with gr.Column():
|
287 |
+
with gr.Group():
|
288 |
+
load_model_btn = gr.Button("Load my model", elem_id="load_model_btn")
|
289 |
+
previous_model = gr.Textbox(
|
290 |
+
visible = False
|
291 |
+
)
|
292 |
+
model_status = gr.Textbox(
|
293 |
+
label = "model status",
|
294 |
+
show_label = False,
|
295 |
+
elem_id = "status_info"
|
296 |
+
)
|
297 |
+
trigger_word = gr.Textbox(label="Trigger word", interactive=False, visible=False)
|
298 |
+
|
299 |
+
image_in = gr.Image(sources="upload", type="filepath")
|
300 |
+
|
301 |
+
with gr.Row():
|
302 |
+
|
303 |
+
with gr.Column():
|
304 |
+
with gr.Group():
|
305 |
+
prompt = gr.Textbox(label="Prompt")
|
306 |
+
negative_prompt = gr.Textbox(label="Negative prompt", value="extra digit, fewer digits, cropped, worst quality, low quality, glitch, deformed, mutated, ugly, disfigured")
|
307 |
+
with gr.Group():
|
308 |
+
guidance_scale = gr.Slider(label="Guidance Scale", minimum=1.0, maximum=10.0, step=0.1, value=7.5)
|
309 |
+
inf_steps = gr.Slider(label="Inference Steps", minimum="25", maximum="50", step=1, value=25)
|
310 |
+
custom_lora_weight = gr.Slider(label="Custom model weights", minimum=0.1, maximum=0.9, step=0.1, value=0.9)
|
311 |
+
|
312 |
+
with gr.Column():
|
313 |
+
with gr.Group():
|
314 |
+
preprocessor = gr.Dropdown(label="Preprocessor", choices=["canny"], value="canny", interactive=False, info="For the moment, only canny is available")
|
315 |
+
controlnet_conditioning_scale = gr.Slider(label="Controlnet conditioning Scale", minimum=0.1, maximum=0.9, step=0.01, value=0.5)
|
316 |
+
with gr.Group():
|
317 |
+
seed = gr.Slider(
|
318 |
+
label="Seed",
|
319 |
+
info = "-1 denotes a random seed",
|
320 |
+
minimum=-1,
|
321 |
+
maximum=423538377342,
|
322 |
+
step=1,
|
323 |
+
value=-1
|
324 |
+
)
|
325 |
+
last_used_seed = gr.Number(
|
326 |
+
label = "Last used seed",
|
327 |
+
info = "the seed used in the last generation",
|
328 |
+
)
|
329 |
+
|
330 |
+
|
331 |
+
submit_btn = gr.Button("Submit")
|
332 |
+
|
333 |
+
result = gr.Image(label="Result")
|
334 |
+
|
335 |
+
use_custom_model.change(
|
336 |
+
fn = check_use_custom_or_no,
|
337 |
+
inputs =[use_custom_model],
|
338 |
+
outputs = [custom_model_box],
|
339 |
+
queue = False
|
340 |
+
)
|
341 |
+
custom_model.blur(
|
342 |
+
fn=custom_model_changed,
|
343 |
+
inputs = [custom_model, previous_model],
|
344 |
+
outputs = [model_status],
|
345 |
+
queue = False
|
346 |
+
)
|
347 |
+
load_model_btn.click(
|
348 |
+
fn = load_model,
|
349 |
+
inputs=[custom_model],
|
350 |
+
outputs = [previous_model, model_status, weight_name, trigger_word],
|
351 |
+
queue = False
|
352 |
+
)
|
353 |
+
submit_btn.click(
|
354 |
+
fn = infer,
|
355 |
+
inputs = [use_custom_model, custom_model, weight_name, custom_lora_weight, image_in, prompt, negative_prompt, preprocessor, controlnet_conditioning_scale, guidance_scale, inf_steps, seed],
|
356 |
+
outputs = [result, last_used_seed]
|
357 |
+
)
|
358 |
+
|
359 |
+
# return demo
|
|
|
360 |
|
361 |
|
362 |
+
demo.queue(max_size=12).launch(share=True)
|