akhaliq HF Staff commited on
Commit
c46d45c
·
verified ·
1 Parent(s): 5e5cf8d

gr.Server studio + gr.Workflow app (backend unchanged)

Browse files
Files changed (1) hide show
  1. app.py +221 -76
app.py CHANGED
@@ -1,16 +1,24 @@
1
- """P-H3-Image on ZeroGPU (`xlarge`: a full RTX PRO 6000 Blackwell, 96 GB).
2
 
3
- The pipeline is loaded once at startup, one component at a time (the host has ~97 GB), and slimmed to fit the slice
4
- with p_h3_image_fast.py (kernels only, no torch.compile): AdaLN tables instead of the transformer's AdaLN projections,
5
- FP8 linears everywhere, fused Triton DiT blocks, batched VAE tiles, and for the prompt upsampler a weight-only FP8 GEMV,
6
- decode attention and speculative decoding with its MTP head in one CUDA graph per step.
 
 
 
 
 
7
  """
8
 
 
9
  import json
10
  import os
11
  import random
 
12
  import tempfile
13
  import time
 
14
 
15
  os.environ.setdefault("DIFFUSERS_TRUST_REMOTE_KERNELS", "true")
16
  os.environ.setdefault("PYTORCH_CUDA_ALLOC_CONF", "expandable_segments:True")
@@ -18,19 +26,23 @@ os.environ.setdefault("PYTORCH_CUDA_ALLOC_CONF", "expandable_segments:True")
18
  import gradio as gr
19
  import spaces
20
  import torch
 
 
 
 
21
  from huggingface_hub import snapshot_download
22
  from PIL import Image, ImageOps
23
  from torchvision.io import encode_jpeg
24
 
25
  import p_h3_image_fast as fast
26
  from diffusers import ModularPipeline
27
- from examples import EXAMPLES
28
- from theme import CSS, HEAD, THEME, render_footer, render_header
29
 
30
  REPO, REVISION = "PrunaAI/P-H3-Image", "ee4a685de1fbd4337a50bcf9b3c407e4f8463d49"
 
31
  # Canvases (width, height), multiples of 32: 4 and 8 MP as tested, 16 MP from the same ratios.
32
  RATIOS = {"1:1": 1.0, "4:3": 4 / 3, "3:4": 3 / 4, "3:2": 3 / 2, "2:3": 2 / 3, "16:9": 16 / 9, "9:16": 9 / 16}
33
  T2I_RESOLUTIONS, EDIT_RESOLUTIONS = ["4 MP", "8 MP", "16 MP"], ["4 MP", "8 MP"]
 
34
 
35
 
36
  def _canvas(ratio: float, megapixels: int) -> tuple[int, int]:
@@ -52,7 +64,7 @@ REFERENCE_BUDGET = 8 * 1024**2
52
 
53
  def load_pipeline():
54
  path = snapshot_download(REPO, revision=REVISION, max_workers=16)
55
- with open(os.path.join(os.path.dirname(__file__), "adaln_calls.json")) as handle:
56
  calls = json.load(handle)
57
  pipe, mtp = fast.load_slim_pipeline(path, calls, log=lambda message: print(message, flush=True))
58
  # The same components without the prompt upsampler step, for requests that skip upsampling.
@@ -166,17 +178,6 @@ def _info(timing, size, seed):
166
  return f"{head} ({upsample:.1f} s prompt upsampling{rate}, {timing['total'] - upsample:.1f} s rendering)"
167
 
168
 
169
- def text_to_image(prompt, aspect, resolution, upsample):
170
- if not prompt or not prompt.strip():
171
- raise gr.Error("Write a prompt first.")
172
- seed = random.randint(0, 2**31 - 1)
173
- try:
174
- path, size, upsampled, timing = _render("t2i", prompt.strip(), [], aspect, resolution, upsample, seed)
175
- except RuntimeError as error:
176
- raise gr.Error(str(error).split("\n")[0])
177
- return path, upsampled, _info(timing, size, seed)
178
-
179
-
180
  def _fit(image: Image.Image, pixels: float) -> Image.Image:
181
  """Downscale to at most `pixels` (the pipeline keeps references at their own size, from 1 MP up)."""
182
  scale = (pixels / (image.width * image.height)) ** 0.5
@@ -185,63 +186,207 @@ def _fit(image: Image.Image, pixels: float) -> Image.Image:
185
  return image.resize((max(1, round(image.width * scale)), max(1, round(image.height * scale))), Image.LANCZOS)
186
 
187
 
188
- def edit(gallery, prompt, resolution, upsample):
189
- images = []
190
- for item in gallery or []:
191
- item = item[0] if isinstance(item, (tuple, list)) else item
192
- item = Image.open(item) if isinstance(item, str) else item
193
- images.append(ImageOps.exif_transpose(item).convert("RGB"))
194
- if not images:
195
- raise gr.Error("Add at least one image; the first one is edited.")
196
- if len(images) > MAX_IMAGES:
197
- raise gr.Error(f"At most {MAX_IMAGES} images.")
198
- if not prompt or not prompt.strip():
199
- raise gr.Error("Describe the edit first.")
200
- images = [_fit(image, REFERENCE_BUDGET / len(images)) for image in images]
201
- seed = random.randint(0, 2**31 - 1)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
202
  try:
203
- path, size, upsampled, timing = _render("edit", prompt.strip(), images, None, resolution, upsample, seed)
204
  except (RuntimeError, ValueError) as error:
205
  raise gr.Error(str(error).split("\n")[0])
206
- return path, upsampled, _info(timing, size, seed)
207
-
208
-
209
- with gr.Blocks(title="P-H3-Image", fill_width=True) as demo:
210
- render_header()
211
- with gr.Tabs(elem_classes="main-tabs"):
212
- with gr.Tab("Text to image"):
213
- with gr.Row():
214
- with gr.Column():
215
- t_prompt = gr.Textbox(label="Prompt", lines=4)
216
- with gr.Row():
217
- t_aspect = gr.Dropdown(list(RATIOS), value="4:3", label="Aspect ratio", filterable=False)
218
- t_resolution = gr.Radio(T2I_RESOLUTIONS, value="8 MP", label="Resolution")
219
- t_upsample = gr.Checkbox(value=True, label="Prompt upsampling")
220
- t_button = gr.Button("Generate", variant="primary")
221
- with gr.Column():
222
- t_image = gr.Image(label="Result", type="filepath")
223
- t_info = gr.Markdown()
224
- with gr.Accordion("Upsampled prompt", open=False):
225
- t_upsampled = gr.Textbox(show_label=False, lines=10)
226
- gr.Examples([[prompt, aspect] for _, aspect, prompt in EXAMPLES], inputs=[t_prompt, t_aspect],
227
- example_labels=[label for label, _, _ in EXAMPLES])
228
- t_button.click(text_to_image, [t_prompt, t_aspect, t_resolution, t_upsample], [t_image, t_upsampled, t_info])
229
- with gr.Tab("Edit"):
230
- with gr.Row():
231
- with gr.Column():
232
- e_images = gr.Gallery(label=f"Images (the first one is edited, up to {MAX_IMAGES})", type="pil",
233
- columns=4, height=260, interactive=True)
234
- e_prompt = gr.Textbox(label="Edit", lines=3, placeholder="e.g. put the fox from <Picture 2> on the bench")
235
- e_resolution = gr.Radio(EDIT_RESOLUTIONS, value="8 MP", label="Resolution",
236
- info="The first image's aspect ratio at this size.")
237
- e_upsample = gr.Checkbox(value=True, label="Prompt upsampling")
238
- e_button = gr.Button("Edit", variant="primary")
239
- with gr.Column():
240
- e_image = gr.Image(label="Result", type="filepath")
241
- e_info = gr.Markdown()
242
- with gr.Accordion("Upsampled prompt", open=False):
243
- e_upsampled = gr.Textbox(show_label=False, lines=10)
244
- e_button.click(edit, [e_images, e_prompt, e_resolution, e_upsample], [e_image, e_upsampled, e_info])
245
- render_footer()
246
-
247
- demo.queue(max_size=20).launch(theme=THEME, css=CSS, head=HEAD)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """P-H3-Image Studio — gr.Server studio + gr.Workflow canvas.
2
 
3
+ The bespoke studio at / replaces the classic form UI; the backend is unchanged from
4
+ PrunaAI/P-H3-Image: the pipeline is loaded once at startup, one component at a time (the host has
5
+ ~97 GB), and slimmed to fit the slice with p_h3_image_fast.py (kernels only, no torch.compile):
6
+ AdaLN tables instead of the transformer's AdaLN projections, FP8 linears everywhere, fused Triton
7
+ DiT blocks, batched VAE tiles, and for the prompt upsampler a weight-only FP8 GEMV, decode
8
+ attention and speculative decoding with its MTP head in one CUDA graph per step. The studio's
9
+ Generate/Edit calls and the workflow canvas at /workflow all run through the same _render.
10
+
11
+ Server + workflow structure adapted from https://huggingface.co/spaces/briaai/Fibo-2.0-editing.
12
  """
13
 
14
+ import base64
15
  import json
16
  import os
17
  import random
18
+ import shutil
19
  import tempfile
20
  import time
21
+ from urllib.parse import unquote, urlparse
22
 
23
  os.environ.setdefault("DIFFUSERS_TRUST_REMOTE_KERNELS", "true")
24
  os.environ.setdefault("PYTORCH_CUDA_ALLOC_CONF", "expandable_segments:True")
 
26
  import gradio as gr
27
  import spaces
28
  import torch
29
+ import uvicorn
30
+ from fastapi.responses import FileResponse, HTMLResponse
31
+ from gradio.utils import get_upload_folder
32
+ from gradio_client import utils as client_utils
33
  from huggingface_hub import snapshot_download
34
  from PIL import Image, ImageOps
35
  from torchvision.io import encode_jpeg
36
 
37
  import p_h3_image_fast as fast
38
  from diffusers import ModularPipeline
 
 
39
 
40
  REPO, REVISION = "PrunaAI/P-H3-Image", "ee4a685de1fbd4337a50bcf9b3c407e4f8463d49"
41
+ APP_DIR = os.path.dirname(os.path.abspath(__file__))
42
  # Canvases (width, height), multiples of 32: 4 and 8 MP as tested, 16 MP from the same ratios.
43
  RATIOS = {"1:1": 1.0, "4:3": 4 / 3, "3:4": 3 / 4, "3:2": 3 / 2, "2:3": 2 / 3, "16:9": 16 / 9, "9:16": 9 / 16}
44
  T2I_RESOLUTIONS, EDIT_RESOLUTIONS = ["4 MP", "8 MP", "16 MP"], ["4 MP", "8 MP"]
45
+ MAX_SEED = 2**31 - 1
46
 
47
 
48
  def _canvas(ratio: float, megapixels: int) -> tuple[int, int]:
 
64
 
65
  def load_pipeline():
66
  path = snapshot_download(REPO, revision=REVISION, max_workers=16)
67
+ with open(os.path.join(APP_DIR, "adaln_calls.json")) as handle:
68
  calls = json.load(handle)
69
  pipe, mtp = fast.load_slim_pipeline(path, calls, log=lambda message: print(message, flush=True))
70
  # The same components without the prompt upsampler step, for requests that skip upsampling.
 
178
  return f"{head} ({upsample:.1f} s prompt upsampling{rate}, {timing['total'] - upsample:.1f} s rendering)"
179
 
180
 
 
 
 
 
 
 
 
 
 
 
 
181
  def _fit(image: Image.Image, pixels: float) -> Image.Image:
182
  """Downscale to at most `pixels` (the pipeline keeps references at their own size, from 1 MP up)."""
183
  scale = (pixels / (image.width * image.height)) ** 0.5
 
186
  return image.resize((max(1, round(image.width * scale)), max(1, round(image.height * scale))), Image.LANCZOS)
187
 
188
 
189
+ # --------------------------------------------------------------------------- helpers
190
+
191
+
192
+ def _resolve_seed(seed, randomize) -> int:
193
+ return random.randint(0, MAX_SEED) if randomize else min(max(int(float(seed)), 0), MAX_SEED)
194
+
195
+
196
+ def _check_choice(value, choices, name):
197
+ if value not in choices:
198
+ raise gr.Error(f"{name} must be one of: {', '.join(choices)}.")
199
+ return value
200
+
201
+
202
+ def _as_data_uri(path: str) -> str:
203
+ """Inline a result image so the studio page never needs a file route."""
204
+ with open(path, "rb") as handle:
205
+ return "data:image/jpeg;base64," + base64.b64encode(handle.read()).decode()
206
+
207
+
208
+ def _requested_path(value) -> str:
209
+ """Turn whatever the canvas/API client sends into a path."""
210
+ if isinstance(value, str):
211
+ return value
212
+ if not isinstance(value, dict):
213
+ raise gr.Error(f"Expected an image, got {type(value).__name__}")
214
+ if path := value.get("path"):
215
+ return path
216
+ url = value.get("url") or ""
217
+ if "/gradio_api/file=" in url:
218
+ return unquote(url.split("/gradio_api/file=", 1)[1])
219
+ if url.startswith(("http://", "https://")):
220
+ raise gr.Error("Upload the image instead of passing a URL.")
221
+ return unquote(urlparse(url).path)
222
+
223
+
224
+ def _local_path(value) -> str:
225
+ """A path this process may open: only files inside gradio's upload folder,
226
+ where /gradio_api/upload puts uploads and _publish puts results."""
227
+ path = os.path.realpath(_requested_path(value))
228
+ folder = os.path.realpath(get_upload_folder())
229
+ if os.path.commonpath([path, folder]) != folder:
230
+ raise gr.Error("Upload the image first.")
231
+ return path
232
+
233
+
234
+ def _open_rgb(value) -> Image.Image:
235
+ return ImageOps.exif_transpose(Image.open(_local_path(value))).convert("RGB")
236
+
237
+
238
+ def _publish(path: str) -> str:
239
+ """Copy a result into gradio's upload folder, which it already serves, so the workflow canvas can render it."""
240
+ folder = get_upload_folder()
241
+ os.makedirs(folder, exist_ok=True)
242
+ out = os.path.join(folder, f"ph3_{os.urandom(8).hex()}.jpg")
243
+ shutil.copyfile(path, out)
244
+ return out
245
+
246
+
247
+ def _as_media(path: str) -> dict:
248
+ """The dict shape the workflow canvas renders media from."""
249
+ return {
250
+ "path": path,
251
+ "url": f"/gradio_api/file={client_utils.encode_file_path(path)}",
252
+ "is_file": True,
253
+ }
254
+
255
+
256
+ def _run(mode, prompt, images, aspect, resolution, upsample, seed):
257
+ """One _render call with the classic app's error surface: messages, not stack traces."""
258
  try:
259
+ return _render(mode, prompt, images, aspect, resolution, upsample, seed)
260
  except (RuntimeError, ValueError) as error:
261
  raise gr.Error(str(error).split("\n")[0])
262
+
263
+
264
+ # ------------------------------------------------------------------ workflow fns
265
+
266
+
267
+ def ph3_generate(prompt, aspect: str = "4:3", resolution: str = "8 MP", seed: str = "42") -> dict:
268
+ """Text-to-image with P-H3-Image (prompt upsampling on)."""
269
+ if not prompt or not str(prompt).strip():
270
+ raise gr.Error("Write a prompt first.")
271
+ _check_choice(aspect, RATIOS, "Aspect ratio")
272
+ _check_choice(resolution, T2I_RESOLUTIONS, "Resolution")
273
+ seed_value = min(max(int(float(seed)), 0), MAX_SEED)
274
+ path, _, _, _ = _run("t2i", str(prompt).strip(), [], aspect, resolution, True, seed_value)
275
+ return _as_media(_publish(path))
276
+
277
+
278
+ def ph3_edit(image, prompt, seed: str = "42") -> dict:
279
+ """Edit an image with P-H3-Image (prompt upsampling on, 8 MP at the image's aspect ratio)."""
280
+ if not prompt or not str(prompt).strip():
281
+ raise gr.Error("Describe the edit first.")
282
+ source = _fit(_open_rgb(image), REFERENCE_BUDGET)
283
+ seed_value = min(max(int(float(seed)), 0), MAX_SEED)
284
+ path, _, _, _ = _run("edit", str(prompt).strip(), [source], None, "8 MP", True, seed_value)
285
+ return _as_media(_publish(path))
286
+
287
+
288
+ # ------------------------------------------------------------------ studio server
289
+
290
+ server = gr.Server(title="P-H3-Image Studio")
291
+
292
+
293
+ @server.get("/", response_class=HTMLResponse)
294
+ def _studio_page():
295
+ with open(os.path.join(APP_DIR, "studio.html"), encoding="utf-8") as handle:
296
+ return handle.read()
297
+
298
+
299
+ @server.api(name="generate", concurrency_limit=1)
300
+ def api_generate(
301
+ prompt: str,
302
+ aspect: str = "4:3",
303
+ resolution: str = "8 MP",
304
+ upsample: bool = True,
305
+ seed: float = 42,
306
+ randomize: bool = True,
307
+ ) -> dict:
308
+ """Text-to-image with P-H3-Image."""
309
+ if not prompt or not prompt.strip():
310
+ raise gr.Error("Write a prompt first.")
311
+ _check_choice(aspect, RATIOS, "Aspect ratio")
312
+ _check_choice(resolution, T2I_RESOLUTIONS, "Resolution")
313
+ seed_value = _resolve_seed(seed, randomize)
314
+ path, size, upsampled, timing = _run("t2i", prompt.strip(), [], aspect, resolution, bool(upsample), seed_value)
315
+ return {"image_data": _as_data_uri(path), "seed": seed_value, "prompt": upsampled,
316
+ "info": _info(timing, size, seed_value)}
317
+
318
+
319
+ @server.api(name="edit", concurrency_limit=1)
320
+ def api_edit(
321
+ source: str,
322
+ instruction: str,
323
+ ref2: str | None = None,
324
+ ref3: str | None = None,
325
+ ref4: str | None = None,
326
+ ref5: str | None = None,
327
+ ref6: str | None = None,
328
+ ref7: str | None = None,
329
+ ref8: str | None = None,
330
+ resolution: str = "8 MP",
331
+ upsample: bool = True,
332
+ seed: float = 42,
333
+ randomize: bool = True,
334
+ ) -> dict:
335
+ """Edit the source (+ up to 7 extra references) with P-H3-Image."""
336
+ if source is None:
337
+ raise gr.Error("Upload a source image first.")
338
+ if not instruction or not instruction.strip():
339
+ raise gr.Error("Describe the edit first.")
340
+ _check_choice(resolution, EDIT_RESOLUTIONS, "Resolution")
341
+ images = [_open_rgb(path) for path in (source, ref2, ref3, ref4, ref5, ref6, ref7, ref8) if path]
342
+ if len(images) > MAX_IMAGES:
343
+ raise gr.Error(f"At most {MAX_IMAGES} images.")
344
+ images = [_fit(image, REFERENCE_BUDGET / len(images)) for image in images]
345
+ seed_value = _resolve_seed(seed, randomize)
346
+ path, size, upsampled, timing = _run("edit", instruction.strip(), images, None, resolution, bool(upsample),
347
+ seed_value)
348
+ return {"image_data": _as_data_uri(path), "seed": seed_value, "prompt": upsampled,
349
+ "info": _info(timing, size, seed_value)}
350
+
351
+
352
+ # The @server.api endpoints are deferred until Server.launch(), which also fires
353
+ # the ZeroGPU registration hook (pack weights + startup report) through the
354
+ # patched Blocks.launch path. Launch on a loopback port for registration, then
355
+ # serve the same app via uvicorn.
356
+ _port_env = os.environ.get("GRADIO_SERVER_PORT")
357
+ os.environ["GRADIO_SERVER_PORT"] = "7861"
358
+ try:
359
+ server.launch(
360
+ prevent_thread_lock=True,
361
+ server_name="127.0.0.1",
362
+ server_port=7861,
363
+ quiet=True,
364
+ show_error=False,
365
+ ssr_mode=False,
366
+ )
367
+ finally:
368
+ if _port_env is None:
369
+ del os.environ["GRADIO_SERVER_PORT"]
370
+ else:
371
+ os.environ["GRADIO_SERVER_PORT"] = _port_env
372
+
373
+ try:
374
+ server.close()
375
+ except Exception:
376
+ pass
377
+
378
+ # ------------------------------------------------------------------ workflow canvas
379
+ # Mounted only after the registration launch above. Mounting first starts the
380
+ # workflow's queue on the loopback server's event loop, and close() then strands
381
+ # it: jobs are accepted on port 7860 but never run.
382
+
383
+ workflow_demo = gr.Workflow(
384
+ graph="workflow.json",
385
+ bind={"ph3_generate": ph3_generate, "ph3_edit": ph3_edit},
386
+ )
387
+ workflow_demo.title = "P-H3-Image Workflow"
388
+
389
+ server = gr.mount_gradio_app(server, workflow_demo, path="/workflow", ssr_mode=False)
390
+
391
+ if __name__ == "__main__":
392
+ uvicorn.run(server, host="0.0.0.0", port=7860)