nqdior commited on
Commit
9907bf7
1 Parent(s): 6481b8a
Files changed (1) hide show
  1. app.py +236 -77
app.py CHANGED
@@ -1,7 +1,6 @@
1
  import gradio as gr
2
  import numpy as np
3
- import random
4
- import torch
5
  import requests
6
  from PIL import Image
7
  from io import BytesIO
@@ -11,44 +10,68 @@ import time
11
  MAX_SEED = np.iinfo(np.int32).max
12
  MAX_IMAGE_SIZE = 1344
13
 
14
- model_url = { "ImageUltra":"https://api.stability.ai/v2beta/stable-image/generate/ultra",
15
- "ImageCore":"https://api.stability.ai/v2beta/stable-image/generate/core",
16
- "StableDiffusion3": "https://api.stability.ai/v2beta/stable-image/generate/sd3"}
17
-
18
- service_url = { "Conservative_Upscale":"https://api.stability.ai/v2beta/stable-image/upscale/conservative",
19
- "Creative_Upscale":"https://api.stability.ai/v2beta/stable-image/upscale/creative",
20
- "Erase":"https://api.stability.ai/v2beta/stable-image/edit/erase",
21
- "Inpaint":"https://api.stability.ai/v2beta/stable-image/edit/inpaint",
22
- "Outpaint":"https://api.stability.ai/v2beta/stable-image/edit/outpaint",
23
- "SR":"https://api.stability.ai/v2beta/stable-image/edit/search-and-replace",
24
- "RMBG":"https://api.stability.ai/v2beta/stable-image/edit/remove-background",
25
- "Sketch":"https://api.stability.ai/v2beta/stable-image/control/sketch",
26
- "Structure":"https://api.stability.ai/v2beta/stable-image/control/structure"
 
 
 
27
  }
 
 
28
  def bytes_to_image(image):
29
  image = BytesIO(image)
30
- image = Image.open(image).convert('RGB')
31
  return image
32
 
 
33
  def image_to_bytes(image):
34
  byte_io = BytesIO()
35
- image.save(byte_io, format='PNG')
36
  byte_data = byte_io.getvalue()
37
  return byte_data
38
 
39
- def send_request(url, api_key, file,data):
 
40
  response = requests.post(
41
  url,
42
- headers={
43
- "Authorization": f"Bearer {api_key}",
44
- "Accept": "image/*"
45
- },
46
  files=file,
47
  data=data,
48
  )
49
  return response
50
 
51
- def generate(prompt, negative_prompt, seed, mode, submode, input_image, mask, CNstrength, search_prompt, op_left, op_right, op_up, op_down, randomize_seed, aspect, model, sd3_model, preset, api_key):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
  if randomize_seed:
53
  seed = 0
54
 
@@ -58,7 +81,7 @@ def generate(prompt, negative_prompt, seed, mode, submode, input_image, mask, CN
58
  "negative_prompt": negative_prompt,
59
  "output_format": "png",
60
  "seed": seed,
61
- "aspect_ratio": aspect
62
  }
63
  if input_image is not None:
64
  file["image"] = image_to_bytes(input_image)
@@ -67,14 +90,22 @@ def generate(prompt, negative_prompt, seed, mode, submode, input_image, mask, CN
67
 
68
  if mode == "Generate":
69
  file["none"] = ""
70
- if model == "ImageUltra":
71
- url = model_url[model]
72
- elif model == "ImageCore":
73
- url = model_url[model]
74
  data["style_preset"] = preset
75
- elif model == "StableDiffusion3":
76
- url = model_url[model]
77
- data["model"] = sd3_model
 
 
 
 
 
 
 
 
78
  else:
79
  raise ValueError("Invalid model type")
80
 
@@ -121,10 +152,7 @@ def generate(prompt, negative_prompt, seed, mode, submode, input_image, mask, CN
121
  while True:
122
  result_response = requests.get(
123
  result_url,
124
- headers={
125
- 'accept': "image/*",
126
- 'authorization': f"Bearer {api_key}"
127
- }
128
  )
129
  if result_response.status_code == 202:
130
  print("Generation in-progress, try again in 10 seconds.")
@@ -144,13 +172,15 @@ def generate(prompt, negative_prompt, seed, mode, submode, input_image, mask, CN
144
  return image, seed, copy_filed_value
145
  else:
146
  raise Exception(str(response.json()))
 
 
147
  examples = [
148
  "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
149
  "An astronaut riding a green horse",
150
  "A delicious ceviche cheesecake slice",
151
  ]
152
 
153
- css="""
154
  #col-container {
155
  margin: 0 auto;
156
  max-width: 50vw;
@@ -158,6 +188,13 @@ css="""
158
  """
159
 
160
 
 
 
 
 
 
 
 
161
  def update_mode(mode):
162
  submode_update = gr.update(choices=["None"], visible=False)
163
  image_label_update = gr.update(visible=False)
@@ -167,20 +204,35 @@ def update_mode(mode):
167
  if mode == "Generate":
168
  submode_update = gr.update(visible=False)
169
  elif mode == "Upscale":
170
- submode_update = gr.update(choices=["Conservative", "Creative"],value="Conservative" ,visible=True)
 
 
171
  img_input_update = gr.update(visible=True)
172
  image_label_update = gr.update(visible=True)
173
  elif mode == "Edit":
174
- submode_update = gr.update(choices=["Erase", "Inpaint", "Outpaint", "Search and Replace", "Remove Background"],value="Erase", visible=True)
 
 
 
 
 
 
 
 
 
 
175
  img_input_update = gr.update(visible=True)
176
  image_label_update = gr.update(visible=True)
177
  elif mode == "Control":
178
- submode_update = gr.update(choices=["Sketch", "Structure"],value="Sketch", visible=True)
 
 
179
  img_input_update = gr.update(visible=True)
180
  image_label_update = gr.update(visible=True)
181
 
182
  return submode_update, img_input_update, mask_update, image_label_update
183
 
 
184
  def update_submode(submode):
185
  mask = gr.update(visible=False)
186
  outpaint = gr.update(visible=False)
@@ -194,7 +246,6 @@ def update_submode(submode):
194
  if submode == "Outpaint":
195
  outpaint = gr.update(visible=True)
196
 
197
-
198
  elif submode == "Control":
199
  cn = gr.update(visible=True)
200
 
@@ -204,22 +255,39 @@ def update_submode(submode):
204
  return mask, outpaint, cn, search_prompt
205
 
206
 
207
-
208
- with gr.Blocks(css=css) as demo:
209
  with gr.Column(elem_id="col-container"):
210
- gr.Markdown(f"""
 
211
  # Demo Stable Image API
212
  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)
213
- """)
 
214
 
215
  with gr.Row():
216
- api_key = gr.Text(label="API Key", type="password", placeholder="Enter your API key", max_lines=1, container=False)
 
 
 
 
 
 
217
 
218
  with gr.Row():
219
- model = gr.Dropdown(label="Model", choices=["ImageUltra", "ImageCore", "StableDiffusion3"], value="ImageUltra")
220
- mode = gr.Dropdown(label="Mode", choices=["Generate", "Upscale", "Edit", "Control"], value="Generate")
 
 
 
 
 
 
 
 
221
 
222
- submode = gr.Dropdown(label="Submode", choices=["None"], visible=False, value="None")
 
 
223
 
224
  with gr.Row():
225
  with gr.Column():
@@ -242,11 +310,28 @@ with gr.Blocks(css=css) as demo:
242
 
243
  with gr.Row():
244
  with gr.Column():
245
- image_label = gr.Markdown(value = "input image",visible=False)
246
- image = gr.Image(type='pil',label="img input", width="20vw", height="20vw",show_label=True,visible=False, interactive=True, container=False)
 
 
 
 
 
 
 
 
 
247
  with gr.Column(visible=False) as mask:
248
  mask_label = gr.Markdown(value="input mask")
249
- mask_input = gr.Image(type='pil',label="mask", width="20vw", height="20vw", show_label=True, interactive=True, container=False)
 
 
 
 
 
 
 
 
250
 
251
  with gr.Row():
252
  result = gr.Image(label="Result", width="20vw", height="20%")
@@ -264,39 +349,113 @@ with gr.Blocks(css=css) as demo:
264
  step=1,
265
  value=0,
266
  )
267
- CN_strength = gr.Slider(label="Control Strength", minimum=0, maximum=1, step=0.01, value=0.5, visible=False)
 
 
 
 
 
 
 
268
  randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
269
- with gr.Row():
270
- aspect = gr.Radio(choices=["1:1", "16:9", "21:9", "2:3", "3:2", "4:5", "5:4", "9:16", "9:21"], label="Aspect raito", value="1:1")
271
- style_preset = gr.Radio(choices=["3d-model", "analog-film", "anime", "cinematic", "comic-book", "digital-art", "enhance", "fantasy-art", "isometric", "line-art", "low-poly", "modeling-compound", "neon-punk", "origami", "photographic", "pixel-art", "tile-texture"], label="Style_preset", value="anime", info="This parameter is only available for ImageCore model.")
272
- sd3_model = gr.Dropdown(label="SD3 Model Size", choices=["sd3-medium", "sd3-large", "sd3-large-turbo"], value="sd3-medium")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
273
  with gr.Row(visible=False) as outpaint_scale:
274
- paint = gr.Markdown(value = "Outpain Scale")
275
- op_left = gr.Slider(label="left", minimum=0, maximum=2000, step=4, value=200)
276
- op_right = gr.Slider(label="right", minimum=0, maximum=2000, step=4, value=200)
277
- op_up = gr.Slider(label="up", minimum=0, maximum=2000, step=4, value=200)
278
- op_down = gr.Slider(label="down", minimum=0, maximum=2000, step=4, value=200)
279
- gr.Examples(
280
- examples=examples,
281
- inputs=[prompt]
282
- )
 
 
 
 
 
283
 
284
  copy_filed = gr.TextArea(
285
- value="",
286
- label="Copy Field",
287
- max_lines=1,
288
- placeholder="Copy the field",
289
- show_copy_button=True,
290
- container=False)
 
291
 
292
  gr.on(
293
  triggers=[run_button.click, prompt.submit, negative_prompt.submit],
294
  fn=generate,
295
- inputs=[prompt, negative_prompt, seed, mode, submode, image, mask_input, CN_strength, search_prompt, op_left, op_right, op_up, op_down, randomize_seed, aspect, model, sd3_model, style_preset, api_key],
296
- outputs=[result, seed, copy_filed]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
297
  )
298
 
299
- mode.change(fn=update_mode, inputs=mode, outputs=[submode, image, mask, image_label])
300
- submode.change(fn=update_submode, inputs=submode, outputs=[mask,outpaint_scale,CN_strength,search_prompt])
301
-
302
- demo.launch()
 
 
 
 
 
 
 
1
  import gradio as gr
2
  import numpy as np
3
+
 
4
  import requests
5
  from PIL import Image
6
  from io import BytesIO
 
10
  MAX_SEED = np.iinfo(np.int32).max
11
  MAX_IMAGE_SIZE = 1344
12
 
13
+ model_url = {
14
+ "ImageUltra": "https://api.stability.ai/v2beta/stable-image/generate/ultra",
15
+ "ImageCore": "https://api.stability.ai/v2beta/stable-image/generate/core",
16
+ "StableDiffusion3": "https://api.stability.ai/v2beta/stable-image/generate/sd3",
17
+ }
18
+
19
+ service_url = {
20
+ "Conservative_Upscale": "https://api.stability.ai/v2beta/stable-image/upscale/conservative",
21
+ "Creative_Upscale": "https://api.stability.ai/v2beta/stable-image/upscale/creative",
22
+ "Erase": "https://api.stability.ai/v2beta/stable-image/edit/erase",
23
+ "Inpaint": "https://api.stability.ai/v2beta/stable-image/edit/inpaint",
24
+ "Outpaint": "https://api.stability.ai/v2beta/stable-image/edit/outpaint",
25
+ "SR": "https://api.stability.ai/v2beta/stable-image/edit/search-and-replace",
26
+ "RMBG": "https://api.stability.ai/v2beta/stable-image/edit/remove-background",
27
+ "Sketch": "https://api.stability.ai/v2beta/stable-image/control/sketch",
28
+ "Structure": "https://api.stability.ai/v2beta/stable-image/control/structure",
29
  }
30
+
31
+
32
  def bytes_to_image(image):
33
  image = BytesIO(image)
34
+ image = Image.open(image).convert("RGB")
35
  return image
36
 
37
+
38
  def image_to_bytes(image):
39
  byte_io = BytesIO()
40
+ image.save(byte_io, format="PNG")
41
  byte_data = byte_io.getvalue()
42
  return byte_data
43
 
44
+
45
+ def send_request(url, api_key, file, data):
46
  response = requests.post(
47
  url,
48
+ headers={"Authorization": f"Bearer {api_key}", "Accept": "image/*"},
 
 
 
49
  files=file,
50
  data=data,
51
  )
52
  return response
53
 
54
+
55
+ def generate(
56
+ prompt,
57
+ negative_prompt,
58
+ seed,
59
+ mode,
60
+ submode,
61
+ input_image,
62
+ mask,
63
+ CNstrength,
64
+ search_prompt,
65
+ op_left,
66
+ op_right,
67
+ op_up,
68
+ op_down,
69
+ randomize_seed,
70
+ aspect,
71
+ model,
72
+ preset,
73
+ api_key,
74
+ ):
75
  if randomize_seed:
76
  seed = 0
77
 
 
81
  "negative_prompt": negative_prompt,
82
  "output_format": "png",
83
  "seed": seed,
84
+ "aspect_ratio": aspect,
85
  }
86
  if input_image is not None:
87
  file["image"] = image_to_bytes(input_image)
 
90
 
91
  if mode == "Generate":
92
  file["none"] = ""
93
+ if model == "Ultra":
94
+ url = model_url["ImageUltra"]
95
+ elif model == "Core":
96
+ url = model_url["ImageCore"]
97
  data["style_preset"] = preset
98
+
99
+ elif model == "sd3-medium":
100
+ url = model_url["StableDiffusion3"]
101
+ data["model"] = model
102
+ elif model == "sd3-large":
103
+ url = model_url["StableDiffusion3"]
104
+ data["model"] = model
105
+ elif model == "sd3-large-turbo":
106
+ url = model_url["StableDiffusion3"]
107
+ data["model"] = model
108
+
109
  else:
110
  raise ValueError("Invalid model type")
111
 
 
152
  while True:
153
  result_response = requests.get(
154
  result_url,
155
+ headers={"accept": "image/*", "authorization": f"Bearer {api_key}"},
 
 
 
156
  )
157
  if result_response.status_code == 202:
158
  print("Generation in-progress, try again in 10 seconds.")
 
172
  return image, seed, copy_filed_value
173
  else:
174
  raise Exception(str(response.json()))
175
+
176
+
177
  examples = [
178
  "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
179
  "An astronaut riding a green horse",
180
  "A delicious ceviche cheesecake slice",
181
  ]
182
 
183
+ css = """
184
  #col-container {
185
  margin: 0 auto;
186
  max-width: 50vw;
 
188
  """
189
 
190
 
191
+ def update_style_visibility(model):
192
+ if model == "Core":
193
+ return gr.update(visible=True)
194
+ else:
195
+ return gr.update(visible=False)
196
+
197
+
198
  def update_mode(mode):
199
  submode_update = gr.update(choices=["None"], visible=False)
200
  image_label_update = gr.update(visible=False)
 
204
  if mode == "Generate":
205
  submode_update = gr.update(visible=False)
206
  elif mode == "Upscale":
207
+ submode_update = gr.update(
208
+ choices=["Conservative", "Creative"], value="Conservative", visible=True
209
+ )
210
  img_input_update = gr.update(visible=True)
211
  image_label_update = gr.update(visible=True)
212
  elif mode == "Edit":
213
+ submode_update = gr.update(
214
+ choices=[
215
+ "Erase",
216
+ "Inpaint",
217
+ "Outpaint",
218
+ "Search and Replace",
219
+ "Remove Background",
220
+ ],
221
+ value="Erase",
222
+ visible=True,
223
+ )
224
  img_input_update = gr.update(visible=True)
225
  image_label_update = gr.update(visible=True)
226
  elif mode == "Control":
227
+ submode_update = gr.update(
228
+ choices=["Sketch", "Structure"], value="Sketch", visible=True
229
+ )
230
  img_input_update = gr.update(visible=True)
231
  image_label_update = gr.update(visible=True)
232
 
233
  return submode_update, img_input_update, mask_update, image_label_update
234
 
235
+
236
  def update_submode(submode):
237
  mask = gr.update(visible=False)
238
  outpaint = gr.update(visible=False)
 
246
  if submode == "Outpaint":
247
  outpaint = gr.update(visible=True)
248
 
 
249
  elif submode == "Control":
250
  cn = gr.update(visible=True)
251
 
 
255
  return mask, outpaint, cn, search_prompt
256
 
257
 
258
+ with gr.Blocks(css=css, theme="NoCrypt/miku") as demo:
 
259
  with gr.Column(elem_id="col-container"):
260
+ gr.Markdown(
261
+ f"""
262
  # Demo Stable Image API
263
  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)
264
+ """
265
+ )
266
 
267
  with gr.Row():
268
+ api_key = gr.Text(
269
+ label="API Key",
270
+ type="password",
271
+ placeholder="Enter your API key",
272
+ max_lines=1,
273
+ container=False,
274
+ )
275
 
276
  with gr.Row():
277
+ model = gr.Dropdown(
278
+ label="Model",
279
+ choices=["Ultra", "Core", "sd3-medium", "sd3-large", "sd3-large-turbo"],
280
+ value="Ultra",
281
+ )
282
+ mode = gr.Dropdown(
283
+ label="Mode",
284
+ choices=["Generate", "Upscale", "Edit", "Control"],
285
+ value="Generate",
286
+ )
287
 
288
+ submode = gr.Dropdown(
289
+ label="Submode", choices=["None"], visible=False, value="None"
290
+ )
291
 
292
  with gr.Row():
293
  with gr.Column():
 
310
 
311
  with gr.Row():
312
  with gr.Column():
313
+ image_label = gr.Markdown(value="input image", visible=False)
314
+ image = gr.Image(
315
+ type="pil",
316
+ label="img input",
317
+ width="20vw",
318
+ height="20vw",
319
+ show_label=True,
320
+ visible=False,
321
+ interactive=True,
322
+ container=False,
323
+ )
324
  with gr.Column(visible=False) as mask:
325
  mask_label = gr.Markdown(value="input mask")
326
+ mask_input = gr.Image(
327
+ type="pil",
328
+ label="mask",
329
+ width="20vw",
330
+ height="20vw",
331
+ show_label=True,
332
+ interactive=True,
333
+ container=False,
334
+ )
335
 
336
  with gr.Row():
337
  result = gr.Image(label="Result", width="20vw", height="20%")
 
349
  step=1,
350
  value=0,
351
  )
352
+ CN_strength = gr.Slider(
353
+ label="Control Strength",
354
+ minimum=0,
355
+ maximum=1,
356
+ step=0.01,
357
+ value=0.5,
358
+ visible=False,
359
+ )
360
  randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
361
+ aspect = gr.Radio(
362
+ choices=[
363
+ "1:1",
364
+ "16:9",
365
+ "21:9",
366
+ "2:3",
367
+ "3:2",
368
+ "4:5",
369
+ "5:4",
370
+ "9:16",
371
+ "9:21",
372
+ ],
373
+ label="Aspect raito",
374
+ value="1:1",
375
+ )
376
+ with gr.Row(visible=False) as style:
377
+ style_preset = gr.Radio(
378
+ choices=[
379
+ "3d-model",
380
+ "analog-film",
381
+ "anime",
382
+ "cinematic",
383
+ "comic-book",
384
+ "digital-art",
385
+ "enhance",
386
+ "fantasy-art",
387
+ "isometric",
388
+ "line-art",
389
+ "low-poly",
390
+ "modeling-compound",
391
+ "neon-punk",
392
+ "origami",
393
+ "photographic",
394
+ "pixel-art",
395
+ "tile-texture",
396
+ ],
397
+ label="Style_preset",
398
+ value="anime",
399
+ info="This parameter is only available for ImageCore model.",
400
+ )
401
  with gr.Row(visible=False) as outpaint_scale:
402
+ paint = gr.Markdown(value="Outpain Scale")
403
+ op_left = gr.Slider(
404
+ label="left", minimum=0, maximum=2000, step=4, value=200
405
+ )
406
+ op_right = gr.Slider(
407
+ label="right", minimum=0, maximum=2000, step=4, value=200
408
+ )
409
+ op_up = gr.Slider(
410
+ label="up", minimum=0, maximum=2000, step=4, value=200
411
+ )
412
+ op_down = gr.Slider(
413
+ label="down", minimum=0, maximum=2000, step=4, value=200
414
+ )
415
+ gr.Examples(examples=examples, inputs=[prompt])
416
 
417
  copy_filed = gr.TextArea(
418
+ value="",
419
+ label="Copy Field",
420
+ max_lines=1,
421
+ placeholder="Copy the field",
422
+ show_copy_button=True,
423
+ container=False,
424
+ )
425
 
426
  gr.on(
427
  triggers=[run_button.click, prompt.submit, negative_prompt.submit],
428
  fn=generate,
429
+ inputs=[
430
+ prompt,
431
+ negative_prompt,
432
+ seed,
433
+ mode,
434
+ submode,
435
+ image,
436
+ mask_input,
437
+ CN_strength,
438
+ search_prompt,
439
+ op_left,
440
+ op_right,
441
+ op_up,
442
+ op_down,
443
+ randomize_seed,
444
+ aspect,
445
+ model,
446
+ style_preset,
447
+ api_key,
448
+ ],
449
+ outputs=[result, seed, copy_filed],
450
  )
451
 
452
+ mode.change(
453
+ fn=update_mode, inputs=mode, outputs=[submode, image, mask, image_label]
454
+ )
455
+ submode.change(
456
+ fn=update_submode,
457
+ inputs=submode,
458
+ outputs=[mask, outpaint_scale, CN_strength, search_prompt],
459
+ )
460
+ model.change(fn=update_style_visibility, inputs=model, outputs=style)
461
+ demo.launch()