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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Thanks: https://huggingface.co/spaces/markmagic/Stable-Diffusion-3/blob/main/app.py
|
| 2 |
+
|
| 3 |
+
import os
|
| 4 |
+
import random
|
| 5 |
+
import uuid
|
| 6 |
+
|
| 7 |
+
import gradio as gr
|
| 8 |
+
import numpy as np
|
| 9 |
+
from PIL import Image
|
| 10 |
+
import spaces
|
| 11 |
+
import torch
|
| 12 |
+
from diffusers import StableDiffusion3Pipeline, DPMSolverMultistepScheduler, AutoencoderKL
|
| 13 |
+
|
| 14 |
+
huggingface_token = os.getenv("TOKEN")
|
| 15 |
+
|
| 16 |
+
DESCRIPTION = """# Stable Diffusion 3 with Japanese"""
|
| 17 |
+
|
| 18 |
+
pipe = StableDiffusion3Pipeline.from_pretrained("stabilityai/stable-diffusion-3-medium-diffusers", torch_dtype=torch.float16)
|
| 19 |
+
pipe = pipe.to("cuda")
|
| 20 |
+
|
| 21 |
+
@spaces.GPU()
|
| 22 |
+
def generate(
|
| 23 |
+
prompt: str,
|
| 24 |
+
negative_prompt: str = "",
|
| 25 |
+
use_negative_prompt: bool = False,
|
| 26 |
+
seed: int = 0,
|
| 27 |
+
width: int = 1024,
|
| 28 |
+
height: int = 1024,
|
| 29 |
+
guidance_scale: float = 7,
|
| 30 |
+
randomize_seed: bool = False,
|
| 31 |
+
num_inference_steps=30,
|
| 32 |
+
use_resolution_binning: bool = True,
|
| 33 |
+
progress=gr.Progress(track_tqdm=True),
|
| 34 |
+
):
|
| 35 |
+
pipe.to(device)
|
| 36 |
+
seed = int(randomize_seed_fn(seed, randomize_seed))
|
| 37 |
+
generator = torch.Generator().manual_seed(seed)
|
| 38 |
+
|
| 39 |
+
#pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
|
| 40 |
+
|
| 41 |
+
if not use_negative_prompt:
|
| 42 |
+
negative_prompt = None # type: ignore
|
| 43 |
+
|
| 44 |
+
output = pipe(
|
| 45 |
+
prompt=prompt,
|
| 46 |
+
negative_prompt=negative_prompt,
|
| 47 |
+
width=width,
|
| 48 |
+
height=height,
|
| 49 |
+
guidance_scale=guidance_scale,
|
| 50 |
+
num_inference_steps=num_inference_steps,
|
| 51 |
+
generator=generator,
|
| 52 |
+
output_type="pil",
|
| 53 |
+
).images
|
| 54 |
+
|
| 55 |
+
return output
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
examples = [
|
| 59 |
+
"A red sofa on top of a white building.",
|
| 60 |
+
"A cardboard which is large and sits on a theater stage.",
|
| 61 |
+
"A painting of an astronaut riding a pig wearing a tutu holding a pink umbrella.",
|
| 62 |
+
"Studio photograph closeup of a chameleon over a black background.",
|
| 63 |
+
"Closeup portrait photo of beautiful goth woman, makeup.",
|
| 64 |
+
"A living room, bright modern Scandinavian style house, large windows.",
|
| 65 |
+
"Portrait photograph of an anthropomorphic tortoise seated on a New York City subway train.",
|
| 66 |
+
"Batman, cute modern Disney style, Pixar 3d portrait, ultra detailed, gorgeous, 3d zbrush, trending on dribbble, 8k render.",
|
| 67 |
+
"Cinnamon bun on the plate, watercolor painting, detailed, brush strokes, light palette, light, cozy.",
|
| 68 |
+
"A lion, colorful, low-poly, cyan and orange eyes, poly-hd, 3d, low-poly game art, polygon mesh, jagged, blocky, wireframe edges, centered composition.",
|
| 69 |
+
"Long exposure photo of Tokyo street, blurred motion, streaks of light, surreal, dreamy, ghosting effect, highly detailed.",
|
| 70 |
+
"A glamorous digital magazine photoshoot, a fashionable model wearing avant-garde clothing, set in a futuristic cyberpunk roof-top environment, with a neon-lit city background, intricate high fashion details, backlit by vibrant city glow, Vogue fashion photography.",
|
| 71 |
+
"Masterpiece, best quality, girl, collarbone, wavy hair, looking at viewer, blurry foreground, upper body, necklace, contemporary, plain pants, intricate, print, pattern, ponytail, freckles, red hair, dappled sunlight, smile, happy."
|
| 72 |
+
|
| 73 |
+
]
|
| 74 |
+
|
| 75 |
+
css = '''
|
| 76 |
+
.gradio-container{max-width: 1000px !important}
|
| 77 |
+
h1{text-align:center}
|
| 78 |
+
'''
|
| 79 |
+
with gr.Blocks(css=css) as demo:
|
| 80 |
+
with gr.Row():
|
| 81 |
+
with gr.Column():
|
| 82 |
+
gr.HTML(
|
| 83 |
+
"""
|
| 84 |
+
<h1 style='text-align: center'>
|
| 85 |
+
Stable Diffusion 3 Medium with Japanese
|
| 86 |
+
</h1>
|
| 87 |
+
"""
|
| 88 |
+
)
|
| 89 |
+
gr.HTML(
|
| 90 |
+
"""
|
| 91 |
+
|
| 92 |
+
"""
|
| 93 |
+
)
|
| 94 |
+
with gr.Group():
|
| 95 |
+
with gr.Row():
|
| 96 |
+
prompt = gr.Text(
|
| 97 |
+
label="Prompt",
|
| 98 |
+
show_label=False,
|
| 99 |
+
max_lines=1,
|
| 100 |
+
placeholder="Enter your prompt",
|
| 101 |
+
container=False,
|
| 102 |
+
)
|
| 103 |
+
run_button = gr.Button("Run", scale=0)
|
| 104 |
+
result = gr.Gallery(label="Result", elem_id="gallery", show_label=False)
|
| 105 |
+
with gr.Accordion("Advanced options", open=False):
|
| 106 |
+
with gr.Row():
|
| 107 |
+
use_negative_prompt = gr.Checkbox(label="Use negative prompt", value=True)
|
| 108 |
+
negative_prompt = gr.Text(
|
| 109 |
+
label="Negative prompt",
|
| 110 |
+
max_lines=1,
|
| 111 |
+
value = "deformed, distorted, disfigured, poorly drawn, bad anatomy, wrong anatomy, extra limb, missing limb, floating limbs, mutated hands and fingers, disconnected limbs, mutation, mutated, ugly, disgusting, blurry, amputation, NSFW",
|
| 112 |
+
visible=True,
|
| 113 |
+
)
|
| 114 |
+
seed = gr.Slider(
|
| 115 |
+
label="Seed",
|
| 116 |
+
minimum=0,
|
| 117 |
+
maximum=MAX_SEED,
|
| 118 |
+
step=1,
|
| 119 |
+
value=0,
|
| 120 |
+
)
|
| 121 |
+
|
| 122 |
+
steps = gr.Slider(
|
| 123 |
+
label="Steps",
|
| 124 |
+
minimum=0,
|
| 125 |
+
maximum=60,
|
| 126 |
+
step=1,
|
| 127 |
+
value=30,
|
| 128 |
+
)
|
| 129 |
+
|
| 130 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
| 131 |
+
with gr.Row():
|
| 132 |
+
guidance_scale = gr.Slider(
|
| 133 |
+
label="Guidance Scale",
|
| 134 |
+
minimum=0.1,
|
| 135 |
+
maximum=10,
|
| 136 |
+
step=0.1,
|
| 137 |
+
value=7.0,
|
| 138 |
+
)
|
| 139 |
+
|
| 140 |
+
gr.Examples(
|
| 141 |
+
examples=examples,
|
| 142 |
+
inputs=prompt,
|
| 143 |
+
outputs=[result],
|
| 144 |
+
fn=generate,
|
| 145 |
+
cache_examples=CACHE_EXAMPLES,
|
| 146 |
+
)
|
| 147 |
+
|
| 148 |
+
use_negative_prompt.change(
|
| 149 |
+
fn=lambda x: gr.update(visible=x),
|
| 150 |
+
inputs=use_negative_prompt,
|
| 151 |
+
outputs=negative_prompt,
|
| 152 |
+
api_name=False,
|
| 153 |
+
)
|
| 154 |
+
|
| 155 |
+
gr.on(
|
| 156 |
+
triggers=[
|
| 157 |
+
prompt.submit,
|
| 158 |
+
negative_prompt.submit,
|
| 159 |
+
run_button.click,
|
| 160 |
+
],
|
| 161 |
+
fn=generate,
|
| 162 |
+
inputs=[
|
| 163 |
+
prompt,
|
| 164 |
+
negative_prompt,
|
| 165 |
+
use_negative_prompt,
|
| 166 |
+
seed,
|
| 167 |
+
guidance_scale,
|
| 168 |
+
randomize_seed,
|
| 169 |
+
steps,
|
| 170 |
+
],
|
| 171 |
+
outputs=[result],
|
| 172 |
+
api_name="run",
|
| 173 |
+
)
|
| 174 |
+
|
| 175 |
+
if __name__ == "__main__":
|
| 176 |
+
demo.queue().launch()
|