P-Q-Image-2.1 / app.py
sdiazlor's picture
feat: update to studio format
caa53cc
Raw History Blame
13.8 kB
"""P-Q-Image-2.1 Studio — gr.Server studio + gr.Workflow canvas.
- "/" Studio: custom UI (studio.html) over the `generate` / `edit` API endpoints
- "/workflow" Workflow canvas: node graph from workflow.json (pq_generate / pq_edit)
"""
import base64
import io
import json
import os
import random
import time
from urllib.parse import unquote, urlparse
import gradio as gr
import spaces
import torch
import uvicorn
from fastapi.responses import FileResponse, HTMLResponse
from gradio.utils import get_upload_folder
from gradio_client import utils as client_utils
from PIL import Image, ImageOps
from diffusers import FlowMatchEulerDiscreteScheduler, QwenImage21Pipeline
from examples import EXAMPLES
APP_DIR = os.path.dirname(os.path.abspath(__file__))
MAX_SEED = 2**31 - 1
# Text-to-image canvases (width, height) at ~1 MP, multiples of 32. 1:1 matches the previous fixed 1024x1024.
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}
def _canvas(ratio: float, megapixels: float = 1.0) -> tuple[int, int]:
pixels = megapixels * 1024**2
return round((pixels * ratio) ** 0.5 / 32) * 32, round((pixels / ratio) ** 0.5 / 32) * 32
SIZES = {name: _canvas(ratio) for name, ratio in RATIOS.items()}
LORA_REPO = "PrunaAI/P-Q-Image-2.1"
DEFAULT_VARIANT = "6-steps"
LORA_STRENGTH = 1.0
# Each adapter is trained for one schedule. v0.2 uses unshifted 6-step sigmas
# plus dynamic shifting; v0.1 uses its raw nodes with shifting off.
VARIANTS = {
"6-steps": {
"weight_name": "p_q_image_2.1_6_steps_v0.2.safetensors",
"adapter_name": "step6_v02",
"steps": 6,
"sigmas": [1.0, 0.925, 0.85, 0.625, 0.375, 0.125],
"dynamic_shifting": True,
"use_kv_cache": False,
},
"6-steps-upsampled": {
"weight_name": "p_q_image_2.1_6_steps_upsampled_v0.2.safetensors",
"adapter_name": "step6_upsampled_v02",
"steps": 6,
"sigmas": [1.0, 0.925, 0.85, 0.625, 0.375, 0.125],
"dynamic_shifting": True,
"use_kv_cache": False,
},
"8-steps": {
"weight_name": "p_q_image_2.1_8_steps_v0.1.safetensors",
"adapter_name": "step8_v01",
"steps": 8,
"sigmas": [1.0, 14 / 15, 6 / 7, 10 / 13, 2 / 3, 6 / 11, 0.4, 2 / 9],
"dynamic_shifting": False,
"use_kv_cache": True,
},
"5-steps": {
"weight_name": "p_q_image_2.1_5_steps_v0.1.safetensors",
"adapter_name": "step5_v01",
"steps": 5,
"sigmas": [1.0, 0.94, 6 / 7, 2 / 3, 0.4],
"dynamic_shifting": False,
"use_kv_cache": True,
},
}
VARIANT_CHOICES = list(VARIANTS)
VARIANT_ALIASES = {
"6-steps": "6-steps",
"6-step": "6-steps",
"6step": "6-steps",
"6": "6-steps",
"basic": "6-steps",
"v0.2": "6-steps",
"v0.2-6-step": "6-steps",
"v0.2-6-steps": "6-steps",
"6-steps-upsampled": "6-steps-upsampled",
"6-step-upsampled": "6-steps-upsampled",
"6step-upsampled": "6-steps-upsampled",
"prompt-upsampled": "6-steps-upsampled",
"upsampled": "6-steps-upsampled",
"upsample": "6-steps-upsampled",
"v0.2-upsampled": "6-steps-upsampled",
"v0.2-6-step-upsampled": "6-steps-upsampled",
"v0.2-6-steps-upsampled": "6-steps-upsampled",
"8-steps": "8-steps",
"8-step": "8-steps",
"8step": "8-steps",
"8": "8-steps",
"v0.1-8-step": "8-steps",
"v0.1-8-steps": "8-steps",
"5-steps": "5-steps",
"5-step": "5-steps",
"5step": "5-steps",
"5": "5-steps",
"v0.1-5-step": "5-steps",
"v0.1-5-steps": "5-steps",
}
pipe = QwenImage21Pipeline.from_pretrained(
"Qwen/Qwen-Image-2.1", torch_dtype=torch.bfloat16
)
for spec in VARIANTS.values():
pipe.load_lora_weights(
LORA_REPO,
weight_name=spec["weight_name"],
adapter_name=spec["adapter_name"],
)
pipe.set_adapters([VARIANTS[DEFAULT_VARIANT]["adapter_name"]], [LORA_STRENGTH])
pipe.scheduler = FlowMatchEulerDiscreteScheduler.from_config(
pipe.scheduler.config,
use_dynamic_shifting=True,
shift_terminal=None,
)
pipe = pipe.to("cuda")
_active_variant = DEFAULT_VARIANT
def _resolve_variant(variant) -> str:
if variant is None or variant == "":
return DEFAULT_VARIANT
key = "-".join(str(variant).strip().lower().replace("_", "-").split())
try:
key = str(int(float(key)))
except (TypeError, ValueError):
pass
return VARIANT_ALIASES.get(key, DEFAULT_VARIANT)
def _ensure_variant(variant) -> dict:
"""Activate the matching adapter and scheduler. Load only one at a time."""
global _active_variant
name = _resolve_variant(variant)
spec = VARIANTS[name]
if _active_variant != name:
pipe.set_adapters([spec["adapter_name"]], [LORA_STRENGTH])
scheduler_kwargs = {"use_dynamic_shifting": spec["dynamic_shifting"], "shift_terminal": None}
if not spec["dynamic_shifting"]:
scheduler_kwargs["shift"] = 1.0
pipe.scheduler = FlowMatchEulerDiscreteScheduler.from_config(
pipe.scheduler.config,
**scheduler_kwargs,
)
_active_variant = name
return spec
# --------------------------------------------------------------------------- rendering
def _run_pipe(prompt: str, spec: dict, seed: int, image=None, size=(1024, 1024)):
kwargs = {
"prompt": prompt,
"generator": torch.Generator("cuda").manual_seed(seed),
"num_inference_steps": spec["steps"],
"sigmas": spec["sigmas"],
"true_cfg_scale": 1.0,
"use_kv_cache": spec["use_kv_cache"],
"output_resolution": 1024,
}
if image is None:
kwargs["width"], kwargs["height"] = size
else:
kwargs["image"] = image
return pipe(**kwargs).images[0]
@spaces.GPU(duration=120)
def _render(prompt, variant, seed, image=None, aspect="1:1"):
spec = _ensure_variant(variant)
torch.cuda.synchronize()
start = time.perf_counter()
result = _run_pipe(prompt, spec, seed, image=image, size=SIZES[aspect])
torch.cuda.synchronize()
return result, time.perf_counter() - start
def _run(prompt, variant, seed, image=None, aspect="1:1"):
"""One _render call with the classic app's error surface: messages, not stack traces."""
try:
return _render(prompt, variant, seed, image=image, aspect=aspect)
except (RuntimeError, ValueError) as error:
raise gr.Error(str(error).split("\n")[0])
def _info(image: Image.Image, seed: int, variant: str, seconds: float) -> str:
return f"{image.width}×{image.height}, seed {seed} · {_resolve_variant(variant)} · {seconds:.1f} s on the GPU"
# --------------------------------------------------------------------------- helpers
def _seed(seed) -> int:
try:
return min(max(int(float(seed)), 0), MAX_SEED)
except (TypeError, ValueError):
return 42
def _resolve_seed(seed, randomize) -> int:
return random.randint(0, MAX_SEED) if randomize else _seed(seed)
def _check_choice(value, choices, name):
if value not in choices:
raise gr.Error(f"{name} must be one of: {', '.join(choices)}.")
return value
def _requested_path(value) -> str:
"""Turn whatever the canvas/API client sends into a path."""
if isinstance(value, str):
return value
if not isinstance(value, dict):
raise gr.Error(f"Expected an image, got {type(value).__name__}")
if path := value.get("path"):
return path
url = value.get("url") or ""
if "/gradio_api/file=" in url:
return unquote(url.split("/gradio_api/file=", 1)[1])
if url.startswith(("http://", "https://")):
raise gr.Error("Upload the image instead of passing a URL.")
return unquote(urlparse(url).path)
def _local_path(value) -> str:
"""A path this process may open: only files inside gradio's upload folder,
where /gradio_api/upload puts uploads and _publish puts results."""
path = os.path.realpath(_requested_path(value))
folder = os.path.realpath(get_upload_folder())
if os.path.commonpath([path, folder]) != folder:
raise gr.Error("Upload the image first.")
return path
def _open_rgb(value) -> Image.Image:
if isinstance(value, Image.Image):
return value.convert("RGB")
return ImageOps.exif_transpose(Image.open(_local_path(value))).convert("RGB")
def _as_data_uri(image: Image.Image) -> str:
"""Inline a result image so the studio page never needs a file route."""
buffer = io.BytesIO()
image.save(buffer, format="PNG")
return "data:image/png;base64," + base64.b64encode(buffer.getvalue()).decode()
def _publish(image: Image.Image) -> str:
"""Save a result into gradio's upload folder, which it already serves, so the workflow canvas can render it."""
folder = get_upload_folder()
os.makedirs(folder, exist_ok=True)
path = os.path.join(folder, f"pruna_{os.urandom(8).hex()}.png")
image.save(path)
return path
def _as_media(path: str) -> dict:
"""The dict shape the workflow canvas renders media from."""
return {
"path": path,
"url": f"/gradio_api/file={client_utils.encode_file_path(path)}",
"is_file": True,
}
# ------------------------------------------------------------------ workflow fns
def pq_generate(prompt, variant: str = DEFAULT_VARIANT, aspect: str = "1:1", seed: str = "42") -> dict:
"""Text-to-image with P-Q-Image-2.1 (no CFG)."""
if not prompt or not str(prompt).strip():
raise gr.Error("Write a prompt first.")
_check_choice(aspect or "1:1", RATIOS, "Aspect ratio")
image, _ = _run(str(prompt).strip(), variant, _seed(seed), aspect=aspect or "1:1")
return _as_media(_publish(image))
def pq_edit(image, prompt, variant: str = DEFAULT_VARIANT, seed: str = "42") -> dict:
"""Edit an image with P-Q-Image-2.1 (no CFG)."""
if image is None or image == "":
raise gr.Error("Connect a source image first.")
if not prompt or not str(prompt).strip():
raise gr.Error("Describe the edit first.")
result, _ = _run(str(prompt).strip(), variant, _seed(seed), image=_open_rgb(image))
return _as_media(_publish(result))
# ------------------------------------------------------------------ studio server
server = gr.Server(title="P-Q-Image-2.1 Studio")
@server.get("/", response_class=HTMLResponse)
def _studio_page():
with open(os.path.join(APP_DIR, "studio.html"), encoding="utf-8") as handle:
page = handle.read()
config = {
"examples": [{"label": label, "aspect": aspect, "prompt": prompt} for label, aspect, prompt in EXAMPLES],
"variants": VARIANT_CHOICES,
"defaultVariant": DEFAULT_VARIANT,
"aspects": list(RATIOS),
}
return page.replace("__STUDIO_CONFIG__", json.dumps(config).replace("</", "<\\/"))
@server.get("/pruna-logo.png")
def _logo():
return FileResponse(os.path.join(APP_DIR, "pruna-logo.png"), media_type="image/png")
@server.api(name="generate", concurrency_limit=1)
def api_generate(
prompt: str,
variant: str = DEFAULT_VARIANT,
aspect: str = "1:1",
seed: float = 42,
randomize: bool = True,
) -> dict:
"""Text-to-image with P-Q-Image-2.1."""
if not prompt or not prompt.strip():
raise gr.Error("Write a prompt first.")
_check_choice(variant, VARIANT_CHOICES, "Variant")
_check_choice(aspect, RATIOS, "Aspect ratio")
seed_value = _resolve_seed(seed, randomize)
image, seconds = _run(prompt.strip(), variant, seed_value, aspect=aspect)
return {"image_data": _as_data_uri(image), "seed": seed_value, "info": _info(image, seed_value, variant, seconds)}
@server.api(name="edit", concurrency_limit=1)
def api_edit(
source: str,
instruction: str,
variant: str = DEFAULT_VARIANT,
seed: float = 42,
randomize: bool = True,
) -> dict:
"""Edit the source image with P-Q-Image-2.1."""
if source is None:
raise gr.Error("Upload a source image first.")
if not instruction or not instruction.strip():
raise gr.Error("Describe the edit first.")
_check_choice(variant, VARIANT_CHOICES, "Variant")
seed_value = _resolve_seed(seed, randomize)
image, seconds = _run(instruction.strip(), variant, seed_value, image=_open_rgb(source))
return {"image_data": _as_data_uri(image), "seed": seed_value, "info": _info(image, seed_value, variant, seconds)}
# The @server.api endpoints are deferred until Server.launch(), which also fires
# the ZeroGPU registration hook (pack weights + startup report) through the
# patched Blocks.launch path. Launch on a loopback port for registration, then
# serve the same app via uvicorn.
_port_env = os.environ.get("GRADIO_SERVER_PORT")
os.environ["GRADIO_SERVER_PORT"] = "7861"
try:
server.launch(
prevent_thread_lock=True,
server_name="127.0.0.1",
server_port=7861,
quiet=True,
show_error=False,
ssr_mode=False,
)
finally:
if _port_env is None:
del os.environ["GRADIO_SERVER_PORT"]
else:
os.environ["GRADIO_SERVER_PORT"] = _port_env
try:
server.close()
except Exception:
pass
# ------------------------------------------------------------------ workflow canvas
# Mounted only after the registration launch above. Mounting first starts the
# workflow's queue on the loopback server's event loop, and close() then strands
# it: jobs are accepted on port 7860 but never run.
workflow_demo = gr.Workflow(
graph="workflow.json",
bind={"pq_generate": pq_generate, "pq_edit": pq_edit},
)
workflow_demo.title = "P-Q-Image-2.1 Workflow"
server = gr.mount_gradio_app(server, workflow_demo, path="/workflow", ssr_mode=False)
if __name__ == "__main__":
uvicorn.run(server, host="0.0.0.0", port=7860)