File size: 5,871 Bytes
bb25bfc
 
 
 
 
208ffe0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bb25bfc
 
208ffe0
 
bb25bfc
 
 
 
208ffe0
bb25bfc
 
 
 
 
 
 
 
 
 
 
 
 
208ffe0
bb25bfc
208ffe0
bb25bfc
 
208ffe0
bb25bfc
208ffe0
 
 
bb25bfc
 
 
 
 
 
 
 
 
 
 
 
 
208ffe0
bb25bfc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import gradio as gr
import spaces
import torch
import numpy as np
from PIL import Image

# ============================================================
# Fix: Monkey-patch transformers video_processing_auto bug
# Latest transformers has a bug where VIDEO_PROCESSOR_MAPPING
# is None, causing TypeError in video_processor_class_from_name
# ============================================================
try:
    from transformers.models.auto import video_processing_auto
    _original_func = video_processing_auto.video_processor_class_from_name

    def _patched_video_processor_class_from_name(class_name):
        try:
            return _original_func(class_name)
        except TypeError:
            # VIDEO_PROCESSOR_MAPPING_NAMES is None in some transformers versions
            return None

    video_processing_auto.video_processor_class_from_name = _patched_video_processor_class_from_name
    print("[PATCH] video_processor_class_from_name patched successfully")
except Exception as e:
    print(f"[PATCH] Could not patch video_processing_auto: {e}")

from diffusers import LongCatImageEditPipeline

# --- Load pipeline on CPU at init time ---
print("Loading LongCat-Image-Edit-Turbo pipeline...")
pipe = LongCatImageEditPipeline.from_pretrained(
    "meituan-longcat/LongCat-Image-Edit-Turbo",
    torch_dtype=torch.bfloat16,
)
print("Pipeline loaded on CPU.")


@spaces.GPU(duration=120)
def edit_image(
    input_image,
    prompt,
    negative_prompt="",
    guidance_scale=1.0,
    num_inference_steps=8,
    seed=43,
    randomize_seed=True,
):
    if input_image is None:
        raise gr.Error("이미지λ₯Ό μ—…λ‘œλ“œν•΄μ£Όμ„Έμš” / Please upload an image.")
    if not prompt.strip():
        raise gr.Error("νŽΈμ§‘ ν”„λ‘¬ν”„νŠΈλ₯Ό μž…λ ₯ν•΄μ£Όμ„Έμš” / Please enter an editing prompt.")

    if randomize_seed:
        seed = int(np.random.randint(0, 2**31))

    # Enable CPU offload β€” model (~29GB) exceeds A10G VRAM (24GB)
    # Safe to call multiple times; ensures proper device mapping with ZeroGPU
    pipe.enable_model_cpu_offload()

    img = Image.fromarray(input_image).convert("RGB")

    result = pipe(
        img,
        prompt,
        negative_prompt=negative_prompt,
        guidance_scale=guidance_scale,
        num_inference_steps=int(num_inference_steps),
        num_images_per_prompt=1,
        generator=torch.Generator("cpu").manual_seed(int(seed)),
    ).images[0]

    return result, int(seed)


# ========== Gradio UI ==========
css = """
#col-main { max-width: 1200px; margin: 0 auto; }
.title-text { text-align: center; margin-bottom: 0.5em; }
footer { display: none !important; }
"""

with gr.Blocks(css=css, title="LongCat Image Edit Turbo", theme=gr.themes.Soft()) as demo:
    gr.Markdown(
        """
        <div class="title-text">
        
        # 🐱 LongCat Image Edit Turbo
        **Meituan LongCat** β€” High-quality image editing with only **8 inference steps**
        
        </div>
        """,
    )

    gr.Markdown(
        """
        > πŸ’‘ **Text Rendering Tip**: Wrap target text in quotes β†’ `Change the text to 'Hello World'`  
        > 🌐 Supports **Chinese & English** prompts (δΈ­ζ–‡/θ‹±ζ–‡ ν”„λ‘¬ν”„νŠΈ 지원)
        """
    )

    with gr.Row(elem_id="col-main"):
        with gr.Column(scale=1):
            input_image = gr.Image(
                label="πŸ“· Input Image",
                type="numpy",
                height=512,
            )
            prompt = gr.Textbox(
                label="✏️ Editing Prompt",
                placeholder="e.g. ε°†ηŒ«ε˜ζˆη‹— / Add sunglasses / Change background to beach",
                lines=2,
            )
            negative_prompt = gr.Textbox(
                label="🚫 Negative Prompt (optional)",
                placeholder="e.g. blurry, low quality, distorted",
                lines=1,
            )

            with gr.Accordion("βš™οΈ Advanced Settings", open=False):
                with gr.Row():
                    guidance_scale = gr.Slider(
                        label="Guidance Scale",
                        minimum=0.0,
                        maximum=5.0,
                        value=1.0,
                        step=0.1,
                    )
                    num_steps = gr.Slider(
                        label="Inference Steps",
                        minimum=4,
                        maximum=20,
                        value=8,
                        step=1,
                    )
                with gr.Row():
                    seed = gr.Number(label="Seed", value=43, precision=0)
                    randomize_seed = gr.Checkbox(label="Randomize Seed", value=True)

            run_btn = gr.Button("πŸš€ Edit Image", variant="primary", size="lg")

        with gr.Column(scale=1):
            output_image = gr.Image(label="🎨 Edited Result", type="pil", height=512)
            used_seed = gr.Number(label="Used Seed", interactive=False)

    # Examples
    gr.Examples(
        examples=[
            ["ε°†ηŒ«ε˜ζˆη‹—"],
            ["Add sunglasses to the person"],
            ["Change the background to a beach"],
            ["Make it look like a watercolor painting"],
            ["ζŠŠζ–‡ε­—ζ”Ήζˆ 'Hello World'"],
            ["Turn it into an anime style illustration"],
        ],
        inputs=[prompt],
        label="πŸ’‘ Example Prompts",
    )

    run_btn.click(
        fn=edit_image,
        inputs=[input_image, prompt, negative_prompt, guidance_scale, num_steps, seed, randomize_seed],
        outputs=[output_image, used_seed],
    )

    gr.Markdown(
        """
        ---
        **Model**: [meituan-longcat/LongCat-Image-Edit-Turbo](https://huggingface.co/meituan-longcat/LongCat-Image-Edit-Turbo) | 
        **Paper**: [arxiv:2512.07584](https://arxiv.org/abs/2512.07584) | 
        **License**: Apache-2.0
        """
    )

demo.launch()