nqdior commited on
Commit
6481b8a
1 Parent(s): af91b11

init commit

Browse files
Files changed (2) hide show
  1. app.py +251 -95
  2. requirements.txt +4 -1
app.py CHANGED
@@ -1,42 +1,149 @@
1
  import gradio as gr
2
  import numpy as np
3
  import random
4
- from diffusers import DiffusionPipeline
5
  import torch
 
 
 
 
6
 
7
- device = "cuda" if torch.cuda.is_available() else "cpu"
 
 
8
 
9
- if torch.cuda.is_available():
10
- torch.cuda.max_memory_allocated(device=device)
11
- pipe = DiffusionPipeline.from_pretrained("stabilityai/sdxl-turbo", torch_dtype=torch.float16, variant="fp16", use_safetensors=True)
12
- pipe.enable_xformers_memory_efficient_attention()
13
- pipe = pipe.to(device)
14
- else:
15
- pipe = DiffusionPipeline.from_pretrained("stabilityai/sdxl-turbo", use_safetensors=True)
16
- pipe = pipe.to(device)
17
 
18
- MAX_SEED = np.iinfo(np.int32).max
19
- MAX_IMAGE_SIZE = 1024
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
- def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps):
 
 
 
 
22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  if randomize_seed:
24
- seed = random.randint(0, MAX_SEED)
25
-
26
- generator = torch.Generator().manual_seed(seed)
27
-
28
- image = pipe(
29
- prompt = prompt,
30
- negative_prompt = negative_prompt,
31
- guidance_scale = guidance_scale,
32
- num_inference_steps = num_inference_steps,
33
- width = width,
34
- height = height,
35
- generator = generator
36
- ).images[0]
37
-
38
- return image
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
  examples = [
41
  "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
42
  "An astronaut riding a green horse",
@@ -46,46 +153,110 @@ examples = [
46
  css="""
47
  #col-container {
48
  margin: 0 auto;
49
- max-width: 520px;
50
  }
51
  """
52
 
53
- if torch.cuda.is_available():
54
- power_device = "GPU"
55
- else:
56
- power_device = "CPU"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
 
58
  with gr.Blocks(css=css) as demo:
59
-
60
  with gr.Column(elem_id="col-container"):
61
  gr.Markdown(f"""
62
- # Text-to-Image Gradio Template
63
- Currently running on {power_device}.
64
  """)
65
-
66
  with gr.Row():
67
-
68
- prompt = gr.Text(
69
- label="Prompt",
70
- show_label=False,
71
- max_lines=1,
72
- placeholder="Enter your prompt",
73
- container=False,
74
- )
75
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76
  run_button = gr.Button("Run", scale=0)
77
-
78
- result = gr.Image(label="Result", show_label=False)
 
 
 
 
 
 
 
 
 
79
 
80
  with gr.Accordion("Advanced Settings", open=False):
81
-
82
  negative_prompt = gr.Text(
83
  label="Negative prompt",
84
  max_lines=1,
85
  placeholder="Enter a negative prompt",
86
- visible=False,
87
  )
88
-
89
  seed = gr.Slider(
90
  label="Seed",
91
  minimum=0,
@@ -93,54 +264,39 @@ with gr.Blocks(css=css) as demo:
93
  step=1,
94
  value=0,
95
  )
96
-
97
  randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
98
-
99
- with gr.Row():
100
-
101
- width = gr.Slider(
102
- label="Width",
103
- minimum=256,
104
- maximum=MAX_IMAGE_SIZE,
105
- step=32,
106
- value=512,
107
- )
108
-
109
- height = gr.Slider(
110
- label="Height",
111
- minimum=256,
112
- maximum=MAX_IMAGE_SIZE,
113
- step=32,
114
- value=512,
115
- )
116
-
117
  with gr.Row():
118
-
119
- guidance_scale = gr.Slider(
120
- label="Guidance scale",
121
- minimum=0.0,
122
- maximum=10.0,
123
- step=0.1,
124
- value=0.0,
125
- )
126
-
127
- num_inference_steps = gr.Slider(
128
- label="Number of inference steps",
129
- minimum=1,
130
- maximum=12,
131
- step=1,
132
- value=2,
133
- )
134
-
135
  gr.Examples(
136
- examples = examples,
137
- inputs = [prompt]
138
  )
139
 
140
- run_button.click(
141
- fn = infer,
142
- inputs = [prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
143
- outputs = [result]
 
 
 
 
 
 
 
 
 
144
  )
145
 
146
- demo.queue().launch()
 
 
 
 
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
8
+ import time
9
 
10
+ # TODO: PNGinfo追加, 使用データの展開(コピーボタン)
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
+
55
+ file = {}
56
+ data = {
57
+ "prompt": prompt,
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)
65
+ if mask is not None:
66
+ file["mask"] = image_to_bytes(mask)
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
+
81
+ elif mode == "Upscale":
82
+ if submode == "Conservative":
83
+ url = service_url["Conservative_Upscale"]
84
+ elif submode == "Creative":
85
+ url = service_url["Creative_Upscale"]
86
+
87
+ elif mode == "Edit":
88
+ if submode == "Erase":
89
+ url = service_url["Erase"]
90
+ elif submode == "Inpaint":
91
+ url = service_url["Inpaint"]
92
+ elif submode == "Outpaint":
93
+ url = service_url["Outpaint"]
94
+ data["left"] = op_left
95
+ data["right"] = op_right
96
+ data["up"] = op_up
97
+ data["down"] = op_down
98
+ elif submode == "Search and Replace":
99
+ url = service_url["SR"]
100
+ data["search_prompt"] = search_prompt
101
+ elif submode == "Remove Background":
102
+ url = service_url["RMBG"]
103
+
104
+ elif mode == "Control":
105
+ data["control_strength"] = CNstrength
106
+ if submode == "Sketch":
107
+ url = service_url["Sketch"]
108
+ elif submode == "Structure":
109
+ url = service_url["Structure"]
110
+
111
+ response = send_request(url, api_key, file, data)
112
 
113
+ if response.status_code == 200:
114
+ if mode == "Upscale" and submode == "Creative":
115
+ generation_id = response.json().get("id")
116
+ if not generation_id:
117
+ raise Exception("No generation ID returned for creative upscale")
118
+
119
+ # Polling for the result
120
+ result_url = f"https://api.stability.ai/v2beta/stable-image/upscale/creative/result/{generation_id}"
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.")
131
+ time.sleep(10)
132
+ elif result_response.status_code == 200:
133
+ print("Generation complete!")
134
+ image = result_response.content
135
+ image = bytes_to_image(image)
136
+ copy_filed_value = f"prompt:{prompt}, negative:{negative_prompt}, mode:{mode}, submode:{submode}"
137
+ return image, seed, copy_filed_value
138
+ else:
139
+ raise Exception(str(result_response.json()))
140
+ else:
141
+ image = response.content
142
+ image = bytes_to_image(image)
143
+ copy_filed_value = f"prompt:{prompt}, negative:{negative_prompt}, mode:{mode}, submode:{submode}"
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",
 
153
  css="""
154
  #col-container {
155
  margin: 0 auto;
156
+ max-width: 50vw;
157
  }
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)
164
+ img_input_update = gr.update(visible=False)
165
+ mask_update = gr.update(visible=False)
166
+
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)
187
+ cn = gr.update(visible=False)
188
+ search_prompt = gr.update(visible=False)
189
+
190
+ if submode in ["Erase", "Inpaint"]:
191
+ mask = gr.update(visible=True)
192
+
193
+ else:
194
+ if submode == "Outpaint":
195
+ outpaint = gr.update(visible=True)
196
+
197
+
198
+ elif submode == "Control":
199
+ cn = gr.update(visible=True)
200
+
201
+ elif submode == "Search and Replace":
202
+ search_prompt = gr.update(visible=True)
203
+
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():
226
+ prompt = gr.Text(
227
+ label="Prompt",
228
+ show_label=False,
229
+ max_lines=1,
230
+ placeholder="Enter your prompt",
231
+ container=False,
232
+ )
233
+ search_prompt = gr.Text(
234
+ label="search prompt",
235
+ visible=False,
236
+ show_label=False,
237
+ max_lines=1,
238
+ placeholder="Enter a search prompt",
239
+ )
240
+
241
  run_button = gr.Button("Run", scale=0)
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%")
253
 
254
  with gr.Accordion("Advanced Settings", open=False):
 
255
  negative_prompt = gr.Text(
256
  label="Negative prompt",
257
  max_lines=1,
258
  placeholder="Enter a negative prompt",
 
259
  )
 
260
  seed = gr.Slider(
261
  label="Seed",
262
  minimum=0,
 
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()
requirements.txt CHANGED
@@ -3,4 +3,7 @@ diffusers
3
  invisible_watermark
4
  torch
5
  transformers
6
- xformers
 
 
 
 
3
  invisible_watermark
4
  torch
5
  transformers
6
+ xformers
7
+ numpy==2.0.0
8
+ Pillow==10.3.0
9
+ Requests==2.32.3