Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,165 @@
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
|
14 |
+
"""
|
15 |
+
|
16 |
+
|
17 |
+
example_instructions = [
|
18 |
+
"A river"
|
19 |
+
]
|
20 |
+
|
21 |
+
model_id = "models/dimentox/heightmapstyle"
|
22 |
+
|
23 |
+
|
24 |
+
def main():
|
25 |
+
pipe = StableDiffusionInstructPix2PixPipeline.from_pretrained(model_id, torch_dtype=torch.float16, safety_checker=None).to("cuda")
|
26 |
+
#example_image = Image.open("imgs/example.jpg").convert("RGB")
|
27 |
+
|
28 |
+
def load_example(
|
29 |
+
steps: int,
|
30 |
+
randomize_seed: bool,
|
31 |
+
seed: int,
|
32 |
+
randomize_cfg: bool,
|
33 |
+
text_cfg_scale: float,
|
34 |
+
image_cfg_scale: float,
|
35 |
+
):
|
36 |
+
example_instruction = random.choice(example_instructions)
|
37 |
+
return [example_instruction] + generate(
|
38 |
+
example_instruction,
|
39 |
+
steps,
|
40 |
+
randomize_seed,
|
41 |
+
seed,
|
42 |
+
randomize_cfg,
|
43 |
+
text_cfg_scale,
|
44 |
+
image_cfg_scale,
|
45 |
+
)
|
46 |
+
|
47 |
+
def generate(
|
48 |
+
instruction: str,
|
49 |
+
steps: int,
|
50 |
+
randomize_seed: bool,
|
51 |
+
seed: int,
|
52 |
+
randomize_cfg: bool,
|
53 |
+
text_cfg_scale: float,
|
54 |
+
image_cfg_scale: float,
|
55 |
+
):
|
56 |
+
seed = random.randint(0, 100000) if randomize_seed else seed
|
57 |
+
text_cfg_scale = round(random.uniform(6.0, 9.0), ndigits=2) if randomize_cfg else text_cfg_scale
|
58 |
+
image_cfg_scale = round(random.uniform(1.2, 1.8), ndigits=2) if randomize_cfg else image_cfg_scale
|
59 |
+
|
60 |
+
width, height = input_image.size
|
61 |
+
factor = 512 / max(width, height)
|
62 |
+
factor = math.ceil(min(width, height) * factor / 64) * 64 / min(width, height)
|
63 |
+
width = int((width * factor) // 64) * 64
|
64 |
+
height = int((height * factor) // 64) * 64
|
65 |
+
input_image = ImageOps.fit(input_image, (width, height), method=Image.Resampling.LANCZOS)
|
66 |
+
|
67 |
+
if instruction == "":
|
68 |
+
return [input_image, seed]
|
69 |
|
70 |
+
generator = torch.manual_seed(seed)
|
71 |
+
edited_image = pipe(
|
72 |
+
instruction,
|
73 |
+
guidance_scale=text_cfg_scale, image_guidance_scale=image_cfg_scale,
|
74 |
+
num_inference_steps=steps, generator=generator,
|
75 |
+
).images[0]
|
76 |
+
return [seed, text_cfg_scale, image_cfg_scale, edited_image]
|
77 |
+
|
78 |
+
def reset():
|
79 |
+
return [0, "Randomize Seed", 1371, "Fix CFG", 7.5, 1.5, None]
|
80 |
+
|
81 |
+
with gr.Blocks() as demo:
|
82 |
+
gr.HTML("""
|
83 |
+
|
84 |
+
""")
|
85 |
+
with gr.Row():
|
86 |
+
with gr.Column(scale=1, min_width=100):
|
87 |
+
generate_button = gr.Button("Generate")
|
88 |
+
with gr.Column(scale=1, min_width=100):
|
89 |
+
load_button = gr.Button("Load Example")
|
90 |
+
with gr.Column(scale=1, min_width=100):
|
91 |
+
reset_button = gr.Button("Reset")
|
92 |
+
with gr.Column(scale=3):
|
93 |
+
instruction = gr.Textbox(lines=1, label="Edit Instruction", interactive=True)
|
94 |
+
|
95 |
+
|
96 |
+
with gr.Row():
|
97 |
+
steps = gr.Number(value=50, precision=0, label="Steps", interactive=True)
|
98 |
+
randomize_seed = gr.Radio(
|
99 |
+
["Fix Seed", "Randomize Seed"],
|
100 |
+
value="Randomize Seed",
|
101 |
+
type="index",
|
102 |
+
show_label=False,
|
103 |
+
interactive=True,
|
104 |
+
)
|
105 |
+
seed = gr.Number(value=1371, precision=0, label="Seed", interactive=True)
|
106 |
+
randomize_cfg = gr.Radio(
|
107 |
+
["Fix CFG", "Randomize CFG"],
|
108 |
+
value="Fix CFG",
|
109 |
+
type="index",
|
110 |
+
show_label=False,
|
111 |
+
interactive=True,
|
112 |
+
)
|
113 |
+
text_cfg_scale = gr.Number(value=7.5, label=f"Text CFG", interactive=True)
|
114 |
+
image_cfg_scale = gr.Number(value=1.5, label=f"Image CFG", interactive=True)
|
115 |
+
|
116 |
+
gr.Markdown(help_text)
|
117 |
+
|
118 |
+
load_button.click(
|
119 |
+
fn=load_example,
|
120 |
+
inputs=[
|
121 |
+
steps,
|
122 |
+
randomize_seed,
|
123 |
+
seed,
|
124 |
+
randomize_cfg,
|
125 |
+
text_cfg_scale,
|
126 |
+
image_cfg_scale,
|
127 |
+
],
|
128 |
+
outputs=[input_image, instruction, seed, text_cfg_scale, image_cfg_scale, edited_image],
|
129 |
+
)
|
130 |
+
generate_button.click(
|
131 |
+
fn=generate,
|
132 |
+
inputs=[
|
133 |
+
input_image,
|
134 |
+
instruction,
|
135 |
+
steps,
|
136 |
+
randomize_seed,
|
137 |
+
seed,
|
138 |
+
randomize_cfg,
|
139 |
+
text_cfg_scale,
|
140 |
+
image_cfg_scale,
|
141 |
+
],
|
142 |
+
outputs=[seed, text_cfg_scale, image_cfg_scale, edited_image],
|
143 |
+
)
|
144 |
+
reset_button.click(
|
145 |
+
fn=reset,
|
146 |
+
inputs=[],
|
147 |
+
outputs=[steps, randomize_seed, seed, randomize_cfg, text_cfg_scale, image_cfg_scale, edited_image],
|
148 |
+
)
|
149 |
+
|
150 |
+
demo.queue(concurrency_count=1)
|
151 |
+
demo.launch(share=False)
|
152 |
+
|
153 |
+
|
154 |
+
if __name__ == "__main__":
|
155 |
+
main()
|
156 |
+
|
157 |
+
|
158 |
+
|
159 |
+
import gradio as gr
|
160 |
+
gr.Examples(
|
161 |
+
[["heightmapsstyle", "a lake with a river"], ["heightmapsstyle","greyscale", "a river running though flat planes"]],
|
162 |
+
[txt, txt_2],
|
163 |
+
cache_examples=True,
|
164 |
+
)
|
165 |
+
gr.load().launch()
|