markmagic commited on
Commit
570979c
1 Parent(s): 68b55a0
Files changed (3) hide show
  1. README.md.txt +12 -0
  2. app.py +232 -3
  3. requirements.txt +4 -5
README.md.txt ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Stable Diffusion 3
3
+ emoji: 🔥
4
+ colorFrom: red
5
+ colorTo: red
6
+ sdk: gradio
7
+ sdk_version: 4.36.1
8
+ app_file: app.py
9
+ pinned: false
10
+ ---
11
+
12
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py CHANGED
@@ -1,3 +1,232 @@
1
- import os
2
-
3
- exec(os.environ.get('CODE'))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import random
3
+ import uuid
4
+
5
+ import gradio as gr
6
+ import numpy as np
7
+ from PIL import Image
8
+ import spaces
9
+ import torch
10
+ from diffusers import StableDiffusion3Pipeline, DPMSolverMultistepScheduler, AutoencoderKL
11
+ from huggingface_hub import snapshot_download
12
+
13
+ huggingface_token = os.getenv("HUGGINGFACE_TOKEN")
14
+
15
+ model_path = snapshot_download(
16
+ repo_id="stabilityai/stable-diffusion-3-medium",
17
+ revision="refs/pr/26",
18
+ repo_type="model",
19
+ ignore_patterns=["*.md", "*..gitattributes"],
20
+ local_dir="stable-diffusion-3-medium",
21
+ token=huggingface_token, # yeni bir token-id yazın.
22
+ )
23
+
24
+ DESCRIPTION = """# Stable Diffusion 3"""
25
+ if not torch.cuda.is_available():
26
+ DESCRIPTION += "\n<p>Running on CPU 🥶 This demo may not work on CPU.</p>"
27
+
28
+ MAX_SEED = np.iinfo(np.int32).max
29
+ CACHE_EXAMPLES = False
30
+ MAX_IMAGE_SIZE = int(os.getenv("MAX_IMAGE_SIZE", "1536"))
31
+ USE_TORCH_COMPILE = False
32
+ ENABLE_CPU_OFFLOAD = os.getenv("ENABLE_CPU_OFFLOAD", "0") == "1"
33
+
34
+ device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
35
+
36
+ pipe = StableDiffusion3Pipeline.from_pretrained(model_path, torch_dtype=torch.float16)
37
+
38
+
39
+ def save_image(img):
40
+ unique_name = str(uuid.uuid4()) + ".png"
41
+ img.save(unique_name)
42
+ return unique_name
43
+
44
+
45
+ def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
46
+ if randomize_seed:
47
+ seed = random.randint(0, MAX_SEED)
48
+ return seed
49
+
50
+
51
+ @spaces.GPU(enable_queue=True)
52
+ def generate(
53
+ prompt: str,
54
+ negative_prompt: str = "",
55
+ use_negative_prompt: bool = False,
56
+ seed: int = 0,
57
+ width: int = 1024,
58
+ height: int = 1024,
59
+ guidance_scale: float = 7,
60
+ randomize_seed: bool = False,
61
+ num_inference_steps=30,
62
+ NUM_IMAGES_PER_PROMPT=1,
63
+ use_resolution_binning: bool = True,
64
+ progress=gr.Progress(track_tqdm=True),
65
+ ):
66
+ pipe.to(device)
67
+ seed = int(randomize_seed_fn(seed, randomize_seed))
68
+ generator = torch.Generator().manual_seed(seed)
69
+
70
+ #pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
71
+
72
+ if not use_negative_prompt:
73
+ negative_prompt = None # type: ignore
74
+
75
+ output = pipe(
76
+ prompt=prompt,
77
+ negative_prompt=negative_prompt,
78
+ width=width,
79
+ height=height,
80
+ guidance_scale=guidance_scale,
81
+ num_inference_steps=num_inference_steps,
82
+ generator=generator,
83
+ num_images_per_prompt=NUM_IMAGES_PER_PROMPT,
84
+ output_type="pil",
85
+ ).images
86
+
87
+ return output
88
+
89
+
90
+ examples = [
91
+ "A red sofa on top of a white building.",
92
+ "A cardboard which is large and sits on a theater stage.",
93
+ "A painting of an astronaut riding a pig wearing a tutu holding a pink umbrella.",
94
+ "Studio photograph closeup of a chameleon over a black background.",
95
+ "Closeup portrait photo of beautiful goth woman, makeup.",
96
+ "A living room, bright modern Scandinavian style house, large windows.",
97
+ "Portrait photograph of an anthropomorphic tortoise seated on a New York City subway train.",
98
+ "Batman, cute modern Disney style, Pixar 3d portrait, ultra detailed, gorgeous, 3d zbrush, trending on dribbble, 8k render.",
99
+ "Cinnamon bun on the plate, watercolor painting, detailed, brush strokes, light palette, light, cozy.",
100
+ "A lion, colorful, low-poly, cyan and orange eyes, poly-hd, 3d, low-poly game art, polygon mesh, jagged, blocky, wireframe edges, centered composition.",
101
+ "Long exposure photo of Tokyo street, blurred motion, streaks of light, surreal, dreamy, ghosting effect, highly detailed.",
102
+ "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.",
103
+ "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."
104
+
105
+ ]
106
+
107
+ css = '''
108
+ .gradio-container{max-width: 1000px !important}
109
+ h1{text-align:center}
110
+ '''
111
+ with gr.Blocks(css=css) as demo:
112
+ with gr.Row():
113
+ with gr.Column():
114
+ gr.HTML(
115
+ """
116
+ <h1 style='text-align: center'>
117
+ Stable Diffusion 3 Medium
118
+ </h1>
119
+ """
120
+ )
121
+ gr.HTML(
122
+ """
123
+
124
+ """
125
+ )
126
+ with gr.Group():
127
+ with gr.Row():
128
+ prompt = gr.Text(
129
+ label="Prompt",
130
+ show_label=False,
131
+ max_lines=1,
132
+ placeholder="Enter your prompt",
133
+ container=False,
134
+ )
135
+ run_button = gr.Button("Run", scale=0)
136
+ result = gr.Gallery(label="Result", elem_id="gallery", show_label=False)
137
+ with gr.Accordion("Advanced options", open=False):
138
+ with gr.Row():
139
+ use_negative_prompt = gr.Checkbox(label="Use negative prompt", value=True)
140
+ negative_prompt = gr.Text(
141
+ label="Negative prompt",
142
+ max_lines=1,
143
+ 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",
144
+ visible=True,
145
+ )
146
+ seed = gr.Slider(
147
+ label="Seed",
148
+ minimum=0,
149
+ maximum=MAX_SEED,
150
+ step=1,
151
+ value=0,
152
+ )
153
+
154
+ steps = gr.Slider(
155
+ label="Steps",
156
+ minimum=0,
157
+ maximum=60,
158
+ step=1,
159
+ value=30,
160
+ )
161
+ number_image = gr.Slider(
162
+ label="Number of Image",
163
+ minimum=1,
164
+ maximum=4,
165
+ step=1,
166
+ value=4,
167
+ )
168
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
169
+ with gr.Row(visible=True):
170
+ width = gr.Slider(
171
+ label="Width",
172
+ minimum=256,
173
+ maximum=MAX_IMAGE_SIZE,
174
+ step=32,
175
+ value=1024,
176
+ )
177
+ height = gr.Slider(
178
+ label="Height",
179
+ minimum=256,
180
+ maximum=MAX_IMAGE_SIZE,
181
+ step=32,
182
+ value=1024,
183
+ )
184
+ with gr.Row():
185
+ guidance_scale = gr.Slider(
186
+ label="Guidance Scale",
187
+ minimum=0.1,
188
+ maximum=10,
189
+ step=0.1,
190
+ value=7.0,
191
+ )
192
+
193
+ gr.Examples(
194
+ examples=examples,
195
+ inputs=prompt,
196
+ outputs=[result],
197
+ fn=generate,
198
+ cache_examples=CACHE_EXAMPLES,
199
+ )
200
+
201
+ use_negative_prompt.change(
202
+ fn=lambda x: gr.update(visible=x),
203
+ inputs=use_negative_prompt,
204
+ outputs=negative_prompt,
205
+ api_name=False,
206
+ )
207
+
208
+ gr.on(
209
+ triggers=[
210
+ prompt.submit,
211
+ negative_prompt.submit,
212
+ run_button.click,
213
+ ],
214
+ fn=generate,
215
+ inputs=[
216
+ prompt,
217
+ negative_prompt,
218
+ use_negative_prompt,
219
+ seed,
220
+ width,
221
+ height,
222
+ guidance_scale,
223
+ randomize_seed,
224
+ steps,
225
+ number_image,
226
+ ],
227
+ outputs=[result],
228
+ api_name="run",
229
+ )
230
+
231
+ if __name__ == "__main__":
232
+ demo.queue().launch()
requirements.txt CHANGED
@@ -1,5 +1,4 @@
1
- torch
2
- diffusers
3
- transformers
4
- gradio
5
- peft
 
1
+ git+https://github.com/huggingface/diffusers.git@sd3
2
+ git+https://github.com/huggingface/transformers
3
+ sentencepiece
4
+ peft