Upload app.py
Browse files64 unmodified lines6566676869707168697071727374757674777879808179828384854 unmodified lines90919290919293949596979899939495262 unmodified lines358359360368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433361362363364365366367161 unmodified lines52953053160160260360460560660753253353464 unmodified linesREALISM_LORA_SCALE = float(os.environ.get("H3_REALISM_LORA_SCALE", "0.8"))REALISM_LORA_TRIGGER = "r34l1sm"
# Polyhedron: Perfect Eyes / Perfect Skin / Perfect Hands. The Civitai model# has multiple versions, so the loader resolves the current version/file at# startup instead of hard-coding a version ID that may become stale.DETAIL_LORA_REPO = os.environ.get("H3_DETAIL_LORA_REPO", "civitai-model://2899220")# Perfect Eyes / Skin / Hands. The original release is on Civitai, but the# weights are mirrored in a Hugging Face repository so this Space never needs# to contact Civitai at startup. The filename is pinned; no model-page/API lookup.DETAIL_LORA_REPO = os.environ.get( "H3_DETAIL_LORA_REPO", "Alex995647/loras-minimax-h3",)DETAIL_LORA_FILE = os.environ.get( "H3_DETAIL_LORA_FILE", "polyhedron_minimax_h3_image_lora_v2__perfe8ct.safetensors", "minimax-h3-t2va-t2v-polyhedron-perfect-eyes-perfect-skin/polyhedron_minimax_h3_image_lora_v2__perfe8ct.safetensors",)DETAIL_LORA_SCALE = float(os.environ.get("H3_DETAIL_LORA_SCALE", "0.7"))
# Spatial & Physics: object collisions, gravity, stacking, falling, occlusion, and# more consistent spatial relationships. The pruned H3 adapter does not require a trigger word.# more consistent spatial relationships. The clean pruned H3 adapter has no trigger word.SPATIAL_LORA_REPO = os.environ.get( "H3_SPATIAL_LORA_REPO", "Jojocodex/minimax-h3-spatial-physics-lora",4 unmodified lines)SPATIAL_LORA_SCALE = float(os.environ.get("H3_SPATIAL_LORA_SCALE", "0.4"))
BLOWJOB_LORA_REPO = os.environ.get( "H3_BLOWJOB_LORA_REPO", "iamgroot1212/minimax-h3-loras",)BLOWJOB_LORA_FILE = os.environ.get( "H3_BLOWJOB_LORA_FILE", "MMH3-Hybrid-BlowJob-lora.safetensors",)BLOWJOB_LORA_SCALE = float(os.environ.get("H3_BLOWJOB_LORA_SCALE", "1.0"))
CONDITIONER_SPACE = os.environ.get("H3_CONDITIONER", "multimodalart/qwen3vl-conditioner")# ``pack`` places the transformer at startup (66 GB on disk via the spaces pack); VAEs move on first GPU call.PLACEMENT = os.environ.get("H3_PLACEMENT", "pack").lower()262 unmodified lines from huggingface_hub import hf_hub_download from safetensors.torch import load_file
if repo_id.startswith("civitai-model://"): # Resolve a Civitai model page to its newest model version, then use # the same streaming downloader as the explicit civitai://version-id # path. This keeps the Space independent of a hard-coded version ID. import requests model_id = repo_id.removeprefix("civitai-model://") api_url = f"https://civitai.com/api/v1/models/{model_id}" metadata = requests.get(api_url, timeout=30).json() versions = metadata.get("modelVersions") or [] if not versions: raise ValueError(f"{label}: Civitai model {model_id} has no model versions") version = versions[0] model_version_id = str(version["id"]) files = [f for f in (version.get("files") or []) if str(f.get("name", "")).lower().endswith(".safetensors")] if not files: raise ValueError(f"{label}: Civitai version {model_version_id} has no .safetensors file") # Prefer the requested filename when it exists; otherwise use the first # safetensors file exposed by the current version. selected = next((f for f in files if f.get("name") == filename), files[0]) filename = os.path.basename(selected["name"]) download_url = selected.get("downloadUrl") or f"https://civitai.com/api/download/models/{model_version_id}" cache_dir = os.path.join(tempfile.gettempdir(), "h3-lora-cache") os.makedirs(cache_dir, exist_ok=True) path = os.path.join(cache_dir, filename) if not os.path.isfile(path) or os.path.getsize(path) == 0: tmp_path = f"{path}.part" with requests.get(download_url, stream=True, timeout=120, allow_redirects=True) as response: response.raise_for_status() with open(tmp_path, "wb") as handle: for chunk in response.iter_content(chunk_size=8 * 1024 * 1024): if chunk: handle.write(chunk) os.replace(tmp_path, path) elif repo_id.startswith("civitai://"): import requests model_version_id = repo_id.removeprefix("civitai://") download_url = f"https://civitai.com/api/download/models/{model_version_id}" cache_dir = os.path.join(tempfile.gettempdir(), "h3-lora-cache") os.makedirs(cache_dir, exist_ok=True) path = os.path.join(cache_dir, filename) if not os.path.isfile(path) or os.path.getsize(path) == 0: tmp_path = f"{path}.part" with requests.get(download_url, stream=True, timeout=120, allow_redirects=True) as response: response.raise_for_status() with open(tmp_path, "wb") as handle: for chunk in response.iter_content(chunk_size=8 * 1024 * 1024): if chunk: handle.write(chunk) os.replace(tmp_path, path) elif repo_id == "aronzhan/H3-Loras-bucket": import requests bucket_url = f"https://huggingface.co/buckets/{repo_id}/resolve/{filename}" cache_dir = os.path.join(tempfile.gettempdir(), "h3-lora-cache") os.makedirs(cache_dir, exist_ok=True) path = os.path.join(cache_dir, filename) if not os.path.isfile(path) or os.path.getsize(path) == 0: tmp_path = f"{path}.part" with requests.get(bucket_url, stream=True, timeout=60, allow_redirects=True) as response: response.raise_for_status() with open(tmp_path, "wb") as handle: for chunk in response.iter_content(chunk_size=8 * 1024 * 1024): if chunk: handle.write(chunk) os.replace(tmp_path, path) else: path = hf_hub_download(repo_id, filename) # All adapters used by this Space are pinned to repository/file paths. # There is deliberately no Civitai/API fallback here: startup must only # resolve repository files through the Hugging Face Hub. path = hf_hub_download(repo_id, filename)
lora = load_file(path, device="cpu")
161 unmodified lines "Spatial & Physics LoRA", SPATIAL_LORA_SCALE, ), _apply_extra_lora( transformer, BLOWJOB_LORA_REPO, BLOWJOB_LORA_FILE, "MiniMax H3 BlowJob LoRA", BLOWJOB_LORA_SCALE, ), ]
# ββ Model loading βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββapp.py+13-89