Spaces:
Running
on
Zero
Running
on
Zero
bo.l
commited on
Commit
·
ee3f2c0
1
Parent(s):
4484346
update input
Browse files
app.py
CHANGED
|
@@ -63,7 +63,8 @@ MAX_IMAGE_SIZE = 512
|
|
| 63 |
@spaces.GPU #[uncomment to use ZeroGPU]
|
| 64 |
def infer(
|
| 65 |
prompt,
|
| 66 |
-
|
|
|
|
| 67 |
seed,
|
| 68 |
randomize_seed,
|
| 69 |
width,
|
|
@@ -109,16 +110,11 @@ with gr.Blocks(css=css) as demo:
|
|
| 109 |
gr.Markdown("# Text-to-Image Gradio Template")
|
| 110 |
|
| 111 |
with gr.Row():
|
| 112 |
-
prompt = gr.Text(
|
| 113 |
-
|
| 114 |
-
show_label=False,
|
| 115 |
-
max_lines=1,
|
| 116 |
-
placeholder="Enter your prompt",
|
| 117 |
-
container=False,
|
| 118 |
-
)
|
| 119 |
run_button = gr.Button("Run", scale=0, variant="primary")
|
| 120 |
|
| 121 |
-
#
|
| 122 |
with gr.Row():
|
| 123 |
ref1 = gr.Image(label="Input Image 1", type="pil")
|
| 124 |
ref2 = gr.Image(label="Input Image 2", type="pil")
|
|
@@ -126,68 +122,54 @@ with gr.Blocks(css=css) as demo:
|
|
| 126 |
result = gr.Image(label="Result", show_label=False)
|
| 127 |
|
| 128 |
with gr.Accordion("Advanced Settings", open=False):
|
| 129 |
-
seed = gr.Slider(
|
| 130 |
-
label="Seed",
|
| 131 |
-
minimum=0,
|
| 132 |
-
maximum=MAX_SEED,
|
| 133 |
-
step=1,
|
| 134 |
-
value=0,
|
| 135 |
-
)
|
| 136 |
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
| 137 |
-
|
| 138 |
with gr.Row():
|
| 139 |
-
width = gr.Slider(
|
| 140 |
-
|
| 141 |
-
minimum=256,
|
| 142 |
-
maximum=MAX_IMAGE_SIZE,
|
| 143 |
-
step=32,
|
| 144 |
-
value=1024,
|
| 145 |
-
)
|
| 146 |
-
height = gr.Slider(
|
| 147 |
-
label="Height",
|
| 148 |
-
minimum=256,
|
| 149 |
-
maximum=MAX_IMAGE_SIZE,
|
| 150 |
-
step=32,
|
| 151 |
-
value=1024,
|
| 152 |
-
)
|
| 153 |
-
|
| 154 |
with gr.Row():
|
| 155 |
-
guidance_scale = gr.Slider(
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
maximum=10.0,
|
| 159 |
-
step=0.1,
|
| 160 |
-
value=0.0,
|
| 161 |
-
)
|
| 162 |
-
num_inference_steps = gr.Slider(
|
| 163 |
-
label="Number of inference steps",
|
| 164 |
-
minimum=1,
|
| 165 |
-
maximum=50,
|
| 166 |
-
step=1,
|
| 167 |
-
value=2,
|
| 168 |
-
)
|
| 169 |
-
|
| 170 |
-
# 如果 examples 只包含文本 prompt,保持如下即可
|
| 171 |
examples = [
|
| 172 |
["a cute corgi in a wizard hat"],
|
| 173 |
["a watercolor painting of yosemite valley at sunrise"],
|
| 174 |
]
|
| 175 |
gr.Examples(examples=examples, inputs=[prompt])
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 191 |
|
| 192 |
if __name__ == "__main__":
|
| 193 |
demo.launch()
|
|
|
|
| 63 |
@spaces.GPU #[uncomment to use ZeroGPU]
|
| 64 |
def infer(
|
| 65 |
prompt,
|
| 66 |
+
ref1,
|
| 67 |
+
ref2,
|
| 68 |
seed,
|
| 69 |
randomize_seed,
|
| 70 |
width,
|
|
|
|
| 110 |
gr.Markdown("# Text-to-Image Gradio Template")
|
| 111 |
|
| 112 |
with gr.Row():
|
| 113 |
+
prompt = gr.Text(label="Prompt", show_label=False, max_lines=1,
|
| 114 |
+
placeholder="Enter your prompt", container=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
run_button = gr.Button("Run", scale=0, variant="primary")
|
| 116 |
|
| 117 |
+
# 两张输入图片,其中 ref2 可为空
|
| 118 |
with gr.Row():
|
| 119 |
ref1 = gr.Image(label="Input Image 1", type="pil")
|
| 120 |
ref2 = gr.Image(label="Input Image 2", type="pil")
|
|
|
|
| 122 |
result = gr.Image(label="Result", show_label=False)
|
| 123 |
|
| 124 |
with gr.Accordion("Advanced Settings", open=False):
|
| 125 |
+
seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
|
|
|
| 127 |
with gr.Row():
|
| 128 |
+
width = gr.Slider(label="Width", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=1024)
|
| 129 |
+
height = gr.Slider(label="Height", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=1024)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
with gr.Row():
|
| 131 |
+
guidance_scale = gr.Slider(label="Guidance scale", minimum=0.0, maximum=10.0, step=0.1, value=0.0)
|
| 132 |
+
num_inference_steps = gr.Slider(label="Number of inference steps", minimum=1, maximum=50, step=1, value=2)
|
| 133 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
examples = [
|
| 135 |
["a cute corgi in a wizard hat"],
|
| 136 |
["a watercolor painting of yosemite valley at sunrise"],
|
| 137 |
]
|
| 138 |
gr.Examples(examples=examples, inputs=[prompt])
|
| 139 |
+
|
| 140 |
+
# 用于装“可变长度”的参考图列表
|
| 141 |
+
refs_state = gr.State([])
|
| 142 |
+
|
| 143 |
+
# 先把两张图打包到 state,自动过滤 None,这样 ref2 就是可选的
|
| 144 |
+
def pack_refs(a, b):
|
| 145 |
+
return [x for x in (a, b) if x is not None]
|
| 146 |
+
|
| 147 |
+
# 你的推理函数接受“列表”refs
|
| 148 |
+
def infer(prompt, refs, seed, randomize_seed, width, height, guidance_scale, num_steps):
|
| 149 |
+
# 如需长度为2,可补齐到 [ref1, None]
|
| 150 |
+
if len(refs) == 0:
|
| 151 |
+
refs = [None, None]
|
| 152 |
+
elif len(refs) == 1:
|
| 153 |
+
refs = [refs[0], None]
|
| 154 |
+
|
| 155 |
+
# TODO: 在这里调用你的模型,使用 refs[0], refs[1](第二张可能是 None)
|
| 156 |
+
# out_img = ...
|
| 157 |
+
# used_seed = ...
|
| 158 |
+
return out_img, used_seed
|
| 159 |
+
|
| 160 |
+
# 第一步:把 ref1/ref2 打包进 refs_state
|
| 161 |
+
dep = gr.on(
|
| 162 |
+
triggers=[run_button.click, prompt.submit],
|
| 163 |
+
fn=pack_refs,
|
| 164 |
+
inputs=[ref1, ref2],
|
| 165 |
+
outputs=refs_state,
|
| 166 |
+
)
|
| 167 |
+
# 第二步:再把打包好的列表传给 infer
|
| 168 |
+
dep.then(
|
| 169 |
+
fn=infer,
|
| 170 |
+
inputs=[prompt, refs_state, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
|
| 171 |
+
outputs=[result, seed],
|
| 172 |
+
)
|
| 173 |
|
| 174 |
if __name__ == "__main__":
|
| 175 |
demo.launch()
|