Spaces:
Paused
Paused
Update app_quant_latent.py
Browse files- app_quant_latent.py +104 -14
app_quant_latent.py
CHANGED
|
@@ -693,27 +693,70 @@ def list_loras_from_repo(repo_id):
|
|
| 693 |
"""
|
| 694 |
Attempts to find safetensors inside HF cache directory for repo_id.
|
| 695 |
This only scans local cache; it does NOT download anything.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 696 |
"""
|
| 697 |
if not repo_id:
|
| 698 |
return []
|
| 699 |
-
|
| 700 |
safe_list = []
|
| 701 |
-
|
|
|
|
| 702 |
hf_cache = os.path.expanduser("~/.cache/huggingface/hub")
|
| 703 |
-
# Also check /home/user/.cache/huggingface/hub (Spaces environments)
|
| 704 |
alt_cache = "/home/user/.cache/huggingface/hub"
|
| 705 |
-
|
| 706 |
candidates = [hf_cache, alt_cache]
|
| 707 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 708 |
for root_cache in candidates:
|
| 709 |
if not os.path.exists(root_cache):
|
| 710 |
continue
|
| 711 |
-
for
|
| 712 |
-
|
| 713 |
-
|
| 714 |
-
|
| 715 |
-
|
| 716 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 717 |
safe_list = sorted(list(dict.fromkeys(safe_list)))
|
| 718 |
return safe_list
|
| 719 |
|
|
@@ -780,15 +823,62 @@ with gr.Blocks(title="Z-Image-Turbo") as demo:
|
|
| 780 |
|
| 781 |
# load selected lora if provided
|
| 782 |
if lora_path:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 783 |
try:
|
| 784 |
-
# repo_name must be HF repo id where load_lora_weights expects it; if user provided repo id use that
|
| 785 |
pipe.load_lora_weights(repo_name or "rahul7star/ZImageLora",
|
| 786 |
-
weight_name=
|
| 787 |
adapter_name="lora")
|
| 788 |
pipe.set_adapters(["lora"], adapter_weights=[1.])
|
| 789 |
pipe.fuse_lora(adapter_names=["lora"], lora_scale=0.75)
|
|
|
|
| 790 |
except Exception as _e:
|
| 791 |
-
log(f"⚠️ Failed to load selected LoRA during rebuild: {_e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 792 |
|
| 793 |
# finalize
|
| 794 |
debug_pipeline(pipe)
|
|
|
|
| 693 |
"""
|
| 694 |
Attempts to find safetensors inside HF cache directory for repo_id.
|
| 695 |
This only scans local cache; it does NOT download anything.
|
| 696 |
+
|
| 697 |
+
Returns:
|
| 698 |
+
A list of strings suitable for showing in the dropdown. Prefer returning
|
| 699 |
+
paths relative to the repo root (e.g. "NSFW/doggystyle_pov.safetensors") so that
|
| 700 |
+
pipe.load_lora_weights(repo_id, weight_name=that_path) works for nested files.
|
| 701 |
+
If a relative path can't be determined, returns absolute cached file paths.
|
| 702 |
"""
|
| 703 |
if not repo_id:
|
| 704 |
return []
|
| 705 |
+
|
| 706 |
safe_list = []
|
| 707 |
+
|
| 708 |
+
# Candidate cache roots
|
| 709 |
hf_cache = os.path.expanduser("~/.cache/huggingface/hub")
|
|
|
|
| 710 |
alt_cache = "/home/user/.cache/huggingface/hub"
|
|
|
|
| 711 |
candidates = [hf_cache, alt_cache]
|
| 712 |
+
|
| 713 |
+
# Normalize repo variants to search for in path
|
| 714 |
+
owner_repo = repo_id.replace("/", "_")
|
| 715 |
+
owner_repo_dash = repo_id.replace("/", "-")
|
| 716 |
+
owner_repo_double = repo_id.replace("/", "--")
|
| 717 |
+
|
| 718 |
+
# Walk caches and collect safetensors
|
| 719 |
for root_cache in candidates:
|
| 720 |
if not os.path.exists(root_cache):
|
| 721 |
continue
|
| 722 |
+
for dirpath, dirnames, filenames in os.walk(root_cache):
|
| 723 |
+
for f in filenames:
|
| 724 |
+
if not f.endswith(".safetensors"):
|
| 725 |
+
continue
|
| 726 |
+
full_path = os.path.join(dirpath, f)
|
| 727 |
+
|
| 728 |
+
# try to find a repo-root-like substring in dirpath
|
| 729 |
+
chosen_base = None
|
| 730 |
+
for pattern in (owner_repo_double, owner_repo_dash, owner_repo):
|
| 731 |
+
idx = dirpath.find(pattern)
|
| 732 |
+
if idx != -1:
|
| 733 |
+
chosen_base = dirpath[: idx + len(pattern)]
|
| 734 |
+
break
|
| 735 |
+
|
| 736 |
+
# fallback: look for the repo folder name (last component) e.g., "ZImageLora"
|
| 737 |
+
if chosen_base is None:
|
| 738 |
+
repo_tail = repo_id.split("/")[-1]
|
| 739 |
+
idx2 = dirpath.find(repo_tail)
|
| 740 |
+
if idx2 != -1:
|
| 741 |
+
chosen_base = dirpath[: idx2 + len(repo_tail)]
|
| 742 |
+
|
| 743 |
+
# If we found a base that looks like the cached repo root, compute relative path
|
| 744 |
+
if chosen_base:
|
| 745 |
+
try:
|
| 746 |
+
rel = os.path.relpath(full_path, chosen_base)
|
| 747 |
+
# If relpath goes up (starts with ..) then prefer full_path
|
| 748 |
+
if rel and not rel.startswith(".."):
|
| 749 |
+
# Normalize to forward slashes for HF repo weight_name usage
|
| 750 |
+
rel_normalized = rel.replace(os.sep, "/")
|
| 751 |
+
safe_list.append(rel_normalized)
|
| 752 |
+
continue
|
| 753 |
+
except Exception:
|
| 754 |
+
pass
|
| 755 |
+
|
| 756 |
+
# Otherwise append absolute path (last resort)
|
| 757 |
+
safe_list.append(full_path)
|
| 758 |
+
|
| 759 |
+
# remove duplicates and sort
|
| 760 |
safe_list = sorted(list(dict.fromkeys(safe_list)))
|
| 761 |
return safe_list
|
| 762 |
|
|
|
|
| 823 |
|
| 824 |
# load selected lora if provided
|
| 825 |
if lora_path:
|
| 826 |
+
weight_name_to_use = None
|
| 827 |
+
|
| 828 |
+
# If dropdown provided a relative-style path (contains a slash or no leading /),
|
| 829 |
+
# use it directly as weight_name (HF expects "path/inside/repo.safetensors")
|
| 830 |
+
if ("/" in lora_path) and not os.path.isabs(lora_path):
|
| 831 |
+
weight_name_to_use = lora_path
|
| 832 |
+
else:
|
| 833 |
+
# It might be an absolute path in cache; try to compute relative path to repo cache root
|
| 834 |
+
abs_path = lora_path if os.path.isabs(lora_path) else None
|
| 835 |
+
if abs_path and os.path.exists(abs_path):
|
| 836 |
+
# attempt to find repo-root-ish substring in abs_path
|
| 837 |
+
repo_variants = [
|
| 838 |
+
repo_name.replace("/", "--"),
|
| 839 |
+
repo_name.replace("/", "-"),
|
| 840 |
+
repo_name.replace("/", "_"),
|
| 841 |
+
repo_name.split("/")[-1],
|
| 842 |
+
]
|
| 843 |
+
chosen_base = None
|
| 844 |
+
for v in repo_variants:
|
| 845 |
+
idx = abs_path.find(v)
|
| 846 |
+
if idx != -1:
|
| 847 |
+
chosen_base = abs_path[: idx + len(v)]
|
| 848 |
+
break
|
| 849 |
+
if chosen_base:
|
| 850 |
+
try:
|
| 851 |
+
rel = os.path.relpath(abs_path, chosen_base)
|
| 852 |
+
if rel and not rel.startswith(".."):
|
| 853 |
+
weight_name_to_use = rel.replace(os.sep, "/")
|
| 854 |
+
except Exception:
|
| 855 |
+
weight_name_to_use = None
|
| 856 |
+
|
| 857 |
+
# fallback to basename
|
| 858 |
+
if weight_name_to_use is None:
|
| 859 |
+
weight_name_to_use = os.path.basename(lora_path)
|
| 860 |
+
|
| 861 |
+
# Now attempt to load
|
| 862 |
try:
|
|
|
|
| 863 |
pipe.load_lora_weights(repo_name or "rahul7star/ZImageLora",
|
| 864 |
+
weight_name=weight_name_to_use,
|
| 865 |
adapter_name="lora")
|
| 866 |
pipe.set_adapters(["lora"], adapter_weights=[1.])
|
| 867 |
pipe.fuse_lora(adapter_names=["lora"], lora_scale=0.75)
|
| 868 |
+
log(f"✅ Loaded LoRA weight: {weight_name_to_use} from repo {repo_name}")
|
| 869 |
except Exception as _e:
|
| 870 |
+
log(f"⚠️ Failed to load selected LoRA during rebuild using weight_name='{weight_name_to_use}': {_e}")
|
| 871 |
+
# as last resort, try loading using basename
|
| 872 |
+
try:
|
| 873 |
+
fallback_name = os.path.basename(lora_path)
|
| 874 |
+
pipe.load_lora_weights(repo_name or "rahul7star/ZImageLora",
|
| 875 |
+
weight_name=fallback_name,
|
| 876 |
+
adapter_name="lora")
|
| 877 |
+
pipe.set_adapters(["lora"], adapter_weights=[1.])
|
| 878 |
+
pipe.fuse_lora(adapter_names=["lora"], lora_scale=0.75)
|
| 879 |
+
log(f"✅ Fallback loaded LoRA weight basename: {fallback_name}")
|
| 880 |
+
except Exception as _e2:
|
| 881 |
+
log(f"❌ Fallback LoRA load also failed: {_e2}")
|
| 882 |
|
| 883 |
# finalize
|
| 884 |
debug_pipeline(pipe)
|