Spaces:
Runtime error
Runtime error
Surface content-safety blocks: gr.Warning + stamped output image for generate and edit routes
Browse files8 unmodified lines910111213141516171819202 unmodified lines23242526272832 unmodified lines6162636465666 unmodified lines7374757677788 unmodified lines# 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 Imagefrom mage_flow.pipeline import MageFlowPipelineT2I_MODEL = "microsoft/Mage-Flow-Turbo"EDIT_MODEL = "microsoft/Mage-Flow-Edit-Turbo"2 unmodified linespipe_edit = MageFlowPipeline.from_pretrained(EDIT_MODEL, device="cuda")@spaces.GPU(duration=60)def generate(prompt: str,32 unmodified linesif isinstance(image, str):image = Image.open(image)refs = [image.convert("RGB")]out = pipe_edit.edit([prompt],[refs],6 unmodified linesreturn out# No image: route to the text-to-image model.img = pipe_t2i.generate([prompt],neg_prompts=[negative_prompt or " "],8 unmodified lines910111213141516171819202122232 unmodified lines26272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727332 unmodified lines1061071081091101111121131141151161171181191201216 unmodified lines1281291301311321331341351361371381391401418 unmodified lines# 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 textwrapimport spaces # MUST be first (after env setup)import torchimport gradio as grfrom PIL import Image, ImageDraw, ImageFontfrom mage_flow.pipeline import MageFlowPipelinefrom mage_flow.models.modules.mage_text import CATEGORY_DISPLAYT2I_MODEL = "microsoft/Mage-Flow-Turbo"EDIT_MODEL = "microsoft/Mage-Flow-Edit-Turbo"2 unmodified linespipe_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 catdef _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 sizefont = ImageFont.load_default()message = ("The safety 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("")continuelines.extend(textwrap.wrap(para, width=max_chars))# Vertically center the text block.line_h = 38total_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_hreturn img@spaces.GPU(duration=60)def generate(prompt: str,32 unmodified linesif 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).verdict = pipe_edit.model.txt_enc.screen_edit(prompt, refs)if verdict.violates:reason_text = _block_reason_text(verdict)gr.Warning(reason_text)w, h = refs[0].sizereturn _blocked_image(reason_text, height=h, width=w)out = pipe_edit.edit([prompt],[refs],6 unmodified linesreturn 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).verdict = pipe_t2i.model.txt_enc.screen_text(prompt)if verdict.violates:reason_text = _block_reason_text(verdict)gr.Warning(reason_text)return _blocked_image(reason_text, height=int(height), width=int(width))img = pipe_t2i.generate([prompt],neg_prompts=[negative_prompt or " "],
app.py+64-1