Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,9 +10,10 @@ from transformers import AutoTokenizer, Qwen3ForCausalLM
|
|
| 10 |
from controlnet_aux.processor import Processor
|
| 11 |
from PIL import Image
|
| 12 |
from safetensors.torch import load_file
|
|
|
|
| 13 |
|
| 14 |
# Import pipeline and model
|
| 15 |
-
# Ensure videox_fun is in your
|
| 16 |
from videox_fun.pipeline import ZImageControlPipeline
|
| 17 |
from videox_fun.models import ZImageControlTransformer2DModel
|
| 18 |
|
|
@@ -24,23 +25,24 @@ except ImportError:
|
|
| 24 |
def polish_prompt(prompt):
|
| 25 |
return prompt
|
| 26 |
|
| 27 |
-
# Configuration
|
| 28 |
MAX_SEED = np.iinfo(np.int32).max
|
| 29 |
MAX_IMAGE_SIZE = 1280
|
| 30 |
|
| 31 |
-
#
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
|
| 36 |
-
print("Loading Z-Image Turbo
|
| 37 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 38 |
weight_dtype = torch.bfloat16
|
| 39 |
|
| 40 |
-
# 1. Load Transformer
|
| 41 |
print("Initializing Transformer...")
|
|
|
|
| 42 |
transformer = ZImageControlTransformer2DModel.from_pretrained(
|
| 43 |
-
|
| 44 |
subfolder="transformer",
|
| 45 |
transformer_additional_kwargs={
|
| 46 |
"control_layers_places": [0, 5, 10, 15, 20, 25],
|
|
@@ -48,8 +50,22 @@ transformer = ZImageControlTransformer2DModel.from_pretrained(
|
|
| 48 |
},
|
| 49 |
).to(device, weight_dtype)
|
| 50 |
|
| 51 |
-
# 2. Load ControlNet Weights
|
| 52 |
-
if
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
print(f"Loading ControlNet weights from {CONTROLNET_WEIGHTS}")
|
| 54 |
try:
|
| 55 |
state_dict = load_file(CONTROLNET_WEIGHTS)
|
|
@@ -61,32 +77,32 @@ if os.path.exists(CONTROLNET_WEIGHTS):
|
|
| 61 |
except Exception as e:
|
| 62 |
print(f"Error loading ControlNet weights: {e}")
|
| 63 |
else:
|
| 64 |
-
print(
|
| 65 |
|
| 66 |
-
# 3. Load
|
| 67 |
-
print("Loading
|
| 68 |
vae = AutoencoderKL.from_pretrained(
|
| 69 |
-
|
| 70 |
subfolder="vae",
|
| 71 |
).to(device, weight_dtype)
|
| 72 |
|
| 73 |
tokenizer = AutoTokenizer.from_pretrained(
|
| 74 |
-
|
| 75 |
subfolder="tokenizer"
|
| 76 |
)
|
| 77 |
|
| 78 |
text_encoder = Qwen3ForCausalLM.from_pretrained(
|
| 79 |
-
|
| 80 |
subfolder="text_encoder",
|
| 81 |
torch_dtype=weight_dtype,
|
| 82 |
).to(device)
|
| 83 |
|
| 84 |
scheduler = FlowMatchEulerDiscreteScheduler.from_pretrained(
|
| 85 |
-
|
| 86 |
subfolder="scheduler"
|
| 87 |
)
|
| 88 |
|
| 89 |
-
# 4. Assemble Pipeline
|
| 90 |
pipe = ZImageControlPipeline(
|
| 91 |
vae=vae,
|
| 92 |
tokenizer=tokenizer,
|
|
@@ -284,7 +300,7 @@ button.primary:hover {
|
|
| 284 |
}
|
| 285 |
"""
|
| 286 |
|
| 287 |
-
with gr.Blocks(title="Z-Image Turbo ControlNet") as demo:
|
| 288 |
|
| 289 |
gr.HTML("""
|
| 290 |
<div class="header-container">
|
|
@@ -369,5 +385,4 @@ with gr.Blocks(title="Z-Image Turbo ControlNet") as demo:
|
|
| 369 |
)
|
| 370 |
|
| 371 |
if __name__ == "__main__":
|
| 372 |
-
demo.launch(share=False
|
| 373 |
-
css=apple_css)
|
|
|
|
| 10 |
from controlnet_aux.processor import Processor
|
| 11 |
from PIL import Image
|
| 12 |
from safetensors.torch import load_file
|
| 13 |
+
from huggingface_hub import hf_hub_download
|
| 14 |
|
| 15 |
# Import pipeline and model
|
| 16 |
+
# Ensure the videox_fun folder is in your current directory
|
| 17 |
from videox_fun.pipeline import ZImageControlPipeline
|
| 18 |
from videox_fun.models import ZImageControlTransformer2DModel
|
| 19 |
|
|
|
|
| 25 |
def polish_prompt(prompt):
|
| 26 |
return prompt
|
| 27 |
|
| 28 |
+
# --- Configuration & Paths ---
|
| 29 |
MAX_SEED = np.iinfo(np.int32).max
|
| 30 |
MAX_IMAGE_SIZE = 1280
|
| 31 |
|
| 32 |
+
# Hugging Face Repo IDs
|
| 33 |
+
MODEL_REPO = "Tongyi-MAI/Z-Image-Turbo"
|
| 34 |
+
CONTROLNET_REPO = "alibaba-pai/Z-Image-Turbo-Fun-Controlnet-Union"
|
| 35 |
+
CONTROLNET_FILENAME = "Z-Image-Turbo-Fun-Controlnet-Union.safetensors"
|
| 36 |
|
| 37 |
+
print(f"Loading Z-Image Turbo from {MODEL_REPO}...")
|
| 38 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 39 |
weight_dtype = torch.bfloat16
|
| 40 |
|
| 41 |
+
# --- 1. Load Transformer ---
|
| 42 |
print("Initializing Transformer...")
|
| 43 |
+
# We load the config and model structure from the main Hub Repo
|
| 44 |
transformer = ZImageControlTransformer2DModel.from_pretrained(
|
| 45 |
+
MODEL_REPO,
|
| 46 |
subfolder="transformer",
|
| 47 |
transformer_additional_kwargs={
|
| 48 |
"control_layers_places": [0, 5, 10, 15, 20, 25],
|
|
|
|
| 50 |
},
|
| 51 |
).to(device, weight_dtype)
|
| 52 |
|
| 53 |
+
# --- 2. Download & Load ControlNet Weights ---
|
| 54 |
+
# Check if weights exist locally; if not, download them
|
| 55 |
+
if not os.path.exists(CONTROLNET_FILENAME):
|
| 56 |
+
print(f"Downloading ControlNet weights from {CONTROLNET_REPO}...")
|
| 57 |
+
try:
|
| 58 |
+
CONTROLNET_WEIGHTS = hf_hub_download(
|
| 59 |
+
repo_id=CONTROLNET_REPO,
|
| 60 |
+
filename=CONTROLNET_FILENAME
|
| 61 |
+
)
|
| 62 |
+
except Exception as e:
|
| 63 |
+
print(f"Failed to download ControlNet weights: {e}")
|
| 64 |
+
CONTROLNET_WEIGHTS = None
|
| 65 |
+
else:
|
| 66 |
+
CONTROLNET_WEIGHTS = CONTROLNET_FILENAME
|
| 67 |
+
|
| 68 |
+
if CONTROLNET_WEIGHTS:
|
| 69 |
print(f"Loading ControlNet weights from {CONTROLNET_WEIGHTS}")
|
| 70 |
try:
|
| 71 |
state_dict = load_file(CONTROLNET_WEIGHTS)
|
|
|
|
| 77 |
except Exception as e:
|
| 78 |
print(f"Error loading ControlNet weights: {e}")
|
| 79 |
else:
|
| 80 |
+
print("Warning: Running without explicit ControlNet weights.")
|
| 81 |
|
| 82 |
+
# --- 3. Load Core Components ---
|
| 83 |
+
print("Loading VAE, Tokenizer, and Text Encoder...")
|
| 84 |
vae = AutoencoderKL.from_pretrained(
|
| 85 |
+
MODEL_REPO,
|
| 86 |
subfolder="vae",
|
| 87 |
).to(device, weight_dtype)
|
| 88 |
|
| 89 |
tokenizer = AutoTokenizer.from_pretrained(
|
| 90 |
+
MODEL_REPO,
|
| 91 |
subfolder="tokenizer"
|
| 92 |
)
|
| 93 |
|
| 94 |
text_encoder = Qwen3ForCausalLM.from_pretrained(
|
| 95 |
+
MODEL_REPO,
|
| 96 |
subfolder="text_encoder",
|
| 97 |
torch_dtype=weight_dtype,
|
| 98 |
).to(device)
|
| 99 |
|
| 100 |
scheduler = FlowMatchEulerDiscreteScheduler.from_pretrained(
|
| 101 |
+
MODEL_REPO,
|
| 102 |
subfolder="scheduler"
|
| 103 |
)
|
| 104 |
|
| 105 |
+
# --- 4. Assemble Pipeline ---
|
| 106 |
pipe = ZImageControlPipeline(
|
| 107 |
vae=vae,
|
| 108 |
tokenizer=tokenizer,
|
|
|
|
| 300 |
}
|
| 301 |
"""
|
| 302 |
|
| 303 |
+
with gr.Blocks(title="Z-Image Turbo ControlNet", css=apple_css) as demo:
|
| 304 |
|
| 305 |
gr.HTML("""
|
| 306 |
<div class="header-container">
|
|
|
|
| 385 |
)
|
| 386 |
|
| 387 |
if __name__ == "__main__":
|
| 388 |
+
demo.launch(share=False)
|
|
|