markmagic commited on
Commit
316a61d
·
verified ·
1 Parent(s): 0f5b575

add img to img

Browse files
Files changed (1) hide show
  1. app.py +363 -232
app.py CHANGED
@@ -1,232 +1,363 @@
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=2,
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()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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, StableDiffusion3Img2ImgPipeline
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, # type a new token-id.
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
+ def load_pipeline(pipeline_type):
37
+ if pipeline_type == "text2img":
38
+ return StableDiffusion3Pipeline.from_pretrained(model_path, torch_dtype=torch.float16)
39
+ elif pipeline_type == "img2img":
40
+ return StableDiffusion3Img2ImgPipeline.from_pretrained(model_path, torch_dtype=torch.float16)
41
+
42
+ def save_image(img):
43
+ unique_name = str(uuid.uuid4()) + ".png"
44
+ img.save(unique_name)
45
+ return unique_name
46
+
47
+
48
+ def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
49
+ if randomize_seed:
50
+ seed = random.randint(0, MAX_SEED)
51
+ return seed
52
+
53
+
54
+ @spaces.GPU
55
+ def generate(
56
+ prompt:str,
57
+ negative_prompt: str = "",
58
+ use_negative_prompt: bool = False,
59
+ seed: int = 0,
60
+ width: int = 1024,
61
+ height: int = 1024,
62
+ guidance_scale: float = 7,
63
+ randomize_seed: bool = False,
64
+ num_inference_steps=30,
65
+ NUM_IMAGES_PER_PROMPT=1,
66
+ use_resolution_binning: bool = True,
67
+ progress=gr.Progress(track_tqdm=True),
68
+ ):
69
+ pipe = load_pipeline("text2img")
70
+ pipe.to(device)
71
+ seed = int(randomize_seed_fn(seed, randomize_seed))
72
+ generator = torch.Generator().manual_seed(seed)
73
+
74
+ if not use_negative_prompt:
75
+ negative_prompt = None # type: ignore
76
+
77
+ output = pipe(
78
+ prompt=prompt,
79
+ negative_prompt=negative_prompt,
80
+ width=width,
81
+ height=height,
82
+ guidance_scale=guidance_scale,
83
+ num_inference_steps=num_inference_steps,
84
+ generator=generator,
85
+ num_images_per_prompt=NUM_IMAGES_PER_PROMPT,
86
+ output_type="battery",
87
+ ).images
88
+
89
+ return output
90
+
91
+
92
+ @spaces.GPU
93
+ def img2img_generate(
94
+ prompt:str,
95
+ init_image: gr.Image,
96
+ negative_prompt: str = "",
97
+ use_negative_prompt: bool = False,
98
+ seed: int = 0,
99
+ guidance_scale: float = 7,
100
+ randomize_seed: bool = False,
101
+ num_inference_steps=30,
102
+ strength: float = 0.8,
103
+ NUM_IMAGES_PER_PROMPT=1,
104
+ use_resolution_binning: bool = True,
105
+ progress=gr.Progress(track_tqdm=True),
106
+ ):
107
+ pipe = load_pipeline("img2img")
108
+ pipe.to(device)
109
+ seed = int(randomize_seed_fn(seed, randomize_seed))
110
+ generator = torch.Generator().manual_seed(seed)
111
+
112
+ if not use_negative_prompt:
113
+ negative_prompt = None # type: ignore
114
+
115
+ init_image = init_image.resize((768, 768))
116
+
117
+ output = pipe(
118
+ prompt=prompt,
119
+ image=init_image,
120
+ negative_prompt=negative_prompt,
121
+ guidance_scale=guidance_scale,
122
+ num_inference_steps=num_inference_steps,
123
+ generator=generator,
124
+ strength=strength,
125
+ num_images_per_prompt=NUM_IMAGES_PER_PROMPT,
126
+ output_type="battery",
127
+ ).images
128
+
129
+ return output
130
+
131
+
132
+
133
+ examples = [
134
+ "A cardboard with text 'New York' which is large and sits on a theater stage.",
135
+ "A red sofa on top of a white building.",
136
+ "A painting of an astronaut riding a pig wearing a tutu holding a pink umbrella.",
137
+ "Studio photograph closeup of a chameleon over a black background.",
138
+ "Closeup portrait photo of beautiful goth woman, makeup.",
139
+ "A living room, bright modern Scandinavian style house, large windows.",
140
+ "Portrait photograph of an anthropomorphic tortoise seated on a New York City subway train.",
141
+ "Batman, cute modern Disney style, Pixar 3d portrait, ultra detailed, gorgeous, 3d zbrush, trending on dribbble, 8k render.",
142
+ "Cinnamon bun on the plate, watercolor painting, detailed, brush strokes, light palette, light, cozy.",
143
+ "A lion, colorful, low-poly, cyan and orange eyes, poly-hd, 3d, low-poly game art, polygon mesh, jagged, blocky, wireframe edges, centered composition.",
144
+ "Long exposure photo of Tokyo street, blurred motion, streaks of light, surreal, dreamy, ghosting effect, highly detailed.",
145
+ "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.",
146
+ "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."
147
+
148
+ ]
149
+
150
+ css = '''
151
+ .gradio-container{max-width: 1000px !important}
152
+ h1{text-align:center}
153
+ '''
154
+ with gr.Blocks(css=css,theme="snehilsanyal/scikit-learn") as demo:
155
+ with gr.Row():
156
+ with gr.Column():
157
+ gr.HTML(
158
+ """
159
+ <h1 style='text-align: center'>
160
+ Stable Diffusion 3 Medium
161
+ </h1>
162
+ """
163
+ )
164
+ gr.HTML(
165
+ """
166
+
167
+ """
168
+ )
169
+
170
+ with gr.Tabs():
171
+ with gr.TabItem("Text to Image"):
172
+ with gr.Group():
173
+ with gr.Row():
174
+ prompt = gr.Text(
175
+ label="Prompt",
176
+ show_label=False,
177
+ max_lines=1,
178
+ placeholder="Enter your prompt",
179
+ container=False,
180
+ )
181
+ run_button = gr.Button("Run", scale=0)
182
+ result = gr.Gallery(label="Result", elem_id="gallery", show_label=False)
183
+ with gr.Accordion("Advanced options", open=False):
184
+ with gr.Row():
185
+ use_negative_prompt = gr.Checkbox(label="Use negative prompt", value=True)
186
+ negative_prompt = gr.Text(
187
+ label="Negative prompt",
188
+ max_lines=1,
189
+ 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",
190
+ visible=True,
191
+ )
192
+ seed = gr.Slider(
193
+ label="Seed",
194
+ minimum=0,
195
+ maximum=MAX_SEED,
196
+ step=1,
197
+ value=0,
198
+ )
199
+
200
+ steps = gr.Slider(
201
+ label="Steps",
202
+ minimum=0,
203
+ maximum=60,
204
+ step=1,
205
+ value=25,
206
+ )
207
+ number_image = gr.Slider(
208
+ label="Number of Images",
209
+ minimum=1,
210
+ maximum=4,
211
+ step=1,
212
+ value=2,
213
+ )
214
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
215
+ with gr.Row(visible=True):
216
+ width = gr.Slider(
217
+ label="Width",
218
+ minimum=256,
219
+ maximum=MAX_IMAGE_SIZE,
220
+ step=32,
221
+ value=1024,
222
+ )
223
+ height = gr.Slider(
224
+ label="Height",
225
+ minimum=256,
226
+ maximum=MAX_IMAGE_SIZE,
227
+ step=32,
228
+ value=1024,
229
+ )
230
+ with gr.Row():
231
+ guidance_scale = gr.Slider(
232
+ label="Guidance Scale",
233
+ minimum=0.1,
234
+ maximum=10,
235
+ step=0.1,
236
+ value=7.0,
237
+ )
238
+
239
+ gr.Examples(
240
+ examples=examples,
241
+ inputs=prompt,
242
+ outputs=[result],
243
+ fn=generate,
244
+ cache_examples=CACHE_EXAMPLES,
245
+ )
246
+
247
+ use_negative_prompt.change(
248
+ fn=lambda x: gr.update(visible=x),
249
+ inputs=use_negative_prompt,
250
+ outputs=negative_prompt,
251
+ api_name=False,
252
+ )
253
+
254
+ gr.on(
255
+ triggers=[
256
+ prompt.submit,
257
+ negative_prompt.submit,
258
+ run_button.click,
259
+ ],
260
+ fn=generate,
261
+ inputs=[
262
+ prompt,
263
+ negative_prompt,
264
+ use_negative_prompt,
265
+ seed,
266
+ width,
267
+ height,
268
+ guidance_scale,
269
+ randomize_seed,
270
+ steps,
271
+ number_image,
272
+ ],
273
+ outputs=[result],
274
+ api_name="run",
275
+ )
276
+ with gr.TabItem("Image to Image"):
277
+ with gr.Group():
278
+ with gr.Row(equal_height=True):
279
+ with gr.Column(scale=1):
280
+ img2img_prompt = gr.Text(
281
+ label="Prompt",
282
+ show_label=False,
283
+ max_lines=1,
284
+ placeholder="Enter your prompt",
285
+ container=False,
286
+ )
287
+ init_image = gr.Image(label="Input Image", type="pil")
288
+ with gr.Row():
289
+ img2img_run_button = gr.Button("Generate", variant="primary")
290
+ with gr.Column(scale=1):
291
+ img2img_output = gr.Gallery(label="Result", elem_id="gallery")
292
+ with gr.Accordion("Advanced options", open=False):
293
+ with gr.Row():
294
+ img2img_use_negative_prompt = gr.Checkbox(label="Use negative prompt", value=True)
295
+ img2img_negative_prompt = gr.Text(
296
+ label="Negative prompt",
297
+ max_lines=1,
298
+ 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",
299
+ visible=True,
300
+ )
301
+ img2img_seed = gr.Slider(
302
+ label="Seed",
303
+ minimum=0,
304
+ maximum=MAX_SEED,
305
+ step=1,
306
+ value=0,
307
+ )
308
+ img2img_steps = gr.Slider(
309
+ label="Steps",
310
+ minimum=0,
311
+ maximum=60,
312
+ step=1,
313
+ value=25,
314
+ )
315
+ img2img_number_image = gr.Slider(
316
+ label="Number of Images",
317
+ minimum=1,
318
+ maximum=4,
319
+ step=1,
320
+ value=2,
321
+ )
322
+ img2img_randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
323
+ with gr.Row():
324
+ img2img_guidance_scale = gr.Slider(
325
+ label="Guidance Scale",
326
+ minimum=0.1,
327
+ maximum=10,
328
+ step=0.1,
329
+ value=7.0,
330
+ )
331
+ strength = gr.Slider(label="Img2Img Strength", minimum=0.0, maximum=1.0, step=0.01, value=0.8)
332
+
333
+ img2img_use_negative_prompt.change(
334
+ fn=lambda x: gr.update(visible=x),
335
+ inputs=img2img_use_negative_prompt,
336
+ outputs=img2img_negative_prompt,
337
+ api_name=False,
338
+ )
339
+
340
+ gr.on(
341
+ triggers=[
342
+ img2img_prompt.submit,
343
+ img2img_negative_prompt.submit,
344
+ img2img_run_button.click,
345
+ ],
346
+ fn=img2img_generate,
347
+ inputs=[
348
+ img2img_prompt,
349
+ init_image,
350
+ img2img_negative_prompt,
351
+ img2img_use_negative_prompt,
352
+ img2img_seed,
353
+ img2img_guidance_scale,
354
+ img2img_randomize_seed,
355
+ img2img_steps,
356
+ strength,
357
+ img2img_number_image,
358
+ ],
359
+ outputs=[img2img_output],
360
+ api_name="img2img_run",
361
+ )
362
+ if __name__ == "__main__":
363
+ demo.queue().launch(show_api=False, debug=False)