Spaces:
Runtime error
Runtime error
Commit Β·
0f15908
1
Parent(s): e93d344
debug for amt model loading with safetensors
Browse files
app.py
CHANGED
|
@@ -28,43 +28,61 @@ model_card = ModelCard(
|
|
| 28 |
|
| 29 |
model_cache = {}
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
def load_amt_model(model_choice):
|
| 32 |
"""Loads and caches the AMT model inside the worker process."""
|
| 33 |
if model_choice in model_cache:
|
| 34 |
-
print(f"β
Model {model_choice} loaded from cache")
|
| 35 |
return model_cache[model_choice]
|
| 36 |
|
| 37 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 38 |
-
cache_dir = "./models"
|
| 39 |
|
| 40 |
try:
|
| 41 |
-
print(f"π
|
| 42 |
|
| 43 |
-
#
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
# Load model configuration
|
| 56 |
-
config = AutoConfig.from_pretrained(model_choice, cache_dir=cache_dir)
|
| 57 |
|
| 58 |
# Load SafeTensors manually
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
model = AutoModelForCausalLM.from_config(config) # Initialize model
|
| 61 |
model.load_state_dict(state_dict) # Load weights
|
| 62 |
model.to(device)
|
|
|
|
| 63 |
|
| 64 |
else:
|
| 65 |
-
raise ValueError(f"β
|
| 66 |
-
|
| 67 |
-
print(f"β
Successfully loaded model: {model_choice}")
|
| 68 |
|
| 69 |
except Exception as e:
|
| 70 |
print(f"β Error loading model {model_choice}: {e}")
|
|
@@ -75,7 +93,6 @@ def load_amt_model(model_choice):
|
|
| 75 |
return model
|
| 76 |
|
| 77 |
|
| 78 |
-
|
| 79 |
@spaces.GPU
|
| 80 |
def generate_accompaniment(midi_file, model_choice, selected_midi_program, history_length):
|
| 81 |
"""Generates accompaniment for the entire MIDI input, conditioned on the user-selected history length."""
|
|
|
|
| 28 |
|
| 29 |
model_cache = {}
|
| 30 |
|
| 31 |
+
'''
|
| 32 |
+
def load_amt_model(model_choice):
|
| 33 |
+
"""Loads and caches the AMT model inside the worker process."""
|
| 34 |
+
if model_choice in model_cache:
|
| 35 |
+
return model_cache[model_choice]
|
| 36 |
+
|
| 37 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 38 |
+
model = AutoModelForCausalLM.from_pretrained(model_choice).to(device)
|
| 39 |
+
|
| 40 |
+
model_cache[model_choice] = model
|
| 41 |
+
return model
|
| 42 |
+
'''
|
| 43 |
+
|
| 44 |
+
import os
|
| 45 |
+
import traceback
|
| 46 |
+
from transformers import AutoModelForCausalLM, AutoConfig
|
| 47 |
+
import torch
|
| 48 |
+
from safetensors.torch import load_file
|
| 49 |
+
|
| 50 |
def load_amt_model(model_choice):
|
| 51 |
"""Loads and caches the AMT model inside the worker process."""
|
| 52 |
if model_choice in model_cache:
|
|
|
|
| 53 |
return model_cache[model_choice]
|
| 54 |
|
| 55 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
|
|
|
| 56 |
|
| 57 |
try:
|
| 58 |
+
print(f"π Loading model: {model_choice}")
|
| 59 |
|
| 60 |
+
# For small and medium models, use the original loading method
|
| 61 |
+
if model_choice in ["stanford-crfm/music-small-800k", "stanford-crfm/music-medium-800k"]:
|
| 62 |
+
model = AutoModelForCausalLM.from_pretrained(model_choice).to(device)
|
| 63 |
+
print(f"β
Successfully loaded {model_choice} using standard PyTorch method.")
|
| 64 |
|
| 65 |
+
# For large model, use SafeTensors method
|
| 66 |
+
elif model_choice == "stanford-crfm/music-large-800k":
|
| 67 |
+
print(f"π Detected SafeTensors format for {model_choice}, loading manually...")
|
| 68 |
|
| 69 |
+
# Load model config
|
| 70 |
+
config = AutoConfig.from_pretrained(model_choice)
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
# Load SafeTensors manually
|
| 73 |
+
safetensor_path = os.path.join("models", model_choice, "model.safetensors")
|
| 74 |
+
|
| 75 |
+
if not os.path.exists(safetensor_path):
|
| 76 |
+
raise FileNotFoundError(f"β SafeTensors file not found: {safetensor_path}")
|
| 77 |
+
|
| 78 |
+
state_dict = load_file(safetensor_path)
|
| 79 |
model = AutoModelForCausalLM.from_config(config) # Initialize model
|
| 80 |
model.load_state_dict(state_dict) # Load weights
|
| 81 |
model.to(device)
|
| 82 |
+
print(f"β
Successfully loaded {model_choice} using SafeTensors.")
|
| 83 |
|
| 84 |
else:
|
| 85 |
+
raise ValueError(f"β Unknown model choice: {model_choice}")
|
|
|
|
|
|
|
| 86 |
|
| 87 |
except Exception as e:
|
| 88 |
print(f"β Error loading model {model_choice}: {e}")
|
|
|
|
| 93 |
return model
|
| 94 |
|
| 95 |
|
|
|
|
| 96 |
@spaces.GPU
|
| 97 |
def generate_accompaniment(midi_file, model_choice, selected_midi_program, history_length):
|
| 98 |
"""Generates accompaniment for the entire MIDI input, conditioned on the user-selected history length."""
|