"""LTX-2.3 Gemma text-encoder backend (API Space for the Pre-Production Playground). Runs Gemma-3-12B + the LTX-2.3 embeddings connectors and returns final EmbeddingsProcessorOutput tensors (video/audio encodings + attention mask) as a .pt file, for positive and (optionally) negative prompts — so the generation backends don't need to hold Gemma in VRAM. Mirrors helpers.encode_prompts at the pinned LTX-2 commit. Adapted from linoyts/gemma-text-encoder (LTX-2) to LTX-2.3. """ import os import subprocess import sys os.environ["TORCH_COMPILE_DISABLE"] = "1" os.environ["TORCHDYNAMO_DISABLE"] = "1" LTX_REPO_URL = "https://github.com/Lightricks/LTX-2.git" LTX_REPO_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "LTX-2") LTX_COMMIT_SHA = os.environ.get("LTX_COMMIT_SHA", "ae855f8538843825f9015a419cf4ba5edaf5eec2") if not os.path.exists(LTX_REPO_DIR): os.makedirs(LTX_REPO_DIR) subprocess.run(["git", "init", LTX_REPO_DIR], check=True) subprocess.run(["git", "remote", "add", "origin", LTX_REPO_URL], cwd=LTX_REPO_DIR, check=True) subprocess.run(["git", "fetch", "--depth", "1", "origin", LTX_COMMIT_SHA], cwd=LTX_REPO_DIR, check=True) subprocess.run(["git", "checkout", LTX_COMMIT_SHA], cwd=LTX_REPO_DIR, check=True) subprocess.run( [sys.executable, "-m", "pip", "install", "--force-reinstall", "--no-deps", "-e", os.path.join(LTX_REPO_DIR, "packages", "ltx-core"), "-e", os.path.join(LTX_REPO_DIR, "packages", "ltx-pipelines")], check=True, ) sys.path.insert(0, os.path.join(LTX_REPO_DIR, "packages", "ltx-pipelines", "src")) sys.path.insert(0, os.path.join(LTX_REPO_DIR, "packages", "ltx-core", "src")) import json import struct import tempfile import time import torch torch._dynamo.config.suppress_errors = True torch._dynamo.config.disable = True import gradio as gr import spaces from huggingface_hub import hf_hub_download, snapshot_download from ltx_pipelines.utils.helpers import generate_enhanced_prompt from ltx_pipelines.utils.model_ledger import ModelLedger # Chunked-read safetensors loader: safe_open mmap deadlocks on FUSE-backed storage. from ltx_core.loader.primitives import StateDict from ltx_core.loader.sft_loader import SafetensorsStateDictLoader _SAFETENSORS_DTYPE_MAP = { "F64": torch.float64, "F32": torch.float32, "F16": torch.float16, "BF16": torch.bfloat16, "F8_E5M2": torch.float8_e5m2, "F8_E4M3": torch.float8_e4m3fn, "I64": torch.int64, "I32": torch.int32, "I16": torch.int16, "I8": torch.int8, "U8": torch.uint8, "BOOL": torch.bool, } def _patched_load(self, path, sd_ops, device=None): sd, size, dtype = {}, 0, set() device = device or torch.device("cpu") for shard_path in (path if isinstance(path, list) else [path]): with open(shard_path, "rb") as f: header_len = struct.unpack("