futurespyhi commited on
Commit
3bfebb6
Β·
1 Parent(s): ef411bc

Add runtime model downloading to avoid storage limits

Browse files

- Add download_models.py script to download xcodec_mini_infer models at runtime
- Modify app.py to automatically download models on startup
- Add huggingface_hub dependency for model downloads
- Remove large model files from repository to stay under 1GB limit

Files changed (3) hide show
  1. app.py +22 -0
  2. download_models.py +128 -0
  3. requirements.txt +1 -0
app.py CHANGED
@@ -76,6 +76,28 @@ def install_flash_attn():
76
  # Setup environment first
77
  setup_spaces_environment()
78
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
  # Install flash-attn if needed
80
  flash_attn_available = install_flash_attn()
81
 
 
76
  # Setup environment first
77
  setup_spaces_environment()
78
 
79
+ # Download required models for YuEGP inference
80
+ def download_required_models():
81
+ """Download required model files at startup"""
82
+ try:
83
+ from download_models import ensure_model_availability
84
+ print("πŸš€ Checking and downloading required models...")
85
+ success = ensure_model_availability()
86
+ if success:
87
+ print("βœ… Model setup completed successfully")
88
+ else:
89
+ print("⚠️ Some models may be missing - continuing with available resources")
90
+ return success
91
+ except ImportError as e:
92
+ print(f"⚠️ Model download script not found: {e}")
93
+ return False
94
+ except Exception as e:
95
+ print(f"❌ Error during model download: {e}")
96
+ return False
97
+
98
+ # Download models before other setup
99
+ models_ready = download_required_models()
100
+
101
  # Install flash-attn if needed
102
  flash_attn_available = install_flash_attn()
103
 
download_models.py ADDED
@@ -0,0 +1,128 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """
3
+ Model Download Script for MiloMusic
4
+ Downloads required model files from Hugging Face Hub at runtime to avoid storage limits.
5
+ """
6
+
7
+ import os
8
+ import shutil
9
+ from pathlib import Path
10
+ from huggingface_hub import hf_hub_download, snapshot_download
11
+
12
+
13
+ def download_xcodec_models():
14
+ """Download xcodec_mini_infer models from Hugging Face Hub"""
15
+
16
+ # Base path for xcodec models
17
+ xcodec_base = Path("YuEGP/inference/xcodec_mini_infer")
18
+
19
+ print("πŸ“₯ Downloading xcodec_mini_infer models...")
20
+
21
+ try:
22
+ # Download the entire xcodec_mini_infer repository
23
+ cache_dir = snapshot_download(
24
+ repo_id="m-a-p/xcodec_mini_infer",
25
+ cache_dir="/tmp/hf_cache",
26
+ local_dir=str(xcodec_base),
27
+ local_dir_use_symlinks=False
28
+ )
29
+
30
+ print("βœ… Successfully downloaded xcodec_mini_infer models")
31
+ return True
32
+
33
+ except Exception as e:
34
+ print(f"❌ Error downloading xcodec_mini_infer models: {e}")
35
+
36
+ # Try downloading individual essential files if full download fails
37
+ try:
38
+ print("πŸ”„ Attempting to download essential files individually...")
39
+
40
+ # Ensure directories exist
41
+ os.makedirs(xcodec_base / "semantic_ckpts" / "hf_1_325000", exist_ok=True)
42
+ os.makedirs(xcodec_base / "final_ckpt", exist_ok=True)
43
+ os.makedirs(xcodec_base / "decoders", exist_ok=True)
44
+
45
+ # Download essential model files
46
+ essential_files = [
47
+ "semantic_ckpts/hf_1_325000/pytorch_model.bin",
48
+ "semantic_ckpts/hf_1_325000/preprocessor_config.json",
49
+ "final_ckpt/ckpt_00360000.pth",
50
+ "final_ckpt/config.yaml",
51
+ "decoders/decoder_131000.pth",
52
+ "decoders/decoder_151000.pth",
53
+ "decoders/config.yaml"
54
+ ]
55
+
56
+ for file_path in essential_files:
57
+ try:
58
+ local_path = xcodec_base / file_path
59
+ os.makedirs(local_path.parent, exist_ok=True)
60
+
61
+ downloaded_file = hf_hub_download(
62
+ repo_id="m-a-p/xcodec_mini_infer",
63
+ filename=file_path,
64
+ cache_dir="/tmp/hf_cache"
65
+ )
66
+
67
+ # Copy to local directory
68
+ shutil.copy2(downloaded_file, local_path)
69
+ print(f"βœ… Downloaded: {file_path}")
70
+
71
+ except Exception as file_error:
72
+ print(f"⚠️ Failed to download {file_path}: {file_error}")
73
+
74
+ return True
75
+
76
+ except Exception as fallback_error:
77
+ print(f"❌ Fallback download also failed: {fallback_error}")
78
+ return False
79
+
80
+
81
+ def ensure_model_availability():
82
+ """
83
+ Ensure all required models are available locally.
84
+ Download them if they don't exist.
85
+ """
86
+
87
+ xcodec_base = Path("YuEGP/inference/xcodec_mini_infer")
88
+
89
+ # Check if essential model files exist
90
+ essential_files = [
91
+ xcodec_base / "semantic_ckpts" / "hf_1_325000" / "pytorch_model.bin",
92
+ xcodec_base / "final_ckpt" / "ckpt_00360000.pth",
93
+ xcodec_base / "decoders" / "decoder_131000.pth"
94
+ ]
95
+
96
+ missing_files = [f for f in essential_files if not f.exists()]
97
+
98
+ if missing_files:
99
+ print(f"⚠️ Missing model files: {[str(f) for f in missing_files]}")
100
+ print("πŸš€ Starting model download...")
101
+
102
+ success = download_xcodec_models()
103
+
104
+ if success:
105
+ print("βœ… Model download completed successfully!")
106
+ else:
107
+ print("❌ Model download failed. Some functionality may be limited.")
108
+ return False
109
+ else:
110
+ print("βœ… All required model files are already present")
111
+
112
+ return True
113
+
114
+
115
+ if __name__ == "__main__":
116
+ """
117
+ Run model download when script is executed directly
118
+ """
119
+ print("🎡 MiloMusic Model Download Script")
120
+ print("=" * 50)
121
+
122
+ success = ensure_model_availability()
123
+
124
+ if success:
125
+ print("\nπŸŽ‰ Setup complete! MiloMusic is ready to generate music.")
126
+ else:
127
+ print("\n⚠️ Setup completed with warnings. Check the logs above.")
128
+ exit(1)
requirements.txt CHANGED
@@ -28,6 +28,7 @@ python-dotenv==1.1.0
28
  spaces==0.34.2
29
  xxhash==3.5.0
30
  soundfile==0.13.1
 
31
 
32
  # Audio processing
33
  librosa==0.11.0
 
28
  spaces==0.34.2
29
  xxhash==3.5.0
30
  soundfile==0.13.1
31
+ huggingface_hub==0.28.0
32
 
33
  # Audio processing
34
  librosa==0.11.0