Add custom inference handler for Maya1 TTS
Browse files- handler.py +38 -24
handler.py
CHANGED
|
@@ -49,38 +49,52 @@ class EndpointHandler:
|
|
| 49 |
if device_override == "cpu":
|
| 50 |
device_map_arg = "cpu"
|
| 51 |
|
| 52 |
-
#
|
| 53 |
-
|
|
|
|
| 54 |
|
| 55 |
-
#
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
try:
|
| 74 |
self.model = AutoModelForCausalLM.from_pretrained(
|
| 75 |
-
|
| 76 |
)
|
| 77 |
-
self.tokenizer = AutoTokenizer.from_pretrained(
|
| 78 |
except Exception as e:
|
| 79 |
raise RuntimeError(
|
| 80 |
-
f"Failed to load Maya1 model from {
|
| 81 |
f"Error: {e}\n\n"
|
| 82 |
-
f"Please ensure
|
| 83 |
-
f"
|
|
|
|
|
|
|
| 84 |
)
|
| 85 |
|
| 86 |
# determine device from model parameters (safer than using `model.device`)
|
|
|
|
| 49 |
if device_override == "cpu":
|
| 50 |
device_map_arg = "cpu"
|
| 51 |
|
| 52 |
+
# Allow loading from HuggingFace Hub via environment variable
|
| 53 |
+
# MAYA_MODEL_ID: HuggingFace model ID (e.g., "Plachta/MARS5-TTS" or similar Maya model)
|
| 54 |
+
model_id = os.getenv("MAYA_MODEL_ID", "")
|
| 55 |
|
| 56 |
+
# Determine model path: use env var if set, otherwise local path
|
| 57 |
+
if model_id:
|
| 58 |
+
# Load from HuggingFace Hub
|
| 59 |
+
model_path = model_id
|
| 60 |
+
print(f"Loading Maya1 model from HuggingFace Hub: {model_id}")
|
| 61 |
+
else:
|
| 62 |
+
# Load from local repository
|
| 63 |
+
model_path = path if path else "/repository"
|
| 64 |
+
|
| 65 |
+
# Check if this is a valid model directory
|
| 66 |
+
config_path = os.path.join(model_path, "config.json")
|
| 67 |
+
if not os.path.exists(config_path):
|
| 68 |
+
raise RuntimeError(
|
| 69 |
+
f"❌ Model configuration not found at: {config_path}\n\n"
|
| 70 |
+
f"The repository appears to be missing model weights and configuration.\n\n"
|
| 71 |
+
f"To fix this:\n"
|
| 72 |
+
f"1. For TESTING: Set environment variable MAYA_USE_FAKE=1 to use fake mode\n"
|
| 73 |
+
f"2. For REMOTE MODEL: Set MAYA_MODEL_ID to a HuggingFace model ID\n"
|
| 74 |
+
f" Example: MAYA_MODEL_ID=Plachta/MARS5-TTS\n"
|
| 75 |
+
f"3. For LOCAL MODEL: Upload Maya1 model weights to your repository:\n"
|
| 76 |
+
f" - config.json\n"
|
| 77 |
+
f" - model.safetensors (or pytorch_model.bin)\n"
|
| 78 |
+
f" - tokenizer.json\n"
|
| 79 |
+
f" - tokenizer_config.json\n"
|
| 80 |
+
f" - generation_config.json (optional)\n\n"
|
| 81 |
+
f"Current path: {model_path}\n"
|
| 82 |
+
f"Files found: {os.listdir(model_path) if os.path.exists(model_path) else 'path does not exist'}\n"
|
| 83 |
+
)
|
| 84 |
|
| 85 |
try:
|
| 86 |
self.model = AutoModelForCausalLM.from_pretrained(
|
| 87 |
+
model_path, torch_dtype=torch_dtype, device_map=device_map_arg
|
| 88 |
)
|
| 89 |
+
self.tokenizer = AutoTokenizer.from_pretrained(model_path)
|
| 90 |
except Exception as e:
|
| 91 |
raise RuntimeError(
|
| 92 |
+
f"Failed to load Maya1 model from {model_path}.\n"
|
| 93 |
f"Error: {e}\n\n"
|
| 94 |
+
f"Please ensure:\n"
|
| 95 |
+
f"1. The model repository contains valid model files, OR\n"
|
| 96 |
+
f"2. Set MAYA_MODEL_ID to a valid HuggingFace model ID, OR\n"
|
| 97 |
+
f"3. Set MAYA_USE_FAKE=1 for testing without a real model"
|
| 98 |
)
|
| 99 |
|
| 100 |
# determine device from model parameters (safer than using `model.device`)
|