Spaces:
Build error
Build error
Update edit_app.py
Browse files- edit_app.py +1 -85
edit_app.py
CHANGED
|
@@ -1,74 +1,14 @@
|
|
| 1 |
-
from __future__ import annotations
|
| 2 |
-
|
| 3 |
import math
|
| 4 |
import random
|
| 5 |
-
|
| 6 |
import gradio as gr
|
| 7 |
import torch
|
| 8 |
from PIL import Image, ImageOps
|
| 9 |
from diffusers import StableDiffusionInstructPix2PixPipeline
|
| 10 |
|
| 11 |
-
|
| 12 |
-
help_text = """
|
| 13 |
-
If you're not getting what you want, there may be a few reasons:
|
| 14 |
-
1. Is the image not changing enough? Your Image CFG weight may be too high. This value dictates how similar the output should be to the input. It's possible your edit requires larger changes from the original image, and your Image CFG weight isn't allowing that. Alternatively, your Text CFG weight may be too low. This value dictates how much to listen to the text instruction. The default Image CFG of 1.5 and Text CFG of 7.5 are a good starting point, but aren't necessarily optimal for each edit. Try:
|
| 15 |
-
* Decreasing the Image CFG weight, or
|
| 16 |
-
* Increasing the Text CFG weight, or
|
| 17 |
-
2. Conversely, is the image changing too much, such that the details in the original image aren't preserved? Try:
|
| 18 |
-
* Increasing the Image CFG weight, or
|
| 19 |
-
* Decreasing the Text CFG weight
|
| 20 |
-
3. Try generating results with different random seeds by setting "Randomize Seed" and running generation multiple times. You can also try setting "Randomize CFG" to sample new Text CFG and Image CFG values each time.
|
| 21 |
-
4. Rephrasing the instruction sometimes improves results (e.g., "turn him into a dog" vs. "make him a dog" vs. "as a dog").
|
| 22 |
-
5. Increasing the number of steps sometimes improves results.
|
| 23 |
-
6. Do faces look weird? The Stable Diffusion autoencoder has a hard time with faces that are small in the image. Try:
|
| 24 |
-
* Cropping the image so the face takes up a larger portion of the frame.
|
| 25 |
-
"""
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
example_instructions = [
|
| 29 |
-
"Make it a picasso painting",
|
| 30 |
-
"as if it were by modigliani",
|
| 31 |
-
"convert to a bronze statue",
|
| 32 |
-
"Turn it into an anime.",
|
| 33 |
-
"have it look like a graphic novel",
|
| 34 |
-
"make him gain weight",
|
| 35 |
-
"what would he look like bald?",
|
| 36 |
-
"Have him smile",
|
| 37 |
-
"Put him in a cocktail party.",
|
| 38 |
-
"move him at the beach.",
|
| 39 |
-
"add dramatic lighting",
|
| 40 |
-
"Convert to black and white",
|
| 41 |
-
"What if it were snowing?",
|
| 42 |
-
"Give him a leather jacket",
|
| 43 |
-
"Turn him into a cyborg!",
|
| 44 |
-
"make him wear a beanie",
|
| 45 |
-
]
|
| 46 |
-
|
| 47 |
model_id = "timbrooks/instruct-pix2pix"
|
| 48 |
|
| 49 |
def main():
|
| 50 |
pipe = StableDiffusionInstructPix2PixPipeline.from_pretrained(model_id, torch_dtype=torch.float16, safety_checker=None).to("cuda")
|
| 51 |
-
example_image = Image.open("imgs/example.jpg").convert("RGB")
|
| 52 |
-
|
| 53 |
-
def load_example(
|
| 54 |
-
steps: int,
|
| 55 |
-
randomize_seed: bool,
|
| 56 |
-
seed: int,
|
| 57 |
-
randomize_cfg: bool,
|
| 58 |
-
text_cfg_scale: float,
|
| 59 |
-
image_cfg_scale: float,
|
| 60 |
-
):
|
| 61 |
-
example_instruction = random.choice(example_instructions)
|
| 62 |
-
return [example_image, example_instruction] + generate(
|
| 63 |
-
example_image,
|
| 64 |
-
example_instruction,
|
| 65 |
-
steps,
|
| 66 |
-
randomize_seed,
|
| 67 |
-
seed,
|
| 68 |
-
randomize_cfg,
|
| 69 |
-
text_cfg_scale,
|
| 70 |
-
image_cfg_scale,
|
| 71 |
-
)
|
| 72 |
|
| 73 |
def generate(
|
| 74 |
input_image: Image.Image,
|
|
@@ -106,19 +46,9 @@ def main():
|
|
| 106 |
return [0, "Randomize Seed", 1371, "Fix CFG", 7.5, 1.5, None]
|
| 107 |
|
| 108 |
with gr.Blocks() as demo:
|
| 109 |
-
gr.HTML("""<h1 style="font-weight: 900; margin-bottom: 7px;">
|
| 110 |
-
InstructPix2Pix: Learning to Follow Image Editing Instructions
|
| 111 |
-
</h1>
|
| 112 |
-
<p>For faster inference without waiting in queue, you may duplicate the space and upgrade to GPU in settings.
|
| 113 |
-
<br/>
|
| 114 |
-
<a href="https://huggingface.co/spaces/timbrooks/instruct-pix2pix?duplicate=true">
|
| 115 |
-
<img style="margin-top: 0em; margin-bottom: 0em" src="https://bit.ly/3gLdBN6" alt="Duplicate Space"></a>
|
| 116 |
-
<p/>""")
|
| 117 |
with gr.Row():
|
| 118 |
with gr.Column(scale=1, min_width=100):
|
| 119 |
generate_button = gr.Button("Generate")
|
| 120 |
-
with gr.Column(scale=1, min_width=100):
|
| 121 |
-
load_button = gr.Button("Load Example")
|
| 122 |
with gr.Column(scale=1, min_width=100):
|
| 123 |
reset_button = gr.Button("Reset")
|
| 124 |
with gr.Column(scale=3):
|
|
@@ -150,20 +80,6 @@ def main():
|
|
| 150 |
text_cfg_scale = gr.Number(value=7.5, label=f"Text CFG", interactive=True)
|
| 151 |
image_cfg_scale = gr.Number(value=1.5, label=f"Image CFG", interactive=True)
|
| 152 |
|
| 153 |
-
gr.Markdown(help_text)
|
| 154 |
-
|
| 155 |
-
load_button.click(
|
| 156 |
-
fn=load_example,
|
| 157 |
-
inputs=[
|
| 158 |
-
steps,
|
| 159 |
-
randomize_seed,
|
| 160 |
-
seed,
|
| 161 |
-
randomize_cfg,
|
| 162 |
-
text_cfg_scale,
|
| 163 |
-
image_cfg_scale,
|
| 164 |
-
],
|
| 165 |
-
outputs=[input_image, instruction, seed, text_cfg_scale, image_cfg_scale, edited_image],
|
| 166 |
-
)
|
| 167 |
generate_button.click(
|
| 168 |
fn=generate,
|
| 169 |
inputs=[
|
|
@@ -189,4 +105,4 @@ def main():
|
|
| 189 |
|
| 190 |
|
| 191 |
if __name__ == "__main__":
|
| 192 |
-
main()
|
|
|
|
|
|
|
|
|
|
| 1 |
import math
|
| 2 |
import random
|
|
|
|
| 3 |
import gradio as gr
|
| 4 |
import torch
|
| 5 |
from PIL import Image, ImageOps
|
| 6 |
from diffusers import StableDiffusionInstructPix2PixPipeline
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
model_id = "timbrooks/instruct-pix2pix"
|
| 9 |
|
| 10 |
def main():
|
| 11 |
pipe = StableDiffusionInstructPix2PixPipeline.from_pretrained(model_id, torch_dtype=torch.float16, safety_checker=None).to("cuda")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
def generate(
|
| 14 |
input_image: Image.Image,
|
|
|
|
| 46 |
return [0, "Randomize Seed", 1371, "Fix CFG", 7.5, 1.5, None]
|
| 47 |
|
| 48 |
with gr.Blocks() as demo:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
with gr.Row():
|
| 50 |
with gr.Column(scale=1, min_width=100):
|
| 51 |
generate_button = gr.Button("Generate")
|
|
|
|
|
|
|
| 52 |
with gr.Column(scale=1, min_width=100):
|
| 53 |
reset_button = gr.Button("Reset")
|
| 54 |
with gr.Column(scale=3):
|
|
|
|
| 80 |
text_cfg_scale = gr.Number(value=7.5, label=f"Text CFG", interactive=True)
|
| 81 |
image_cfg_scale = gr.Number(value=1.5, label=f"Image CFG", interactive=True)
|
| 82 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
generate_button.click(
|
| 84 |
fn=generate,
|
| 85 |
inputs=[
|
|
|
|
| 105 |
|
| 106 |
|
| 107 |
if __name__ == "__main__":
|
| 108 |
+
main()
|