Update app.py
Browse files
app.py
CHANGED
|
@@ -3,13 +3,37 @@ import spaces
|
|
| 3 |
import torch
|
| 4 |
import numpy as np
|
| 5 |
from PIL import 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)
|
|
@@ -23,15 +47,16 @@ def edit_image(
|
|
| 23 |
randomize_seed=True,
|
| 24 |
):
|
| 25 |
if input_image is None:
|
| 26 |
-
raise gr.Error("
|
| 27 |
if not prompt.strip():
|
| 28 |
-
raise gr.Error("
|
| 29 |
|
| 30 |
if randomize_seed:
|
| 31 |
-
seed = np.random.randint(0, 2**31)
|
| 32 |
|
| 33 |
-
#
|
| 34 |
-
|
|
|
|
| 35 |
|
| 36 |
img = Image.fromarray(input_image).convert("RGB")
|
| 37 |
|
|
@@ -45,7 +70,7 @@ def edit_image(
|
|
| 45 |
generator=torch.Generator("cpu").manual_seed(int(seed)),
|
| 46 |
).images[0]
|
| 47 |
|
| 48 |
-
return result, seed
|
| 49 |
|
| 50 |
|
| 51 |
# ========== Gradio UI ==========
|
|
|
|
| 3 |
import torch
|
| 4 |
import numpy as np
|
| 5 |
from PIL import Image
|
| 6 |
+
|
| 7 |
+
# ============================================================
|
| 8 |
+
# Fix: Monkey-patch transformers video_processing_auto bug
|
| 9 |
+
# Latest transformers has a bug where VIDEO_PROCESSOR_MAPPING
|
| 10 |
+
# is None, causing TypeError in video_processor_class_from_name
|
| 11 |
+
# ============================================================
|
| 12 |
+
try:
|
| 13 |
+
from transformers.models.auto import video_processing_auto
|
| 14 |
+
_original_func = video_processing_auto.video_processor_class_from_name
|
| 15 |
+
|
| 16 |
+
def _patched_video_processor_class_from_name(class_name):
|
| 17 |
+
try:
|
| 18 |
+
return _original_func(class_name)
|
| 19 |
+
except TypeError:
|
| 20 |
+
# VIDEO_PROCESSOR_MAPPING_NAMES is None in some transformers versions
|
| 21 |
+
return None
|
| 22 |
+
|
| 23 |
+
video_processing_auto.video_processor_class_from_name = _patched_video_processor_class_from_name
|
| 24 |
+
print("[PATCH] video_processor_class_from_name patched successfully")
|
| 25 |
+
except Exception as e:
|
| 26 |
+
print(f"[PATCH] Could not patch video_processing_auto: {e}")
|
| 27 |
+
|
| 28 |
from diffusers import LongCatImageEditPipeline
|
| 29 |
|
| 30 |
+
# --- Load pipeline on CPU at init time ---
|
| 31 |
+
print("Loading LongCat-Image-Edit-Turbo pipeline...")
|
| 32 |
pipe = LongCatImageEditPipeline.from_pretrained(
|
| 33 |
"meituan-longcat/LongCat-Image-Edit-Turbo",
|
| 34 |
torch_dtype=torch.bfloat16,
|
| 35 |
)
|
| 36 |
+
print("Pipeline loaded on CPU.")
|
| 37 |
|
| 38 |
|
| 39 |
@spaces.GPU(duration=120)
|
|
|
|
| 47 |
randomize_seed=True,
|
| 48 |
):
|
| 49 |
if input_image is None:
|
| 50 |
+
raise gr.Error("이미지를 업로드해주세요 / Please upload an image.")
|
| 51 |
if not prompt.strip():
|
| 52 |
+
raise gr.Error("편집 프롬프트를 입력해주세요 / Please enter an editing prompt.")
|
| 53 |
|
| 54 |
if randomize_seed:
|
| 55 |
+
seed = int(np.random.randint(0, 2**31))
|
| 56 |
|
| 57 |
+
# Enable CPU offload — model (~29GB) exceeds A10G VRAM (24GB)
|
| 58 |
+
# Safe to call multiple times; ensures proper device mapping with ZeroGPU
|
| 59 |
+
pipe.enable_model_cpu_offload()
|
| 60 |
|
| 61 |
img = Image.fromarray(input_image).convert("RGB")
|
| 62 |
|
|
|
|
| 70 |
generator=torch.Generator("cpu").manual_seed(int(seed)),
|
| 71 |
).images[0]
|
| 72 |
|
| 73 |
+
return result, int(seed)
|
| 74 |
|
| 75 |
|
| 76 |
# ========== Gradio UI ==========
|