Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
import os
|
| 2 |
import gc
|
| 3 |
import inspect
|
| 4 |
import gradio as gr
|
|
@@ -7,67 +6,54 @@ import torch
|
|
| 7 |
from PIL import Image
|
| 8 |
|
| 9 |
from qwenimage.pipeline_qwenimage_edit_plus import QwenImageEditPlusPipeline
|
|
|
|
| 10 |
|
| 11 |
MODEL_ID = "ovedrive/Qwen-Image-Edit-2511-4bit"
|
| 12 |
|
| 13 |
-
# Global pipeline (lazy-loaded)
|
| 14 |
pipe = None
|
| 15 |
|
| 16 |
|
| 17 |
-
def _patch_transformer_forward_ignore_image_rotary_emb(
|
| 18 |
"""
|
| 19 |
-
|
| 20 |
-
|
| 21 |
"""
|
| 22 |
-
if not hasattr(p, "transformer") or p.transformer is None:
|
| 23 |
-
return
|
| 24 |
-
|
| 25 |
try:
|
| 26 |
-
sig = inspect.signature(
|
| 27 |
if "image_rotary_emb" in sig.parameters:
|
| 28 |
-
# Já suporta; não precisa patch.
|
| 29 |
print("[patch] transformer.forward já aceita image_rotary_emb (sem patch).")
|
| 30 |
return
|
| 31 |
|
| 32 |
-
orig_forward =
|
| 33 |
|
| 34 |
def wrapped_forward(*args, **kwargs):
|
| 35 |
-
|
| 36 |
-
kwargs.pop("image_rotary_emb", None)
|
| 37 |
return orig_forward(*args, **kwargs)
|
| 38 |
|
| 39 |
-
|
| 40 |
print("[patch] Aplicado: ignorando kwarg image_rotary_emb no transformer.forward().")
|
| 41 |
-
|
| 42 |
except Exception as e:
|
| 43 |
-
# Se der algum erro no patch, melhor logar e seguir (vai estourar depois se for necessário).
|
| 44 |
print(f"[patch] Falhou ao aplicar patch image_rotary_emb: {e}")
|
| 45 |
|
| 46 |
|
| 47 |
def _low_mem_tweaks(p):
|
| 48 |
-
# Reduz pico e consumo
|
| 49 |
try:
|
| 50 |
p.enable_attention_slicing()
|
| 51 |
except Exception:
|
| 52 |
pass
|
| 53 |
-
|
| 54 |
try:
|
| 55 |
p.enable_vae_slicing()
|
| 56 |
except Exception:
|
| 57 |
pass
|
| 58 |
-
|
| 59 |
try:
|
| 60 |
p.set_progress_bar_config(disable=True)
|
| 61 |
except Exception:
|
| 62 |
pass
|
| 63 |
-
|
| 64 |
-
# Segurança: nada deve requerer grad em inferência
|
| 65 |
try:
|
| 66 |
if hasattr(p, "transformer") and p.transformer is not None:
|
| 67 |
p.transformer.requires_grad_(False)
|
| 68 |
except Exception:
|
| 69 |
pass
|
| 70 |
-
|
| 71 |
try:
|
| 72 |
if hasattr(p, "vae") and p.vae is not None:
|
| 73 |
p.vae.requires_grad_(False)
|
|
@@ -77,38 +63,32 @@ def _low_mem_tweaks(p):
|
|
| 77 |
|
| 78 |
@spaces.GPU
|
| 79 |
def load_pipe():
|
| 80 |
-
"""
|
| 81 |
-
Carrega o pipeline SOMENTE quando a GPU já está anexada (ZeroGPU).
|
| 82 |
-
"""
|
| 83 |
global pipe
|
| 84 |
if pipe is not None:
|
| 85 |
return pipe
|
| 86 |
|
| 87 |
if not torch.cuda.is_available():
|
| 88 |
-
raise RuntimeError(
|
| 89 |
-
"GPU ainda não foi anexada (ZeroGPU). "
|
| 90 |
-
"Não carregue o modelo fora do @spaces.GPU."
|
| 91 |
-
)
|
| 92 |
|
| 93 |
torch.cuda.empty_cache()
|
| 94 |
gc.collect()
|
| 95 |
|
| 96 |
-
# Carrega o
|
| 97 |
-
|
| 98 |
MODEL_ID,
|
|
|
|
| 99 |
torch_dtype=torch.float16,
|
| 100 |
-
)
|
| 101 |
|
| 102 |
-
|
| 103 |
-
try:
|
| 104 |
-
pipe = pipe.to("cuda")
|
| 105 |
-
except Exception:
|
| 106 |
-
pass
|
| 107 |
|
| 108 |
-
#
|
| 109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
|
| 111 |
-
# Tweaks de memória
|
| 112 |
_low_mem_tweaks(pipe)
|
| 113 |
|
| 114 |
return pipe
|
|
@@ -123,15 +103,14 @@ def infer(image: Image.Image, prompt: str):
|
|
| 123 |
|
| 124 |
p = load_pipe()
|
| 125 |
|
| 126 |
-
# limpeza antes
|
| 127 |
torch.cuda.empty_cache()
|
| 128 |
gc.collect()
|
| 129 |
|
| 130 |
image = image.convert("RGB")
|
| 131 |
|
| 132 |
-
#
|
| 133 |
w, h = image.size
|
| 134 |
-
max_side = 768 #
|
| 135 |
scale = min(max_side / max(w, h), 1.0)
|
| 136 |
nw = max(64, int((w * scale) // 8 * 8))
|
| 137 |
nh = max(64, int((h * scale) // 8 * 8))
|
|
@@ -139,19 +118,17 @@ def infer(image: Image.Image, prompt: str):
|
|
| 139 |
|
| 140 |
negative_prompt = "worst quality, low quality, text, watermark, blurry, artifacts"
|
| 141 |
|
| 142 |
-
# inferência sem grad
|
| 143 |
with torch.inference_mode():
|
| 144 |
out = p(
|
| 145 |
-
image=[image],
|
| 146 |
prompt=prompt,
|
| 147 |
negative_prompt=negative_prompt,
|
| 148 |
width=nw,
|
| 149 |
height=nh,
|
| 150 |
-
num_inference_steps=4,
|
| 151 |
-
true_cfg_scale=1.0,
|
| 152 |
).images[0]
|
| 153 |
|
| 154 |
-
# limpeza pós
|
| 155 |
torch.cuda.empty_cache()
|
| 156 |
gc.collect()
|
| 157 |
|
|
|
|
|
|
|
| 1 |
import gc
|
| 2 |
import inspect
|
| 3 |
import gradio as gr
|
|
|
|
| 6 |
from PIL import Image
|
| 7 |
|
| 8 |
from qwenimage.pipeline_qwenimage_edit_plus import QwenImageEditPlusPipeline
|
| 9 |
+
from qwenimage.transformer_qwenimage import QwenImageTransformer2DModel
|
| 10 |
|
| 11 |
MODEL_ID = "ovedrive/Qwen-Image-Edit-2511-4bit"
|
| 12 |
|
|
|
|
| 13 |
pipe = None
|
| 14 |
|
| 15 |
|
| 16 |
+
def _patch_transformer_forward_ignore_image_rotary_emb(transformer):
|
| 17 |
"""
|
| 18 |
+
Se o forward do transformer NÃO aceita image_rotary_emb,
|
| 19 |
+
removemos esse kwarg antes de chamar o forward real.
|
| 20 |
"""
|
|
|
|
|
|
|
|
|
|
| 21 |
try:
|
| 22 |
+
sig = inspect.signature(transformer.forward)
|
| 23 |
if "image_rotary_emb" in sig.parameters:
|
|
|
|
| 24 |
print("[patch] transformer.forward já aceita image_rotary_emb (sem patch).")
|
| 25 |
return
|
| 26 |
|
| 27 |
+
orig_forward = transformer.forward
|
| 28 |
|
| 29 |
def wrapped_forward(*args, **kwargs):
|
| 30 |
+
kwargs.pop("image_rotary_emb", None)
|
|
|
|
| 31 |
return orig_forward(*args, **kwargs)
|
| 32 |
|
| 33 |
+
transformer.forward = wrapped_forward
|
| 34 |
print("[patch] Aplicado: ignorando kwarg image_rotary_emb no transformer.forward().")
|
|
|
|
| 35 |
except Exception as e:
|
|
|
|
| 36 |
print(f"[patch] Falhou ao aplicar patch image_rotary_emb: {e}")
|
| 37 |
|
| 38 |
|
| 39 |
def _low_mem_tweaks(p):
|
|
|
|
| 40 |
try:
|
| 41 |
p.enable_attention_slicing()
|
| 42 |
except Exception:
|
| 43 |
pass
|
|
|
|
| 44 |
try:
|
| 45 |
p.enable_vae_slicing()
|
| 46 |
except Exception:
|
| 47 |
pass
|
|
|
|
| 48 |
try:
|
| 49 |
p.set_progress_bar_config(disable=True)
|
| 50 |
except Exception:
|
| 51 |
pass
|
|
|
|
|
|
|
| 52 |
try:
|
| 53 |
if hasattr(p, "transformer") and p.transformer is not None:
|
| 54 |
p.transformer.requires_grad_(False)
|
| 55 |
except Exception:
|
| 56 |
pass
|
|
|
|
| 57 |
try:
|
| 58 |
if hasattr(p, "vae") and p.vae is not None:
|
| 59 |
p.vae.requires_grad_(False)
|
|
|
|
| 63 |
|
| 64 |
@spaces.GPU
|
| 65 |
def load_pipe():
|
|
|
|
|
|
|
|
|
|
| 66 |
global pipe
|
| 67 |
if pipe is not None:
|
| 68 |
return pipe
|
| 69 |
|
| 70 |
if not torch.cuda.is_available():
|
| 71 |
+
raise RuntimeError("GPU não anexada. Em ZeroGPU, carregue apenas dentro de @spaces.GPU.")
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
torch.cuda.empty_cache()
|
| 74 |
gc.collect()
|
| 75 |
|
| 76 |
+
# 1) Carrega o TRANSFORMER explicitamente do qwenimage (evita pegar o do diffusers)
|
| 77 |
+
transformer = QwenImageTransformer2DModel.from_pretrained(
|
| 78 |
MODEL_ID,
|
| 79 |
+
subfolder="transformer",
|
| 80 |
torch_dtype=torch.float16,
|
| 81 |
+
).to("cuda")
|
| 82 |
|
| 83 |
+
_patch_transformer_forward_ignore_image_rotary_emb(transformer)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
+
# 2) Carrega o pipeline, INJETANDO o transformer compatível
|
| 86 |
+
pipe = QwenImageEditPlusPipeline.from_pretrained(
|
| 87 |
+
MODEL_ID,
|
| 88 |
+
transformer=transformer,
|
| 89 |
+
torch_dtype=torch.float16,
|
| 90 |
+
).to("cuda")
|
| 91 |
|
|
|
|
| 92 |
_low_mem_tweaks(pipe)
|
| 93 |
|
| 94 |
return pipe
|
|
|
|
| 103 |
|
| 104 |
p = load_pipe()
|
| 105 |
|
|
|
|
| 106 |
torch.cuda.empty_cache()
|
| 107 |
gc.collect()
|
| 108 |
|
| 109 |
image = image.convert("RGB")
|
| 110 |
|
| 111 |
+
# baixa resolução pra economizar
|
| 112 |
w, h = image.size
|
| 113 |
+
max_side = 768 # se OOM, use 512
|
| 114 |
scale = min(max_side / max(w, h), 1.0)
|
| 115 |
nw = max(64, int((w * scale) // 8 * 8))
|
| 116 |
nh = max(64, int((h * scale) // 8 * 8))
|
|
|
|
| 118 |
|
| 119 |
negative_prompt = "worst quality, low quality, text, watermark, blurry, artifacts"
|
| 120 |
|
|
|
|
| 121 |
with torch.inference_mode():
|
| 122 |
out = p(
|
| 123 |
+
image=[image], # mantém como lista (pipeline suporta multi-imagem)
|
| 124 |
prompt=prompt,
|
| 125 |
negative_prompt=negative_prompt,
|
| 126 |
width=nw,
|
| 127 |
height=nh,
|
| 128 |
+
num_inference_steps=4, # baixo pra economizar
|
| 129 |
+
true_cfg_scale=1.0, # <=1 desliga CFG (mais leve)
|
| 130 |
).images[0]
|
| 131 |
|
|
|
|
| 132 |
torch.cuda.empty_cache()
|
| 133 |
gc.collect()
|
| 134 |
|