Update blocks/img2img.py
Browse files- blocks/img2img.py +71 -60
blocks/img2img.py
CHANGED
|
@@ -8,11 +8,13 @@ from PIL import Image
|
|
| 8 |
from .download import get_share_js, get_community_loading_icon, CSS
|
| 9 |
|
| 10 |
IMG2IMG_MODEL_LIST = {
|
|
|
|
| 11 |
"StableDiffusion 1.5" : "runwayml/stable-diffusion-v1-5",
|
| 12 |
"StableDiffusion 2.1" : "stabilityai/stable-diffusion-2-1",
|
| 13 |
-
"OpenJourney v4" : "prompthero/openjourney-v4",
|
| 14 |
"DreamLike 1.0" : "dreamlike-art/dreamlike-diffusion-1.0",
|
| 15 |
-
"DreamLike 2.0" : "dreamlike-art/dreamlike-photoreal-2.0"
|
|
|
|
|
|
|
| 16 |
}
|
| 17 |
|
| 18 |
class StableDiffusionImage2ImageGenerator:
|
|
@@ -30,7 +32,7 @@ class StableDiffusionImage2ImageGenerator:
|
|
| 30 |
self.pipe = get_scheduler_list(pipe=self.pipe, scheduler=scheduler)
|
| 31 |
|
| 32 |
self.pipe.to(device)
|
| 33 |
-
|
| 34 |
|
| 35 |
return self.pipe
|
| 36 |
|
|
@@ -40,7 +42,6 @@ class StableDiffusionImage2ImageGenerator:
|
|
| 40 |
model_path: str,
|
| 41 |
prompt: str,
|
| 42 |
negative_prompt: str,
|
| 43 |
-
num_images_per_prompt: int,
|
| 44 |
scheduler: str,
|
| 45 |
guidance_scale: int,
|
| 46 |
num_inference_step: int,
|
|
@@ -62,7 +63,7 @@ class StableDiffusionImage2ImageGenerator:
|
|
| 62 |
prompt,
|
| 63 |
image=image,
|
| 64 |
negative_prompt=negative_prompt,
|
| 65 |
-
num_images_per_prompt=
|
| 66 |
num_inference_steps=num_inference_step,
|
| 67 |
guidance_scale=guidance_scale,
|
| 68 |
generator=generator,
|
|
@@ -81,7 +82,7 @@ class StableDiffusionImage2ImageGenerator:
|
|
| 81 |
|
| 82 |
image2image_prompt = gr.Textbox(
|
| 83 |
lines=1,
|
| 84 |
-
placeholder="Prompt",
|
| 85 |
show_label=False,
|
| 86 |
elem_id="prompt-text-input-img2img",
|
| 87 |
value=''
|
|
@@ -89,7 +90,7 @@ class StableDiffusionImage2ImageGenerator:
|
|
| 89 |
|
| 90 |
image2image_negative_prompt = gr.Textbox(
|
| 91 |
lines=1,
|
| 92 |
-
placeholder="Negative Prompt",
|
| 93 |
show_label=False,
|
| 94 |
elem_id = "negative-prompt-text-input-img2img",
|
| 95 |
value=''
|
|
@@ -107,60 +108,56 @@ class StableDiffusionImage2ImageGenerator:
|
|
| 107 |
image2image_generated_prompt = gr.Textbox(
|
| 108 |
lines=1,
|
| 109 |
placeholder="Generated Prompt",
|
| 110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
)
|
| 112 |
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
label="Scheduler",
|
| 145 |
-
elem_id="scheduler-dropdown-img2img",
|
| 146 |
-
)
|
| 147 |
-
image2image_num_images_per_prompt = gr.Slider(
|
| 148 |
-
minimum=1,
|
| 149 |
-
maximum=30,
|
| 150 |
-
step=1,
|
| 151 |
-
value=1,
|
| 152 |
-
label="Number Of Images",
|
| 153 |
-
)
|
| 154 |
-
|
| 155 |
-
image2image_seed_generator = gr.Slider(
|
| 156 |
-
label="Seed(0 for random)",
|
| 157 |
-
minimum=0,
|
| 158 |
-
maximum=1000000,
|
| 159 |
-
value=0,
|
| 160 |
-
elem_id="seed-slider-img2img",
|
| 161 |
-
)
|
| 162 |
-
|
| 163 |
-
image2image_predict_button = gr.Button(value="Generator")
|
| 164 |
|
| 165 |
with gr.Column():
|
| 166 |
output_image = gr.Gallery(
|
|
@@ -176,6 +173,21 @@ class StableDiffusionImage2ImageGenerator:
|
|
| 176 |
loading_icon = gr.HTML(loading_icon_html)
|
| 177 |
share_button = gr.Button("Save artwork", elem_id="share-btn-img2img")
|
| 178 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
image2image_predict_button.click(
|
| 180 |
fn=StableDiffusionImage2ImageGenerator().generate_image,
|
| 181 |
inputs=[
|
|
@@ -183,7 +195,6 @@ class StableDiffusionImage2ImageGenerator:
|
|
| 183 |
image2image_model_path,
|
| 184 |
image2image_prompt,
|
| 185 |
image2image_negative_prompt,
|
| 186 |
-
image2image_num_images_per_prompt,
|
| 187 |
image2image_scheduler,
|
| 188 |
image2image_guidance_scale,
|
| 189 |
image2image_num_inference_step,
|
|
|
|
| 8 |
from .download import get_share_js, get_community_loading_icon, CSS
|
| 9 |
|
| 10 |
IMG2IMG_MODEL_LIST = {
|
| 11 |
+
"OpenJourney v4" : "prompthero/openjourney-v4",
|
| 12 |
"StableDiffusion 1.5" : "runwayml/stable-diffusion-v1-5",
|
| 13 |
"StableDiffusion 2.1" : "stabilityai/stable-diffusion-2-1",
|
|
|
|
| 14 |
"DreamLike 1.0" : "dreamlike-art/dreamlike-diffusion-1.0",
|
| 15 |
+
"DreamLike 2.0" : "dreamlike-art/dreamlike-photoreal-2.0",
|
| 16 |
+
"DreamShaper" : "Lykon/DreamShaper",
|
| 17 |
+
"NeverEnding-Dream" : "Lykon/NeverEnding-Dream"
|
| 18 |
}
|
| 19 |
|
| 20 |
class StableDiffusionImage2ImageGenerator:
|
|
|
|
| 32 |
self.pipe = get_scheduler_list(pipe=self.pipe, scheduler=scheduler)
|
| 33 |
|
| 34 |
self.pipe.to(device)
|
| 35 |
+
self.pipe.enable_attention_slicing()
|
| 36 |
|
| 37 |
return self.pipe
|
| 38 |
|
|
|
|
| 42 |
model_path: str,
|
| 43 |
prompt: str,
|
| 44 |
negative_prompt: str,
|
|
|
|
| 45 |
scheduler: str,
|
| 46 |
guidance_scale: int,
|
| 47 |
num_inference_step: int,
|
|
|
|
| 63 |
prompt,
|
| 64 |
image=image,
|
| 65 |
negative_prompt=negative_prompt,
|
| 66 |
+
num_images_per_prompt=1,
|
| 67 |
num_inference_steps=num_inference_step,
|
| 68 |
guidance_scale=guidance_scale,
|
| 69 |
generator=generator,
|
|
|
|
| 82 |
|
| 83 |
image2image_prompt = gr.Textbox(
|
| 84 |
lines=1,
|
| 85 |
+
placeholder="Prompt, keywords that describe the changes you want to apply to your image",
|
| 86 |
show_label=False,
|
| 87 |
elem_id="prompt-text-input-img2img",
|
| 88 |
value=''
|
|
|
|
| 90 |
|
| 91 |
image2image_negative_prompt = gr.Textbox(
|
| 92 |
lines=1,
|
| 93 |
+
placeholder="Negative Prompt, keywords that describe what you don't want in your image",
|
| 94 |
show_label=False,
|
| 95 |
elem_id = "negative-prompt-text-input-img2img",
|
| 96 |
value=''
|
|
|
|
| 108 |
image2image_generated_prompt = gr.Textbox(
|
| 109 |
lines=1,
|
| 110 |
placeholder="Generated Prompt",
|
| 111 |
+
label = "Generated Prompt",
|
| 112 |
+
show_label=True,
|
| 113 |
+
info="Auto generated prompts for inspiration.",
|
| 114 |
+
)
|
| 115 |
+
image2image_model_path = gr.Dropdown(
|
| 116 |
+
choices=list(IMG2IMG_MODEL_LIST.keys()),
|
| 117 |
+
value=list(IMG2IMG_MODEL_LIST.keys())[0],
|
| 118 |
+
label="Imaget2Image Model Selection",
|
| 119 |
+
elem_id="model-dropdown-img2img",
|
| 120 |
+
info="Select the model you want to use for image2image generation."
|
| 121 |
+
)
|
| 122 |
+
image2image_scheduler = gr.Dropdown(
|
| 123 |
+
choices=SCHEDULER_LIST,
|
| 124 |
+
value=SCHEDULER_LIST[0],
|
| 125 |
+
label="Scheduler",
|
| 126 |
+
elem_id="scheduler-dropdown-img2img",
|
| 127 |
+
info="Scheduler list for models. Different schdulers result in different outputs."
|
| 128 |
)
|
| 129 |
|
| 130 |
+
image2image_seed_generator = gr.Slider(
|
| 131 |
+
label="Seed(0 for random)",
|
| 132 |
+
minimum=0,
|
| 133 |
+
maximum=1000000,
|
| 134 |
+
value=0,
|
| 135 |
+
elem_id="seed-slider-img2img",
|
| 136 |
+
info="Set the seed to a specific value to reproduce the results."
|
| 137 |
+
)
|
| 138 |
+
|
| 139 |
+
|
| 140 |
+
image2image_guidance_scale = gr.Slider(
|
| 141 |
+
minimum=0.1,
|
| 142 |
+
maximum=15,
|
| 143 |
+
step=0.1,
|
| 144 |
+
value=7.5,
|
| 145 |
+
label="Guidance Scale",
|
| 146 |
+
elem_id = "guidance-scale-slider-img2img",
|
| 147 |
+
info = "Guidance scale determines how much the prompt will affect the image. Higher the value, more the effect."
|
| 148 |
+
)
|
| 149 |
+
|
| 150 |
+
image2image_num_inference_step = gr.Slider(
|
| 151 |
+
minimum=1,
|
| 152 |
+
maximum=100,
|
| 153 |
+
step=1,
|
| 154 |
+
value=50,
|
| 155 |
+
label="Num Inference Step",
|
| 156 |
+
elem_id = "num-inference-step-slider-img2img",
|
| 157 |
+
info = "Number of inference step determines the quality of the image. Higher the number, better the quality."
|
| 158 |
+
)
|
| 159 |
+
|
| 160 |
+
image2image_predict_button = gr.Button(value="Generate image")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
|
| 162 |
with gr.Column():
|
| 163 |
output_image = gr.Gallery(
|
|
|
|
| 173 |
loading_icon = gr.HTML(loading_icon_html)
|
| 174 |
share_button = gr.Button("Save artwork", elem_id="share-btn-img2img")
|
| 175 |
|
| 176 |
+
# Create an html for describing the models
|
| 177 |
+
gr.HTML(
|
| 178 |
+
"""
|
| 179 |
+
<div id="model-description-img2img">
|
| 180 |
+
<h3>Image2Image Models</h3>
|
| 181 |
+
<p>Image2Image models are trained to generate images from a given prompt. The prompt should specify what to change in general for the provided image.</p>
|
| 182 |
+
<p>For example with a butterfly image, the prompt can be "blue butterfly".</p>
|
| 183 |
+
<p>Negative prompt can be used to specify what not to change in the image. For example, with the butterfly image, the negative prompt can be "dark blue, blurry image".</p>
|
| 184 |
+
<hr>
|
| 185 |
+
<p>Stable Diffusion 1.5 & 2.1: Default model for many tasks. </p>
|
| 186 |
+
<p>OpenJourney v4: Generates fantasy themed images similar to the Midjourney model. </p>
|
| 187 |
+
<p>Dreamlike Photoreal 1.0 & 2.0 is SD 1.5 that generates realistic images. </p>
|
| 188 |
+
</div>
|
| 189 |
+
"""
|
| 190 |
+
)
|
| 191 |
image2image_predict_button.click(
|
| 192 |
fn=StableDiffusionImage2ImageGenerator().generate_image,
|
| 193 |
inputs=[
|
|
|
|
| 195 |
image2image_model_path,
|
| 196 |
image2image_prompt,
|
| 197 |
image2image_negative_prompt,
|
|
|
|
| 198 |
image2image_scheduler,
|
| 199 |
image2image_guidance_scale,
|
| 200 |
image2image_num_inference_step,
|