Spaces:
Runtime error
Runtime error
Add quality model option and streamline blocked-content handling
#4
by Xinjie-Q - opened
Files changed (3)
- README.md+5-5
- app.py+66-71
- mage_flow/pipeline.py-2
16 unmodified lines17181920212021222323242526272829293031323334353516 unmodified lines
This Space demonstrates [Mage-Flow](https://huggingface.co/papers/2607.19064) from Microsoft, a 4B-scale image generation and editing foundation model through a single unified interface:
- **No image** → the prompt generates a new image (routes to [Mage-Flow-Turbo](https://huggingface.co/microsoft/Mage-Flow-Turbo)).- **With an uploaded image** → the prompt is treated as an edit instruction (routes to [Mage-Flow-Edit-Turbo](https://huggingface.co/microsoft/Mage-Flow-Edit-Turbo)).- **No image** → text-to-image generation.- **With an uploaded image** → instruction-based image editing.
The demo uses the Turbo variants for fast, few-step inference (4 steps, CFG 1.0).Choose **Mage-Flow-Turbo · Fast** (4 steps, CFG 1.0) or **Mage-Flow · Quality**. The quality variant uses 20 steps / CFG 5.0 for generation and 30 steps / CFG 5.0 for editing; uploading or removing an image updates these defaults automatically.
## Usage
1. Enter a prompt.2. *(Optional)* Upload an image to edit — the prompt becomes an edit instruction.3. Click **Run**.3. Choose Fast or Quality, then click **Run**.
## References
- Paper: [Mage-Flow: An Efficient Native-Resolution Foundation Model for Image Generation and Editing](https://huggingface.co/papers/2607.19064)- GitHub: [microsoft/Mage](https://github.com/microsoft/Mage)- Models: [microsoft/Mage-Flow-Turbo](https://huggingface.co/microsoft/Mage-Flow-Turbo), [microsoft/Mage-Flow-Edit-Turbo](https://huggingface.co/microsoft/Mage-Flow-Edit-Turbo)- Models: [Mage-Flow](https://huggingface.co/microsoft/Mage-Flow), [Mage-Flow-Turbo](https://huggingface.co/microsoft/Mage-Flow-Turbo), [Mage-Flow-Edit](https://huggingface.co/microsoft/Mage-Flow-Edit), [Mage-Flow-Edit-Turbo](https://huggingface.co/microsoft/Mage-Flow-Edit-Turbo)123453456789101112121313141517161718202122232425262728293031323334353637383940414243444519474849505152535455565758596061626364656667686970202122232425262728293031323334353637383940414243444546474849505152535455565758596061624 unmodified lines6768697071727374858687757677787911 unmodified lines9192939495969798109110991001011131141021161031041051067 unmodified lines11411511613013111711811912013413513612112212312420 unmodified lines14514614716314814915015115215312 unmodified lines16616716816917017117217317417517647 unmodified lines224225226227228229235230231232233"""Mage-Flow: Efficient Native-Resolution Foundation Model for Image Generation and Editing.
Gradio Space demo with a single unified interface: enter a prompt and optionallyupload an image. If an image is provided, the request is routed to theMage-Flow-Edit-Turbo model; otherwise it is routed to the Mage-Flow-Turbo model.Gradio 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 textwrap
import spaces # MUST be first (after env setup)import torchimport gradio as grfrom PIL import Image, ImageDraw, ImageFontfrom PIL import Image
from mage_flow.pipeline import MageFlowPipelinefrom mage_flow.models.modules.mage_text import CATEGORY_DISPLAY
T2I_MODEL = "microsoft/Mage-Flow-Turbo"EDIT_MODEL = "microsoft/Mage-Flow-Edit-Turbo"
pipe_t2i = MageFlowPipeline.from_pretrained(T2I_MODEL, device="cuda")pipe_edit = MageFlowPipeline.from_pretrained(EDIT_MODEL, device="cuda")
def _block_reason_text(verdict) -> str: """Human-readable block reason: '<category> · <explanation>'.""" cat = ", ".join( CATEGORY_DISPLAY.get(c, c) for c in verdict.categories ) or "policy violation" reason = (verdict.reason or "").strip() return f"{cat} · {reason}" if reason else cat
def _blocked_image(reason_text: str, height: int = 1024, width: int = 1024) -> Image.Image: """Neutral background image with the safety-filter message drawn on it.""" img = Image.new("RGB", (int(width), int(height)), color=(245, 245, 245)) draw = ImageDraw.Draw(img) try: font = ImageFont.load_default(size=28) except TypeError: # older Pillow: load_default() takes no size font = ImageFont.load_default()
message = ( "The classifiers included by default in the Microsoft Mage Flow model flagged the following:\n\n" + reason_text )
# Wrap each paragraph to a column width that fits the image. max_chars = max(20, int(width / 16)) lines = [] for para in message.split("\n"): if not para: lines.append("") continue lines.extend(textwrap.wrap(para, width=max_chars))
# Vertically center the text block. line_h = 38 total_h = line_h * len(lines) y = max(20, (int(height) - total_h) // 2) for line in lines: draw.text((40, y), line, fill=(20, 20, 20), font=font) y += line_h return img
@spaces.GPU(duration=60)MODEL_VARIANTS = { "turbo": { "t2i": "microsoft/Mage-Flow-Turbo", "edit": "microsoft/Mage-Flow-Edit-Turbo", "t2i_steps": 4, "edit_steps": 4, "cfg": 1.0, }, "quality": { "t2i": "microsoft/Mage-Flow", "edit": "microsoft/Mage-Flow-Edit", "t2i_steps": 20, "edit_steps": 30, "cfg": 5.0, },}
_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")},}_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): spec = MODEL_VARIANTS[variant] return (spec["edit_steps"] if image is not None else spec["t2i_steps"], spec["cfg"])
@spaces.GPU(duration=120)def generate( prompt: str, image=None,4 unmodified lines width: int = 1024, max_size: int = 1024, seed: int = 42, model_variant: str = "turbo", progress=gr.Progress(track_tqdm=True),): """Generate or edit an image with Mage-Flow.
If ``image`` is provided, the reference image is edited with Mage-Flow-Edit-Turbo. Otherwise a new image is generated from the text prompt with Mage-Flow-Turbo. If ``image`` is provided, route to the selected edit model; otherwise route to the selected text-to-image model.
Args: prompt: Text description (generation) or edit instruction (editing).11 unmodified lines
if image is not None: # Route to the edit model when an image is provided. pipe_edit = _get_pipe("edit", model_variant) if isinstance(image, str): image = Image.open(image) refs = [image.convert("RGB")]
# Content-safety gate: surface a block to the user instead of failing # silently (gr.Warning toast + a stamped output image). # Content-safety gate: blocked requests return a blank image. verdict = pipe_edit.model.txt_enc.screen_edit(prompt, refs) if verdict.violates: reason_text = _block_reason_text(verdict) gr.Warning("The classifiers included by default in the Microsoft Mage Flow model flagged the following: " + reason_text) w, h = refs[0].size return _blocked_image(reason_text, height=h, width=w) return Image.new("RGB", (w, h), (255, 255, 255))
out = pipe_edit.edit( [prompt],7 unmodified lines return out
# No image: route to the text-to-image model. # Content-safety gate: surface a block to the user instead of failing # silently (gr.Warning toast + a stamped output image). # Content-safety gate: blocked requests return a blank image. pipe_t2i = _get_pipe("t2i", model_variant) verdict = pipe_t2i.model.txt_enc.screen_text(prompt) if verdict.violates: reason_text = _block_reason_text(verdict) gr.Warning("The classifiers included by default in the Microsoft Mage Flow model flagged the following: " + reason_text) return _blocked_image(reason_text, height=int(height), width=int(width)) return Image.new("RGB", (int(width), int(height)), (255, 255, 255))
img = pipe_t2i.generate( [prompt],20 unmodified lines "# Mage-Flow\n" "Efficient Native-Resolution Foundation Model for Image Generation and Editing. " "Enter a prompt to generate an image, or upload an image to edit it.\n\n" "Models: [Mage-Flow-Turbo](https://huggingface.co/microsoft/Mage-Flow-Turbo) & " "Models: [Mage-Flow](https://huggingface.co/microsoft/Mage-Flow), " "[Mage-Flow-Turbo](https://huggingface.co/microsoft/Mage-Flow-Turbo), " "[Mage-Flow-Edit](https://huggingface.co/microsoft/Mage-Flow-Edit), " "[Mage-Flow-Edit-Turbo](https://huggingface.co/microsoft/Mage-Flow-Edit-Turbo) | " "[Paper](https://huggingface.co/papers/2607.19064) | " "[GitHub](https://github.com/microsoft/Mage)"12 unmodified lines ) run_btn = gr.Button("Run", variant="primary", scale=1)
model_variant = gr.Radio( [("Mage-Flow-Turbo · Fast", "turbo"), ("Mage-Flow · Quality", "quality")], value="turbo", label="Model", )
with gr.Accordion("Input image (optional — enables editing)", open=True): image = gr.Image( type="pil",47 unmodified lines cache_mode="lazy", )
model_variant.change(_recommended, [model_variant, image], [steps, cfg], api_name=False) image.change(_recommended, [model_variant, image], [steps, cfg], api_name=False)
inputs = [prompt, image, negative_prompt, steps, cfg, height, width, max_size, seed] inputs = [prompt, image, negative_prompt, steps, cfg, height, width, max_size, seed, model_variant] run_btn.click(lambda: None, None, result).then( generate, inputs, result, api_name="generate", )285 unmodified lines286287288289289290291184 unmodified lines476477478480479480481285 unmodified lines verdict = model.txt_enc.screen_text(prompts[i]) if verdict.violates: h_, w_ = _make_divisible_by_16(heights[i]), _make_divisible_by_16(widths[i]) print(verdict.banner()) results[i] = make_refusal_image(verdict, height=h_, width=w_) continue active.append(i)184 unmodified lines verdict = model.txt_enc.screen_edit(prompts[i], pils_per_sample[i]) if verdict.violates: h_, w_ = res_hw[i] print(verdict.banner()) results[i] = make_refusal_image(verdict, height=h_, width=w_) continue active.append(i)README.md+5-5
app.py+66-71
mage_flow/pipeline.py-2