Spaces:
Running
on
Zero
Running
on
Zero
Commit
·
660c6ca
0
Parent(s):
first commit
Browse files- .gitattributes +35 -0
- .gitignore +14 -0
- README.md +114 -0
- app.py +755 -0
- infer.py +77 -0
- pipeline_z_image_mod.py +214 -0
- prompt_check.py +30 -0
- requirements.txt +17 -0
- requirements_local.txt +21 -0
- style.css +246 -0
.gitattributes
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
| 2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
| 3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
| 4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
| 5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
| 6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
| 7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
| 8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
| 9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
| 10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
| 11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
| 12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
| 13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
| 14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
| 15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
| 16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
| 17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
| 18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
| 19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
| 20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
| 21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
| 22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
| 23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
| 24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
| 25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
| 26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
| 27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
| 28 |
+
*.tar filter=lfs diff=lfs merge=lfs -text
|
| 29 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
| 30 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
| 31 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
| 32 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
| 33 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
.gitignore
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
__pycache__/
|
| 2 |
+
*.py[cod]
|
| 3 |
+
mod_tests/
|
| 4 |
+
/.vs
|
| 5 |
+
.vscode/
|
| 6 |
+
.idea/
|
| 7 |
+
venv/
|
| 8 |
+
.venv/
|
| 9 |
+
*.log
|
| 10 |
+
.DS_Store
|
| 11 |
+
.gradio
|
| 12 |
+
download.py
|
| 13 |
+
bk
|
| 14 |
+
outputs/
|
README.md
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Panorama Z-Image Turbo
|
| 3 |
+
emoji: 🏞️
|
| 4 |
+
colorFrom: red
|
| 5 |
+
colorTo: orange
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: 5.50.0
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: true
|
| 10 |
+
license: apache-2.0
|
| 11 |
+
short_description: Create stunning panoramas with Z-Image-Turbo.
|
| 12 |
+
---
|
| 13 |
+
|
| 14 |
+
# Panorama Z-Image Turbo 🏞️✨
|
| 15 |
+
|
| 16 |
+
[](https://huggingface.co/spaces/elismasilva/z-image-panorama)
|
| 17 |
+
|
| 18 |
+
Create stunning, seamless panoramic images by combining multiple distinct scenes with the power of the **Z-Image-Turbo** model. This application uses an advanced "Mixture of Diffusers" tiling pipeline to generate high-resolution compositions from left, center, and right text prompts, even on memory-constrained hardware, thanks to GGUF model support.
|
| 19 |
+
|
| 20 |
+

|
| 21 |
+
|
| 22 |
+
## What is Panorama Z-Image Turbo?
|
| 23 |
+
|
| 24 |
+
Panorama Z-Image Turbo is a creative tool that leverages a sophisticated tiling mechanism to generate a single, wide-format image from three separate text prompts. Instead of stretching a single concept, you can describe different but related scenes for the left, center, and right portions of the image. The pipeline then intelligently generates each part and seamlessly blends them together.
|
| 25 |
+
|
| 26 |
+
This is ideal for:
|
| 27 |
+
* **Creating expansive cityscapes or landscapes:** Describe a bustling street that transitions to a central plaza, which then leads to a quiet park.
|
| 28 |
+
* **Composing complex scenes:** Place different characters or objects side-by-side in a shared, coherent environment.
|
| 29 |
+
* **Generating ultra-wide art:** Create unique, high-resolution images perfect for wallpapers or digital art.
|
| 30 |
+
* **Multi-language creation:** With built-in translation, you can write prompts in English, Korean, or Chinese.
|
| 31 |
+
|
| 32 |
+
The core technology uses a custom `ZImageMoDTilingPipeline` built on the Diffusers library. It has been meticulously adapted to the unique architecture of the `ZImageTransformer2DModel`, correctly handling its 16-channel latent space and specific input tensor format.
|
| 33 |
+
|
| 34 |
+
### Key Features
|
| 35 |
+
* **Multi-Prompt Composition:** Control the left, center, and right of your image with unique prompts.
|
| 36 |
+
* **GGUF Transformer Support:** Utilizes a quantized GGUF version of the transformer, significantly reducing VRAM usage and making it possible to run on consumer GPUs.
|
| 37 |
+
* **Seamless Stitching:** Uses advanced blending methods (Cosine or Gaussian) to eliminate visible seams between tiles.
|
| 38 |
+
* **High-Resolution Output:** Generates images far wider than what a standard pipeline can handle in a single pass.
|
| 39 |
+
* **Efficient Memory Management:** Employs `diffusers`' standard CPU offloading to manage memory for non-transformer components.
|
| 40 |
+
* **Multi-Language Prompts:** Supports on-the-fly translation for prompts written in Korean and Chinese.
|
| 41 |
+
|
| 42 |
+
---
|
| 43 |
+
|
| 44 |
+
## Running the App Locally
|
| 45 |
+
|
| 46 |
+
Follow these steps to run the Gradio application on your own machine.
|
| 47 |
+
|
| 48 |
+
### 1. Prerequisites
|
| 49 |
+
* Python 3.10+
|
| 50 |
+
* Git and Git LFS installed.
|
| 51 |
+
|
| 52 |
+
### 2. Clone the Repository
|
| 53 |
+
```bash
|
| 54 |
+
git clone https://huggingface.co/spaces/elismasilva/z-image-panorama
|
| 55 |
+
cd z-image-panorama
|
| 56 |
+
```
|
| 57 |
+
|
| 58 |
+
### 3. Set Up a Virtual Environment (Recommended)
|
| 59 |
+
```bash
|
| 60 |
+
# Windows
|
| 61 |
+
python -m venv venv
|
| 62 |
+
.\venv\Scripts\activate
|
| 63 |
+
|
| 64 |
+
# macOS / Linux
|
| 65 |
+
python3 -m venv venv
|
| 66 |
+
source venv/bin/activate
|
| 67 |
+
```
|
| 68 |
+
|
| 69 |
+
### 4. Install Dependencies
|
| 70 |
+
```bash
|
| 71 |
+
pip install -r requirements.txt
|
| 72 |
+
```
|
| 73 |
+
|
| 74 |
+
### 5. Configure Local Model Paths (Optional)
|
| 75 |
+
The app is configured to download models from the Hugging Face Hub by default. For faster startup and offline use, you can use local models by setting environment variables.
|
| 76 |
+
|
| 77 |
+
For example, if you have the GGUF model in `F:\models\Z-Image-Turbo`:
|
| 78 |
+
```bash
|
| 79 |
+
# Windows CMD
|
| 80 |
+
set GGUF_LOCAL_DIR=F:\models\Z-Image-Turbo
|
| 81 |
+
|
| 82 |
+
# Windows PowerShell
|
| 83 |
+
$env:GGUF_LOCAL_DIR="F:\models\Z-Image-Turbo"
|
| 84 |
+
|
| 85 |
+
# Linux/macOS
|
| 86 |
+
export GGUF_LOCAL_DIR=/path/to/your/models/Z-Image-Turbo
|
| 87 |
+
```
|
| 88 |
+
|
| 89 |
+
### 6. Run the Gradio App
|
| 90 |
+
```bash
|
| 91 |
+
python app.py
|
| 92 |
+
```
|
| 93 |
+
The application will start and provide a local URL (usually `http://127.0.0.1:7860`) that you can open in your web browser.
|
| 94 |
+
|
| 95 |
+
---
|
| 96 |
+
|
| 97 |
+
## Using the Command-Line Script (`infer.py`)
|
| 98 |
+
|
| 99 |
+
The `infer.py` script allows you to test the pipeline directly from the command line.
|
| 100 |
+
|
| 101 |
+
### 1. Configure the Script
|
| 102 |
+
Open `infer.py` and modify the parameters inside the `main()` function, such as the `prompt_grid`, `target_height`, `target_width`, etc., to match your desired output.
|
| 103 |
+
|
| 104 |
+
### 2. Run the Script
|
| 105 |
+
Execute the script from your terminal:
|
| 106 |
+
```bash
|
| 107 |
+
python infer.py
|
| 108 |
+
```
|
| 109 |
+
The script will print its progress to the console, including the `tqdm` progress bar, and save the final image to the `outputs/` directory.
|
| 110 |
+
|
| 111 |
+
## Acknowledgements
|
| 112 |
+
* **Alibaba Tongyi-MAI Team** for the powerful Z-Image Turbo model.
|
| 113 |
+
* The original authors of the **Mixture of Diffusers** technique.
|
| 114 |
+
* **Hugging Face** for the `diffusers` library and the Spaces platform.
|
app.py
ADDED
|
@@ -0,0 +1,755 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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 |
+
import spaces
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import os
|
| 4 |
+
import random
|
| 5 |
+
import re
|
| 6 |
+
import numpy as np
|
| 7 |
+
import torch
|
| 8 |
+
from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer
|
| 9 |
+
|
| 10 |
+
# Import the custom tiling pipeline and its components
|
| 11 |
+
from pipeline_z_image_mod import ZImageMoDTilingPipeline
|
| 12 |
+
from diffusers import (
|
| 13 |
+
AutoencoderKL,
|
| 14 |
+
FlowMatchEulerDiscreteScheduler,
|
| 15 |
+
GGUFQuantizationConfig,
|
| 16 |
+
)
|
| 17 |
+
from diffusers.models import ZImageTransformer2DModel
|
| 18 |
+
from huggingface_hub import hf_hub_download
|
| 19 |
+
|
| 20 |
+
# Import the safety checker function
|
| 21 |
+
try:
|
| 22 |
+
from prompt_check import is_unsafe_prompt
|
| 23 |
+
|
| 24 |
+
UNSAFE_CHECK_AVAILABLE = True
|
| 25 |
+
except ImportError:
|
| 26 |
+
print("Warning: 'prompt_check.py' not found. NSFW prompt check will be disabled.")
|
| 27 |
+
UNSAFE_CHECK_AVAILABLE = False
|
| 28 |
+
|
| 29 |
+
def is_unsafe_prompt(*args, **kwargs):
|
| 30 |
+
return False
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
# 1. Environment Variables for Model Paths
|
| 34 |
+
# Base model for VAE, text_encoder, tokenizer
|
| 35 |
+
BASE_MODEL_ID = os.getenv("BASE_MODEL_ID", "Tongyi-MAI/Z-Image-Turbo")
|
| 36 |
+
# GGUF model for the transformer
|
| 37 |
+
GGUF_REPO_ID = os.getenv("GGUF_REPO_ID", "jayn7/Z-Image-Turbo-GGUF")
|
| 38 |
+
GGUF_FILENAME = os.getenv("GGUF_FILENAME", "z_image_turbo-Q4_K_M.gguf")
|
| 39 |
+
GGUF_LOCAL_DIR = os.getenv(
|
| 40 |
+
"GGUF_LOCAL_DIR", None
|
| 41 |
+
) # Set this path for local use, e.g., "F:\\models\\Z-Image-Turbo"
|
| 42 |
+
USE_SPACES_ENV = os.getenv("USE_SPACES", "false").lower()
|
| 43 |
+
USE_SPACES = USE_SPACES_ENV not in ("false", "0", "no", "none")
|
| 44 |
+
|
| 45 |
+
# System prompt for the safety checker
|
| 46 |
+
UNSAFE_PROMPT_CHECK = os.getenv("UNSAFE_PROMPT_CHECK")
|
| 47 |
+
MAX_SEED = np.iinfo(np.int32).max
|
| 48 |
+
|
| 49 |
+
# 2. Load Models (GGUF + Standard Components)
|
| 50 |
+
print("--- Loading Models ---")
|
| 51 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 52 |
+
|
| 53 |
+
print("Loading VAE...")
|
| 54 |
+
if USE_SPACES:
|
| 55 |
+
vae = AutoencoderKL.from_pretrained(
|
| 56 |
+
BASE_MODEL_ID, subfolder="vae", torch_dtype=torch.bfloat16
|
| 57 |
+
).to(device)
|
| 58 |
+
else:
|
| 59 |
+
vae = AutoencoderKL.from_pretrained(
|
| 60 |
+
BASE_MODEL_ID, subfolder="vae", torch_dtype=torch.bfloat16
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
print("Loading Text Encoder and Tokenizer...")
|
| 64 |
+
if USE_SPACES:
|
| 65 |
+
text_encoder = AutoModelForCausalLM.from_pretrained(
|
| 66 |
+
BASE_MODEL_ID, subfolder="text_encoder", dtype=torch.bfloat16
|
| 67 |
+
).to(device)
|
| 68 |
+
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL_ID, subfolder="tokenizer")
|
| 69 |
+
else:
|
| 70 |
+
text_encoder = AutoModelForCausalLM.from_pretrained(
|
| 71 |
+
BASE_MODEL_ID, subfolder="text_encoder", dtype=torch.bfloat16
|
| 72 |
+
)
|
| 73 |
+
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL_ID, subfolder="tokenizer")
|
| 74 |
+
|
| 75 |
+
print(f"Loading Transformer from GGUF: {GGUF_REPO_ID}/{GGUF_FILENAME}...")
|
| 76 |
+
transformer_path = (
|
| 77 |
+
hf_hub_download(
|
| 78 |
+
GGUF_REPO_ID,
|
| 79 |
+
GGUF_FILENAME,
|
| 80 |
+
local_dir=GGUF_LOCAL_DIR,
|
| 81 |
+
local_dir_use_symlinks=False,
|
| 82 |
+
)
|
| 83 |
+
if GGUF_LOCAL_DIR
|
| 84 |
+
else hf_hub_download(GGUF_REPO_ID, GGUF_FILENAME)
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
if USE_SPACES:
|
| 88 |
+
transformer = ZImageTransformer2DModel.from_single_file(
|
| 89 |
+
transformer_path,
|
| 90 |
+
quantization_config=GGUFQuantizationConfig(compute_dtype=torch.bfloat16),
|
| 91 |
+
dtype=torch.bfloat16,
|
| 92 |
+
).to(device)
|
| 93 |
+
else:
|
| 94 |
+
transformer = ZImageTransformer2DModel.from_single_file(
|
| 95 |
+
transformer_path,
|
| 96 |
+
quantization_config=GGUFQuantizationConfig(compute_dtype=torch.bfloat16),
|
| 97 |
+
dtype=torch.bfloat16,
|
| 98 |
+
)
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
# 3. Assemble the Tiling Pipeline
|
| 102 |
+
print("\nAssembling the ZImageMoDTilingPipeline...")
|
| 103 |
+
scheduler = FlowMatchEulerDiscreteScheduler(num_train_timesteps=1000, shift=3.0)
|
| 104 |
+
if USE_SPACES:
|
| 105 |
+
pipe = ZImageMoDTilingPipeline(
|
| 106 |
+
vae=vae,
|
| 107 |
+
text_encoder=text_encoder,
|
| 108 |
+
tokenizer=tokenizer,
|
| 109 |
+
scheduler=scheduler,
|
| 110 |
+
transformer=transformer,
|
| 111 |
+
).to(device)
|
| 112 |
+
else:
|
| 113 |
+
pipe = ZImageMoDTilingPipeline(
|
| 114 |
+
vae=vae,
|
| 115 |
+
text_encoder=text_encoder,
|
| 116 |
+
tokenizer=tokenizer,
|
| 117 |
+
scheduler=scheduler,
|
| 118 |
+
transformer=transformer,
|
| 119 |
+
)
|
| 120 |
+
|
| 121 |
+
print("Enabling model CPU offload...")
|
| 122 |
+
pipe.enable_model_cpu_offload()
|
| 123 |
+
|
| 124 |
+
# Load Translation Models
|
| 125 |
+
print("Loading translation models...")
|
| 126 |
+
try:
|
| 127 |
+
ko_en_translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ko-en")
|
| 128 |
+
zh_en_translator = pipeline("translation", model="Helsinki-NLP/opus-mt-zh-en")
|
| 129 |
+
except Exception as e:
|
| 130 |
+
ko_en_translator, zh_en_translator = None, None
|
| 131 |
+
print(f"Warning: Could not load translation models: {e}")
|
| 132 |
+
|
| 133 |
+
print("Pipeline loaded and ready.")
|
| 134 |
+
|
| 135 |
+
|
| 136 |
+
# Helper Functions
|
| 137 |
+
def translate_prompt(text: str, language: str) -> str:
|
| 138 |
+
"""Translates text to English if the selected language is not English."""
|
| 139 |
+
if language == "English" or not text.strip():
|
| 140 |
+
return text
|
| 141 |
+
translated = text
|
| 142 |
+
if (
|
| 143 |
+
language == "Korean"
|
| 144 |
+
and ko_en_translator
|
| 145 |
+
and any("\uac00" <= char <= "\ud7a3" for char in text)
|
| 146 |
+
):
|
| 147 |
+
translated = ko_en_translator(text)[0]["translation_text"]
|
| 148 |
+
elif (
|
| 149 |
+
language == "Chinese"
|
| 150 |
+
and zh_en_translator
|
| 151 |
+
and any("\u4e00" <= char <= "\u9fff" for char in text)
|
| 152 |
+
):
|
| 153 |
+
translated = zh_en_translator(text)[0]["translation_text"]
|
| 154 |
+
return translated
|
| 155 |
+
|
| 156 |
+
|
| 157 |
+
def create_hdr_effect(image, hdr_strength):
|
| 158 |
+
if hdr_strength == 0:
|
| 159 |
+
return image
|
| 160 |
+
from PIL import ImageEnhance, Image
|
| 161 |
+
|
| 162 |
+
if isinstance(image, Image.Image):
|
| 163 |
+
image = np.array(image)
|
| 164 |
+
from scipy.ndimage import gaussian_filter
|
| 165 |
+
|
| 166 |
+
blurred = gaussian_filter(image, sigma=5)
|
| 167 |
+
sharpened = np.clip(image + hdr_strength * (image - blurred), 0, 255).astype(
|
| 168 |
+
np.uint8
|
| 169 |
+
)
|
| 170 |
+
pil_img = Image.fromarray(sharpened)
|
| 171 |
+
converter = ImageEnhance.Color(pil_img)
|
| 172 |
+
return converter.enhance(1 + hdr_strength)
|
| 173 |
+
|
| 174 |
+
|
| 175 |
+
@spaces.GPU(duration=120)
|
| 176 |
+
def generate_z_image_panorama(
|
| 177 |
+
left_prompt,
|
| 178 |
+
center_prompt,
|
| 179 |
+
right_prompt,
|
| 180 |
+
left_gs,
|
| 181 |
+
center_gs,
|
| 182 |
+
right_gs,
|
| 183 |
+
overlap_pixels,
|
| 184 |
+
steps,
|
| 185 |
+
shift,
|
| 186 |
+
generation_seed,
|
| 187 |
+
tile_weighting_method,
|
| 188 |
+
<<<<<<< HEAD
|
| 189 |
+
prompt_language,
|
| 190 |
+
=======
|
| 191 |
+
prompt_language,
|
| 192 |
+
>>>>>>> 9805178aafc167973e2c82d7be94d5a21a004362
|
| 193 |
+
target_height,
|
| 194 |
+
target_width,
|
| 195 |
+
hdr,
|
| 196 |
+
randomize_seed,
|
| 197 |
+
progress=gr.Progress(track_tqdm=True),
|
| 198 |
+
):
|
| 199 |
+
"""
|
| 200 |
+
Generate a panoramic image using the Z-Image Turbo model with tiling and composition.
|
| 201 |
+
Args:
|
| 202 |
+
left_prompt (str): Text prompt for the left section of the panorama.
|
| 203 |
+
center_prompt (str): Text prompt for the center section of the panorama.
|
| 204 |
+
right_prompt (str): Text prompt for the right section of the panorama.
|
| 205 |
+
left_gs (float): Guidance scale for the left tile.
|
| 206 |
+
center_gs (float): Guidance scale for the center tile.
|
| 207 |
+
right_gs (float): Guidance scale for the right tile.
|
| 208 |
+
overlap_pixels (int): Number of pixels to overlap between tiles.
|
| 209 |
+
steps (int): Number of inference steps for generation.
|
| 210 |
+
shift (float): Time Shift.
|
| 211 |
+
generation_seed (int): Random seed for reproducibility.
|
| 212 |
+
tile_weighting_method (str): Method for weighting overlapping tile regions.
|
| 213 |
+
prompt_language (str): Language code for prompt translation.
|
| 214 |
+
target_height (int): Height of the generated panorama in pixels.
|
| 215 |
+
target_width (int): Width of the generated panorama in pixels.
|
| 216 |
+
hdr (float): HDR effect intensity.
|
| 217 |
+
randomize_seed (boolean): Not used.
|
| 218 |
+
progress (gr.Progress): Gradio progress tracker.
|
| 219 |
+
Returns:
|
| 220 |
+
PIL.Image: The generated panoramic image with optional HDR effect applied.
|
| 221 |
+
"""
|
| 222 |
+
if not left_prompt or not center_prompt or not right_prompt:
|
| 223 |
+
gr.Info("⚡️ Prompts must be provided!")
|
| 224 |
+
return gr.skip()
|
| 225 |
+
# Safety Check
|
| 226 |
+
prompts_to_check = [left_prompt, center_prompt, right_prompt]
|
| 227 |
+
for p in prompts_to_check:
|
| 228 |
+
if UNSAFE_CHECK_AVAILABLE and is_unsafe_prompt(
|
| 229 |
+
pipe.text_encoder,
|
| 230 |
+
device,
|
| 231 |
+
pipe.tokenizer,
|
| 232 |
+
system_prompt=UNSAFE_PROMPT_CHECK,
|
| 233 |
+
user_prompt=p,
|
| 234 |
+
):
|
| 235 |
+
raise gr.Error(
|
| 236 |
+
f"Unsafe prompt detected. Please modify your prompt and try again."
|
| 237 |
+
)
|
| 238 |
+
|
| 239 |
+
generator = torch.Generator("cuda").manual_seed(generation_seed)
|
| 240 |
+
pipe.scheduler = FlowMatchEulerDiscreteScheduler(num_train_timesteps=1000, shift=shift)
|
| 241 |
+
|
| 242 |
+
final_height, final_width = int(target_height), int(target_width)
|
| 243 |
+
|
| 244 |
+
translated_left = translate_prompt(left_prompt, prompt_language)
|
| 245 |
+
translated_center = translate_prompt(center_prompt, prompt_language)
|
| 246 |
+
translated_right = translate_prompt(right_prompt, prompt_language)
|
| 247 |
+
|
| 248 |
+
image = pipe(
|
| 249 |
+
prompt=[[translated_left, translated_center, translated_right]],
|
| 250 |
+
height=final_height,
|
| 251 |
+
width=final_width,
|
| 252 |
+
num_inference_steps=steps,
|
| 253 |
+
guidance_scale_tiles=[[left_gs, center_gs, right_gs]],
|
| 254 |
+
tile_overlap=overlap_pixels,
|
| 255 |
+
tile_weighting_method=tile_weighting_method,
|
| 256 |
+
generator=generator,
|
| 257 |
+
).images[0]
|
| 258 |
+
|
| 259 |
+
return create_hdr_effect(image, hdr)
|
| 260 |
+
|
| 261 |
+
|
| 262 |
+
def calculate_tile_size(target_height, target_width, overlap_pixels):
|
| 263 |
+
"""
|
| 264 |
+
Calculate tile dimensions for panoramic image generation.
|
| 265 |
+
|
| 266 |
+
Args:
|
| 267 |
+
target_height (int): The target height of the final panoramic image in pixels.
|
| 268 |
+
target_width (int): The target width of the final panoramic image in pixels.
|
| 269 |
+
overlap_pixels (int): The number of overlapping pixels between adjacent tiles.
|
| 270 |
+
|
| 271 |
+
Returns:
|
| 272 |
+
tuple: A tuple of 2 gr.update objects containing:
|
| 273 |
+
- final_height: Final panorama height after tiling
|
| 274 |
+
- final_width: Final panorama width after tiling
|
| 275 |
+
"""
|
| 276 |
+
num_cols = 3
|
| 277 |
+
num_rows = 1
|
| 278 |
+
tile_width = (target_width + (num_cols - 1) * overlap_pixels) // num_cols
|
| 279 |
+
tile_height = (target_height + (num_rows - 1) * overlap_pixels) // num_rows
|
| 280 |
+
tile_width -= tile_width % 16
|
| 281 |
+
tile_height -= tile_height % 16
|
| 282 |
+
final_width = tile_width * num_cols - (num_cols - 1) * overlap_pixels
|
| 283 |
+
final_height = tile_height * num_rows - (num_rows - 1) * overlap_pixels
|
| 284 |
+
return (
|
| 285 |
+
gr.update(value=final_height),
|
| 286 |
+
gr.update(value=final_width),
|
| 287 |
+
)
|
| 288 |
+
|
| 289 |
+
|
| 290 |
+
def randomize_seed_fn(generation_seed: int, randomize_seed: bool) -> int:
|
| 291 |
+
if randomize_seed:
|
| 292 |
+
return random.randint(0, MAX_SEED)
|
| 293 |
+
return generation_seed
|
| 294 |
+
|
| 295 |
+
|
| 296 |
+
def run_for_examples(
|
| 297 |
+
left_prompt,
|
| 298 |
+
center_prompt,
|
| 299 |
+
right_prompt,
|
| 300 |
+
left_gs,
|
| 301 |
+
center_gs,
|
| 302 |
+
right_gs,
|
| 303 |
+
overlap_pixels,
|
| 304 |
+
steps,
|
| 305 |
+
shift,
|
| 306 |
+
generation_seed,
|
| 307 |
+
<<<<<<< HEAD
|
| 308 |
+
tile_weighting_method,
|
| 309 |
+
=======
|
| 310 |
+
tile_weighting_method,
|
| 311 |
+
>>>>>>> 9805178aafc167973e2c82d7be94d5a21a004362
|
| 312 |
+
target_height,
|
| 313 |
+
target_width,
|
| 314 |
+
hdr,
|
| 315 |
+
randomize_seed
|
| 316 |
+
):
|
| 317 |
+
return generate_z_image_panorama(
|
| 318 |
+
left_prompt,
|
| 319 |
+
center_prompt,
|
| 320 |
+
right_prompt,
|
| 321 |
+
left_gs,
|
| 322 |
+
center_gs,
|
| 323 |
+
right_gs,
|
| 324 |
+
overlap_pixels,
|
| 325 |
+
steps,
|
| 326 |
+
shift,
|
| 327 |
+
generation_seed,
|
| 328 |
+
tile_weighting_method,
|
| 329 |
+
<<<<<<< HEAD
|
| 330 |
+
"English",
|
| 331 |
+
=======
|
| 332 |
+
"English",
|
| 333 |
+
>>>>>>> 9805178aafc167973e2c82d7be94d5a21a004362
|
| 334 |
+
target_height,
|
| 335 |
+
target_width,
|
| 336 |
+
hdr,
|
| 337 |
+
randomize_seed
|
| 338 |
+
)
|
| 339 |
+
|
| 340 |
+
|
| 341 |
+
def clear_result():
|
| 342 |
+
return gr.update(value=None)
|
| 343 |
+
|
| 344 |
+
|
| 345 |
+
def update_dimensions_from_preset(resolution_preset):
|
| 346 |
+
"""Updates the width and height sliders based on a preset string."""
|
| 347 |
+
match = re.search(r"(\d+)\s*[x]\s*(\d+)", resolution_preset)
|
| 348 |
+
if match:
|
| 349 |
+
width, height = int(match.group(1)), int(match.group(2))
|
| 350 |
+
return gr.update(value=width), gr.update(value=height)
|
| 351 |
+
return gr.update(), gr.update()
|
| 352 |
+
|
| 353 |
+
|
| 354 |
+
# UI Layout
|
| 355 |
+
theme = gr.themes.Default(
|
| 356 |
+
primary_hue="red", secondary_hue="orange", neutral_hue="gray"
|
| 357 |
+
).set(
|
| 358 |
+
body_background_fill="*neutral_100",
|
| 359 |
+
body_background_fill_dark="*neutral_900",
|
| 360 |
+
body_text_color="*neutral_900",
|
| 361 |
+
body_text_color_dark="*neutral_100",
|
| 362 |
+
panel_background_fill="*neutral_800",
|
| 363 |
+
panel_background_fill_dark="*neutral_900",
|
| 364 |
+
input_background_fill="white",
|
| 365 |
+
input_background_fill_dark="*neutral_800",
|
| 366 |
+
button_primary_background_fill="*primary_500",
|
| 367 |
+
button_primary_background_fill_dark="*primary_700",
|
| 368 |
+
button_primary_text_color="white",
|
| 369 |
+
button_primary_text_color_dark="white",
|
| 370 |
+
button_secondary_background_fill="*secondary_500",
|
| 371 |
+
button_secondary_background_fill_dark="*secondary_700",
|
| 372 |
+
button_secondary_text_color="white",
|
| 373 |
+
button_secondary_text_color_dark="white",
|
| 374 |
+
)
|
| 375 |
+
|
| 376 |
+
css_code = ""
|
| 377 |
+
|
| 378 |
+
try:
|
| 379 |
+
with open("./style.css", "r", encoding="utf-8") as f:
|
| 380 |
+
css_code += f.read() + "\n"
|
| 381 |
+
except FileNotFoundError:
|
| 382 |
+
pass
|
| 383 |
+
|
| 384 |
+
title = """<h1 align="center">Panorama Z-Image 🏞️✨</h1>
|
| 385 |
+
<div style="text-align: center;">
|
| 386 |
+
<span>Create panoramic compositions with Z-Image-Turbo.</span>
|
| 387 |
+
</div>
|
| 388 |
+
"""
|
| 389 |
+
# Adapted panoramic resolutions
|
| 390 |
+
PANORAMIC_RESOLUTIONS = [
|
| 391 |
+
"2048x1024 (2:1)",
|
| 392 |
+
"2560x1024 (2.5:1)",
|
| 393 |
+
"3072x1024 (3:1)",
|
| 394 |
+
"2304x896 ( ~2.5:1)",
|
| 395 |
+
"2880x1152 (~2.5:1)",
|
| 396 |
+
"3840x1536 (2.5:1)",
|
| 397 |
+
]
|
| 398 |
+
|
| 399 |
+
with gr.Blocks(theme=theme, css=css_code, title="Panorama Z-Image") as app:
|
| 400 |
+
gr.Markdown(title)
|
| 401 |
+
with gr.Row():
|
| 402 |
+
with gr.Column(scale=7):
|
| 403 |
+
generate_button = gr.Button("Generate", variant="primary")
|
| 404 |
+
with gr.Row():
|
| 405 |
+
with gr.Column(scale=1):
|
| 406 |
+
gr.Markdown("### Left Region")
|
| 407 |
+
left_prompt = gr.Textbox(lines=4, label="Prompt (Left)")
|
| 408 |
+
left_gs = gr.Slider(
|
| 409 |
+
minimum=0.0,
|
| 410 |
+
maximum=10.0,
|
| 411 |
+
value=0.0,
|
| 412 |
+
step=0.1,
|
| 413 |
+
label="Left Guidance",
|
| 414 |
+
<<<<<<< HEAD
|
| 415 |
+
visible=False,
|
| 416 |
+
=======
|
| 417 |
+
visible=False
|
| 418 |
+
>>>>>>> 9805178aafc167973e2c82d7be94d5a21a004362
|
| 419 |
+
)
|
| 420 |
+
with gr.Column(scale=1):
|
| 421 |
+
gr.Markdown("### Center Region")
|
| 422 |
+
center_prompt = gr.Textbox(lines=4, label="Prompt (Center)")
|
| 423 |
+
center_gs = gr.Slider(
|
| 424 |
+
minimum=0.0,
|
| 425 |
+
maximum=10.0,
|
| 426 |
+
value=0.0,
|
| 427 |
+
step=0.1,
|
| 428 |
+
label="Center Guidance",
|
| 429 |
+
<<<<<<< HEAD
|
| 430 |
+
visible=False,
|
| 431 |
+
=======
|
| 432 |
+
visible=False
|
| 433 |
+
>>>>>>> 9805178aafc167973e2c82d7be94d5a21a004362
|
| 434 |
+
)
|
| 435 |
+
with gr.Column(scale=1):
|
| 436 |
+
gr.Markdown("### Right Region")
|
| 437 |
+
right_prompt = gr.Textbox(lines=4, label="Prompt (Right)")
|
| 438 |
+
right_gs = gr.Slider(
|
| 439 |
+
minimum=0.0,
|
| 440 |
+
maximum=10.0,
|
| 441 |
+
value=0.0,
|
| 442 |
+
step=0.1,
|
| 443 |
+
label="Right Guidance",
|
| 444 |
+
<<<<<<< HEAD
|
| 445 |
+
visible=False,
|
| 446 |
+
=======
|
| 447 |
+
visible=False
|
| 448 |
+
>>>>>>> 9805178aafc167973e2c82d7be94d5a21a004362
|
| 449 |
+
)
|
| 450 |
+
with gr.Row():
|
| 451 |
+
result = gr.Image(
|
| 452 |
+
label="Generated Image",
|
| 453 |
+
show_label=True,
|
| 454 |
+
format="png",
|
| 455 |
+
interactive=False,
|
| 456 |
+
)
|
| 457 |
+
with gr.Column(scale=1, min_width=200):
|
| 458 |
+
gr.Markdown(
|
| 459 |
+
"""
|
| 460 |
+
### Related Apps
|
| 461 |
+
Check out these other great demos:
|
| 462 |
+
|
| 463 |
+
- **[Official Z-Image Demo](https://huggingface.co/spaces/Tongyi-MAI/Z-Image-Turbo)**
|
| 464 |
+
*The official demo for single image generation.*
|
| 465 |
+
|
| 466 |
+
- **[Panorama FLUX](https://huggingface.co/spaces/elismasilva/flux-1-panorama)**
|
| 467 |
+
*The Panorama app using the FLUX model.*
|
| 468 |
+
"""
|
| 469 |
+
)
|
| 470 |
+
with gr.Sidebar():
|
| 471 |
+
gr.Markdown("### Generation Parameters")
|
| 472 |
+
prompt_language = gr.Radio(
|
| 473 |
+
choices=["English", "Korean", "Chinese"],
|
| 474 |
+
value="English",
|
| 475 |
+
label="Prompt Language",
|
| 476 |
+
)
|
| 477 |
+
|
| 478 |
+
resolution_preset = gr.Dropdown(
|
| 479 |
+
choices=PANORAMIC_RESOLUTIONS,
|
| 480 |
+
value=PANORAMIC_RESOLUTIONS[2],
|
| 481 |
+
label="Panoramic Resolution Preset",
|
| 482 |
+
)
|
| 483 |
+
|
| 484 |
+
with gr.Row():
|
| 485 |
+
height = gr.Slider(
|
| 486 |
+
label="Target Height", value=1024, step=16, minimum=512, maximum=2048
|
| 487 |
+
)
|
| 488 |
+
width = gr.Slider(
|
| 489 |
+
label="Target Width", value=3072, step=16, minimum=512, maximum=4096
|
| 490 |
+
)
|
| 491 |
+
with gr.Row():
|
| 492 |
+
overlap = gr.Slider(
|
| 493 |
+
minimum=0, maximum=512, value=256, step=16, label="Tile Overlap"
|
| 494 |
+
)
|
| 495 |
+
tile_weighting_method = gr.Dropdown(
|
| 496 |
+
label="Blending Method", choices=["Cosine", "Gaussian"], value="Cosine"
|
| 497 |
+
)
|
| 498 |
+
with gr.Row():
|
| 499 |
+
calc_tile = gr.Button("Calculate Final Dimensions", variant="primary")
|
| 500 |
+
with gr.Row():
|
| 501 |
+
new_target_height = gr.Textbox(
|
| 502 |
+
label="Actual Height", value=1024, interactive=False
|
| 503 |
+
)
|
| 504 |
+
new_target_width = gr.Textbox(
|
| 505 |
+
label="Actual Width", value=3072, interactive=False
|
| 506 |
+
)
|
| 507 |
+
with gr.Row():
|
| 508 |
+
steps = gr.Slider(
|
| 509 |
+
minimum=1, maximum=50, value=9, step=1, label="Inference Steps"
|
| 510 |
+
)
|
| 511 |
+
shift = gr.Slider(label="Time Shift", minimum=1.0, maximum=10.0, value=3.0, step=0.1)
|
| 512 |
+
with gr.Row():
|
| 513 |
+
generation_seed = gr.Slider(
|
| 514 |
+
label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0
|
| 515 |
+
)
|
| 516 |
+
randomize_seed = gr.Checkbox(label="Randomize Seed", value=True)
|
| 517 |
+
with gr.Row():
|
| 518 |
+
hdr = gr.Slider(
|
| 519 |
+
minimum=0.0, maximum=1.0, value=0.1, step=0.00, label="HDR Effect"
|
| 520 |
+
)
|
| 521 |
+
|
| 522 |
+
with gr.Row():
|
| 523 |
+
gr.Examples(
|
| 524 |
+
examples=[
|
| 525 |
+
[
|
| 526 |
+
<<<<<<< HEAD
|
| 527 |
+
"On the left side of a wide, wet stone courtyard at night, a traditional wooden temple building has a soft, warm glow from its illuminated windows, the environment is a deep, dark night sky.",
|
| 528 |
+
"Young Chinese woman in red Hanfu, standing gracefully in the center of the stone-paved temple courtyard, intricate embroidery. Impeccable makeup, red floral forehead pattern. Elaborate high bun, golden phoenix headdress, red flowers, beads. Holds round folding fan with lady, trees, bird. Neon lightning-bolt lamp (⚡️), bright yellow glow, above extended left palm. Soft-lit outdoor night background, silhouetted tiered pagoda (西安大雁塔), blurred colorful distant lights.",
|
| 529 |
+
"On the right side of the frame, a wooden temple corridor with a row of glowing paper lanterns recedes into the background. The lantern light reflects on the wet stone path below, and the distant pagoda is silhouetted against the same deep, dark night sky.",
|
| 530 |
+
=======
|
| 531 |
+
"Iron Man, repulsor rays blasting enemies in destroyed cityscape, cinematic lighting, photorealistic. Focus: Iron Man.",
|
| 532 |
+
"Captain America charging forward, vibranium shield deflecting energy blasts in destroyed cityscape, cinematic composition. Focus: Captain America.",
|
| 533 |
+
"Thor wielding Stormbreaker in destroyed cityscape, lightning crackling, powerful strike downwards, cinematic photography. Focus: Thor.",
|
| 534 |
+
0.0,
|
| 535 |
+
0.0,
|
| 536 |
+
0.0,
|
| 537 |
+
200,
|
| 538 |
+
8,
|
| 539 |
+
619517442,
|
| 540 |
+
"Cosine",
|
| 541 |
+
1024,
|
| 542 |
+
3440,
|
| 543 |
+
0.0,
|
| 544 |
+
],
|
| 545 |
+
[
|
| 546 |
+
"A charming house in the countryside, by jakub rozalski, sunset lighting, elegant, highly detailed, sharp focus, artstation, stunning masterpiece",
|
| 547 |
+
"A dirt road in the countryside crossing pastures, by jakub rozalski, sunset lighting, elegant, highly detailed, sharp focus, artstation, stunning masterpiece",
|
| 548 |
+
"An old and rusty giant robot lying on a dirt road, by jakub rozalski, dark sunset lighting, elegant, highly detailed, sharp focus, artstation, stunning masterpiece",
|
| 549 |
+
>>>>>>> 9805178aafc167973e2c82d7be94d5a21a004362
|
| 550 |
+
0.0,
|
| 551 |
+
0.0,
|
| 552 |
+
0.0,
|
| 553 |
+
256,
|
| 554 |
+
<<<<<<< HEAD
|
| 555 |
+
8,
|
| 556 |
+
3.0,
|
| 557 |
+
109320357,
|
| 558 |
+
"Cosine",
|
| 559 |
+
1024,
|
| 560 |
+
3072,
|
| 561 |
+
=======
|
| 562 |
+
4,
|
| 563 |
+
358867853,
|
| 564 |
+
"Gaussian",
|
| 565 |
+
1024,
|
| 566 |
+
3808,
|
| 567 |
+
0.0,
|
| 568 |
+
],
|
| 569 |
+
[
|
| 570 |
+
"A vibrant mountain slope in full spring bloom, covered in colorful wildflowers and lush green grass, a small stream meandering down, cinematic photo, bright morning light.",
|
| 571 |
+
"The majestic, rocky peak of the same mountain under a clear summer sky, patches of green tundra, eagles soaring high above, strong midday sun. cinematic photo.",
|
| 572 |
+
"The other side of the mountain descending into a valley ablaze with autumn colors, forests of red, orange, and yellow trees, a gentle haze in the air. cinematic photo, golden hour light.",
|
| 573 |
+
0.0,
|
| 574 |
+
0.0,
|
| 575 |
+
0.0,
|
| 576 |
+
280,
|
| 577 |
+
4,
|
| 578 |
+
20240521,
|
| 579 |
+
"Cosine",
|
| 580 |
+
1024,
|
| 581 |
+
2896,
|
| 582 |
+
>>>>>>> 9805178aafc167973e2c82d7be94d5a21a004362
|
| 583 |
+
0.0,
|
| 584 |
+
False,
|
| 585 |
+
],
|
| 586 |
+
[
|
| 587 |
+
<<<<<<< HEAD
|
| 588 |
+
"Street photography of a bustling, modern Chinese city square in the morning. On the left, people in business attire walk past gleaming glass and steel office buildings under a hazy, bright sun.",
|
| 589 |
+
"The center of the vast, modern plaza, where a large, elegant water fountain sprays water high into the air. In the background, through the mist of the fountain, the same modern glass and steel office buildings continue across the scene. The sun reflects off the wet plaza floor.",
|
| 590 |
+
"The right side of the plaza, where groups of fashionable teenagers are casually standing near the concrete pillars of a modern building, looking at their smartphones under the same hazy, bright sun.",
|
| 591 |
+
=======
|
| 592 |
+
"Enchanted forest with glowing bioluminescent plants and mystical fog, fantasy art.",
|
| 593 |
+
"Ancient elven castle with towering white spires bathed in moonlight, fantasy art.",
|
| 594 |
+
"Majestic griffin soaring above a starry night sky, fantasy art.",
|
| 595 |
+
0.0,
|
| 596 |
+
0.0,
|
| 597 |
+
0.0,
|
| 598 |
+
224,
|
| 599 |
+
6,
|
| 600 |
+
1029384756,
|
| 601 |
+
"Gaussian",
|
| 602 |
+
1024,
|
| 603 |
+
3392,
|
| 604 |
+
>>>>>>> 9805178aafc167973e2c82d7be94d5a21a004362
|
| 605 |
+
0.0,
|
| 606 |
+
0.0,
|
| 607 |
+
0.0,
|
| 608 |
+
256,
|
| 609 |
+
8,
|
| 610 |
+
6.0,
|
| 611 |
+
2024,
|
| 612 |
+
"Cosine",
|
| 613 |
+
1024,
|
| 614 |
+
3072,
|
| 615 |
+
0.0,
|
| 616 |
+
False
|
| 617 |
+
],
|
| 618 |
+
[
|
| 619 |
+
"The edge of the green carpeted arena floor, with the blurred figures of spectators in the stands rising up in the background. A judge's table is partially visible on the far left.",
|
| 620 |
+
"In the center of the green carpeted arena floor, a proud man in a fine suit stands with his perfectly groomed white poodle, both looking forward. The poodle wears a matching tiny tuxedo.",
|
| 621 |
+
"Further down the green carpeted arena on the right, a line of other dog handlers and their breeds are waiting their turn in the distance, their forms slightly blurred by the depth of field.",
|
| 622 |
+
0.0,
|
| 623 |
+
0.0,
|
| 624 |
+
0.0,
|
| 625 |
+
200,
|
| 626 |
+
<<<<<<< HEAD
|
| 627 |
+
8,
|
| 628 |
+
3.0,
|
| 629 |
+
1146985307,
|
| 630 |
+
"Gaussian",
|
| 631 |
+
=======
|
| 632 |
+
4,
|
| 633 |
+
54321,
|
| 634 |
+
"Gaussian",
|
| 635 |
+
>>>>>>> 9805178aafc167973e2c82d7be94d5a21a004362
|
| 636 |
+
1024,
|
| 637 |
+
2672,
|
| 638 |
+
0.0,
|
| 639 |
+
False
|
| 640 |
+
],
|
| 641 |
+
[
|
| 642 |
+
<<<<<<< HEAD
|
| 643 |
+
"Iron Man, repulsor rays blasting enemies in destroyed cityscape, cinematic lighting, photorealistic. Focus: Iron Man.",
|
| 644 |
+
"Captain America charging forward, vibranium shield deflecting energy blasts in destroyed cityscape, cinematic composition. Focus: Captain America.",
|
| 645 |
+
"Thor wielding Stormbreaker in destroyed cityscape, lightning crackling, powerful strike downwards, cinematic photography. Focus: Thor.",
|
| 646 |
+
0,
|
| 647 |
+
0,
|
| 648 |
+
0,
|
| 649 |
+
200,
|
| 650 |
+
8,
|
| 651 |
+
4.0,
|
| 652 |
+
85512926,
|
| 653 |
+
"Cosine",
|
| 654 |
+
1024,
|
| 655 |
+
3440,
|
| 656 |
+
=======
|
| 657 |
+
"A futuristic neon-lit city street at night, flying cars zooming past, towering holographic billboards. cyberpunk aesthetic, blade runner style.",
|
| 658 |
+
"The entrance to a grimy, underground nightclub, a mysterious figure in a trench coat standing in the doorway, steam rising from a vent. cyberpunk aesthetic, blade runner style.",
|
| 659 |
+
"A dark alleyway off the main street, piles of discarded electronics, graffiti glowing on the walls, rain glistening on the pavement. cyberpunk aesthetic, blade runner style.",
|
| 660 |
+
0.0,
|
| 661 |
+
0.0,
|
| 662 |
+
0.0,
|
| 663 |
+
300,
|
| 664 |
+
8,
|
| 665 |
+
98765,
|
| 666 |
+
"Cosine",
|
| 667 |
+
1024,
|
| 668 |
+
3240,
|
| 669 |
+
>>>>>>> 9805178aafc167973e2c82d7be94d5a21a004362
|
| 670 |
+
0.0,
|
| 671 |
+
False
|
| 672 |
+
]
|
| 673 |
+
],
|
| 674 |
+
inputs=[
|
| 675 |
+
left_prompt,
|
| 676 |
+
center_prompt,
|
| 677 |
+
right_prompt,
|
| 678 |
+
left_gs,
|
| 679 |
+
center_gs,
|
| 680 |
+
right_gs,
|
| 681 |
+
overlap,
|
| 682 |
+
steps,
|
| 683 |
+
shift,
|
| 684 |
+
generation_seed,
|
| 685 |
+
<<<<<<< HEAD
|
| 686 |
+
tile_weighting_method,
|
| 687 |
+
=======
|
| 688 |
+
tile_weighting_method,
|
| 689 |
+
>>>>>>> 9805178aafc167973e2c82d7be94d5a21a004362
|
| 690 |
+
height,
|
| 691 |
+
width,
|
| 692 |
+
hdr,
|
| 693 |
+
randomize_seed
|
| 694 |
+
],
|
| 695 |
+
fn=run_for_examples,
|
| 696 |
+
outputs=result,
|
| 697 |
+
cache_examples=False,
|
| 698 |
+
api_name=False,
|
| 699 |
+
)
|
| 700 |
+
|
| 701 |
+
# Event Handling
|
| 702 |
+
predict_inputs = [
|
| 703 |
+
left_prompt,
|
| 704 |
+
center_prompt,
|
| 705 |
+
right_prompt,
|
| 706 |
+
left_gs,
|
| 707 |
+
center_gs,
|
| 708 |
+
right_gs,
|
| 709 |
+
overlap,
|
| 710 |
+
steps,
|
| 711 |
+
shift,
|
| 712 |
+
generation_seed,
|
| 713 |
+
tile_weighting_method,
|
| 714 |
+
<<<<<<< HEAD
|
| 715 |
+
prompt_language,
|
| 716 |
+
=======
|
| 717 |
+
prompt_language,
|
| 718 |
+
>>>>>>> 9805178aafc167973e2c82d7be94d5a21a004362
|
| 719 |
+
new_target_height,
|
| 720 |
+
new_target_width,
|
| 721 |
+
hdr,
|
| 722 |
+
]
|
| 723 |
+
|
| 724 |
+
resolution_preset.change(
|
| 725 |
+
fn=update_dimensions_from_preset,
|
| 726 |
+
inputs=resolution_preset,
|
| 727 |
+
outputs=[width, height],
|
| 728 |
+
api_name=False,
|
| 729 |
+
)
|
| 730 |
+
calc_tile.click(
|
| 731 |
+
fn=calculate_tile_size,
|
| 732 |
+
inputs=[height, width, overlap],
|
| 733 |
+
outputs=[new_target_height, new_target_width],
|
| 734 |
+
show_api=False,
|
| 735 |
+
)
|
| 736 |
+
generate_button.click(
|
| 737 |
+
fn=randomize_seed_fn,
|
| 738 |
+
inputs=[generation_seed, randomize_seed],
|
| 739 |
+
outputs=generation_seed,
|
| 740 |
+
queue=False,
|
| 741 |
+
api_name=False,
|
| 742 |
+
).then(
|
| 743 |
+
fn=calculate_tile_size,
|
| 744 |
+
inputs=[height, width, overlap],
|
| 745 |
+
outputs=[new_target_height, new_target_width],
|
| 746 |
+
show_api=False,
|
| 747 |
+
).then(
|
| 748 |
+
fn=generate_z_image_panorama,
|
| 749 |
+
inputs=predict_inputs,
|
| 750 |
+
outputs=result,
|
| 751 |
+
show_progress="full",
|
| 752 |
+
api_name="generate_z_image_panorama",
|
| 753 |
+
)
|
| 754 |
+
|
| 755 |
+
app.queue().launch(mcp_server=True, share=True)
|
infer.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import torch
|
| 3 |
+
import time
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
# 1. Import the final, self-contained tiling pipeline
|
| 7 |
+
from pipeline_z_image_mod import ZImageMoDTilingPipeline
|
| 8 |
+
from diffusers import AutoencoderKL, FlowMatchEulerDiscreteScheduler, GGUFQuantizationConfig
|
| 9 |
+
from diffusers.models import ZImageTransformer2DModel
|
| 10 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 11 |
+
from huggingface_hub import hf_hub_download
|
| 12 |
+
|
| 13 |
+
def main():
|
| 14 |
+
# 1. Load Components Manually, using GGUF for the Transformer
|
| 15 |
+
print("--- 1. Loading Model Components (with GGUF) ---")
|
| 16 |
+
BASE_MODEL_ID = "Tongyi-MAI/Z-Image-Turbo"
|
| 17 |
+
GGUF_REPO_ID = "jayn7/Z-Image-Turbo-GGUF"
|
| 18 |
+
GGUF_FILENAME = "z_image_turbo-Q4_K_M.gguf"
|
| 19 |
+
GGUF_LOCAL_DIR = "F:\\models\\Z-Image-Turbo"
|
| 20 |
+
|
| 21 |
+
print("Loading VAE...")
|
| 22 |
+
vae = AutoencoderKL.from_pretrained(BASE_MODEL_ID, subfolder="vae", torch_dtype=torch.bfloat16)
|
| 23 |
+
print("Loading Text Encoder and Tokenizer...")
|
| 24 |
+
text_encoder = AutoModelForCausalLM.from_pretrained(BASE_MODEL_ID, subfolder="text_encoder", torch_dtype=torch.bfloat16)
|
| 25 |
+
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL_ID, subfolder="tokenizer")
|
| 26 |
+
print(f"Loading Transformer from GGUF file: {GGUF_FILENAME}...")
|
| 27 |
+
transformer = ZImageTransformer2DModel.from_single_file(hf_hub_download(GGUF_REPO_ID, GGUF_FILENAME, local_dir=GGUF_LOCAL_DIR, local_dir_use_symlinks=False), quantization_config=GGUFQuantizationConfig(compute_dtype=torch.bfloat16), torch_dtype=torch.bfloat16)
|
| 28 |
+
|
| 29 |
+
scheduler = FlowMatchEulerDiscreteScheduler(num_train_timesteps=1000, shift=3.0)
|
| 30 |
+
|
| 31 |
+
# 2. Initialize our Tiling Pipeline with Pre-loaded Components
|
| 32 |
+
print("\n--- 2. Assembling the ZImageMoDTilingPipeline ---")
|
| 33 |
+
pipe = ZImageMoDTilingPipeline(vae=vae, text_encoder=text_encoder, tokenizer=tokenizer, scheduler=scheduler, transformer=transformer)
|
| 34 |
+
|
| 35 |
+
print("Enabling model CPU offload...")
|
| 36 |
+
pipe.enable_model_cpu_offload()
|
| 37 |
+
|
| 38 |
+
# 3. Set Up Tiling and Inference Parameters
|
| 39 |
+
print("\n--- 3. Setting Up Inference Parameters ---")
|
| 40 |
+
|
| 41 |
+
prompt_grid = [[
|
| 42 |
+
"On the left side of a wide, wet stone courtyard at night, a traditional wooden temple building has a soft, warm glow from its illuminated windows, the environment is a deep, dark night sky.",
|
| 43 |
+
"Young Chinese woman in red Hanfu, standing gracefully in the center of the stone-paved temple courtyard, intricate embroidery. Impeccable makeup, red floral forehead pattern. Elaborate high bun, golden phoenix headdress, red flowers, beads. Holds round folding fan with lady, trees, bird. Neon lightning-bolt lamp (⚡️), bright yellow glow, above extended left palm. Soft-lit outdoor night background, silhouetted tiered pagoda (西安大雁塔), blurred colorful distant lights.",
|
| 44 |
+
"On the right side of the frame, a wooden temple corridor with a row of glowing paper lanterns recedes into the background. The lantern light reflects on the wet stone path below, and the distant pagoda is silhouetted against the same deep, dark night sky.",
|
| 45 |
+
]]
|
| 46 |
+
|
| 47 |
+
target_height, canvas_width = 1024, 3072
|
| 48 |
+
num_inference_steps = 8
|
| 49 |
+
seed = 109320357
|
| 50 |
+
generator = torch.Generator("cuda").manual_seed(seed)
|
| 51 |
+
|
| 52 |
+
# 4. Start Inference
|
| 53 |
+
print("\n--- 4. Starting Inference ---")
|
| 54 |
+
|
| 55 |
+
start_inference_time = time.time()
|
| 56 |
+
|
| 57 |
+
# A single, clean call to our self-contained pipeline.
|
| 58 |
+
# No guidance_scale or negative_prompt is needed for the Turbo model.
|
| 59 |
+
image = pipe(
|
| 60 |
+
prompt=prompt_grid,
|
| 61 |
+
height=target_height,
|
| 62 |
+
width=canvas_width,
|
| 63 |
+
num_inference_steps=num_inference_steps,
|
| 64 |
+
generator=generator,
|
| 65 |
+
).images[0]
|
| 66 |
+
|
| 67 |
+
end_inference_time = time.time()
|
| 68 |
+
print(f"\nInference finished in {end_inference_time - start_inference_time:.2f} seconds.")
|
| 69 |
+
|
| 70 |
+
# 5. Save Output
|
| 71 |
+
if not os.path.exists("outputs"): os.makedirs("outputs")
|
| 72 |
+
output_filename = "outputs/z_image_panorama_final.png"
|
| 73 |
+
image.save(output_filename)
|
| 74 |
+
print(f"Image successfully saved as '{output_filename}'")
|
| 75 |
+
|
| 76 |
+
if __name__ == "__main__":
|
| 77 |
+
main()
|
pipeline_z_image_mod.py
ADDED
|
@@ -0,0 +1,214 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2025, DEVAIEXP Team, Alibaba Z-Image Team and The HuggingFace Team. All rights reserved.
|
| 2 |
+
#
|
| 3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
# you may not use this file except in compliance with the License.
|
| 5 |
+
# You may obtain a copy of the License at
|
| 6 |
+
#
|
| 7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
#
|
| 9 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
# See the License for the specific language governing permissions and
|
| 13 |
+
# limitations under the License.
|
| 14 |
+
|
| 15 |
+
import gc
|
| 16 |
+
import torch
|
| 17 |
+
import numpy as np
|
| 18 |
+
from enum import Enum
|
| 19 |
+
from typing import List, Optional, Union
|
| 20 |
+
|
| 21 |
+
from diffusers.utils import logging
|
| 22 |
+
from diffusers.pipelines.z_image.pipeline_z_image import ZImagePipeline, calculate_shift
|
| 23 |
+
from diffusers.pipelines.z_image.pipeline_output import ZImagePipelineOutput
|
| 24 |
+
|
| 25 |
+
logger = logging.get_logger(__name__)
|
| 26 |
+
|
| 27 |
+
# Tiling Engine Helper Functions
|
| 28 |
+
def _adaptive_tile_size(image_size, base_tile_size=512, max_tile_size=1280):
|
| 29 |
+
width, height = image_size; aspect_ratio = width / height
|
| 30 |
+
if aspect_ratio > 1:
|
| 31 |
+
tile_width = min(width, max_tile_size); tile_height = min(int(tile_width / aspect_ratio), max_tile_size)
|
| 32 |
+
else:
|
| 33 |
+
tile_height = min(height, max_tile_size); tile_width = min(int(tile_height * aspect_ratio), max_tile_size)
|
| 34 |
+
return max(tile_width, base_tile_size), max(tile_height, base_tile_size)
|
| 35 |
+
|
| 36 |
+
def _calculate_tile_positions(image_dim: int, tile_dim: int, overlap: int) -> List[int]:
|
| 37 |
+
if image_dim <= tile_dim: return [0]
|
| 38 |
+
positions = []; current_pos = 0; stride = tile_dim - overlap
|
| 39 |
+
while True:
|
| 40 |
+
positions.append(current_pos)
|
| 41 |
+
if current_pos + tile_dim >= image_dim: break
|
| 42 |
+
current_pos += stride
|
| 43 |
+
if current_pos > image_dim - tile_dim: break
|
| 44 |
+
if positions[-1] + tile_dim < image_dim: positions.append(image_dim - tile_dim)
|
| 45 |
+
return sorted(list(set(positions)))
|
| 46 |
+
|
| 47 |
+
def _tile2pixel_indices(tile_row_pos, tile_col_pos, tile_width, tile_height, image_width, image_height):
|
| 48 |
+
px_row_init = tile_row_pos; px_col_init = tile_col_pos
|
| 49 |
+
px_row_end = min(px_row_init + tile_height, image_height)
|
| 50 |
+
px_col_end = min(px_col_init + tile_width, image_width)
|
| 51 |
+
return px_row_init, px_row_end, px_col_init, px_col_end
|
| 52 |
+
|
| 53 |
+
def _tile2latent_indices(px_row_init, px_row_end, px_col_init, px_col_end, vae_scale_factor):
|
| 54 |
+
return px_row_init // vae_scale_factor, px_row_end // vae_scale_factor, px_col_init // vae_scale_factor, px_col_end // vae_scale_factor
|
| 55 |
+
|
| 56 |
+
def release_memory(device):
|
| 57 |
+
gc.collect()
|
| 58 |
+
if torch.cuda.is_available():
|
| 59 |
+
torch.cuda.empty_cache()
|
| 60 |
+
|
| 61 |
+
class ZImageMoDTilingPipeline(ZImagePipeline):
|
| 62 |
+
class TileWeightingMethod(Enum):
|
| 63 |
+
COSINE = "Cosine"; GAUSSIAN = "Gaussian"
|
| 64 |
+
|
| 65 |
+
def _generate_gaussian_weights(self, tile_width, tile_height, nbatches, device, dtype, sigma=0.4):
|
| 66 |
+
latent_width, latent_height = tile_width // self.vae_scale_factor, tile_height // self.vae_scale_factor
|
| 67 |
+
x, y = np.linspace(-1, 1, latent_width), np.linspace(-1, 1, latent_height)
|
| 68 |
+
xx, yy = np.meshgrid(x, y)
|
| 69 |
+
gaussian_weight_np = np.exp(-(xx**2 + yy**2) / (2 * sigma**2))
|
| 70 |
+
weights_torch_f32 = torch.tensor(gaussian_weight_np, device=device, dtype=torch.float32)
|
| 71 |
+
weights_torch_target_dtype = weights_torch_f32.to(dtype)
|
| 72 |
+
return torch.tile(weights_torch_target_dtype, (nbatches, self.transformer.in_channels, 1, 1))
|
| 73 |
+
|
| 74 |
+
def _generate_cosine_weights(self, tile_width, tile_height, nbatches, device, dtype):
|
| 75 |
+
latent_width, latent_height = tile_width // self.vae_scale_factor, tile_height // self.vae_scale_factor
|
| 76 |
+
x, y = np.arange(latent_width), np.arange(latent_height)
|
| 77 |
+
mid_x, mid_y = (latent_width - 1) / 2, (latent_height - 1) / 2
|
| 78 |
+
x_probs, y_probs = np.cos(np.pi * (x - mid_x) / latent_width), np.cos(np.pi * (y - mid_y) / latent_height)
|
| 79 |
+
weights_np = np.outer(y_probs, x_probs)
|
| 80 |
+
weights_torch = torch.tensor(weights_np, device=device, dtype=dtype)
|
| 81 |
+
return torch.tile(weights_torch, (nbatches, self.transformer.in_channels, 1, 1))
|
| 82 |
+
|
| 83 |
+
def prepare_tiles_weights(self, y_steps, x_steps, tile_height, tile_width, final_height, final_width, tile_weighting_method, tile_gaussian_sigma, batch_size, device, dtype):
|
| 84 |
+
tile_weights = np.empty((len(y_steps), len(x_steps)), dtype=object)
|
| 85 |
+
for row, y_start in enumerate(y_steps):
|
| 86 |
+
for col, x_start in enumerate(x_steps):
|
| 87 |
+
_, px_row_end, _, px_col_end = _tile2pixel_indices(y_start, x_start, tile_width, tile_height, final_width, final_height)
|
| 88 |
+
current_tile_h, current_tile_w = px_row_end - y_start, px_col_end - x_start
|
| 89 |
+
if tile_weighting_method == self.TileWeightingMethod.COSINE.value:
|
| 90 |
+
tile_weights[row, col] = self._generate_cosine_weights(current_tile_w, current_tile_h, batch_size, device, dtype)
|
| 91 |
+
else:
|
| 92 |
+
tile_weights[row, col] = self._generate_gaussian_weights(current_tile_w, current_tile_h, batch_size, device, dtype, sigma=tile_gaussian_sigma)
|
| 93 |
+
return tile_weights
|
| 94 |
+
|
| 95 |
+
@torch.no_grad()
|
| 96 |
+
def __call__(
|
| 97 |
+
self,
|
| 98 |
+
prompt: Union[str, List[List[str]]],
|
| 99 |
+
height: int = 1024,
|
| 100 |
+
width: int = 1024,
|
| 101 |
+
num_inference_steps: int = 9,
|
| 102 |
+
guidance_scale: float = 0.0,
|
| 103 |
+
generator: Optional[Union[torch.Generator, List[torch.Generator]]] = None,
|
| 104 |
+
max_tile_size: int = 1024,
|
| 105 |
+
tile_overlap: int = 256,
|
| 106 |
+
tile_weighting_method: str = "Cosine",
|
| 107 |
+
tile_gaussian_sigma: float = 0.4,
|
| 108 |
+
guidance_scale_tiles: Optional[List[List[float]]] = None,
|
| 109 |
+
max_sequence_length: int = 512,
|
| 110 |
+
output_type: Optional[str] = "pil",
|
| 111 |
+
return_dict: bool = True,
|
| 112 |
+
**kwargs,
|
| 113 |
+
):
|
| 114 |
+
# Handle negative_prompt from kwargs for backward compatibility if needed, but default to empty
|
| 115 |
+
negative_prompt = kwargs.get("negative_prompt", "")
|
| 116 |
+
|
| 117 |
+
device = self._execution_device; batch_size = 1
|
| 118 |
+
is_prompt_grid = isinstance(prompt, list) and all(isinstance(row, list) for row in prompt)
|
| 119 |
+
PIXEL_MULTIPLE = self.vae_scale_factor * 2 # 16
|
| 120 |
+
|
| 121 |
+
# Grid and Dimension Calculation
|
| 122 |
+
if is_prompt_grid:
|
| 123 |
+
grid_rows, grid_cols = len(prompt), len(prompt[0])
|
| 124 |
+
tile_width = (width + (grid_cols - 1) * tile_overlap) // grid_cols
|
| 125 |
+
tile_height = (height + (grid_rows - 1) * tile_overlap) // grid_rows
|
| 126 |
+
tile_width -= tile_width % PIXEL_MULTIPLE; tile_height -= tile_height % PIXEL_MULTIPLE
|
| 127 |
+
final_width = tile_width * grid_cols - (grid_cols - 1) * tile_overlap
|
| 128 |
+
final_height = tile_height * grid_rows - (grid_rows - 1) * tile_overlap
|
| 129 |
+
x_steps = [i * (tile_width - tile_overlap) for i in range(grid_cols)]
|
| 130 |
+
y_steps = [i * (tile_height - tile_overlap) for i in range(grid_rows)]
|
| 131 |
+
else:
|
| 132 |
+
final_width, final_height = width, height
|
| 133 |
+
tile_width, tile_height = _adaptive_tile_size((final_width, final_height), max_tile_size=max_tile_size)
|
| 134 |
+
tile_width -= tile_width % PIXEL_MULTIPLE; tile_height -= tile_height % PIXEL_MULTIPLE
|
| 135 |
+
y_steps = _calculate_tile_positions(final_height, tile_height, tile_overlap)
|
| 136 |
+
x_steps = _calculate_tile_positions(final_width, tile_width, tile_overlap)
|
| 137 |
+
grid_rows, grid_cols = len(y_steps), len(x_steps)
|
| 138 |
+
|
| 139 |
+
# Prompt Encoding
|
| 140 |
+
text_embeddings = []
|
| 141 |
+
for r in range(grid_rows):
|
| 142 |
+
row_embeddings = []
|
| 143 |
+
for c in range(grid_cols):
|
| 144 |
+
p = prompt[r][c] if is_prompt_grid else prompt
|
| 145 |
+
prompt_embeds, _ = self.encode_prompt(
|
| 146 |
+
prompt=p, do_classifier_free_guidance=False, device=device, max_sequence_length=max_sequence_length
|
| 147 |
+
)
|
| 148 |
+
row_embeddings.append({"prompt": prompt_embeds})
|
| 149 |
+
text_embeddings.append(row_embeddings)
|
| 150 |
+
|
| 151 |
+
# Latent and Scheduler Setup
|
| 152 |
+
num_latent_channels = self.transformer.in_channels
|
| 153 |
+
latents = self.prepare_latents(
|
| 154 |
+
batch_size, num_latent_channels, final_height, final_width, torch.float32, device, generator
|
| 155 |
+
)
|
| 156 |
+
|
| 157 |
+
image_seq_len = (tile_height // 2) * (tile_width // 2)
|
| 158 |
+
mu = calculate_shift(image_seq_len)
|
| 159 |
+
self.scheduler.set_timesteps(num_inference_steps, device=device, mu=mu)
|
| 160 |
+
timesteps = self.scheduler.timesteps
|
| 161 |
+
|
| 162 |
+
# Prepare Weights and Offload
|
| 163 |
+
tile_weights = self.prepare_tiles_weights(y_steps, x_steps, tile_height, tile_width, final_height, final_width, tile_weighting_method, tile_gaussian_sigma, batch_size, device, latents.dtype)
|
| 164 |
+
self.text_encoder.to("cpu");
|
| 165 |
+
release_memory(device)
|
| 166 |
+
|
| 167 |
+
# Denoising Loop
|
| 168 |
+
with self.progress_bar(total=num_inference_steps) as progress_bar:
|
| 169 |
+
for t in timesteps:
|
| 170 |
+
denoised_step_canvas = torch.zeros_like(latents)
|
| 171 |
+
contributors = torch.zeros_like(latents)
|
| 172 |
+
|
| 173 |
+
for r, y_start in enumerate(y_steps):
|
| 174 |
+
for c, x_start in enumerate(x_steps):
|
| 175 |
+
px_r_init, px_r_end, px_c_init, px_c_end = _tile2pixel_indices(y_start, x_start, tile_width, tile_height, final_width, final_height)
|
| 176 |
+
r_init, r_end, c_init, c_end = _tile2latent_indices(px_r_init, px_r_end, px_c_init, px_c_end, self.vae_scale_factor)
|
| 177 |
+
|
| 178 |
+
tile_latents = latents[:, :, r_init:r_end, c_init:c_end]
|
| 179 |
+
latents_typed = tile_latents.to(self.transformer.dtype)
|
| 180 |
+
embeds = text_embeddings[r][c]
|
| 181 |
+
timestep_model_input = t.expand(latents_typed.shape[0])
|
| 182 |
+
timestep_norm = (1000 - timestep_model_input) / 1000
|
| 183 |
+
|
| 184 |
+
latent_model_input_list = list(latents_typed.unsqueeze(2).unbind(dim=0))
|
| 185 |
+
|
| 186 |
+
model_out_list = self.transformer(
|
| 187 |
+
latent_model_input_list,
|
| 188 |
+
timestep_norm,
|
| 189 |
+
embeds["prompt"],
|
| 190 |
+
)[0]
|
| 191 |
+
|
| 192 |
+
|
| 193 |
+
noise_pred_tile = model_out_list[0].float()
|
| 194 |
+
noise_pred_tile = -noise_pred_tile.squeeze(1)
|
| 195 |
+
|
| 196 |
+
denoised_step_canvas[:, :, r_init:r_end, c_init:c_end] += noise_pred_tile * tile_weights[r, c]
|
| 197 |
+
contributors[:, :, r_init:r_end, c_init:c_end] += tile_weights[r, c]
|
| 198 |
+
|
| 199 |
+
noise_pred_canvas = denoised_step_canvas / contributors
|
| 200 |
+
latents = self.scheduler.step(noise_pred_canvas.to(torch.float32), t, latents).prev_sample
|
| 201 |
+
progress_bar.update()
|
| 202 |
+
|
| 203 |
+
# Post-processing
|
| 204 |
+
if output_type == "latent":
|
| 205 |
+
image = latents
|
| 206 |
+
else:
|
| 207 |
+
self.vae.to(device)
|
| 208 |
+
latents = latents.to(self.vae.dtype)
|
| 209 |
+
latents = (latents / self.vae.config.scaling_factor) + self.vae.config.shift_factor
|
| 210 |
+
image = self.vae.decode(latents, return_dict=False)[0]
|
| 211 |
+
image = self.image_processor.postprocess(image, output_type=output_type)
|
| 212 |
+
|
| 213 |
+
self.maybe_free_model_hooks();
|
| 214 |
+
return ZImagePipelineOutput(images=image) if return_dict else image
|
prompt_check.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import re
|
| 2 |
+
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
def clean_model_output(text):
|
| 7 |
+
text = re.sub(r"<think>.*?</think>", "", text, flags=re.DOTALL)
|
| 8 |
+
text = re.sub(r"\n*(assistant|user)\n*", "", text)
|
| 9 |
+
text = re.sub(r"\n+", "\n", text).strip()
|
| 10 |
+
return text
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def is_unsafe_prompt(model, device, tokenizer, system_prompt=None, user_prompt=None, max_new_token=10):
|
| 14 |
+
messages = [{"role": "system", "content": system_prompt}, {"role": "user", "content": user_prompt}]
|
| 15 |
+
with torch.inference_mode():
|
| 16 |
+
text = tokenizer.apply_chat_template(
|
| 17 |
+
messages,
|
| 18 |
+
tokenize=False,
|
| 19 |
+
add_generation_prompt=True,
|
| 20 |
+
enable_thinking=False,
|
| 21 |
+
)
|
| 22 |
+
model = model.to(device)
|
| 23 |
+
model_inputs = tokenizer([text], return_tensors="pt").to(device)
|
| 24 |
+
|
| 25 |
+
generated_ids = model.generate(**model_inputs, max_new_tokens=max_new_token)
|
| 26 |
+
output_ids = generated_ids[0][-max_new_token:].tolist()
|
| 27 |
+
|
| 28 |
+
content = tokenizer.decode(output_ids, skip_special_tokens=True).strip("\n")
|
| 29 |
+
|
| 30 |
+
return "yes" in content.lower()
|
requirements.txt
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio[mcp]==5.50.0
|
| 2 |
+
spaces
|
| 3 |
+
git+https://github.com/huggingface/diffusers.git@refs/pull/12756/head
|
| 4 |
+
torch==2.8.0
|
| 5 |
+
torchvision==0.23.0
|
| 6 |
+
torchaudio==2.8.0
|
| 7 |
+
transformers==4.56.0
|
| 8 |
+
bitsandbytes==0.48.1
|
| 9 |
+
xformers==0.0.32.post2
|
| 10 |
+
hf_xet
|
| 11 |
+
protobuf
|
| 12 |
+
sacremoses
|
| 13 |
+
sentencepiece
|
| 14 |
+
ligo-segments
|
| 15 |
+
scipy
|
| 16 |
+
triton-windows<3.5; sys_platform == 'win32'
|
| 17 |
+
triton==3.4.0; sys_platform != 'win32'
|
requirements_local.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
--extra-index-url https://download.pytorch.org/whl/cu126
|
| 2 |
+
|
| 3 |
+
gradio[mcp]==5.50.0
|
| 4 |
+
spaces
|
| 5 |
+
gradio==5.50.0
|
| 6 |
+
git+https://github.com/huggingface/diffusers.git@refs/pull/12756/head
|
| 7 |
+
torch==2.8.0+cu126
|
| 8 |
+
torchvision==0.23.0+cu126
|
| 9 |
+
torchaudio==2.8.0+cu126
|
| 10 |
+
transformers==4.56.0
|
| 11 |
+
bitsandbytes==0.48.1
|
| 12 |
+
xformers==0.0.32.post2
|
| 13 |
+
hf_xet
|
| 14 |
+
gguf
|
| 15 |
+
protobuf
|
| 16 |
+
sacremoses
|
| 17 |
+
sentencepiece
|
| 18 |
+
ligo-segments
|
| 19 |
+
scipy
|
| 20 |
+
triton-windows<3.5; sys_platform == 'win32'
|
| 21 |
+
triton==3.4.0; sys_platform != 'win32'
|
style.css
ADDED
|
@@ -0,0 +1,246 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
| 1 |
+
/* ==========================================================================
|
| 2 |
+
1. GLOBAL & LAYOUT STYLES
|
| 3 |
+
========================================================================== */
|
| 4 |
+
|
| 5 |
+
body {
|
| 6 |
+
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
|
| 7 |
+
margin: 0;
|
| 8 |
+
padding: 0;
|
| 9 |
+
}
|
| 10 |
+
|
| 11 |
+
/* Main application container styling */
|
| 12 |
+
.gradio-container {
|
| 13 |
+
border-radius: 15px;
|
| 14 |
+
padding: 10px 20px;
|
| 15 |
+
box-shadow: 0 8px 30px rgba(0, 0, 0, 0.3);
|
| 16 |
+
margin: 10px;
|
| 17 |
+
}
|
| 18 |
+
.column-container {
|
| 19 |
+
border-radius: 15px;
|
| 20 |
+
padding: 15px;
|
| 21 |
+
box-shadow: 0 8px 30px rgba(0, 0, 0, 0.3);
|
| 22 |
+
margin: auto;
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
.action-console-col {
|
| 26 |
+
padding: 10px !important;
|
| 27 |
+
border-color: var(--border-color-primary) !important;
|
| 28 |
+
border-width: 1px !important;
|
| 29 |
+
border-radius: 8px;
|
| 30 |
+
margin-top: 10px;
|
| 31 |
+
}
|
| 32 |
+
.chatbot-container .message-row{
|
| 33 |
+
max-width: calc(100% - var(--spacing-sm) * 6) !important;
|
| 34 |
+
}
|
| 35 |
+
.chatbot-container pre code {
|
| 36 |
+
text-wrap: auto !important;
|
| 37 |
+
}
|
| 38 |
+
/* 2. Media Query for larger screens (desktops) */
|
| 39 |
+
@media (min-width: 1281px) {
|
| 40 |
+
.gradio-container {
|
| 41 |
+
padding: 10px 370px;
|
| 42 |
+
max-width: 2400px;
|
| 43 |
+
}
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
/* Text shadow for main headings for better readability */
|
| 47 |
+
.gradio-container h1 {
|
| 48 |
+
text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.2);
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
/* Utility class to make elements fill their container's width */
|
| 52 |
+
/* .fillable {
|
| 53 |
+
width: 100% !important;
|
| 54 |
+
max-width: unset !important;
|
| 55 |
+
} */
|
| 56 |
+
#trace-log {
|
| 57 |
+
height: 200px !important;
|
| 58 |
+
overflow-y: auto !important;
|
| 59 |
+
background-color: var(--background-fill-primary);
|
| 60 |
+
border: 1px solid var(--border-color-primary);
|
| 61 |
+
border-radius: 8px;
|
| 62 |
+
padding: 10px;
|
| 63 |
+
}
|
| 64 |
+
.fillable .sidebar-parent {
|
| 65 |
+
padding-left: 10px !important;
|
| 66 |
+
padding-right: 10px !important;
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
/* ==========================================================================
|
| 71 |
+
2. CUSTOM SCROLLBAR STYLES (GLOBAL & SIDEBAR)
|
| 72 |
+
========================================================================== */
|
| 73 |
+
|
| 74 |
+
/* --- For WebKit browsers (Chrome, Safari, Edge, etc.) --- */
|
| 75 |
+
.sidebar-content::-webkit-scrollbar,
|
| 76 |
+
body::-webkit-scrollbar {
|
| 77 |
+
width: 8px;
|
| 78 |
+
height: 8px;
|
| 79 |
+
background-color: transparent;
|
| 80 |
+
}
|
| 81 |
+
|
| 82 |
+
.sidebar-content::-webkit-scrollbar-track,
|
| 83 |
+
body::-webkit-scrollbar-track {
|
| 84 |
+
background: transparent;
|
| 85 |
+
border-radius: 10px;
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
.sidebar-content::-webkit-scrollbar-thumb,
|
| 89 |
+
body::-webkit-scrollbar-thumb {
|
| 90 |
+
background-color: rgba(136, 136, 136, 0.4);
|
| 91 |
+
border-radius: 10px;
|
| 92 |
+
border: 2px solid transparent;
|
| 93 |
+
background-clip: content-box;
|
| 94 |
+
}
|
| 95 |
+
|
| 96 |
+
.sidebar-content::-webkit-scrollbar-thumb:hover,
|
| 97 |
+
body::-webkit-scrollbar-thumb:hover {
|
| 98 |
+
background-color: rgba(136, 136, 136, 0.7);
|
| 99 |
+
}
|
| 100 |
+
|
| 101 |
+
/* --- For Firefox --- */
|
| 102 |
+
.sidebar-content,
|
| 103 |
+
html {
|
| 104 |
+
scrollbar-width: thin;
|
| 105 |
+
scrollbar-color: rgba(136, 136, 136, 0.7) transparent;
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
/* ==========================================================================
|
| 110 |
+
3. SIDEBAR STYLES
|
| 111 |
+
========================================================================== */
|
| 112 |
+
|
| 113 |
+
.sidebar {
|
| 114 |
+
border-radius: 10px;
|
| 115 |
+
padding: 10px;
|
| 116 |
+
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
|
| 117 |
+
}
|
| 118 |
+
|
| 119 |
+
/* Overrides for the content padding inside the sidebar */
|
| 120 |
+
.sidebar .sidebar-content {
|
| 121 |
+
padding-left: 10px !important;
|
| 122 |
+
padding-right: 10px !important;
|
| 123 |
+
}
|
| 124 |
+
|
| 125 |
+
/* Centers text within Markdown blocks inside the sidebar */
|
| 126 |
+
/* .sidebar .sidebar-content .column .block div .prose {
|
| 127 |
+
text-align: center;
|
| 128 |
+
} */
|
| 129 |
+
|
| 130 |
+
/* Stylish override for the sidebar toggle button */
|
| 131 |
+
.sidebar .toggle-button {
|
| 132 |
+
background: linear-gradient(135deg, var(--primary-500), var(--primary-600)) !important;
|
| 133 |
+
border: none;
|
| 134 |
+
padding: 12px 18px;
|
| 135 |
+
text-transform: uppercase;
|
| 136 |
+
font-weight: bold;
|
| 137 |
+
letter-spacing: 1px;
|
| 138 |
+
/* border-radius: 5px; */
|
| 139 |
+
position: absolute;
|
| 140 |
+
top: 50%;
|
| 141 |
+
right: -28px !important;
|
| 142 |
+
left: auto !important;
|
| 143 |
+
transform: unset !important;
|
| 144 |
+
border-radius: 0 var(--radius-lg) var(--radius-lg) 0 !important
|
| 145 |
+
}
|
| 146 |
+
|
| 147 |
+
.sidebar.right .toggle-button {
|
| 148 |
+
left: -28px !important;
|
| 149 |
+
right: auto !important;
|
| 150 |
+
transform: rotate(180deg) !important;
|
| 151 |
+
}
|
| 152 |
+
|
| 153 |
+
.sidebar.open .chevron-left {
|
| 154 |
+
transform: rotate(-135deg);
|
| 155 |
+
}
|
| 156 |
+
|
| 157 |
+
.bottom-bar .toggle-bottom-button {
|
| 158 |
+
background: linear-gradient(135deg, var(--primary-500), var(--primary-600)) !important;
|
| 159 |
+
}
|
| 160 |
+
|
| 161 |
+
.toggle-button:hover {
|
| 162 |
+
transform: scale(1.05);
|
| 163 |
+
}
|
| 164 |
+
.bottom-bar .toggle-bottom-button .chevron {
|
| 165 |
+
width: 100%;
|
| 166 |
+
height: 100%;
|
| 167 |
+
position: relative;
|
| 168 |
+
display: flex;
|
| 169 |
+
align-items: center;
|
| 170 |
+
justify-content: center;
|
| 171 |
+
}
|
| 172 |
+
|
| 173 |
+
.bottom-bar .toggle-bottom-button .chevron-arrow {
|
| 174 |
+
position: relative !important;
|
| 175 |
+
top: auto !important;
|
| 176 |
+
left: auto !important;
|
| 177 |
+
width: 10px !important;
|
| 178 |
+
height: 10px !important;
|
| 179 |
+
border-bottom: 2px solid var(--body-text-color) !important;
|
| 180 |
+
border-right: 2px solid var(--body-text-color) !important;
|
| 181 |
+
transform: rotate(-135deg) translateY(2px);
|
| 182 |
+
margin-bottom: -4px;
|
| 183 |
+
}
|
| 184 |
+
.bottom-bar.open .chevron-arrow {
|
| 185 |
+
transform: rotate(45deg) translateY(-2px) !important;
|
| 186 |
+
}
|
| 187 |
+
|
| 188 |
+
/* ==========================================================================
|
| 189 |
+
5. COMPONENT-SPECIFIC STYLES & OVERRIDES
|
| 190 |
+
========================================================================== */
|
| 191 |
+
|
| 192 |
+
/* Custom styling for the cancel button */
|
| 193 |
+
#cancel-button { /* Corrected the typo from 'cancell' to 'cancel' */
|
| 194 |
+
background: linear-gradient(120deg, var(--neutral-500) 0%, var(--neutral-600) 60%, var(--neutral-700) 100%) !important;
|
| 195 |
+
}
|
| 196 |
+
.custom-dropdown .wrap-inner input {
|
| 197 |
+
padding-right: 22px;
|
| 198 |
+
}
|
| 199 |
+
|
| 200 |
+
#pagination-row {
|
| 201 |
+
display: flex !important;
|
| 202 |
+
justify-content: center !important;
|
| 203 |
+
align-items: center !important;
|
| 204 |
+
gap: 4px !important;
|
| 205 |
+
margin-top: 8px !important;
|
| 206 |
+
padding: 0 !important;
|
| 207 |
+
min-height: 0 !important;
|
| 208 |
+
background: transparent !important;
|
| 209 |
+
border: none !important;
|
| 210 |
+
}
|
| 211 |
+
|
| 212 |
+
.pagination-btn {
|
| 213 |
+
width: 28px !important;
|
| 214 |
+
height: 28px !important;
|
| 215 |
+
min-width: 28px !important;
|
| 216 |
+
padding: 0 !important;
|
| 217 |
+
display: flex !important;
|
| 218 |
+
align-items: center !important;
|
| 219 |
+
justify-content: center !important;
|
| 220 |
+
font-size: 12px !important;
|
| 221 |
+
line-height: 1 !important;
|
| 222 |
+
border-radius: 4px !important;
|
| 223 |
+
flex-grow: 0 !important;
|
| 224 |
+
box-shadow: none !important;
|
| 225 |
+
}
|
| 226 |
+
|
| 227 |
+
#page_label {
|
| 228 |
+
flex: 0 0 auto !important;
|
| 229 |
+
width: auto !important;
|
| 230 |
+
display: inline-block !important;
|
| 231 |
+
margin: 0 8px !important;
|
| 232 |
+
font-size: 11px !important;
|
| 233 |
+
font-family: monospace !important;
|
| 234 |
+
white-space: nowrap !important;
|
| 235 |
+
color: var(--body-text-color-subdued);
|
| 236 |
+
}
|
| 237 |
+
|
| 238 |
+
#page_label p {
|
| 239 |
+
margin: 0 !important;
|
| 240 |
+
padding: 0 !important;
|
| 241 |
+
display: inline !important;
|
| 242 |
+
}
|
| 243 |
+
#chat-examples {
|
| 244 |
+
max-height: 300px;
|
| 245 |
+
overflow: auto !important;
|
| 246 |
+
}
|