Spaces:
Running
Running
Commit ·
90d4c19
1
Parent(s): caa25e2
made moonshine default
Browse files- README.md +4 -0
- app.py +63 -10
- requirements.txt +1 -0
README.md
CHANGED
|
@@ -16,3 +16,7 @@ This Space defaults to tiny-voca with the M2 voice. It downloads the ONNX model
|
|
| 16 |
Set `KOKORO_ONNX_MODE` to `auto`, `legacy`, `stts2`, or `piper` to select an existing backend instead. For tiny-voca, `TINY_VOCA_VOICE` accepts `F2` or `M2`; `TINY_VOCA_SPEED`, `TINY_VOCA_PITCH_SHIFT`, and `TINY_VOCA_PITCH_RANGE` configure synthesis. Model source and download path can be changed with `TINY_VOCA_REPO_ID` and `TINY_VOCA_MODEL_DIR`.
|
| 17 |
|
| 18 |
The tiny-voca phonemizer requires Python 3.10 or newer and is installed from [dizione](https://github.com/n4ze3m/dizione).
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
Set `KOKORO_ONNX_MODE` to `auto`, `legacy`, `stts2`, or `piper` to select an existing backend instead. For tiny-voca, `TINY_VOCA_VOICE` accepts `F2` or `M2`; `TINY_VOCA_SPEED`, `TINY_VOCA_PITCH_SHIFT`, and `TINY_VOCA_PITCH_RANGE` configure synthesis. Model source and download path can be changed with `TINY_VOCA_REPO_ID` and `TINY_VOCA_MODEL_DIR`.
|
| 17 |
|
| 18 |
The tiny-voca phonemizer requires Python 3.10 or newer and is installed from [dizione](https://github.com/n4ze3m/dizione).
|
| 19 |
+
|
| 20 |
+
## Speech-to-text
|
| 21 |
+
|
| 22 |
+
Speech-to-text defaults to Moonshine Voice with the English `medium-streaming` model. Set `ASR_ENGINE=wav2vec2_onnx` or `ASR_ENGINE=whisper_pt` to select an alternate backend. Set `MOONSHINE_MODEL_ARCH` to `tiny`, `base`, `tiny-streaming`, `small-streaming`, or `medium-streaming` to choose a Moonshine model.
|
app.py
CHANGED
|
@@ -143,7 +143,8 @@ def is_cached(cached_file_path):
|
|
| 143 |
return exists
|
| 144 |
|
| 145 |
use_wav2vec2 = os.environ.get("USE_WAV2VEC2", "").lower() in {"1", "true", "yes", "on"}
|
| 146 |
-
ASR_ENGINE = os.environ.get("ASR_ENGINE", "wav2vec2_onnx" if use_wav2vec2 else "
|
|
|
|
| 147 |
ASR_MODEL_NAME = os.environ.get("ASR_MODEL_NAME", "facebook/wav2vec2-base-960h")
|
| 148 |
ASR_ONNX_REPO = os.environ.get("ASR_ONNX_REPO", "onnx-community/wav2vec2-base-960h-ONNX")
|
| 149 |
PUNCTUATE_TEXT = os.environ.get("PUNCTUATE_TEXT", "0").lower() in {"1", "true", "yes", "on"}
|
|
@@ -170,7 +171,7 @@ TINY_VOCA_SPEED = float(os.environ.get("TINY_VOCA_SPEED", "1.0"))
|
|
| 170 |
TINY_VOCA_PITCH_SHIFT = float(os.environ.get("TINY_VOCA_PITCH_SHIFT", "0.0"))
|
| 171 |
TINY_VOCA_PITCH_RANGE = float(os.environ.get("TINY_VOCA_PITCH_RANGE", "1.0"))
|
| 172 |
TINY_VOCA_MAX_TOKENS = int(os.environ.get("TINY_VOCA_MAX_TOKENS", "512"))
|
| 173 |
-
MODEL_INIT_MODE = os.environ.get("MODEL_INIT_MODE", "
|
| 174 |
PIPER_PREFETCH_ON_STARTUP = os.environ.get("PIPER_PREFETCH_ON_STARTUP", "0").lower() in {"1", "true", "yes", "on"}
|
| 175 |
REQUEST_LOCK_TIMEOUT_SEC = float(os.environ.get("REQUEST_LOCK_TIMEOUT_SEC", "15"))
|
| 176 |
PIPER_TIMEOUT_SEC = float(os.environ.get("PIPER_TIMEOUT_SEC", "120"))
|
|
@@ -180,6 +181,7 @@ sess = None
|
|
| 180 |
voice_style = None
|
| 181 |
processor = None
|
| 182 |
whisper_model = None
|
|
|
|
| 183 |
asr_session = None
|
| 184 |
asr_processor = None
|
| 185 |
punctuation_model = None
|
|
@@ -190,7 +192,7 @@ model_init_lock = threading.Lock()
|
|
| 190 |
|
| 191 |
# Initialize models
|
| 192 |
def initialize_models(init_tts=True, init_asr=True):
|
| 193 |
-
global sess, voice_style, processor, whisper_model, asr_session, asr_processor
|
| 194 |
global kokoro_tts_mode, kokoro_input_names
|
| 195 |
global punctuation_model, punctuation_tokenizer
|
| 196 |
|
|
@@ -331,7 +333,37 @@ def initialize_models(init_tts=True, init_asr=True):
|
|
| 331 |
logger.info(f"Voice style vector loaded successfully from {voice_style_path}")
|
| 332 |
|
| 333 |
# Initialize ASR engine
|
| 334 |
-
if init_asr and ASR_ENGINE
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 335 |
logger.info(f"Loading Wav2Vec2 ONNX ASR model ({ASR_MODEL_NAME})...")
|
| 336 |
# Load processor for feature extraction + CTC labels
|
| 337 |
asr_processor = Wav2Vec2Processor.from_pretrained(ASR_MODEL_NAME)
|
|
@@ -375,7 +407,7 @@ def initialize_models(init_tts=True, init_asr=True):
|
|
| 375 |
raise
|
| 376 |
asr_session = InferenceSession(asr_onnx_path_env, sess_options)
|
| 377 |
logger.info("Wav2Vec2 ONNX ASR model loaded")
|
| 378 |
-
elif init_asr and ASR_ENGINE
|
| 379 |
logger.info("ASR_ENGINE set to whisper_pt; loading Whisper model...")
|
| 380 |
processor = WhisperProcessor.from_pretrained("openai/whisper-base")
|
| 381 |
whisper_model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-base")
|
|
@@ -401,15 +433,19 @@ def ensure_models_for_tts():
|
|
| 401 |
initialize_models(init_tts=True, init_asr=False)
|
| 402 |
|
| 403 |
def ensure_models_for_asr():
|
| 404 |
-
asr_ready = (
|
| 405 |
-
ASR_ENGINE
|
|
|
|
|
|
|
| 406 |
)
|
| 407 |
punct_ready = (not PUNCTUATE_TEXT) or (punctuation_model is not None and punctuation_tokenizer is not None)
|
| 408 |
if asr_ready and punct_ready:
|
| 409 |
return
|
| 410 |
with model_init_lock:
|
| 411 |
-
asr_ready = (
|
| 412 |
-
ASR_ENGINE
|
|
|
|
|
|
|
| 413 |
)
|
| 414 |
punct_ready = (not PUNCTUATE_TEXT) or (punctuation_model is not None and punctuation_tokenizer is not None)
|
| 415 |
if not (asr_ready and punct_ready):
|
|
@@ -1010,7 +1046,24 @@ def transcribe_audio():
|
|
| 1010 |
f"[{request_id}] ASR (Wav2Vec2 ONNX) complete in {asr_elapsed:.2f}s "
|
| 1011 |
f"(chars={len(transcription)})"
|
| 1012 |
)
|
| 1013 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1014 |
asr_start = time.monotonic()
|
| 1015 |
# Whisper fallback
|
| 1016 |
input_features = processor(
|
|
|
|
| 143 |
return exists
|
| 144 |
|
| 145 |
use_wav2vec2 = os.environ.get("USE_WAV2VEC2", "").lower() in {"1", "true", "yes", "on"}
|
| 146 |
+
ASR_ENGINE = os.environ.get("ASR_ENGINE", "wav2vec2_onnx" if use_wav2vec2 else "moonshine").lower()
|
| 147 |
+
MOONSHINE_MODEL_ARCH = os.environ.get("MOONSHINE_MODEL_ARCH", "medium-streaming").lower()
|
| 148 |
ASR_MODEL_NAME = os.environ.get("ASR_MODEL_NAME", "facebook/wav2vec2-base-960h")
|
| 149 |
ASR_ONNX_REPO = os.environ.get("ASR_ONNX_REPO", "onnx-community/wav2vec2-base-960h-ONNX")
|
| 150 |
PUNCTUATE_TEXT = os.environ.get("PUNCTUATE_TEXT", "0").lower() in {"1", "true", "yes", "on"}
|
|
|
|
| 171 |
TINY_VOCA_PITCH_SHIFT = float(os.environ.get("TINY_VOCA_PITCH_SHIFT", "0.0"))
|
| 172 |
TINY_VOCA_PITCH_RANGE = float(os.environ.get("TINY_VOCA_PITCH_RANGE", "1.0"))
|
| 173 |
TINY_VOCA_MAX_TOKENS = int(os.environ.get("TINY_VOCA_MAX_TOKENS", "512"))
|
| 174 |
+
MODEL_INIT_MODE = os.environ.get("MODEL_INIT_MODE", "startup").lower() # lazy | startup
|
| 175 |
PIPER_PREFETCH_ON_STARTUP = os.environ.get("PIPER_PREFETCH_ON_STARTUP", "0").lower() in {"1", "true", "yes", "on"}
|
| 176 |
REQUEST_LOCK_TIMEOUT_SEC = float(os.environ.get("REQUEST_LOCK_TIMEOUT_SEC", "15"))
|
| 177 |
PIPER_TIMEOUT_SEC = float(os.environ.get("PIPER_TIMEOUT_SEC", "120"))
|
|
|
|
| 181 |
voice_style = None
|
| 182 |
processor = None
|
| 183 |
whisper_model = None
|
| 184 |
+
asr_transcriber = None
|
| 185 |
asr_session = None
|
| 186 |
asr_processor = None
|
| 187 |
punctuation_model = None
|
|
|
|
| 192 |
|
| 193 |
# Initialize models
|
| 194 |
def initialize_models(init_tts=True, init_asr=True):
|
| 195 |
+
global sess, voice_style, processor, whisper_model, asr_transcriber, asr_session, asr_processor
|
| 196 |
global kokoro_tts_mode, kokoro_input_names
|
| 197 |
global punctuation_model, punctuation_tokenizer
|
| 198 |
|
|
|
|
| 333 |
logger.info(f"Voice style vector loaded successfully from {voice_style_path}")
|
| 334 |
|
| 335 |
# Initialize ASR engine
|
| 336 |
+
if init_asr and ASR_ENGINE not in {"moonshine", "wav2vec2_onnx", "whisper_pt"}:
|
| 337 |
+
raise ValueError(
|
| 338 |
+
f"Unsupported ASR_ENGINE={ASR_ENGINE!r}; "
|
| 339 |
+
"supported engines are moonshine, wav2vec2_onnx, and whisper_pt"
|
| 340 |
+
)
|
| 341 |
+
|
| 342 |
+
if init_asr and ASR_ENGINE == "moonshine" and asr_transcriber is None:
|
| 343 |
+
from moonshine_voice import ModelArch, Transcriber, get_model_for_language
|
| 344 |
+
|
| 345 |
+
model_arches = {
|
| 346 |
+
"tiny": ModelArch.TINY,
|
| 347 |
+
"base": ModelArch.BASE,
|
| 348 |
+
"tiny-streaming": ModelArch.TINY_STREAMING,
|
| 349 |
+
"small-streaming": ModelArch.SMALL_STREAMING,
|
| 350 |
+
"medium-streaming": ModelArch.MEDIUM_STREAMING,
|
| 351 |
+
}
|
| 352 |
+
if MOONSHINE_MODEL_ARCH not in model_arches:
|
| 353 |
+
raise ValueError(
|
| 354 |
+
f"Unsupported MOONSHINE_MODEL_ARCH={MOONSHINE_MODEL_ARCH!r}; "
|
| 355 |
+
f"choose one of {', '.join(model_arches)}"
|
| 356 |
+
)
|
| 357 |
+
model_path_for_language, model_arch = get_model_for_language(
|
| 358 |
+
"en", model_arches[MOONSHINE_MODEL_ARCH]
|
| 359 |
+
)
|
| 360 |
+
logger.info(
|
| 361 |
+
f"Loading Moonshine ASR model ({MOONSHINE_MODEL_ARCH}) from {model_path_for_language}..."
|
| 362 |
+
)
|
| 363 |
+
asr_transcriber = Transcriber(model_path=model_path_for_language, model_arch=model_arch)
|
| 364 |
+
logger.info("Moonshine ASR model loaded successfully")
|
| 365 |
+
|
| 366 |
+
elif init_asr and ASR_ENGINE == "wav2vec2_onnx" and asr_session is None:
|
| 367 |
logger.info(f"Loading Wav2Vec2 ONNX ASR model ({ASR_MODEL_NAME})...")
|
| 368 |
# Load processor for feature extraction + CTC labels
|
| 369 |
asr_processor = Wav2Vec2Processor.from_pretrained(ASR_MODEL_NAME)
|
|
|
|
| 407 |
raise
|
| 408 |
asr_session = InferenceSession(asr_onnx_path_env, sess_options)
|
| 409 |
logger.info("Wav2Vec2 ONNX ASR model loaded")
|
| 410 |
+
elif init_asr and ASR_ENGINE == "whisper_pt" and (processor is None or whisper_model is None):
|
| 411 |
logger.info("ASR_ENGINE set to whisper_pt; loading Whisper model...")
|
| 412 |
processor = WhisperProcessor.from_pretrained("openai/whisper-base")
|
| 413 |
whisper_model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-base")
|
|
|
|
| 433 |
initialize_models(init_tts=True, init_asr=False)
|
| 434 |
|
| 435 |
def ensure_models_for_asr():
|
| 436 |
+
asr_ready = (
|
| 437 |
+
(ASR_ENGINE == "moonshine" and asr_transcriber is not None)
|
| 438 |
+
or (ASR_ENGINE == "wav2vec2_onnx" and asr_session is not None)
|
| 439 |
+
or (ASR_ENGINE == "whisper_pt" and processor is not None and whisper_model is not None)
|
| 440 |
)
|
| 441 |
punct_ready = (not PUNCTUATE_TEXT) or (punctuation_model is not None and punctuation_tokenizer is not None)
|
| 442 |
if asr_ready and punct_ready:
|
| 443 |
return
|
| 444 |
with model_init_lock:
|
| 445 |
+
asr_ready = (
|
| 446 |
+
(ASR_ENGINE == "moonshine" and asr_transcriber is not None)
|
| 447 |
+
or (ASR_ENGINE == "wav2vec2_onnx" and asr_session is not None)
|
| 448 |
+
or (ASR_ENGINE == "whisper_pt" and processor is not None and whisper_model is not None)
|
| 449 |
)
|
| 450 |
punct_ready = (not PUNCTUATE_TEXT) or (punctuation_model is not None and punctuation_tokenizer is not None)
|
| 451 |
if not (asr_ready and punct_ready):
|
|
|
|
| 1046 |
f"[{request_id}] ASR (Wav2Vec2 ONNX) complete in {asr_elapsed:.2f}s "
|
| 1047 |
f"(chars={len(transcription)})"
|
| 1048 |
)
|
| 1049 |
+
elif ASR_ENGINE == "moonshine" and asr_transcriber is not None:
|
| 1050 |
+
asr_start = time.monotonic()
|
| 1051 |
+
transcript_result = asr_transcriber.transcribe_without_streaming(
|
| 1052 |
+
audio_array,
|
| 1053 |
+
sample_rate=sampling_rate,
|
| 1054 |
+
flags=0,
|
| 1055 |
+
)
|
| 1056 |
+
transcription = " ".join(
|
| 1057 |
+
line.text.strip()
|
| 1058 |
+
for line in transcript_result.lines
|
| 1059 |
+
if line.text.strip()
|
| 1060 |
+
)
|
| 1061 |
+
asr_elapsed = time.monotonic() - asr_start
|
| 1062 |
+
logger.info(
|
| 1063 |
+
f"[{request_id}] ASR (Moonshine) complete in {asr_elapsed:.2f}s "
|
| 1064 |
+
f"(chars={len(transcription)})"
|
| 1065 |
+
)
|
| 1066 |
+
elif ASR_ENGINE == "whisper_pt":
|
| 1067 |
asr_start = time.monotonic()
|
| 1068 |
# Whisper fallback
|
| 1069 |
input_features = processor(
|
requirements.txt
CHANGED
|
@@ -15,4 +15,5 @@ ftfy
|
|
| 15 |
sentencepiece
|
| 16 |
torch --index-url https://download.pytorch.org/whl/cpu
|
| 17 |
onnxruntime
|
|
|
|
| 18 |
dizione @ git+https://github.com/n4ze3m/dizione
|
|
|
|
| 15 |
sentencepiece
|
| 16 |
torch --index-url https://download.pytorch.org/whl/cpu
|
| 17 |
onnxruntime
|
| 18 |
+
moonshine-voice
|
| 19 |
dizione @ git+https://github.com/n4ze3m/dizione
|