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image
|
| 6 |
+
from diffusers import LongCatImageEditPipeline
|
| 7 |
+
|
| 8 |
+
# --- Load pipeline on CPU at init ---
|
| 9 |
+
pipe = LongCatImageEditPipeline.from_pretrained(
|
| 10 |
+
"meituan-longcat/LongCat-Image-Edit-Turbo",
|
| 11 |
+
torch_dtype=torch.bfloat16,
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
@spaces.GPU(duration=120)
|
| 16 |
+
def edit_image(
|
| 17 |
+
input_image,
|
| 18 |
+
prompt,
|
| 19 |
+
negative_prompt="",
|
| 20 |
+
guidance_scale=1.0,
|
| 21 |
+
num_inference_steps=8,
|
| 22 |
+
seed=43,
|
| 23 |
+
randomize_seed=True,
|
| 24 |
+
):
|
| 25 |
+
if input_image is None:
|
| 26 |
+
raise gr.Error("Please upload an image / μ΄λ―Έμ§λ₯Ό μ
λ‘λν΄μ£ΌμΈμ.")
|
| 27 |
+
if not prompt.strip():
|
| 28 |
+
raise gr.Error("Please enter an editing prompt / νΈμ§ ν둬ννΈλ₯Ό μ
λ ₯ν΄μ£ΌμΈμ.")
|
| 29 |
+
|
| 30 |
+
if randomize_seed:
|
| 31 |
+
seed = np.random.randint(0, 2**31)
|
| 32 |
+
|
| 33 |
+
# Move to GPU (ZeroGPU allocated)
|
| 34 |
+
pipe.to("cuda")
|
| 35 |
+
|
| 36 |
+
img = Image.fromarray(input_image).convert("RGB")
|
| 37 |
+
|
| 38 |
+
result = pipe(
|
| 39 |
+
img,
|
| 40 |
+
prompt,
|
| 41 |
+
negative_prompt=negative_prompt,
|
| 42 |
+
guidance_scale=guidance_scale,
|
| 43 |
+
num_inference_steps=int(num_inference_steps),
|
| 44 |
+
num_images_per_prompt=1,
|
| 45 |
+
generator=torch.Generator("cpu").manual_seed(int(seed)),
|
| 46 |
+
).images[0]
|
| 47 |
+
|
| 48 |
+
return result, seed
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
# ========== Gradio UI ==========
|
| 52 |
+
css = """
|
| 53 |
+
#col-main { max-width: 1200px; margin: 0 auto; }
|
| 54 |
+
.title-text { text-align: center; margin-bottom: 0.5em; }
|
| 55 |
+
footer { display: none !important; }
|
| 56 |
+
"""
|
| 57 |
+
|
| 58 |
+
with gr.Blocks(css=css, title="LongCat Image Edit Turbo", theme=gr.themes.Soft()) as demo:
|
| 59 |
+
gr.Markdown(
|
| 60 |
+
"""
|
| 61 |
+
<div class="title-text">
|
| 62 |
+
|
| 63 |
+
# π± LongCat Image Edit Turbo
|
| 64 |
+
**Meituan LongCat** β High-quality image editing with only **8 inference steps**
|
| 65 |
+
|
| 66 |
+
</div>
|
| 67 |
+
""",
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
gr.Markdown(
|
| 71 |
+
"""
|
| 72 |
+
> π‘ **Text Rendering Tip**: Wrap target text in quotes β `Change the text to 'Hello World'`
|
| 73 |
+
> π Supports **Chinese & English** prompts (δΈζ/θ±ζ ν둬ννΈ μ§μ)
|
| 74 |
+
"""
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
with gr.Row(elem_id="col-main"):
|
| 78 |
+
with gr.Column(scale=1):
|
| 79 |
+
input_image = gr.Image(
|
| 80 |
+
label="π· Input Image",
|
| 81 |
+
type="numpy",
|
| 82 |
+
height=512,
|
| 83 |
+
)
|
| 84 |
+
prompt = gr.Textbox(
|
| 85 |
+
label="βοΈ Editing Prompt",
|
| 86 |
+
placeholder="e.g. ε°η«εζη / Add sunglasses / Change background to beach",
|
| 87 |
+
lines=2,
|
| 88 |
+
)
|
| 89 |
+
negative_prompt = gr.Textbox(
|
| 90 |
+
label="π« Negative Prompt (optional)",
|
| 91 |
+
placeholder="e.g. blurry, low quality, distorted",
|
| 92 |
+
lines=1,
|
| 93 |
+
)
|
| 94 |
+
|
| 95 |
+
with gr.Accordion("βοΈ Advanced Settings", open=False):
|
| 96 |
+
with gr.Row():
|
| 97 |
+
guidance_scale = gr.Slider(
|
| 98 |
+
label="Guidance Scale",
|
| 99 |
+
minimum=0.0,
|
| 100 |
+
maximum=5.0,
|
| 101 |
+
value=1.0,
|
| 102 |
+
step=0.1,
|
| 103 |
+
)
|
| 104 |
+
num_steps = gr.Slider(
|
| 105 |
+
label="Inference Steps",
|
| 106 |
+
minimum=4,
|
| 107 |
+
maximum=20,
|
| 108 |
+
value=8,
|
| 109 |
+
step=1,
|
| 110 |
+
)
|
| 111 |
+
with gr.Row():
|
| 112 |
+
seed = gr.Number(label="Seed", value=43, precision=0)
|
| 113 |
+
randomize_seed = gr.Checkbox(label="Randomize Seed", value=True)
|
| 114 |
+
|
| 115 |
+
run_btn = gr.Button("π Edit Image", variant="primary", size="lg")
|
| 116 |
+
|
| 117 |
+
with gr.Column(scale=1):
|
| 118 |
+
output_image = gr.Image(label="π¨ Edited Result", type="pil", height=512)
|
| 119 |
+
used_seed = gr.Number(label="Used Seed", interactive=False)
|
| 120 |
+
|
| 121 |
+
# Examples
|
| 122 |
+
gr.Examples(
|
| 123 |
+
examples=[
|
| 124 |
+
["ε°η«εζη"],
|
| 125 |
+
["Add sunglasses to the person"],
|
| 126 |
+
["Change the background to a beach"],
|
| 127 |
+
["Make it look like a watercolor painting"],
|
| 128 |
+
["ζζεζΉζ 'Hello World'"],
|
| 129 |
+
["Turn it into an anime style illustration"],
|
| 130 |
+
],
|
| 131 |
+
inputs=[prompt],
|
| 132 |
+
label="π‘ Example Prompts",
|
| 133 |
+
)
|
| 134 |
+
|
| 135 |
+
run_btn.click(
|
| 136 |
+
fn=edit_image,
|
| 137 |
+
inputs=[input_image, prompt, negative_prompt, guidance_scale, num_steps, seed, randomize_seed],
|
| 138 |
+
outputs=[output_image, used_seed],
|
| 139 |
+
)
|
| 140 |
+
|
| 141 |
+
gr.Markdown(
|
| 142 |
+
"""
|
| 143 |
+
---
|
| 144 |
+
**Model**: [meituan-longcat/LongCat-Image-Edit-Turbo](https://huggingface.co/meituan-longcat/LongCat-Image-Edit-Turbo) |
|
| 145 |
+
**Paper**: [arxiv:2512.07584](https://arxiv.org/abs/2512.07584) |
|
| 146 |
+
**License**: Apache-2.0
|
| 147 |
+
"""
|
| 148 |
+
)
|
| 149 |
+
|
| 150 |
+
demo.launch()
|