Spaces:
Running
on
Zero
Running
on
Zero
Update app.py (#4)
Browse files- Update app.py (c8c7ad29afe6cd2cc968035f5e5fac9f472c8a3b)
- Update requirements.txt (1009104527a596fd93416406d64e1c8a16d545a6)
- app.py +29 -27
- requirements.txt +2 -1
app.py
CHANGED
@@ -31,6 +31,7 @@ from diffusers import (
|
|
31 |
)
|
32 |
from huggingface_hub import snapshot_download
|
33 |
import spaces
|
|
|
34 |
|
35 |
|
36 |
models_path = snapshot_download(repo_id="Snapchat/w2w")
|
@@ -209,7 +210,7 @@ def inference(net, prompt, negative_prompt, guidance_scale, ddim_steps, seed):
|
|
209 |
|
210 |
@torch.no_grad()
|
211 |
@spaces.GPU()
|
212 |
-
def edit_inference(net, prompt, negative_prompt, guidance_scale, ddim_steps, seed, start_noise, a1, a2, a3, a4):
|
213 |
device = "cuda"
|
214 |
mean.to(device)
|
215 |
std.to(device)
|
@@ -290,7 +291,7 @@ def edit_inference(net, prompt, negative_prompt, guidance_scale, ddim_steps, see
|
|
290 |
image = image.detach().cpu().float().permute(0, 2, 3, 1).numpy()[0]
|
291 |
image = Image.fromarray((image * 255).round().astype("uint8"))
|
292 |
|
293 |
-
return net, image
|
294 |
|
295 |
|
296 |
class CustomImageDataset(Dataset):
|
@@ -420,7 +421,23 @@ def file_upload(file, net):
|
|
420 |
image = inference(net, prompt, negative_prompt, cfg, steps, seed)
|
421 |
return net, image
|
422 |
|
423 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
424 |
|
425 |
|
426 |
intro = """
|
@@ -445,7 +462,7 @@ with gr.Blocks(css="style.css") as demo:
|
|
445 |
gr.HTML(intro)
|
446 |
|
447 |
gr.Markdown(""" **Getting Started:** Sample a random identity or invert to get an identity-encoding model 👩🏻🎨(or - Upload a previously downloaded model using the `Uploading a model` option in `Advanced Options`).
|
448 |
-
**What You
|
449 |
with gr.Column():
|
450 |
with gr.Row():
|
451 |
with gr.Column():
|
@@ -461,7 +478,8 @@ with gr.Blocks(css="style.css") as demo:
|
|
461 |
|
462 |
with gr.Column():
|
463 |
gr.Markdown("""❷ Generate new images of the sampled/inverted identity & edit with the sliders""")
|
464 |
-
gallery = gr.Image(label="Generated Image",height=512, width=512, interactive=False)
|
|
|
465 |
submit = gr.Button("Generate")
|
466 |
|
467 |
|
@@ -505,7 +523,9 @@ with gr.Blocks(css="style.css") as demo:
|
|
505 |
file_input = gr.File(label="Upload Model", container=True)
|
506 |
|
507 |
|
508 |
-
|
|
|
|
|
509 |
|
510 |
gr.Markdown("""<div style="text-align: justify;"> After sampling a new model or inverting, you can download the model below.""")
|
511 |
|
@@ -523,33 +543,15 @@ with gr.Blocks(css="style.css") as demo:
|
|
523 |
sample.click(fn=sample_then_run,inputs = [net], outputs=[net, file_output, input_image])
|
524 |
|
525 |
submit.click(
|
526 |
-
fn=edit_inference, inputs=[net, prompt, negative_prompt, cfg, steps, seed, injection_step, a1, a2, a3, a4], outputs=[net,
|
527 |
)
|
528 |
file_input.change(fn=file_upload, inputs=[file_input, net], outputs = [net, input_image])
|
529 |
|
530 |
|
531 |
|
532 |
-
|
533 |
-
<b>Instructions</b>:
|
534 |
-
1. To get results faster without waiting in queue, you can duplicate into a private space with an A100 GPU.
|
535 |
-
2. To begin, you will have to get an identity-encoding model. You can either sample one from *weights2weights* space by clicking `Sample New Model` or by uploading an image and clicking `invert` to invert the identity into a model. You can optionally draw over the head to define a mask in the image for better results. Sampling a model takes around 10 seconds and inversion takes around 2 minutes. After this is done, you can optionally download this model for later use. A model can be uploaded in the \"Uploading a model\" tab in the `Advanced Options`.
|
536 |
-
3. After getting a model, an image of the identity will be displayed on the right. You can sample from the model by changing seeds as well as prompts and then clicking `Generate`. Make sure to include \"sks person\" in your prompt to keep the same identity.
|
537 |
-
4. The identity in the model can be edited by changing the sliders for various attributes. After clicking `Generate`, you can see how the identity has changed and the effects are maintained across different seeds and prompts.
|
538 |
-
"""
|
539 |
-
help_text2 = """<b>Tips</b>:
|
540 |
-
1. Editing and Identity Generation
|
541 |
-
* If you are interested in preserving more of the image during identity-editing (i.e., where the same seed and prompt results in the same image with only the identity changed), you can play with the "Injection Step" parameter in the \"Sampling\" tab in the `Advanced Options`. During the first *n* timesteps, the original model's weights will be used, and then the edited weights will be set during the remaining steps. Values closer to 1000 will set the edited weights early, having a more pronounced effect, which may disrupt some semantics and structure of the generated image. Lower values will set the edited weights later, better preserving image context. We notice that around 600-800 tends to produce the best results. Larger values in the range (700-1000) are helpful for more global attribute changes, while smaller (400-700) can be used for more finegrained edits. Although it is not always needed.
|
542 |
-
* You can play around with negative prompts, number of inference steps, and CFG in the \"Sampling\" tab in the `Advanced Options` to affect the ultimate image quality.
|
543 |
-
* Sometimes the identity will not be perfectly consistent (e.g., there might be small variations of the face) when you use some seeds or prompts. This is a limitation of our method as well as an open-problem in personalized models.
|
544 |
-
2. Inversion
|
545 |
-
* To obtain the best results for inversion, upload a high resolution photo of the face with minimal occlusion. It is recommended to draw over the face and hair to define a mask. But inversion should still work generally for non-closeup face shots.
|
546 |
-
* For inverting a realistic photo of an identity, typically 800 epochs with lr=1e-1 and 10,000 principal components (PCs) works well. If the resulting generations have artifacted and unrealstic textures, there is probably overfitting and you may want to reduce the number of epochs or learning rate, or play with weight decay. If the generations do not look like the input photo, then you may want to increase the number of epochs.
|
547 |
-
* For inverting out-of-distribution identities, such as artistic renditions of people or non-humans (e.g. the ones shown in the paper), it is recommended to use 1000 PCs, lr=1, and train for 800 epochs.
|
548 |
-
* Note that if you change the number of PCs, you will probably need to change the learning rate. For less PCs, higher learning rates are typically required."""
|
549 |
|
550 |
-
|
551 |
-
gr.Markdown(help_text1)
|
552 |
-
gr.Markdown(help_text2)
|
553 |
|
554 |
demo.queue().launch()
|
555 |
|
|
|
31 |
)
|
32 |
from huggingface_hub import snapshot_download
|
33 |
import spaces
|
34 |
+
from gradio_imageslider import ImageSlider
|
35 |
|
36 |
|
37 |
models_path = snapshot_download(repo_id="Snapchat/w2w")
|
|
|
210 |
|
211 |
@torch.no_grad()
|
212 |
@spaces.GPU()
|
213 |
+
def edit_inference(net, prompt, negative_prompt, guidance_scale, ddim_steps, seed, start_noise, a1, a2, a3, a4, input_image):
|
214 |
device = "cuda"
|
215 |
mean.to(device)
|
216 |
std.to(device)
|
|
|
291 |
image = image.detach().cpu().float().permute(0, 2, 3, 1).numpy()[0]
|
292 |
image = Image.fromarray((image * 255).round().astype("uint8"))
|
293 |
|
294 |
+
return net, (image, input_image["background"])
|
295 |
|
296 |
|
297 |
class CustomImageDataset(Dataset):
|
|
|
421 |
image = inference(net, prompt, negative_prompt, cfg, steps, seed)
|
422 |
return net, image
|
423 |
|
424 |
+
help_text1 = """
|
425 |
+
<b>Instructions</b>:
|
426 |
+
1. To get results faster without waiting in queue, you can duplicate into a private space with an A100 GPU.
|
427 |
+
2. To begin, you will have to get an identity-encoding model. You can either sample one from *weights2weights* space by clicking `Sample New Model` or by uploading an image and clicking `invert` to invert the identity into a model. You can optionally draw over the head to define a mask in the image for better results. Sampling a model takes around 10 seconds and inversion takes around 2 minutes. After this is done, you can optionally download this model for later use. A model can be uploaded in the \"Uploading a model\" tab in the `Advanced Options`.
|
428 |
+
3. After getting a model, an image of the identity will be displayed on the right. You can sample from the model by changing seeds as well as prompts and then clicking `Generate`. Make sure to include \"sks person\" in your prompt to keep the same identity.
|
429 |
+
4. The identity in the model can be edited by changing the sliders for various attributes. After clicking `Generate`, you can see how the identity has changed and the effects are maintained across different seeds and prompts.
|
430 |
+
"""
|
431 |
+
help_text2 = """<b>Tips</b>:
|
432 |
+
1. Editing and Identity Generation
|
433 |
+
* If you are interested in preserving more of the image during identity-editing (i.e., where the same seed and prompt results in the same image with only the identity changed), you can play with the "Injection Step" parameter in the \"Sampling\" tab in the `Advanced Options`. During the first *n* timesteps, the original model's weights will be used, and then the edited weights will be set during the remaining steps. Values closer to 1000 will set the edited weights early, having a more pronounced effect, which may disrupt some semantics and structure of the generated image. Lower values will set the edited weights later, better preserving image context. We notice that around 600-800 tends to produce the best results. Larger values in the range (700-1000) are helpful for more global attribute changes, while smaller (400-700) can be used for more finegrained edits. Although it is not always needed.
|
434 |
+
* You can play around with negative prompts, number of inference steps, and CFG in the \"Sampling\" tab in the `Advanced Options` to affect the ultimate image quality.
|
435 |
+
* Sometimes the identity will not be perfectly consistent (e.g., there might be small variations of the face) when you use some seeds or prompts. This is a limitation of our method as well as an open-problem in personalized models.
|
436 |
+
2. Inversion
|
437 |
+
* To obtain the best results for inversion, upload a high resolution photo of the face with minimal occlusion. It is recommended to draw over the face and hair to define a mask. But inversion should still work generally for non-closeup face shots.
|
438 |
+
* For inverting a realistic photo of an identity, typically 800 epochs with lr=1e-1 and 10,000 principal components (PCs) works well. If the resulting generations have artifacted and unrealstic textures, there is probably overfitting and you may want to reduce the number of epochs or learning rate, or play with weight decay. If the generations do not look like the input photo, then you may want to increase the number of epochs.
|
439 |
+
* For inverting out-of-distribution identities, such as artistic renditions of people or non-humans (e.g. the ones shown in the paper), it is recommended to use 1000 PCs, lr=1, and train for 800 epochs.
|
440 |
+
* Note that if you change the number of PCs, you will probably need to change the learning rate. For less PCs, higher learning rates are typically required."""
|
441 |
|
442 |
|
443 |
intro = """
|
|
|
462 |
gr.HTML(intro)
|
463 |
|
464 |
gr.Markdown(""" **Getting Started:** Sample a random identity or invert to get an identity-encoding model 👩🏻🎨(or - Upload a previously downloaded model using the `Uploading a model` option in `Advanced Options`).
|
465 |
+
**What Can You Do?** Generate new images & edit the encoded identity 👩🏻->👩🏻🦱. See further instructions and tips at the bottom of the page 🤗.""")
|
466 |
with gr.Column():
|
467 |
with gr.Row():
|
468 |
with gr.Column():
|
|
|
478 |
|
479 |
with gr.Column():
|
480 |
gr.Markdown("""❷ Generate new images of the sampled/inverted identity & edit with the sliders""")
|
481 |
+
#gallery = gr.Image(label="Generated Image",height=512, width=512, interactive=False)
|
482 |
+
image_slider = ImageSlider(position=0.5, type="pil", height=512, width=512)
|
483 |
submit = gr.Button("Generate")
|
484 |
|
485 |
|
|
|
523 |
file_input = gr.File(label="Upload Model", container=True)
|
524 |
|
525 |
|
526 |
+
with gr.Accordion("💡Instructions & Tips⬇️", open=False):
|
527 |
+
gr.Markdown(help_text1)
|
528 |
+
gr.Markdown(help_text2)
|
529 |
|
530 |
gr.Markdown("""<div style="text-align: justify;"> After sampling a new model or inverting, you can download the model below.""")
|
531 |
|
|
|
543 |
sample.click(fn=sample_then_run,inputs = [net], outputs=[net, file_output, input_image])
|
544 |
|
545 |
submit.click(
|
546 |
+
fn=edit_inference, inputs=[net, prompt, negative_prompt, cfg, steps, seed, injection_step, a1, a2, a3, a4, input_image], outputs=[net, image_slider]
|
547 |
)
|
548 |
file_input.change(fn=file_upload, inputs=[file_input, net], outputs = [net, input_image])
|
549 |
|
550 |
|
551 |
|
552 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
553 |
|
554 |
+
|
|
|
|
|
555 |
|
556 |
demo.queue().launch()
|
557 |
|
requirements.txt
CHANGED
@@ -70,4 +70,5 @@ urllib3==2.2.1
|
|
70 |
wandb==0.17.0
|
71 |
xxhash==3.4.1
|
72 |
yarl==1.9.4
|
73 |
-
zipp==3.19.0
|
|
|
|
70 |
wandb==0.17.0
|
71 |
xxhash==3.4.1
|
72 |
yarl==1.9.4
|
73 |
+
zipp==3.19.0
|
74 |
+
gradio_imageslider==0.0.20
|