Spaces:
Runtime error
Runtime error
Commit
Β·
a20be5f
1
Parent(s):
8a0d8ee
Update edit_app.py
Browse files- edit_app.py +66 -23
edit_app.py
CHANGED
|
@@ -1,13 +1,15 @@
|
|
| 1 |
-
|
| 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:
|
|
@@ -79,27 +81,61 @@ def main():
|
|
| 79 |
randomize_cfg: bool,
|
| 80 |
text_cfg_scale: float,
|
| 81 |
image_cfg_scale: float,
|
|
|
|
|
|
|
|
|
|
| 82 |
):
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
return [seed, text_cfg_scale, image_cfg_scale, edited_image]
|
| 104 |
|
| 105 |
def reset():
|
|
@@ -139,6 +175,7 @@ def main():
|
|
| 139 |
show_label=False,
|
| 140 |
interactive=True,
|
| 141 |
)
|
|
|
|
| 142 |
seed = gr.Number(value=1371, precision=0, label="Seed", interactive=True)
|
| 143 |
randomize_cfg = gr.Radio(
|
| 144 |
["Fix CFG", "Randomize CFG"],
|
|
@@ -149,6 +186,9 @@ def main():
|
|
| 149 |
)
|
| 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 |
|
|
@@ -175,6 +215,9 @@ def main():
|
|
| 175 |
randomize_cfg,
|
| 176 |
text_cfg_scale,
|
| 177 |
image_cfg_scale,
|
|
|
|
|
|
|
|
|
|
| 178 |
],
|
| 179 |
outputs=[seed, text_cfg_scale, image_cfg_scale, edited_image],
|
| 180 |
)
|
|
@@ -189,4 +232,4 @@ def main():
|
|
| 189 |
|
| 190 |
|
| 191 |
if __name__ == "__main__":
|
| 192 |
-
main()
|
|
|
|
| 1 |
+
rom __future__ import annotations
|
| 2 |
|
| 3 |
import math
|
| 4 |
+
import os
|
| 5 |
import random
|
| 6 |
+
import csv
|
| 7 |
import gradio as gr
|
| 8 |
import torch
|
| 9 |
from PIL import Image, ImageOps
|
| 10 |
from diffusers import StableDiffusionInstructPix2PixPipeline
|
| 11 |
|
| 12 |
+
header = ['file', 'steps', 'seed', 'image_cfg', 'text_cfg', 'prompt', 'input height', 'input width']
|
| 13 |
|
| 14 |
help_text = """
|
| 15 |
If you're not getting what you want, there may be a few reasons:
|
|
|
|
| 81 |
randomize_cfg: bool,
|
| 82 |
text_cfg_scale: float,
|
| 83 |
image_cfg_scale: float,
|
| 84 |
+
number_of_images: int,
|
| 85 |
+
folder_path: str,
|
| 86 |
+
file_name: str
|
| 87 |
):
|
| 88 |
+
for i in range(0, int(number_of_images)):
|
| 89 |
+
seed = random.randint(0, 100000) if randomize_seed else seed
|
| 90 |
+
text_cfg_scale = round(random.uniform(6.0, 9.0), ndigits=2) if randomize_cfg else text_cfg_scale
|
| 91 |
+
image_cfg_scale = round(random.uniform(1.2, 1.8), ndigits=2) if randomize_cfg else image_cfg_scale
|
| 92 |
+
|
| 93 |
+
width, height = input_image.size
|
| 94 |
+
factor = 512 / max(width, height)
|
| 95 |
+
factor = math.ceil(min(width, height) * factor / 64) * 64 / min(width, height)
|
| 96 |
+
width = int((width * factor) // 64) * 64
|
| 97 |
+
height = int((height * factor) // 64) * 64
|
| 98 |
+
#input_image = ImageOps.fit(input_image, (width, height), method=Image.Resampling.LANCZOS)
|
| 99 |
+
|
| 100 |
+
if instruction == "":
|
| 101 |
+
return [input_image, seed]
|
| 102 |
+
|
| 103 |
+
generator = torch.manual_seed(seed)
|
| 104 |
+
edited_image = pipe(
|
| 105 |
+
instruction, image=input_image,
|
| 106 |
+
guidance_scale=text_cfg_scale, image_guidance_scale=image_cfg_scale,
|
| 107 |
+
num_inference_steps=steps, generator=generator,
|
| 108 |
+
).images[0]
|
| 109 |
+
|
| 110 |
+
if(os.path.exists(folder_path)):
|
| 111 |
+
file_path = folder_path + "/" + "records.csv"
|
| 112 |
+
if(os.path.isfile(file_path)):
|
| 113 |
+
with open(file_path, 'a', encoding='UTF8') as f:
|
| 114 |
+
writer = csv.writer(f)
|
| 115 |
+
if(i == 0):
|
| 116 |
+
data = [file_name + "_" + str(i), steps, seed, image_cfg_scale, text_cfg_scale, instruction, height, width]
|
| 117 |
+
print(data)
|
| 118 |
+
writer.writerow(data)
|
| 119 |
+
f.close()
|
| 120 |
+
if(i != 0):
|
| 121 |
+
data = [file_name + "_" + str(i), steps, seed, image_cfg_scale, text_cfg_scale, instruction, height, width]
|
| 122 |
+
print(data)
|
| 123 |
+
writer.writerow(data)
|
| 124 |
+
f.close()
|
| 125 |
+
else:
|
| 126 |
+
with open(file_path, 'w', encoding = 'UTF8') as f:
|
| 127 |
+
writer = csv.writer(f)
|
| 128 |
+
writer.writerow(header)
|
| 129 |
+
data = [file_name + "_" + str(i), steps, seed, image_cfg_scale, text_cfg_scale, instruction, height, width]
|
| 130 |
+
print(data)
|
| 131 |
+
writer.writerow(data)
|
| 132 |
+
f.close()
|
| 133 |
+
else:
|
| 134 |
+
os.mkdir(folder_path)
|
| 135 |
+
|
| 136 |
+
|
| 137 |
+
parent_path = folder_path
|
| 138 |
+
edited_image.save(f"{parent_path}/{file_name}_{i}.png")
|
| 139 |
return [seed, text_cfg_scale, image_cfg_scale, edited_image]
|
| 140 |
|
| 141 |
def reset():
|
|
|
|
| 175 |
show_label=False,
|
| 176 |
interactive=True,
|
| 177 |
)
|
| 178 |
+
|
| 179 |
seed = gr.Number(value=1371, precision=0, label="Seed", interactive=True)
|
| 180 |
randomize_cfg = gr.Radio(
|
| 181 |
["Fix CFG", "Randomize CFG"],
|
|
|
|
| 186 |
)
|
| 187 |
text_cfg_scale = gr.Number(value=7.5, label=f"Text CFG", interactive=True)
|
| 188 |
image_cfg_scale = gr.Number(value=1.5, label=f"Image CFG", interactive=True)
|
| 189 |
+
number_of_images = gr.Number(value=0, label=f"Number of Images", interactive=True)
|
| 190 |
+
folder_path = gr.Textbox(lines=1, label="Folder Path", interactive=True)
|
| 191 |
+
file_name = gr.Textbox(lines=1, label="File Name", interactive=True)
|
| 192 |
|
| 193 |
gr.Markdown(help_text)
|
| 194 |
|
|
|
|
| 215 |
randomize_cfg,
|
| 216 |
text_cfg_scale,
|
| 217 |
image_cfg_scale,
|
| 218 |
+
number_of_images,
|
| 219 |
+
folder_path,
|
| 220 |
+
file_name
|
| 221 |
],
|
| 222 |
outputs=[seed, text_cfg_scale, image_cfg_scale, edited_image],
|
| 223 |
)
|
|
|
|
| 232 |
|
| 233 |
|
| 234 |
if __name__ == "__main__":
|
| 235 |
+
main()
|