Upload folder using huggingface_hub
Browse files
app.py
CHANGED
|
@@ -11,54 +11,41 @@ if torch.cuda.is_available():
|
|
| 11 |
pipe = StableDiffusion3Pipeline.from_pretrained(repo, torch_dtype=torch.float16).to("cuda")
|
| 12 |
else:
|
| 13 |
pipe = StableDiffusion3Pipeline.from_pretrained(repo)
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
MAX_IMAGE_SIZE = 1344
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
@spaces.GPU
|
| 20 |
-
def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps, progress=gr.Progress(track_tqdm=True)):
|
| 21 |
-
|
| 22 |
-
if randomize_seed:
|
| 23 |
-
seed = random.randint(0, MAX_SEED)
|
| 24 |
-
|
| 25 |
-
generator = torch.Generator().manual_seed(seed)
|
| 26 |
-
|
| 27 |
-
image = pipe(
|
| 28 |
-
prompt = prompt,
|
| 29 |
-
negative_prompt = negative_prompt,
|
| 30 |
-
guidance_scale = guidance_scale,
|
| 31 |
-
num_inference_steps = num_inference_steps,
|
| 32 |
-
width = width,
|
| 33 |
-
height = height,
|
| 34 |
-
generator = generator
|
| 35 |
-
).images[0]
|
| 36 |
-
|
| 37 |
-
return image, seed
|
| 38 |
-
|
| 39 |
examples = [
|
| 40 |
"Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
|
| 41 |
"An astronaut riding a green horse",
|
| 42 |
"A delicious ceviche cheesecake slice",
|
| 43 |
]
|
| 44 |
-
|
| 45 |
-
css="""
|
| 46 |
#col-container {
|
| 47 |
margin: 0 auto;
|
| 48 |
max-width: 580px;
|
| 49 |
}
|
| 50 |
"""
|
| 51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
with gr.Blocks(css=css) as demo:
|
| 53 |
-
|
| 54 |
with gr.Column(elem_id="col-container"):
|
| 55 |
-
gr.Markdown(
|
| 56 |
-
# Demo [Stable Diffusion 3 Medium](https://huggingface.co/stabilityai/stable-diffusion-3-medium)
|
| 57 |
-
Learn more about the [Stable Diffusion 3 series](https://stability.ai/news/stable-diffusion-3). Try on [Stability AI API](https://platform.stability.ai/docs/api-reference#tag/Generate/paths/~1v2beta~1stable-image~1generate~1sd3/post), [Stable Assistant](https://stability.ai/stable-assistant), or on Discord via [Stable Artisan](https://stability.ai/stable-artisan). Run locally with [ComfyUI](https://github.com/comfyanonymous/ComfyUI) or [diffusers](https://github.com/huggingface/diffusers)
|
| 58 |
-
""")
|
| 59 |
-
|
| 60 |
with gr.Row():
|
| 61 |
-
|
| 62 |
prompt = gr.Text(
|
| 63 |
label="Prompt",
|
| 64 |
show_label=False,
|
|
@@ -66,57 +53,45 @@ with gr.Blocks(css=css) as demo:
|
|
| 66 |
placeholder="Enter your prompt",
|
| 67 |
container=False,
|
| 68 |
)
|
| 69 |
-
|
| 70 |
run_button = gr.Button("Run", scale=0)
|
| 71 |
-
|
| 72 |
result = gr.Image(label="Result", show_label=False)
|
| 73 |
-
|
| 74 |
with gr.Accordion("Advanced Settings", open=False):
|
| 75 |
-
|
| 76 |
negative_prompt = gr.Text(
|
| 77 |
-
label="Negative
|
| 78 |
max_lines=1,
|
| 79 |
placeholder="Enter a negative prompt",
|
| 80 |
)
|
| 81 |
-
|
| 82 |
seed = gr.Slider(
|
| 83 |
label="Seed",
|
| 84 |
minimum=0,
|
| 85 |
-
maximum=
|
| 86 |
step=1,
|
| 87 |
value=0,
|
| 88 |
)
|
| 89 |
-
|
| 90 |
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
| 91 |
-
|
| 92 |
with gr.Row():
|
| 93 |
-
|
| 94 |
width = gr.Slider(
|
| 95 |
label="Width",
|
| 96 |
minimum=256,
|
| 97 |
-
maximum=
|
| 98 |
step=64,
|
| 99 |
value=1024,
|
| 100 |
)
|
| 101 |
-
|
| 102 |
height = gr.Slider(
|
| 103 |
label="Height",
|
| 104 |
minimum=256,
|
| 105 |
-
maximum=
|
| 106 |
step=64,
|
| 107 |
value=1024,
|
| 108 |
)
|
| 109 |
-
|
| 110 |
with gr.Row():
|
| 111 |
-
|
| 112 |
guidance_scale = gr.Slider(
|
| 113 |
label="Guidance scale",
|
| 114 |
minimum=0.0,
|
| 115 |
maximum=10.0,
|
| 116 |
step=0.1,
|
| 117 |
-
value=5
|
| 118 |
)
|
| 119 |
-
|
| 120 |
num_inference_steps = gr.Slider(
|
| 121 |
label="Number of inference steps",
|
| 122 |
minimum=1,
|
|
@@ -124,16 +99,14 @@ with gr.Blocks(css=css) as demo:
|
|
| 124 |
step=1,
|
| 125 |
value=5,
|
| 126 |
)
|
| 127 |
-
|
| 128 |
gr.Examples(
|
| 129 |
-
examples
|
| 130 |
-
inputs
|
| 131 |
)
|
| 132 |
gr.on(
|
| 133 |
triggers=[run_button.click, prompt.submit, negative_prompt.submit],
|
| 134 |
-
fn
|
| 135 |
-
inputs
|
| 136 |
-
outputs
|
| 137 |
)
|
| 138 |
-
|
| 139 |
-
demo.launch()
|
|
|
|
| 11 |
pipe = StableDiffusion3Pipeline.from_pretrained(repo, torch_dtype=torch.float16).to("cuda")
|
| 12 |
else:
|
| 13 |
pipe = StableDiffusion3Pipeline.from_pretrained(repo)
|
| 14 |
+
max_seed = np.iinfo(np.int32).max
|
| 15 |
+
max_image_size = 1344
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
examples = [
|
| 17 |
"Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
|
| 18 |
"An astronaut riding a green horse",
|
| 19 |
"A delicious ceviche cheesecake slice",
|
| 20 |
]
|
| 21 |
+
css = """
|
|
|
|
| 22 |
#col-container {
|
| 23 |
margin: 0 auto;
|
| 24 |
max-width: 580px;
|
| 25 |
}
|
| 26 |
"""
|
| 27 |
|
| 28 |
+
|
| 29 |
+
@spaces.GPU
|
| 30 |
+
def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps):
|
| 31 |
+
if randomize_seed:
|
| 32 |
+
seed = random.randint(0, max_seed)
|
| 33 |
+
image = pipe(
|
| 34 |
+
prompt=prompt,
|
| 35 |
+
negative_prompt=negative_prompt,
|
| 36 |
+
guidance_scale=guidance_scale,
|
| 37 |
+
num_inference_steps=num_inference_steps,
|
| 38 |
+
width=width,
|
| 39 |
+
height=height,
|
| 40 |
+
generator=torch.Generator().manual_seed(seed)
|
| 41 |
+
).images[0]
|
| 42 |
+
return image, seed
|
| 43 |
+
|
| 44 |
+
|
| 45 |
with gr.Blocks(css=css) as demo:
|
|
|
|
| 46 |
with gr.Column(elem_id="col-container"):
|
| 47 |
+
gr.Markdown("# Demo [Stable Diffusion 3 Medium](https://huggingface.co/stabilityai/stable-diffusion-3-medium)")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
with gr.Row():
|
|
|
|
| 49 |
prompt = gr.Text(
|
| 50 |
label="Prompt",
|
| 51 |
show_label=False,
|
|
|
|
| 53 |
placeholder="Enter your prompt",
|
| 54 |
container=False,
|
| 55 |
)
|
|
|
|
| 56 |
run_button = gr.Button("Run", scale=0)
|
|
|
|
| 57 |
result = gr.Image(label="Result", show_label=False)
|
|
|
|
| 58 |
with gr.Accordion("Advanced Settings", open=False):
|
|
|
|
| 59 |
negative_prompt = gr.Text(
|
| 60 |
+
label="Negative Prompt",
|
| 61 |
max_lines=1,
|
| 62 |
placeholder="Enter a negative prompt",
|
| 63 |
)
|
|
|
|
| 64 |
seed = gr.Slider(
|
| 65 |
label="Seed",
|
| 66 |
minimum=0,
|
| 67 |
+
maximum=max_seed,
|
| 68 |
step=1,
|
| 69 |
value=0,
|
| 70 |
)
|
|
|
|
| 71 |
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
|
|
|
| 72 |
with gr.Row():
|
|
|
|
| 73 |
width = gr.Slider(
|
| 74 |
label="Width",
|
| 75 |
minimum=256,
|
| 76 |
+
maximum=max_image_size,
|
| 77 |
step=64,
|
| 78 |
value=1024,
|
| 79 |
)
|
|
|
|
| 80 |
height = gr.Slider(
|
| 81 |
label="Height",
|
| 82 |
minimum=256,
|
| 83 |
+
maximum=max_image_size,
|
| 84 |
step=64,
|
| 85 |
value=1024,
|
| 86 |
)
|
|
|
|
| 87 |
with gr.Row():
|
|
|
|
| 88 |
guidance_scale = gr.Slider(
|
| 89 |
label="Guidance scale",
|
| 90 |
minimum=0.0,
|
| 91 |
maximum=10.0,
|
| 92 |
step=0.1,
|
| 93 |
+
value=7.5,
|
| 94 |
)
|
|
|
|
| 95 |
num_inference_steps = gr.Slider(
|
| 96 |
label="Number of inference steps",
|
| 97 |
minimum=1,
|
|
|
|
| 99 |
step=1,
|
| 100 |
value=5,
|
| 101 |
)
|
|
|
|
| 102 |
gr.Examples(
|
| 103 |
+
examples=examples,
|
| 104 |
+
inputs=[prompt]
|
| 105 |
)
|
| 106 |
gr.on(
|
| 107 |
triggers=[run_button.click, prompt.submit, negative_prompt.submit],
|
| 108 |
+
fn=infer,
|
| 109 |
+
inputs=[prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
|
| 110 |
+
outputs=[result, seed]
|
| 111 |
)
|
| 112 |
+
demo.launch()
|
|
|