HelloSun's picture
Add one-command Z-Image generation script
3c02e19 verified
Raw History Blame Contribute Delete
4.29 kB
#!/usr/bin/env bash
set -euo pipefail
# One-command setup -> 1024x1024 generation -> upload to Hugging Face.
ROOT="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
REPO="${HF_REPO:-HelloSun/Z-Image-Turbo-OpenVINO-INT4}"
PROMPT="${1:-A cinematic photograph of a red fox in a snowy pine forest at sunrise, natural light, highly detailed, realistic.}"
OUTPUT="${2:-$ROOT/generated_$(date -u +%Y%m%dT%H%M%SZ).png}"
MODEL_DIR="$ROOT/openvino_model_hf"
RAW_DIR="$ROOT/openvino-model"
BASE_URL="https://huggingface.co/$REPO/resolve/main"
if [[ -n "${OV_PYTHON:-}" ]]; then
PYTHON="$OV_PYTHON"
elif [[ -x "$ROOT/.venv/bin/python" ]]; then
PYTHON="$ROOT/.venv/bin/python"
elif [[ -x /tmp/opencode/zimage-ov-venv/bin/python ]]; then
PYTHON=/tmp/opencode/zimage-ov-venv/bin/python
else
PYTHON="$ROOT/.venv/bin/python"
fi
if [[ ! -x "$PYTHON" ]]; then
VENV_DIR="$(dirname "$(dirname "$PYTHON")")"
printf 'Creating Python environment: %s\n' "$VENV_DIR"
python3 -m venv "$VENV_DIR"
fi
if ! "$PYTHON" -c 'import diffusers, transformers, tokenizers, optimum, openvino, torch, huggingface_hub' >/dev/null 2>&1; then
printf 'Installing inference dependencies into %s\n' "$(dirname "$PYTHON")"
"$PYTHON" -m pip install --disable-pip-version-check \
'diffusers==0.37.1' \
'transformers==4.57.6' \
'tokenizers==0.22.0' \
'optimum==2.3.0' \
'optimum-intel==2.2.0' \
'openvino==2026.4.0' \
'huggingface-hub==0.36.2' \
'torch==2.14.1'
else
printf 'Inference environment exists; skipping dependency installation.\n'
fi
mkdir -p "$RAW_DIR" \
"$MODEL_DIR/scheduler" "$MODEL_DIR/text_encoder" "$MODEL_DIR/tokenizer" \
"$MODEL_DIR/transformer" "$MODEL_DIR/vae_decoder" "$MODEL_DIR/vae_encoder"
download_if_missing() {
local relative="$1"
local destination="$2"
if [[ -s "$destination" ]]; then
printf 'Already present, skipping: %s\n' "$destination"
return
fi
mkdir -p "$(dirname "$destination")"
printf 'Downloading %s\n' "$relative"
curl --silent --show-error --location --fail --retry 4 --retry-delay 2 \
--continue-at - "$BASE_URL/$relative" --output "$destination"
if [[ ! -s "$destination" ]]; then
printf 'Download failed or returned an empty file: %s\n' "$destination" >&2
exit 1
fi
}
# Keep large IR weights outside the diffusers directory and symlink them in.
for component in transformer text_encoder vae_decoder vae_encoder; do
download_if_missing "$component/openvino_model.bin" "$RAW_DIR/$component.bin"
download_if_missing "$component/openvino_model.xml" "$RAW_DIR/$component.xml"
ln -sfn "../../openvino-model/$component.bin" "$MODEL_DIR/$component/openvino_model.bin"
ln -sfn "../../openvino-model/$component.xml" "$MODEL_DIR/$component/openvino_model.xml"
done
for relative in \
model_index.json \
scheduler/scheduler_config.json \
text_encoder/config.json \
transformer/config.json \
vae_decoder/config.json \
vae_encoder/config.json \
tokenizer/added_tokens.json \
tokenizer/chat_template.jinja \
tokenizer/merges.txt \
tokenizer/special_tokens_map.json \
tokenizer/tokenizer_config.json \
tokenizer/vocab.json; do
download_if_missing "$relative" "$MODEL_DIR/$relative"
done
# Fail early if credentials are absent. Credential content is never printed or saved.
if [[ -z "${HF_TOKEN:-}" && -z "${HUGGINGFACE_HUB_TOKEN:-}" ]]; then
printf 'Set HF_TOKEN to a Hugging Face write token for %s, then rerun.\n' "$REPO" >&2
exit 2
fi
export OV_THREADS="${OV_THREADS:-24}"
export OV_TENSOR_PARALLEL="${OV_TENSOR_PARALLEL:-1}"
export OV_HYPERTHREADING="${OV_HYPERTHREADING:-1}"
export OV_PINNING="${OV_PINNING:-1}"
export OV_RUNS=1
export OMP_NUM_THREADS="${OMP_NUM_THREADS:-16}"
export OMP_DYNAMIC=FALSE
export MKL_NUM_THREADS="${MKL_NUM_THREADS:-1}"
export OPENBLAS_NUM_THREADS="${OPENBLAS_NUM_THREADS:-1}"
export TOKENIZERS_PARALLELISM=false
export OV_MODEL_DIR="$MODEL_DIR"
mkdir -p "$(dirname "$OUTPUT")"
printf 'Generating 1024x1024 image: %s\n' "$OUTPUT"
"$PYTHON" "$ROOT/benchmark_openvino.py" "$PROMPT" "$OUTPUT"
printf 'Uploading %s to %s\n' "$OUTPUT" "$REPO"
"$PYTHON" "$ROOT/upload_image.py" "$OUTPUT" --repo "$REPO"