mrboost's picture
Deploy flux-image-studio on ZeroGPU
d793324 verified
Raw History Blame Contribute Delete
8.35 kB
"""FLUX.1 & Image-to-Image Edit Studio on ZeroGPU."""
import os
import random
import shutil
import tempfile
os.environ.setdefault("PYTORCH_ALLOC_CONF", "backend:cudaMallocAsync")
os.environ.setdefault("PYTORCH_CUDA_ALLOC_CONF", "backend:cudaMallocAsync")
import spaces # Must be imported before torch
import gradio as gr
import PIL.Image
import torch
from diffusers import AutoPipelineForImage2Image, AutoPipelineForText2Image
from gradio_client import Client
HF_TOKEN = os.environ.get("HF_TOKEN")
LOCAL_MODEL_ID = "stabilityai/sdxl-turbo"
MAX_SEED = 2**31 - 1
ASPECT_PRESETS = {
"Cinema 16:9 (1472 脳 832 路 LTX-2.5 Ready)": (1472, 832),
"720P 16:9 (1280 脳 720 路 SkyReels-V2 Ready)": (1280, 720),
"Full HD 16:9 (1920 脳 1088)": (1920, 1088),
"Portrait 9:16 (832 脳 1472 路 Reels/Shorts)": (832, 1472),
"Square 1:1 (1024 脳 1024 路 Logo/Product)": (1024, 1024),
}
print(f"[flux-image-studio] Loading {LOCAL_MODEL_ID} pipelines at module scope for ZeroGPU...", flush=True)
pipe_t2i = AutoPipelineForText2Image.from_pretrained(
LOCAL_MODEL_ID,
torch_dtype=torch.float16,
variant="fp16",
)
pipe_t2i.to("cuda")
pipe_i2i = AutoPipelineForImage2Image.from_pipe(pipe_t2i)
print("[flux-image-studio] Local ZeroGPU T2I + I2I pipelines ready!", flush=True)
@spaces.GPU(duration=45)
def generate_or_edit_local(
prompt: str,
init_image_path: str | None,
width: int,
height: int,
steps: int,
strength: float,
seed: int,
) -> tuple[str, int, str]:
"""Run local ZeroGPU Text-to-Image or Image-to-Image editing."""
generator = torch.Generator("cuda").manual_seed(int(seed))
w = max(512, min(1536, (int(width) // 64) * 64))
h = max(512, min(1536, (int(height) // 64) * 64))
num_steps = max(2, min(8, int(steps)))
if init_image_path:
init_img = PIL.Image.open(init_image_path).convert("RGB").resize((w, h), PIL.Image.Resampling.LANCZOS)
eff_steps = max(2, int(num_steps / max(0.2, float(strength))))
image = pipe_i2i(
prompt=prompt.strip(),
image=init_img,
num_inference_steps=eff_steps,
strength=float(strength),
guidance_scale=0.0,
generator=generator,
).images[0]
mode = f"Local ZeroGPU Image Edit (strength={strength:.2f})"
else:
image = pipe_t2i(
prompt=prompt.strip(),
width=w,
height=h,
num_inference_steps=num_steps,
guidance_scale=0.0,
generator=generator,
).images[0]
mode = "Local ZeroGPU SDXL-Turbo"
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
image.save(tmp.name, format="PNG")
return tmp.name, int(seed), f"{w}脳{h} 路 {mode}"
def generate_image(
prompt: str,
init_image_path: str | None = None,
engine: str = "FLUX.1-schnell (12B Photorealistic 路 ZeroGPU Cluster)",
aspect_preset: str = "Cinema 16:9 (1472 脳 832 路 LTX-2.5 Ready)",
width: int = 1472,
height: int = 832,
steps: int = 4,
strength: float = 0.65,
seed: int = 42,
randomize_seed: bool = True,
) -> tuple[str, int, str]:
"""Generate a photorealistic image or edit an existing uploaded image."""
del aspect_preset
if not prompt or not prompt.strip():
raise gr.Error("Please enter a prompt.")
if randomize_seed:
seed = random.randint(0, MAX_SEED)
seed = int(seed)
if not init_image_path and "FLUX.1" in str(engine):
try:
client = Client("black-forest-labs/FLUX.1-schnell", token=HF_TOKEN, httpx_kwargs={"timeout": 180.0})
w = max(256, min(2048, (int(width) // 32) * 32))
h = max(256, min(2048, (int(height) // 32) * 32))
res = client.predict(
prompt=prompt.strip(),
seed=seed,
randomize_seed=False,
width=w,
height=h,
num_inference_steps=max(1, min(16, int(steps))),
api_name="/infer",
)
img_path = res[0] if isinstance(res, (list, tuple)) else res
used_s = int(res[1]) if isinstance(res, (list, tuple)) and len(res) > 1 else seed
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
shutil.copyfile(img_path, tmp.name)
return tmp.name, used_s, f"{w}脳{h} 路 FLUX.1-schnell (12B ZeroGPU)"
except Exception as err: # noqa: BLE001
print(f"[flux-image-studio] FLUX relay fallback to local ZeroGPU ({err!r})", flush=True)
return generate_or_edit_local(
prompt=prompt,
init_image_path=init_image_path,
width=width,
height=height,
steps=steps,
strength=strength,
seed=seed,
)
def apply_aspect(preset: str):
dims = ASPECT_PRESETS.get(preset)
if not dims:
return gr.update(), gr.update()
return dims[0], dims[1]
with gr.Blocks(title="FLUX.1 & Image Edit ZeroGPU Studio", fill_width=True) as demo:
gr.Markdown(
"# 馃帹 FLUX.1 & Image Edit ZeroGPU Studio\n"
"Generate **1080p photorealistic starting frames** for your **LTX-2.5** and **SkyReels-V2** video studios, "
"design logos/UI mockups, or upload an image to edit on **ZeroGPU (`zero-a10g`)** at **$0/hour**."
)
with gr.Row():
with gr.Column(scale=1):
prompt_input = gr.Textbox(
label="Image Prompt / Edit Instruction",
lines=4,
placeholder="Describe the scene, lighting, camera lens, logo, or image edit...",
)
init_image_input = gr.Image(
label="Upload Image to Edit / Restyle (Optional 路 Image-to-Image)",
type="filepath",
)
with gr.Accordion("Studio Settings", open=True):
engine_choice = gr.Dropdown(
choices=[
"FLUX.1-schnell (12B Photorealistic 路 ZeroGPU Cluster)",
"SDXL-Turbo (Local ZeroGPU 路 Instant T2I + Image Editing)",
],
value="FLUX.1-schnell (12B Photorealistic 路 ZeroGPU Cluster)",
label="Generation Engine",
)
aspect_dropdown = gr.Dropdown(
choices=list(ASPECT_PRESETS),
value="Cinema 16:9 (1472 脳 832 路 LTX-2.5 Ready)",
label="Aspect Ratio Preset (Video-Ready)",
)
with gr.Row():
width_slider = gr.Slider(512, 1920, value=1472, step=64, label="Width (px)")
height_slider = gr.Slider(512, 1920, value=832, step=64, label="Height (px)")
with gr.Row():
steps_slider = gr.Slider(1, 12, value=4, step=1, label="Inference Steps")
strength_slider = gr.Slider(
0.2, 0.95, value=0.65, step=0.05, label="Edit Strength (for Image-to-Image)"
)
with gr.Row():
seed_slider = gr.Slider(0, MAX_SEED, value=42, step=1, label="Seed")
rand_check = gr.Checkbox(value=True, label="Randomize seed")
gen_btn = gr.Button("馃帹 Generate / Edit Image", variant="primary")
with gr.Column(scale=1):
out_img = gr.Image(label="Generated Image (Ready for Video Studio)", type="filepath")
with gr.Row():
out_seed = gr.Number(label="Seed Used", precision=0, interactive=False)
out_info = gr.Textbox(label="Render Info", interactive=False)
aspect_dropdown.change(
fn=apply_aspect,
inputs=aspect_dropdown,
outputs=[width_slider, height_slider],
api_name=False,
)
gen_btn.click(
fn=generate_image,
inputs=[
prompt_input,
init_image_input,
engine_choice,
aspect_dropdown,
width_slider,
height_slider,
steps_slider,
strength_slider,
seed_slider,
rand_check,
],
outputs=[out_img, out_seed, out_info],
api_name="generate_image",
)
if __name__ == "__main__":
demo.launch(theme=gr.themes.Citrus(), mcp_server=True)