AnimeOverlord commited on
Commit
3217a86
·
1 Parent(s): 554ee56

new update fix

Browse files
Files changed (1) hide show
  1. app.py +31 -68
app.py CHANGED
@@ -646,11 +646,11 @@ BASE_MODEL_ID = "black-forest-labs/FLUX.2-klein-4B"
646
  LORA_ID = "AnimeOverlord/flux2-klein-4b-mc"
647
 
648
  MODEL_CACHE_ROOT = os.environ.get(
649
- "MODEL_CACHE_ROOT",
650
- "/data/minecraftify" if os.path.isdir("/data") else ".",
651
  )
652
- MODEL_DIR = os.path.join(MODEL_CACHE_ROOT, "black-forest-labs", "FLUX.2-klein-4B")
653
- LORA_DIR = os.path.join(MODEL_CACHE_ROOT, "AnimeOverlord", "flux2-klein-4b-mc-v2")
654
 
655
  _pipe = None
656
  _pipe_ready = False
@@ -689,7 +689,7 @@ def _ensure_model_files():
689
  os.makedirs(MODEL_DIR, exist_ok=True)
690
  os.makedirs(LORA_DIR, exist_ok=True)
691
 
692
- # Download base model once into the volume
693
  if not os.path.exists(os.path.join(MODEL_DIR, "model_index.json")):
694
  log_event(f"Downloading base model: {BASE_MODEL_ID}")
695
  snapshot_download(
@@ -697,10 +697,11 @@ def _ensure_model_files():
697
  local_dir=MODEL_DIR,
698
  local_dir_use_symlinks=False,
699
  )
 
700
  else:
701
- log_event("Base model already present")
702
 
703
- # Download LoRA once into the volume
704
  if not os.path.exists(os.path.join(LORA_DIR, "pytorch_lora_weights.safetensors")):
705
  log_event(f"Downloading LoRA: {LORA_ID}")
706
  snapshot_download(
@@ -709,8 +710,9 @@ def _ensure_model_files():
709
  local_dir_use_symlinks=False,
710
  allow_patterns=["pytorch_lora_weights.safetensors"],
711
  )
 
712
  else:
713
- log_event("LoRA already present")
714
 
715
 
716
  def _load_pipe():
@@ -790,7 +792,10 @@ def _minecraftify_image(
790
  )
791
 
792
  log_event("Inference complete")
793
- return out.images[0]
 
 
 
794
 
795
 
796
  @spaces.GPU(duration=120)
@@ -874,6 +879,15 @@ def minecraftify_live_loop(
874
  yield output
875
 
876
 
 
 
 
 
 
 
 
 
 
877
  def switch_input_mode(mode: str):
878
  global _live_active
879
 
@@ -892,37 +906,6 @@ def switch_input_mode(mode: str):
892
  )
893
 
894
 
895
- def start_live(image: Image.Image):
896
- global _live_active
897
-
898
- with _live_lock:
899
- has_captured_frame = _live_latest_frame is not None
900
-
901
- if not has_captured_frame:
902
- log_event("Start live blocked: webcam is not on or no streamed frame was captured")
903
- raise gr.Error("📷 Webcam is not streaming yet. Click Record and wait for the preview before starting live.")
904
-
905
- _live_active = True
906
- log_event(f"Live mode started with webcam frame size={getattr(image, 'size', None)}")
907
- return (
908
- True,
909
- gr.update(interactive=False),
910
- gr.update(interactive=True),
911
- )
912
-
913
-
914
- def stop_live():
915
- global _live_active
916
-
917
- _live_active = False
918
- log_event("Live mode stopped")
919
- return (
920
- False,
921
- gr.update(interactive=True),
922
- gr.update(interactive=False),
923
- )
924
-
925
-
926
  # ----------------------------------------------------------------------
927
  # UI
928
  # ----------------------------------------------------------------------
@@ -976,18 +959,7 @@ with gr.Blocks(**BLOCKS_KWARGS) as demo:
976
  elem_classes="image-upload mc-upload-area",
977
  height=400,
978
  )
979
-
980
- with gr.Row():
981
- start_live_btn = gr.Button(
982
- "▶ START LIVE",
983
- variant="primary",
984
- elem_classes="mc-btn-primary",
985
- )
986
- stop_live_btn = gr.Button(
987
- "■ STOP LIVE",
988
- variant="secondary",
989
- interactive=False,
990
- )
991
 
992
  extra_details = gr.Textbox(
993
  label="🗒️ Extra scene details (optional)",
@@ -1069,8 +1041,6 @@ with gr.Blocks(**BLOCKS_KWARGS) as demo:
1069
  image_input_group,
1070
  run_btn,
1071
  live_input_group,
1072
- start_live_btn,
1073
- stop_live_btn,
1074
  live_running,
1075
  ],
1076
  queue=False,
@@ -1082,32 +1052,25 @@ with gr.Blocks(**BLOCKS_KWARGS) as demo:
1082
  outputs=output_image,
1083
  )
1084
 
1085
- start_live_event = start_live_btn.click(
1086
- fn=start_live,
1087
  inputs=live_image,
1088
- outputs=[live_running, start_live_btn, stop_live_btn],
 
 
1089
  queue=False,
1090
- )
1091
-
1092
- start_live_event.then(
1093
  fn=minecraftify_live_loop,
1094
  inputs=[steps, guidance_scale, seed, extra_details],
1095
  outputs=[output_image],
1096
  concurrency_limit=1,
1097
  )
1098
 
1099
- stop_live_btn.click(
1100
- fn=stop_live,
1101
- inputs=None,
1102
- outputs=[live_running, start_live_btn, stop_live_btn],
1103
- queue=False,
1104
- )
1105
-
1106
  live_image.stream(
1107
  fn=capture_live_frame,
1108
  inputs=live_image,
1109
  outputs=None,
1110
- stream_every=0.1,
1111
  trigger_mode="always_last",
1112
  queue=False,
1113
  )
 
646
  LORA_ID = "AnimeOverlord/flux2-klein-4b-mc"
647
 
648
  MODEL_CACHE_ROOT = os.environ.get(
649
+ "HF_HOME",
650
+ os.path.join(os.path.expanduser("~"), ".cache", "huggingface"),
651
  )
652
+ MODEL_DIR = os.path.join(MODEL_CACHE_ROOT, "hub", "models--black-forest-labs--FLUX.2-klein-4B")
653
+ LORA_DIR = os.path.join(MODEL_CACHE_ROOT, "hub", "models--AnimeOverlord--flux2-klein-4b-mc")
654
 
655
  _pipe = None
656
  _pipe_ready = False
 
689
  os.makedirs(MODEL_DIR, exist_ok=True)
690
  os.makedirs(LORA_DIR, exist_ok=True)
691
 
692
+ # Download base model on first run
693
  if not os.path.exists(os.path.join(MODEL_DIR, "model_index.json")):
694
  log_event(f"Downloading base model: {BASE_MODEL_ID}")
695
  snapshot_download(
 
697
  local_dir=MODEL_DIR,
698
  local_dir_use_symlinks=False,
699
  )
700
+ log_event("Base model download complete")
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")):
706
  log_event(f"Downloading LoRA: {LORA_ID}")
707
  snapshot_download(
 
710
  local_dir_use_symlinks=False,
711
  allow_patterns=["pytorch_lora_weights.safetensors"],
712
  )
713
+ log_event("LoRA download complete")
714
  else:
715
+ log_event("LoRA already cached")
716
 
717
 
718
  def _load_pipe():
 
792
  )
793
 
794
  log_event("Inference complete")
795
+ output_img = out.images[0]
796
+ log_event(f"Output image size: {output_img.size}")
797
+ torch.cuda.empty_cache()
798
+ return output_img
799
 
800
 
801
  @spaces.GPU(duration=120)
 
879
  yield output
880
 
881
 
882
+ def _set_live_active(image: Image.Image):
883
+ """Auto-enable live processing when recording starts."""
884
+ global _live_active
885
+ if image is not None and not _live_active:
886
+ _live_active = True
887
+ log_event("Live recording detected, auto-starting processing")
888
+ return None
889
+
890
+
891
  def switch_input_mode(mode: str):
892
  global _live_active
893
 
 
906
  )
907
 
908
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
909
  # ----------------------------------------------------------------------
910
  # UI
911
  # ----------------------------------------------------------------------
 
959
  elem_classes="image-upload mc-upload-area",
960
  height=400,
961
  )
962
+ 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>')
 
 
 
 
 
 
 
 
 
 
 
963
 
964
  extra_details = gr.Textbox(
965
  label="🗒️ Extra scene details (optional)",
 
1041
  image_input_group,
1042
  run_btn,
1043
  live_input_group,
 
 
1044
  live_running,
1045
  ],
1046
  queue=False,
 
1052
  outputs=output_image,
1053
  )
1054
 
1055
+ live_image.stream(
1056
+ fn=lambda img: _set_live_active(img),
1057
  inputs=live_image,
1058
+ outputs=None,
1059
+ stream_every=0.05,
1060
+ trigger_mode="always_last",
1061
  queue=False,
1062
+ ).then(
 
 
1063
  fn=minecraftify_live_loop,
1064
  inputs=[steps, guidance_scale, seed, extra_details],
1065
  outputs=[output_image],
1066
  concurrency_limit=1,
1067
  )
1068
 
 
 
 
 
 
 
 
1069
  live_image.stream(
1070
  fn=capture_live_frame,
1071
  inputs=live_image,
1072
  outputs=None,
1073
+ stream_every=0.05,
1074
  trigger_mode="always_last",
1075
  queue=False,
1076
  )