Add custom inference handler for Maya1 TTS
Browse files- handler.py +11 -8
handler.py
CHANGED
|
@@ -52,17 +52,17 @@ class EndpointHandler:
|
|
| 52 |
# Allow loading from HuggingFace Hub via environment variable
|
| 53 |
# MAYA_MODEL_ID: HuggingFace model ID (e.g., "maya-research/maya1" 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 |
local_path = path if path else "/repository"
|
| 58 |
-
|
| 59 |
# Check if trying to load from self (circular reference)
|
| 60 |
# Only block if it's exactly "binhqd/maya1" - other repos ending in /maya1 are fine
|
| 61 |
if model_id == "binhqd/maya1":
|
| 62 |
print(f"⚠️ MAYA_MODEL_ID is set to {model_id} which is this repository!")
|
| 63 |
print("This creates a circular reference. Checking local files instead...")
|
| 64 |
model_id = "" # Force local loading
|
| 65 |
-
|
| 66 |
if model_id:
|
| 67 |
# Load from HuggingFace Hub
|
| 68 |
model_path = model_id
|
|
@@ -70,7 +70,7 @@ class EndpointHandler:
|
|
| 70 |
else:
|
| 71 |
# Load from local repository
|
| 72 |
model_path = local_path
|
| 73 |
-
|
| 74 |
# Check if this is a valid model directory
|
| 75 |
config_path = os.path.join(model_path, "config.json")
|
| 76 |
if not os.path.exists(config_path):
|
|
@@ -243,10 +243,13 @@ class EndpointHandler:
|
|
| 243 |
waveform = self.snac.decode(snac_codes.to(self.device))
|
| 244 |
|
| 245 |
# Extract audio and convert to numpy
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
|
|
|
|
|
|
|
|
|
| 250 |
|
| 251 |
waveform = waveform.cpu().numpy()
|
| 252 |
|
|
|
|
| 52 |
# Allow loading from HuggingFace Hub via environment variable
|
| 53 |
# MAYA_MODEL_ID: HuggingFace model ID (e.g., "maya-research/maya1" 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 |
local_path = path if path else "/repository"
|
| 58 |
+
|
| 59 |
# Check if trying to load from self (circular reference)
|
| 60 |
# Only block if it's exactly "binhqd/maya1" - other repos ending in /maya1 are fine
|
| 61 |
if model_id == "binhqd/maya1":
|
| 62 |
print(f"⚠️ MAYA_MODEL_ID is set to {model_id} which is this repository!")
|
| 63 |
print("This creates a circular reference. Checking local files instead...")
|
| 64 |
model_id = "" # Force local loading
|
| 65 |
+
|
| 66 |
if model_id:
|
| 67 |
# Load from HuggingFace Hub
|
| 68 |
model_path = model_id
|
|
|
|
| 70 |
else:
|
| 71 |
# Load from local repository
|
| 72 |
model_path = local_path
|
| 73 |
+
|
| 74 |
# Check if this is a valid model directory
|
| 75 |
config_path = os.path.join(model_path, "config.json")
|
| 76 |
if not os.path.exists(config_path):
|
|
|
|
| 243 |
waveform = self.snac.decode(snac_codes.to(self.device))
|
| 244 |
|
| 245 |
# Extract audio and convert to numpy
|
| 246 |
+
# SNAC outputs shape (batch, samples) or (batch, 1, samples)
|
| 247 |
+
# Safely remove all size-1 dimensions
|
| 248 |
+
waveform = waveform.squeeze() # Remove all dimensions of size 1
|
| 249 |
+
|
| 250 |
+
# Ensure we have a 1D tensor
|
| 251 |
+
if waveform.dim() > 1:
|
| 252 |
+
waveform = waveform[0] # Take first item if still multi-dimensional
|
| 253 |
|
| 254 |
waveform = waveform.cpu().numpy()
|
| 255 |
|