Spaces:
Sleeping
Sleeping
Commit
·
914e82c
1
Parent(s):
91b245b
upload app.py and requirements
Browse files- app.py +137 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
import random
|
| 4 |
+
import torch
|
| 5 |
+
from diffusers import StableDiffusion3Pipeline, SD3Transformer2DModel, FlowMatchEulerDiscreteScheduler
|
| 6 |
+
import spaces
|
| 7 |
+
|
| 8 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 9 |
+
dtype = torch.float16
|
| 10 |
+
|
| 11 |
+
model_file = "https://huggingface.co/silveroxides/sd3-safetensors/blob/main/sd3_medium_XYembmod_incl_creativeclips.safetensors"
|
| 12 |
+
pipe = StableDiffusion3Pipeline.from_single_file(model_file, torch_dtype=torch.float16, text_encoder_3=None).to(device)
|
| 13 |
+
|
| 14 |
+
MAX_SEED = np.iinfo(np.int32).max
|
| 15 |
+
MAX_IMAGE_SIZE = 1536
|
| 16 |
+
|
| 17 |
+
@spaces.GPU
|
| 18 |
+
def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps, progress=gr.Progress(track_tqdm=True)):
|
| 19 |
+
|
| 20 |
+
if randomize_seed:
|
| 21 |
+
seed = random.randint(0, MAX_SEED)
|
| 22 |
+
|
| 23 |
+
generator = torch.Generator().manual_seed(seed)
|
| 24 |
+
|
| 25 |
+
image = pipe(
|
| 26 |
+
prompt = prompt,
|
| 27 |
+
negative_prompt = negative_prompt,
|
| 28 |
+
guidance_scale = guidance_scale,
|
| 29 |
+
num_inference_steps = num_inference_steps,
|
| 30 |
+
width = width,
|
| 31 |
+
height = height,
|
| 32 |
+
generator = generator
|
| 33 |
+
).images[0]
|
| 34 |
+
|
| 35 |
+
return image, seed
|
| 36 |
+
|
| 37 |
+
examples = [
|
| 38 |
+
"Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
|
| 39 |
+
"An astronaut riding a green horse",
|
| 40 |
+
"A delicious ceviche cheesecake slice",
|
| 41 |
+
]
|
| 42 |
+
|
| 43 |
+
css="""
|
| 44 |
+
#col-container {
|
| 45 |
+
margin: 0 auto;
|
| 46 |
+
max-width: 580px;
|
| 47 |
+
}
|
| 48 |
+
"""
|
| 49 |
+
|
| 50 |
+
with gr.Blocks(css=css) as demo:
|
| 51 |
+
|
| 52 |
+
with gr.Column(elem_id="col-container"):
|
| 53 |
+
gr.Markdown(f"""
|
| 54 |
+
# Demo [Stable Diffusion 3 Medium](https://huggingface.co/silveroxides/SD3-custom-clip)
|
| 55 |
+
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)
|
| 56 |
+
""")
|
| 57 |
+
|
| 58 |
+
with gr.Row():
|
| 59 |
+
|
| 60 |
+
prompt = gr.Text(
|
| 61 |
+
label="Prompt",
|
| 62 |
+
show_label=False,
|
| 63 |
+
max_lines=1,
|
| 64 |
+
placeholder="Enter your prompt",
|
| 65 |
+
container=False,
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
run_button = gr.Button("Run", scale=0)
|
| 69 |
+
|
| 70 |
+
result = gr.Image(label="Result", show_label=False)
|
| 71 |
+
|
| 72 |
+
with gr.Accordion("Advanced Settings", open=False):
|
| 73 |
+
|
| 74 |
+
negative_prompt = gr.Text(
|
| 75 |
+
label="Negative prompt",
|
| 76 |
+
max_lines=1,
|
| 77 |
+
placeholder="Enter a negative prompt",
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
seed = gr.Slider(
|
| 81 |
+
label="Seed",
|
| 82 |
+
minimum=0,
|
| 83 |
+
maximum=MAX_SEED,
|
| 84 |
+
step=1,
|
| 85 |
+
value=0,
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
| 89 |
+
|
| 90 |
+
with gr.Row():
|
| 91 |
+
|
| 92 |
+
width = gr.Slider(
|
| 93 |
+
label="Width",
|
| 94 |
+
minimum=256,
|
| 95 |
+
maximum=MAX_IMAGE_SIZE,
|
| 96 |
+
step=8,
|
| 97 |
+
value=1024,
|
| 98 |
+
)
|
| 99 |
+
|
| 100 |
+
height = gr.Slider(
|
| 101 |
+
label="Height",
|
| 102 |
+
minimum=256,
|
| 103 |
+
maximum=MAX_IMAGE_SIZE,
|
| 104 |
+
step=8,
|
| 105 |
+
value=1024,
|
| 106 |
+
)
|
| 107 |
+
|
| 108 |
+
with gr.Row():
|
| 109 |
+
|
| 110 |
+
guidance_scale = gr.Slider(
|
| 111 |
+
label="Guidance scale",
|
| 112 |
+
minimum=0.0,
|
| 113 |
+
maximum=10.0,
|
| 114 |
+
step=0.1,
|
| 115 |
+
value=5.0,
|
| 116 |
+
)
|
| 117 |
+
|
| 118 |
+
num_inference_steps = gr.Slider(
|
| 119 |
+
label="Number of inference steps",
|
| 120 |
+
minimum=1,
|
| 121 |
+
maximum=70,
|
| 122 |
+
step=1,
|
| 123 |
+
value=28,
|
| 124 |
+
)
|
| 125 |
+
|
| 126 |
+
gr.Examples(
|
| 127 |
+
examples = examples,
|
| 128 |
+
inputs = [prompt]
|
| 129 |
+
)
|
| 130 |
+
gr.on(
|
| 131 |
+
triggers=[run_button.click, prompt.submit, negative_prompt.submit],
|
| 132 |
+
fn = infer,
|
| 133 |
+
inputs = [prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
|
| 134 |
+
outputs = [result, seed]
|
| 135 |
+
)
|
| 136 |
+
|
| 137 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
diffusers==0.29.1
|
| 2 |
+
transformers
|
| 3 |
+
accelerate
|
| 4 |
+
sentencepiece
|