Commit ·
76639c0
1
Parent(s): 3217a86
new update fix
Browse files
app.py
CHANGED
|
@@ -645,12 +645,29 @@ input[type="range"]::-webkit-slider-thumb {
|
|
| 645 |
BASE_MODEL_ID = "black-forest-labs/FLUX.2-klein-4B"
|
| 646 |
LORA_ID = "AnimeOverlord/flux2-klein-4b-mc"
|
| 647 |
|
| 648 |
-
|
| 649 |
-
|
| 650 |
-
|
|
|
|
|
|
|
| 651 |
)
|
| 652 |
-
|
| 653 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 654 |
|
| 655 |
_pipe = None
|
| 656 |
_pipe_ready = False
|
|
@@ -684,8 +701,40 @@ log_event(f"Blocks kwargs: {BLOCKS_KWARGS}")
|
|
| 684 |
log_event(f"Launch kwargs: {LAUNCH_KWARGS}")
|
| 685 |
|
| 686 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 687 |
def _ensure_model_files():
|
| 688 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 689 |
os.makedirs(MODEL_DIR, exist_ok=True)
|
| 690 |
os.makedirs(LORA_DIR, exist_ok=True)
|
| 691 |
|
|
@@ -697,9 +746,9 @@ def _ensure_model_files():
|
|
| 697 |
local_dir=MODEL_DIR,
|
| 698 |
local_dir_use_symlinks=False,
|
| 699 |
)
|
| 700 |
-
log_event("Base model
|
| 701 |
else:
|
| 702 |
-
log_event("Base model already cached")
|
| 703 |
|
| 704 |
# Download LoRA on first run
|
| 705 |
if not os.path.exists(os.path.join(LORA_DIR, "pytorch_lora_weights.safetensors")):
|
|
@@ -710,9 +759,12 @@ def _ensure_model_files():
|
|
| 710 |
local_dir_use_symlinks=False,
|
| 711 |
allow_patterns=["pytorch_lora_weights.safetensors"],
|
| 712 |
)
|
| 713 |
-
log_event("LoRA
|
| 714 |
else:
|
| 715 |
-
log_event("LoRA already cached")
|
|
|
|
|
|
|
|
|
|
| 716 |
|
| 717 |
|
| 718 |
def _load_pipe():
|
|
|
|
| 645 |
BASE_MODEL_ID = "black-forest-labs/FLUX.2-klein-4B"
|
| 646 |
LORA_ID = "AnimeOverlord/flux2-klein-4b-mc"
|
| 647 |
|
| 648 |
+
# Model paths with fallback: check local repo first, then HF cache
|
| 649 |
+
_LOCAL_MODELS = os.path.join(os.path.dirname(__file__), "models")
|
| 650 |
+
_HF_CACHE = os.path.join(
|
| 651 |
+
os.environ.get("HF_HOME", os.path.join(os.path.expanduser("~"), ".cache", "huggingface")),
|
| 652 |
+
"hub"
|
| 653 |
)
|
| 654 |
+
|
| 655 |
+
def _get_model_path(repo_name: str, fallback_name: str) -> str:
|
| 656 |
+
"""Get model path with fallback from local repo to HF cache."""
|
| 657 |
+
local_path = os.path.join(_LOCAL_MODELS, repo_name)
|
| 658 |
+
hf_path = os.path.join(_HF_CACHE, f"models--{fallback_name}")
|
| 659 |
+
|
| 660 |
+
# Check local first
|
| 661 |
+
if os.path.exists(local_path):
|
| 662 |
+
log_event(f"Using local model: {local_path}")
|
| 663 |
+
return local_path
|
| 664 |
+
|
| 665 |
+
# Fallback to HF cache
|
| 666 |
+
log_event(f"Model not in local repo, will use HF cache: {hf_path}")
|
| 667 |
+
return hf_path
|
| 668 |
+
|
| 669 |
+
MODEL_DIR = _get_model_path("FLUX.2-klein-4B", "black-forest-labs--FLUX.2-klein-4B")
|
| 670 |
+
LORA_DIR = _get_model_path("flux2-klein-4b-mc", "AnimeOverlord--flux2-klein-4b-mc")
|
| 671 |
|
| 672 |
_pipe = None
|
| 673 |
_pipe_ready = False
|
|
|
|
| 701 |
log_event(f"Launch kwargs: {LAUNCH_KWARGS}")
|
| 702 |
|
| 703 |
|
| 704 |
+
def _setup_models_on_spaces():
|
| 705 |
+
"""Auto-download and commit models on Spaces (runs once on first deploy)."""
|
| 706 |
+
if not os.environ.get("SPACE_ID"):
|
| 707 |
+
return # Not running on Spaces
|
| 708 |
+
|
| 709 |
+
try:
|
| 710 |
+
log_event("Detected Spaces environment, setting up models...")
|
| 711 |
+
os.system("git config user.email 'space@bot.local'")
|
| 712 |
+
os.system("git config user.name 'Space Bot'")
|
| 713 |
+
os.system("git lfs install")
|
| 714 |
+
|
| 715 |
+
# Track large files
|
| 716 |
+
os.system("git lfs track 'models/**/*.bin' 'models/**/*.safetensors' 'models/**/*.json' 2>/dev/null || true")
|
| 717 |
+
os.system("git add .gitattributes 2>/dev/null || true")
|
| 718 |
+
|
| 719 |
+
# Commit models if they exist
|
| 720 |
+
os.system("git add models/ 2>/dev/null || true")
|
| 721 |
+
result = os.system("git commit -m 'Add pre-downloaded models' 2>/dev/null || true")
|
| 722 |
+
|
| 723 |
+
if result == 0:
|
| 724 |
+
log_event("Attempting to push models to repo...")
|
| 725 |
+
os.system("git push 2>/dev/null || true")
|
| 726 |
+
log_event("Models committed to repo (if any changes)")
|
| 727 |
+
except Exception as e:
|
| 728 |
+
log_event(f"Warning: Could not commit models to Spaces repo: {e}")
|
| 729 |
+
|
| 730 |
+
|
| 731 |
def _ensure_model_files():
|
| 732 |
+
"""Download models if not already cached."""
|
| 733 |
+
global MODEL_DIR, LORA_DIR
|
| 734 |
+
|
| 735 |
+
log_event("Checking model files...")
|
| 736 |
+
|
| 737 |
+
# Ensure directories exist
|
| 738 |
os.makedirs(MODEL_DIR, exist_ok=True)
|
| 739 |
os.makedirs(LORA_DIR, exist_ok=True)
|
| 740 |
|
|
|
|
| 746 |
local_dir=MODEL_DIR,
|
| 747 |
local_dir_use_symlinks=False,
|
| 748 |
)
|
| 749 |
+
log_event(f"Base model downloaded to {MODEL_DIR}")
|
| 750 |
else:
|
| 751 |
+
log_event(f"Base model already cached at {MODEL_DIR}")
|
| 752 |
|
| 753 |
# Download LoRA on first run
|
| 754 |
if not os.path.exists(os.path.join(LORA_DIR, "pytorch_lora_weights.safetensors")):
|
|
|
|
| 759 |
local_dir_use_symlinks=False,
|
| 760 |
allow_patterns=["pytorch_lora_weights.safetensors"],
|
| 761 |
)
|
| 762 |
+
log_event(f"LoRA downloaded to {LORA_DIR}")
|
| 763 |
else:
|
| 764 |
+
log_event(f"LoRA already cached at {LORA_DIR}")
|
| 765 |
+
|
| 766 |
+
# Auto-commit to repo on Spaces after first download
|
| 767 |
+
_setup_models_on_spaces()
|
| 768 |
|
| 769 |
|
| 770 |
def _load_pipe():
|