Update blocks/text2img.py
Browse files- blocks/text2img.py +82 -71
blocks/text2img.py
CHANGED
|
@@ -36,7 +36,7 @@ class StableDiffusionText2ImageGenerator:
|
|
| 36 |
device = get_device()
|
| 37 |
self.pipe = get_scheduler_list(pipe=self.pipe, scheduler=scheduler)
|
| 38 |
self.pipe.to(device)
|
| 39 |
-
|
| 40 |
|
| 41 |
return self.pipe
|
| 42 |
|
|
@@ -45,7 +45,6 @@ class StableDiffusionText2ImageGenerator:
|
|
| 45 |
model_path: str,
|
| 46 |
prompt: str,
|
| 47 |
negative_prompt: str,
|
| 48 |
-
num_images_per_prompt: int,
|
| 49 |
scheduler: str,
|
| 50 |
guidance_scale: int,
|
| 51 |
num_inference_step: int,
|
|
@@ -56,7 +55,7 @@ class StableDiffusionText2ImageGenerator:
|
|
| 56 |
print("model_path", model_path)
|
| 57 |
print("prompt", prompt)
|
| 58 |
print("negative_prompt", negative_prompt)
|
| 59 |
-
print("num_images_per_prompt",
|
| 60 |
print("scheduler", scheduler)
|
| 61 |
print("guidance_scale", guidance_scale)
|
| 62 |
print("num_inference_step", num_inference_step)
|
|
@@ -79,7 +78,7 @@ class StableDiffusionText2ImageGenerator:
|
|
| 79 |
height=height,
|
| 80 |
width=width,
|
| 81 |
negative_prompt=negative_prompt,
|
| 82 |
-
num_images_per_prompt=
|
| 83 |
num_inference_steps=num_inference_step,
|
| 84 |
guidance_scale=guidance_scale,
|
| 85 |
generator=generator,
|
|
@@ -95,18 +94,18 @@ class StableDiffusionText2ImageGenerator:
|
|
| 95 |
with gr.Column():
|
| 96 |
text2image_prompt = gr.Textbox(
|
| 97 |
lines=1,
|
| 98 |
-
placeholder="Prompt",
|
| 99 |
show_label=False,
|
| 100 |
elem_id="prompt-text-input",
|
| 101 |
-
value=''
|
| 102 |
-
|
|
|
|
| 103 |
|
| 104 |
text2image_negative_prompt = gr.Textbox(
|
| 105 |
lines=1,
|
| 106 |
-
placeholder="Negative Prompt",
|
| 107 |
show_label=False,
|
| 108 |
elem_id = "negative-prompt-text-input",
|
| 109 |
-
value=''
|
|
|
|
| 110 |
)
|
| 111 |
|
| 112 |
# add button for generating a prompt from the prompt
|
|
@@ -122,67 +121,66 @@ class StableDiffusionText2ImageGenerator:
|
|
| 122 |
lines=1,
|
| 123 |
placeholder="Generated Prompt",
|
| 124 |
show_label=False,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
)
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
text2image_size = gr.Slider(
|
| 170 |
-
minimum=128,
|
| 171 |
-
maximum=1280,
|
| 172 |
-
step=32,
|
| 173 |
-
value=512,
|
| 174 |
-
label="Image Size",
|
| 175 |
-
elem_id="image-size-slider",
|
| 176 |
-
)
|
| 177 |
-
|
| 178 |
-
text2image_seed_generator = gr.Slider(
|
| 179 |
-
label="Seed(0 for random)",
|
| 180 |
-
minimum=0,
|
| 181 |
-
maximum=1000000,
|
| 182 |
-
value=0,
|
| 183 |
-
elem_id="seed-slider",
|
| 184 |
-
)
|
| 185 |
-
text2image_predict = gr.Button(value="Generator")
|
| 186 |
|
| 187 |
with gr.Column():
|
| 188 |
output_image = gr.Gallery(
|
|
@@ -197,14 +195,27 @@ class StableDiffusionText2ImageGenerator:
|
|
| 197 |
loading_icon = gr.HTML(loading_icon_html)
|
| 198 |
share_button = gr.Button("Save artwork", elem_id="share-btn")
|
| 199 |
|
| 200 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 201 |
text2image_predict.click(
|
| 202 |
fn=StableDiffusionText2ImageGenerator().generate_image,
|
| 203 |
inputs=[
|
| 204 |
text2image_model_path,
|
| 205 |
text2image_prompt,
|
| 206 |
text2image_negative_prompt,
|
| 207 |
-
text2image_num_images_per_prompt,
|
| 208 |
text2image_scheduler,
|
| 209 |
text2image_guidance_scale,
|
| 210 |
text2image_num_inference_step,
|
|
|
|
| 36 |
device = get_device()
|
| 37 |
self.pipe = get_scheduler_list(pipe=self.pipe, scheduler=scheduler)
|
| 38 |
self.pipe.to(device)
|
| 39 |
+
self.pipe.enable_attention_slicing()
|
| 40 |
|
| 41 |
return self.pipe
|
| 42 |
|
|
|
|
| 45 |
model_path: str,
|
| 46 |
prompt: str,
|
| 47 |
negative_prompt: str,
|
|
|
|
| 48 |
scheduler: str,
|
| 49 |
guidance_scale: int,
|
| 50 |
num_inference_step: int,
|
|
|
|
| 55 |
print("model_path", model_path)
|
| 56 |
print("prompt", prompt)
|
| 57 |
print("negative_prompt", negative_prompt)
|
| 58 |
+
print("num_images_per_prompt", 1)
|
| 59 |
print("scheduler", scheduler)
|
| 60 |
print("guidance_scale", guidance_scale)
|
| 61 |
print("num_inference_step", num_inference_step)
|
|
|
|
| 78 |
height=height,
|
| 79 |
width=width,
|
| 80 |
negative_prompt=negative_prompt,
|
| 81 |
+
num_images_per_prompt=1,
|
| 82 |
num_inference_steps=num_inference_step,
|
| 83 |
guidance_scale=guidance_scale,
|
| 84 |
generator=generator,
|
|
|
|
| 94 |
with gr.Column():
|
| 95 |
text2image_prompt = gr.Textbox(
|
| 96 |
lines=1,
|
|
|
|
| 97 |
show_label=False,
|
| 98 |
elem_id="prompt-text-input",
|
| 99 |
+
value='',
|
| 100 |
+
placeholder="Prompt, keywords that describe your image"
|
| 101 |
+
)
|
| 102 |
|
| 103 |
text2image_negative_prompt = gr.Textbox(
|
| 104 |
lines=1,
|
|
|
|
| 105 |
show_label=False,
|
| 106 |
elem_id = "negative-prompt-text-input",
|
| 107 |
+
value='',
|
| 108 |
+
placeholder="Negative Prompt, keywords that describe what you don't want in your image",
|
| 109 |
)
|
| 110 |
|
| 111 |
# add button for generating a prompt from the prompt
|
|
|
|
| 121 |
lines=1,
|
| 122 |
placeholder="Generated Prompt",
|
| 123 |
show_label=False,
|
| 124 |
+
info="Auto generated prompts for inspiration.",
|
| 125 |
+
)
|
| 126 |
+
|
| 127 |
+
text2image_model_path = gr.Dropdown(
|
| 128 |
+
choices=list(TEXT2IMG_MODEL_LIST.keys()),
|
| 129 |
+
value=list(TEXT2IMG_MODEL_LIST.keys())[0],
|
| 130 |
+
label="Text2Image Model Selection",
|
| 131 |
+
elem_id="model-dropdown",
|
| 132 |
+
info="Select the model you want to use for text2image generation."
|
| 133 |
+
)
|
| 134 |
+
|
| 135 |
+
text2image_scheduler = gr.Dropdown(
|
| 136 |
+
choices=SCHEDULER_LIST,
|
| 137 |
+
value=SCHEDULER_LIST[0],
|
| 138 |
+
label="Scheduler",
|
| 139 |
+
elem_id="scheduler-dropdown",
|
| 140 |
+
info="Scheduler list for models. Different schdulers result in different outputs."
|
| 141 |
)
|
| 142 |
+
|
| 143 |
+
|
| 144 |
+
|
| 145 |
+
text2image_size = gr.Slider(
|
| 146 |
+
minimum=128,
|
| 147 |
+
maximum=1280,
|
| 148 |
+
step=32,
|
| 149 |
+
value=768,
|
| 150 |
+
label="Image Size",
|
| 151 |
+
elem_id="image-size-slider",
|
| 152 |
+
info = "Image size determines the height and width of the generated image. Higher the value, better the quality however slower the computation."
|
| 153 |
+
)
|
| 154 |
+
text2image_seed_generator = gr.Slider(
|
| 155 |
+
label="Seed(0 for random)",
|
| 156 |
+
minimum=0,
|
| 157 |
+
maximum=1000000,
|
| 158 |
+
value=0,
|
| 159 |
+
elem_id="seed-slider",
|
| 160 |
+
info="Set the seed to a specific value to reproduce the results."
|
| 161 |
+
)
|
| 162 |
+
|
| 163 |
+
|
| 164 |
+
text2image_guidance_scale = gr.Slider(
|
| 165 |
+
minimum=0.1,
|
| 166 |
+
maximum=15,
|
| 167 |
+
step=0.1,
|
| 168 |
+
value=7.5,
|
| 169 |
+
label="Guidance Scale",
|
| 170 |
+
elem_id = "guidance-scale-slider",
|
| 171 |
+
info = "Guidance scale determines how much the prompt will affect the image. Higher the value, more the effect."
|
| 172 |
+
)
|
| 173 |
+
|
| 174 |
+
text2image_num_inference_step = gr.Slider(
|
| 175 |
+
minimum=1,
|
| 176 |
+
maximum=100,
|
| 177 |
+
step=1,
|
| 178 |
+
value=50,
|
| 179 |
+
label="Num Inference Step",
|
| 180 |
+
elem_id = "num-inference-step-slider",
|
| 181 |
+
info = "Number of inference step determines the quality of the image. Higher the number, better the quality."
|
| 182 |
+
)
|
| 183 |
+
text2image_predict = gr.Button(value="Generate image")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
|
| 185 |
with gr.Column():
|
| 186 |
output_image = gr.Gallery(
|
|
|
|
| 195 |
loading_icon = gr.HTML(loading_icon_html)
|
| 196 |
share_button = gr.Button("Save artwork", elem_id="share-btn")
|
| 197 |
|
| 198 |
+
gr.HTML(
|
| 199 |
+
"""
|
| 200 |
+
<div id="model-description-text2img">
|
| 201 |
+
<h3>Text2Image Models</h3>
|
| 202 |
+
<p>Text to image models will generate an image guided by the prompt that is provided</p>
|
| 203 |
+
<p>A prompt should be specified with keywords that describe the image you want to generate.</p>
|
| 204 |
+
<p>Negative prompt can be used to specify keywords that you don't want in your image such as "blood" or "violence".</p>
|
| 205 |
+
<p>Example prompt: "A painting of a cat sitting on a chair, fantasy themed, starry background"</p>
|
| 206 |
+
<hr>
|
| 207 |
+
<p>Stable Diffusion 1.5 & 2.1: Default model for many tasks. </p>
|
| 208 |
+
<p>OpenJourney v4: Generates fantasy themed images similar to the Midjourney model. </p>
|
| 209 |
+
<p>Dreamlike Photoreal 1.0 & 2.0 is SD 1.5 that generates realistic images. </p>
|
| 210 |
+
</div>
|
| 211 |
+
"""
|
| 212 |
+
)
|
| 213 |
text2image_predict.click(
|
| 214 |
fn=StableDiffusionText2ImageGenerator().generate_image,
|
| 215 |
inputs=[
|
| 216 |
text2image_model_path,
|
| 217 |
text2image_prompt,
|
| 218 |
text2image_negative_prompt,
|
|
|
|
| 219 |
text2image_scheduler,
|
| 220 |
text2image_guidance_scale,
|
| 221 |
text2image_num_inference_step,
|