Spaces:
Runtime error
Runtime error
Load all model variants at module scope for ZeroGPU
#7
by a7543 - opened
2 unmodified lines34566878910111412131410 unmodified lines25262731323328293031323334353637353637383940414243444546474849505138394036 unmodified lines777879948081828317 unmodified lines1011021031181041051061072 unmodified linesGradio Space demo with a single unified interface: image presence selectsediting vs. generation, while the model control selects fast vs. quality."""import gcimport osimport threading
# Use flash_attention_2 for the HF text encoder (flash_attn is installed via wheel)os.environ.setdefault("VF_HF_ATTN_IMPL", "flash_attention_2")
import spaces # MUST be first (after env setup)import torchimport gradio as grfrom PIL import Image
10 unmodified lines },}
_pipe_slots = { "t2i": {"variant": "turbo", "pipe": MageFlowPipeline.from_pretrained(MODEL_VARIANTS["turbo"]["t2i"], device="cuda")}, "edit": {"variant": "turbo", "pipe": MageFlowPipeline.from_pretrained(MODEL_VARIANTS["turbo"]["edit"], device="cuda")},# ZeroGPU requires every model to be placed on CUDA at module scope: the backend# registers the weights, offloads them to disk at startup, and streams them into# VRAM for each @spaces.GPU call. Loading inside the GPU function instead would# charge every switch to the caller's GPU quota and is not carried across the# forked GPU workers.PIPES = { (task, variant): MageFlowPipeline.from_pretrained(spec[task], device="cuda") for variant, spec in MODEL_VARIANTS.items() for task in ("t2i", "edit")}_pipe_lock = threading.Lock()
def _get_pipe(task: str, variant: str): """Keep one loaded variant per task, matching the original two-pipeline footprint.""" with _pipe_lock: slot = _pipe_slots.get(task) if slot and slot["variant"] == variant: return slot["pipe"] if slot: del _pipe_slots[task] del slot gc.collect() torch.cuda.empty_cache() pipe = MageFlowPipeline.from_pretrained(MODEL_VARIANTS[variant][task], device="cuda") _pipe_slots[task] = {"variant": variant, "pipe": pipe} return pipe
def _recommended(variant: str, image):36 unmodified lines
if image is not None: # Route to the edit model when an image is provided. pipe_edit = _get_pipe("edit", model_variant) pipe_edit = PIPES[("edit", model_variant)] if isinstance(image, str): image = Image.open(image) refs = [image.convert("RGB")]17 unmodified lines
# No image: route to the text-to-image model. # Content-safety gate: blocked requests return a blank image. pipe_t2i = _get_pipe("t2i", model_variant) pipe_t2i = PIPES[("t2i", model_variant)] verdict = pipe_t2i.model.txt_enc.screen_text(prompt) if verdict.violates: return Image.new("RGB", (int(width), int(height)), (255, 255, 255))app.py+11-25