Spaces:
Runtime error
Runtime error
File size: 5,112 Bytes
8f873ac 3494400 8f873ac 3494400 8f873ac 376f4dc 8f873ac 8c7d74b 8f873ac 376f4dc 8f873ac d562c6a 8f873ac ec270b9 7bcfad3 8f873ac 24214af 8f873ac 376f4dc 8f873ac 376f4dc 8f873ac 376f4dc 8f873ac 8c7d74b 8f873ac |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
#!/usr/bin/env python
import gradio as gr
import PIL.Image
import torch
import torchvision.transforms.functional as TF
from model import Model
from utils import (
DEFAULT_STYLE_NAME,
MAX_SEED,
STYLE_NAMES,
apply_style,
randomize_seed_fn,
)
def create_demo(model: Model) -> gr.Blocks:
def run(
image: PIL.Image.Image,
prompt: str,
negative_prompt: str,
style_name: str = DEFAULT_STYLE_NAME,
num_steps: int = 25,
guidance_scale: float = 5,
adapter_conditioning_scale: float = 0.8,
adapter_conditioning_factor: float = 0.8,
seed: int = 0,
progress=gr.Progress(track_tqdm=True),
) -> PIL.Image.Image:
image = image.convert("RGB")
image = TF.to_tensor(image) > 0.5
image = TF.to_pil_image(image.to(torch.float32))
prompt, negative_prompt = apply_style(style_name, prompt, negative_prompt)
return model.run(
image=image,
prompt=prompt,
negative_prompt=negative_prompt,
adapter_name="sketch",
num_inference_steps=num_steps,
guidance_scale=guidance_scale,
adapter_conditioning_scale=adapter_conditioning_scale,
adapter_conditioning_factor=adapter_conditioning_factor,
seed=seed,
apply_preprocess=False,
)[1]
with gr.Blocks() as demo:
with gr.Row():
with gr.Column():
with gr.Group():
image = gr.ImageEditor(
type="numpy",
crop_size="1:1",
)
prompt = gr.Textbox(label="Prompt")
style = gr.Dropdown(label="Style", choices=STYLE_NAMES, value=DEFAULT_STYLE_NAME)
run_button = gr.Button("Run")
with gr.Accordion("Advanced options", open=False):
negative_prompt = gr.Textbox(
label="Negative prompt",
value=" extra digit, fewer digits, cropped, worst quality, low quality, glitch, deformed, mutated, ugly, disfigured",
)
num_steps = gr.Slider(
label="Number of steps",
minimum=1,
maximum=50,
step=1,
value=25,
)
guidance_scale = gr.Slider(
label="Guidance scale",
minimum=0.1,
maximum=10.0,
step=0.1,
value=5,
)
adapter_conditioning_scale = gr.Slider(
label="Adapter conditioning scale",
minimum=0.5,
maximum=1,
step=0.1,
value=0.8,
)
adapter_conditioning_factor = gr.Slider(
label="Adapter conditioning factor",
info="Fraction of timesteps for which adapter should be applied",
minimum=0.5,
maximum=1,
step=0.1,
value=0.8,
)
seed = gr.Slider(
label="Seed",
minimum=0,
maximum=MAX_SEED,
step=1,
value=0,
)
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
with gr.Column():
result = gr.Image(label="Result", height=600)
inputs = [
image,
prompt,
negative_prompt,
style,
num_steps,
guidance_scale,
adapter_conditioning_scale,
adapter_conditioning_factor,
seed,
]
prompt.submit(
fn=randomize_seed_fn,
inputs=[seed, randomize_seed],
outputs=seed,
queue=False,
api_name=False,
).then(
fn=run,
inputs=inputs,
outputs=result,
api_name=False,
)
negative_prompt.submit(
fn=randomize_seed_fn,
inputs=[seed, randomize_seed],
outputs=seed,
queue=False,
api_name=False,
).then(
fn=run,
inputs=inputs,
outputs=result,
api_name=False,
)
run_button.click(
fn=randomize_seed_fn,
inputs=[seed, randomize_seed],
outputs=seed,
queue=False,
api_name=False,
).then(
fn=run,
inputs=inputs,
outputs=result,
api_name=False,
)
return demo
if __name__ == "__main__":
model = Model("sketch")
demo = create_demo(model)
demo.queue(max_size=20).launch()
|