Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,74 +1,70 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
| 3 |
import random
|
| 4 |
-
|
| 5 |
-
# import spaces #[uncomment to use ZeroGPU]
|
| 6 |
from diffusers import DiffusionPipeline
|
| 7 |
import torch
|
| 8 |
|
| 9 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 10 |
-
model_repo_id = "stabilityai/sdxl-turbo" # Replace to the model you would like to use
|
| 11 |
|
| 12 |
if torch.cuda.is_available():
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
pipe =
|
|
|
|
| 19 |
|
| 20 |
MAX_SEED = np.iinfo(np.int32).max
|
| 21 |
MAX_IMAGE_SIZE = 1024
|
| 22 |
|
|
|
|
| 23 |
|
| 24 |
-
# @spaces.GPU #[uncomment to use ZeroGPU]
|
| 25 |
-
def infer(
|
| 26 |
-
prompt,
|
| 27 |
-
negative_prompt,
|
| 28 |
-
seed,
|
| 29 |
-
randomize_seed,
|
| 30 |
-
width,
|
| 31 |
-
height,
|
| 32 |
-
guidance_scale,
|
| 33 |
-
num_inference_steps,
|
| 34 |
-
progress=gr.Progress(track_tqdm=True),
|
| 35 |
-
):
|
| 36 |
if randomize_seed:
|
| 37 |
seed = random.randint(0, MAX_SEED)
|
| 38 |
-
|
| 39 |
generator = torch.Generator().manual_seed(seed)
|
| 40 |
-
|
| 41 |
image = pipe(
|
| 42 |
-
prompt=prompt,
|
| 43 |
-
negative_prompt=negative_prompt,
|
| 44 |
-
guidance_scale=guidance_scale,
|
| 45 |
-
num_inference_steps=num_inference_steps,
|
| 46 |
-
width=width,
|
| 47 |
-
height=height,
|
| 48 |
-
generator=generator
|
| 49 |
-
).images[0]
|
| 50 |
-
|
| 51 |
-
return image
|
| 52 |
-
|
| 53 |
|
| 54 |
examples = [
|
| 55 |
-
"
|
| 56 |
-
"
|
| 57 |
-
"
|
| 58 |
]
|
| 59 |
|
| 60 |
-
css
|
| 61 |
#col-container {
|
| 62 |
margin: 0 auto;
|
| 63 |
-
max-width:
|
| 64 |
}
|
| 65 |
"""
|
| 66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
with gr.Blocks(css=css) as demo:
|
|
|
|
| 68 |
with gr.Column(elem_id="col-container"):
|
| 69 |
-
gr.Markdown("
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
| 71 |
with gr.Row():
|
|
|
|
| 72 |
prompt = gr.Text(
|
| 73 |
label="Prompt",
|
| 74 |
show_label=False,
|
|
@@ -76,19 +72,21 @@ with gr.Blocks(css=css) as demo:
|
|
| 76 |
placeholder="Enter your prompt",
|
| 77 |
container=False,
|
| 78 |
)
|
| 79 |
-
|
| 80 |
-
run_button = gr.Button("Run", scale=0
|
| 81 |
-
|
| 82 |
result = gr.Image(label="Result", show_label=False)
|
| 83 |
|
| 84 |
with gr.Accordion("Advanced Settings", open=False):
|
|
|
|
| 85 |
negative_prompt = gr.Text(
|
| 86 |
label="Negative prompt",
|
| 87 |
max_lines=1,
|
| 88 |
placeholder="Enter a negative prompt",
|
| 89 |
-
visible=
|
|
|
|
| 90 |
)
|
| 91 |
-
|
| 92 |
seed = gr.Slider(
|
| 93 |
label="Seed",
|
| 94 |
minimum=0,
|
|
@@ -96,59 +94,56 @@ with gr.Blocks(css=css) as demo:
|
|
| 96 |
step=1,
|
| 97 |
value=0,
|
| 98 |
)
|
| 99 |
-
|
| 100 |
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
| 101 |
-
|
| 102 |
with gr.Row():
|
|
|
|
| 103 |
width = gr.Slider(
|
| 104 |
label="Width",
|
| 105 |
minimum=256,
|
| 106 |
maximum=MAX_IMAGE_SIZE,
|
| 107 |
step=32,
|
| 108 |
-
value=
|
| 109 |
)
|
| 110 |
-
|
| 111 |
height = gr.Slider(
|
| 112 |
label="Height",
|
| 113 |
minimum=256,
|
| 114 |
maximum=MAX_IMAGE_SIZE,
|
| 115 |
step=32,
|
| 116 |
-
value=
|
| 117 |
)
|
| 118 |
-
|
| 119 |
with gr.Row():
|
|
|
|
| 120 |
guidance_scale = gr.Slider(
|
| 121 |
label="Guidance scale",
|
| 122 |
minimum=0.0,
|
| 123 |
maximum=10.0,
|
| 124 |
step=0.1,
|
| 125 |
-
value=0.0,
|
|
|
|
| 126 |
)
|
| 127 |
-
|
| 128 |
num_inference_steps = gr.Slider(
|
| 129 |
label="Number of inference steps",
|
| 130 |
minimum=1,
|
| 131 |
-
maximum=
|
| 132 |
step=1,
|
| 133 |
-
value=2,
|
|
|
|
| 134 |
)
|
| 135 |
-
|
| 136 |
-
gr.Examples(
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
width,
|
| 146 |
-
height,
|
| 147 |
-
guidance_scale,
|
| 148 |
-
num_inference_steps,
|
| 149 |
-
],
|
| 150 |
-
outputs=[result, seed],
|
| 151 |
)
|
| 152 |
|
| 153 |
-
|
| 154 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
| 3 |
import random
|
|
|
|
|
|
|
| 4 |
from diffusers import DiffusionPipeline
|
| 5 |
import torch
|
| 6 |
|
| 7 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
|
|
| 8 |
|
| 9 |
if torch.cuda.is_available():
|
| 10 |
+
torch.cuda.max_memory_allocated(device=device)
|
| 11 |
+
pipe = DiffusionPipeline.from_pretrained("stabilityai/sdxl-turbo", torch_dtype=torch.float16, variant="fp16", use_safetensors=True)
|
| 12 |
+
pipe.enable_xformers_memory_efficient_attention()
|
| 13 |
+
pipe = pipe.to(device)
|
| 14 |
+
else:
|
| 15 |
+
pipe = DiffusionPipeline.from_pretrained("stabilityai/sdxl-turbo", use_safetensors=True)
|
| 16 |
+
pipe = pipe.to(device)
|
| 17 |
|
| 18 |
MAX_SEED = np.iinfo(np.int32).max
|
| 19 |
MAX_IMAGE_SIZE = 1024
|
| 20 |
|
| 21 |
+
def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps):
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
if randomize_seed:
|
| 24 |
seed = random.randint(0, MAX_SEED)
|
| 25 |
+
|
| 26 |
generator = torch.Generator().manual_seed(seed)
|
| 27 |
+
|
| 28 |
image = pipe(
|
| 29 |
+
prompt = prompt,
|
| 30 |
+
negative_prompt = negative_prompt,
|
| 31 |
+
guidance_scale = guidance_scale,
|
| 32 |
+
num_inference_steps = num_inference_steps,
|
| 33 |
+
width = width,
|
| 34 |
+
height = height,
|
| 35 |
+
generator = generator
|
| 36 |
+
).images[0]
|
| 37 |
+
|
| 38 |
+
return image
|
|
|
|
| 39 |
|
| 40 |
examples = [
|
| 41 |
+
"Sunset in Hawaii, cold color palette, muted colors, detailed, 8k",
|
| 42 |
+
"A dog catching a baseball",
|
| 43 |
+
"Vanilla ice cream with hot fudge",
|
| 44 |
]
|
| 45 |
|
| 46 |
+
css="""
|
| 47 |
#col-container {
|
| 48 |
margin: 0 auto;
|
| 49 |
+
max-width: 520px;
|
| 50 |
}
|
| 51 |
"""
|
| 52 |
|
| 53 |
+
if torch.cuda.is_available():
|
| 54 |
+
power_device = "GPU"
|
| 55 |
+
else:
|
| 56 |
+
power_device = "CPU"
|
| 57 |
+
|
| 58 |
with gr.Blocks(css=css) as demo:
|
| 59 |
+
|
| 60 |
with gr.Column(elem_id="col-container"):
|
| 61 |
+
gr.Markdown(f"""
|
| 62 |
+
# Text-to-Image Generation with Stable Diffusion
|
| 63 |
+
Currently running on {power_device}.
|
| 64 |
+
""")
|
| 65 |
+
|
| 66 |
with gr.Row():
|
| 67 |
+
|
| 68 |
prompt = gr.Text(
|
| 69 |
label="Prompt",
|
| 70 |
show_label=False,
|
|
|
|
| 72 |
placeholder="Enter your prompt",
|
| 73 |
container=False,
|
| 74 |
)
|
| 75 |
+
|
| 76 |
+
run_button = gr.Button("Run", scale=0)
|
| 77 |
+
|
| 78 |
result = gr.Image(label="Result", show_label=False)
|
| 79 |
|
| 80 |
with gr.Accordion("Advanced Settings", open=False):
|
| 81 |
+
|
| 82 |
negative_prompt = gr.Text(
|
| 83 |
label="Negative prompt",
|
| 84 |
max_lines=1,
|
| 85 |
placeholder="Enter a negative prompt",
|
| 86 |
+
visible=True,
|
| 87 |
+
info="Do not draw this"
|
| 88 |
)
|
| 89 |
+
|
| 90 |
seed = gr.Slider(
|
| 91 |
label="Seed",
|
| 92 |
minimum=0,
|
|
|
|
| 94 |
step=1,
|
| 95 |
value=0,
|
| 96 |
)
|
| 97 |
+
|
| 98 |
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
| 99 |
+
|
| 100 |
with gr.Row():
|
| 101 |
+
|
| 102 |
width = gr.Slider(
|
| 103 |
label="Width",
|
| 104 |
minimum=256,
|
| 105 |
maximum=MAX_IMAGE_SIZE,
|
| 106 |
step=32,
|
| 107 |
+
value=512,
|
| 108 |
)
|
| 109 |
+
|
| 110 |
height = gr.Slider(
|
| 111 |
label="Height",
|
| 112 |
minimum=256,
|
| 113 |
maximum=MAX_IMAGE_SIZE,
|
| 114 |
step=32,
|
| 115 |
+
value=512,
|
| 116 |
)
|
| 117 |
+
|
| 118 |
with gr.Row():
|
| 119 |
+
|
| 120 |
guidance_scale = gr.Slider(
|
| 121 |
label="Guidance scale",
|
| 122 |
minimum=0.0,
|
| 123 |
maximum=10.0,
|
| 124 |
step=0.1,
|
| 125 |
+
value=0.0,
|
| 126 |
+
info="how much the text prompt influences the result[0 - 10]"
|
| 127 |
)
|
| 128 |
+
|
| 129 |
num_inference_steps = gr.Slider(
|
| 130 |
label="Number of inference steps",
|
| 131 |
minimum=1,
|
| 132 |
+
maximum=12,
|
| 133 |
step=1,
|
| 134 |
+
value=2,
|
| 135 |
+
info="to denoise the image"
|
| 136 |
)
|
| 137 |
+
|
| 138 |
+
gr.Examples(
|
| 139 |
+
examples = examples,
|
| 140 |
+
inputs = [prompt]
|
| 141 |
+
)
|
| 142 |
+
|
| 143 |
+
run_button.click(
|
| 144 |
+
fn = infer,
|
| 145 |
+
inputs = [prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
|
| 146 |
+
outputs = [result]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
)
|
| 148 |
|
| 149 |
+
demo.queue().launch()
|
|
|