AnimeOverlord commited on
Commit
1ab8ed2
·
1 Parent(s): 76639c0

new update fix

Browse files
Files changed (1) hide show
  1. app.py +26 -62
app.py CHANGED
@@ -645,29 +645,24 @@ 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
- # 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
@@ -700,46 +695,17 @@ log_event(f"Gradio version detected: {gr.__version__}")
700
  log_event(f"Blocks kwargs: {BLOCKS_KWARGS}")
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
 
741
- # Download base model on first run
742
- if not os.path.exists(os.path.join(MODEL_DIR, "model_index.json")):
 
 
743
  log_event(f"Downloading base model: {BASE_MODEL_ID}")
744
  snapshot_download(
745
  repo_id=BASE_MODEL_ID,
@@ -750,8 +716,7 @@ def _ensure_model_files():
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")):
755
  log_event(f"Downloading LoRA: {LORA_ID}")
756
  snapshot_download(
757
  repo_id=LORA_ID,
@@ -762,9 +727,6 @@ def _ensure_model_files():
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():
@@ -841,6 +803,8 @@ def _minecraftify_image(
841
  num_inference_steps=int(steps),
842
  guidance_scale=float(guidance_scale),
843
  generator=generator,
 
 
844
  )
845
 
846
  log_event("Inference complete")
@@ -1011,7 +975,7 @@ with gr.Blocks(**BLOCKS_KWARGS) as demo:
1011
  elem_classes="image-upload mc-upload-area",
1012
  height=400,
1013
  )
1014
- gr.HTML('<div style="color: var(--mc-gold); font-family: VT323; text-align: center; margin-top: 10px;">▶ Click Record to start capturing and auto-process live frames</div>')
1015
 
1016
  extra_details = gr.Textbox(
1017
  label="🗒️ Extra scene details (optional)",
 
645
  BASE_MODEL_ID = "black-forest-labs/FLUX.2-klein-4B"
646
  LORA_ID = "AnimeOverlord/flux2-klein-4b-mc"
647
 
648
+ # Mount this path in the Space settings
649
+ SPACE_DATA_DIR = "/data"
650
+
651
+ # Put Hugging Face cache on persistent storage too
652
+ HF_HOME = os.path.join(SPACE_DATA_DIR, ".huggingface")
653
+ HF_HUB_CACHE = os.path.join(HF_HOME, "hub")
654
+
655
+ # Store the downloaded model files here
656
+ MODEL_ROOT = os.path.join(SPACE_DATA_DIR, "models")
657
+ MODEL_DIR = os.path.join(MODEL_ROOT, "FLUX.2-klein-4B")
658
+ LORA_DIR = os.path.join(MODEL_ROOT, "flux2-klein-4b-mc")
659
 
660
+ # Set these before any HF downloads happen
661
+ os.environ["HF_HOME"] = HF_HOME
662
+ os.environ["HF_HUB_CACHE"] = HF_HUB_CACHE
663
+
664
+ os.makedirs(MODEL_DIR, exist_ok=True)
665
+ os.makedirs(LORA_DIR, exist_ok=True)
 
 
 
 
 
 
 
 
 
 
666
 
667
  _pipe = None
668
  _pipe_ready = False
 
695
  log_event(f"Blocks kwargs: {BLOCKS_KWARGS}")
696
  log_event(f"Launch kwargs: {LAUNCH_KWARGS}")
697
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
698
  def _ensure_model_files():
699
  """Download models if not already cached."""
 
 
700
  log_event("Checking model files...")
701
+
 
702
  os.makedirs(MODEL_DIR, exist_ok=True)
703
  os.makedirs(LORA_DIR, exist_ok=True)
704
 
705
+ base_marker = os.path.join(MODEL_DIR, "model_index.json")
706
+ lora_marker = os.path.join(LORA_DIR, "pytorch_lora_weights.safetensors")
707
+
708
+ if not os.path.exists(base_marker):
709
  log_event(f"Downloading base model: {BASE_MODEL_ID}")
710
  snapshot_download(
711
  repo_id=BASE_MODEL_ID,
 
716
  else:
717
  log_event(f"Base model already cached at {MODEL_DIR}")
718
 
719
+ if not os.path.exists(lora_marker):
 
720
  log_event(f"Downloading LoRA: {LORA_ID}")
721
  snapshot_download(
722
  repo_id=LORA_ID,
 
727
  log_event(f"LoRA downloaded to {LORA_DIR}")
728
  else:
729
  log_event(f"LoRA already cached at {LORA_DIR}")
 
 
 
730
 
731
 
732
  def _load_pipe():
 
803
  num_inference_steps=int(steps),
804
  guidance_scale=float(guidance_scale),
805
  generator=generator,
806
+ height=360,
807
+ width=640
808
  )
809
 
810
  log_event("Inference complete")
 
975
  elem_classes="image-upload mc-upload-area",
976
  height=400,
977
  )
978
+ gr.HTML('<div style="color: var(--mc-gold); font-family: VT323; text-align: center; margin-top: 10px;">▶ Click Record to start MINECRAFTIFICATION OF LIVE FRAMES</div>')
979
 
980
  extra_details = gr.Textbox(
981
  label="🗒️ Extra scene details (optional)",