Spaces:
Runtime error
Runtime error
Update backend/asr.py
Browse files- backend/asr.py +38 -38
backend/asr.py
CHANGED
|
@@ -5,7 +5,7 @@ from __future__ import annotations
|
|
| 5 |
import numpy as np
|
| 6 |
|
| 7 |
from backend.utils import device
|
| 8 |
-
|
| 9 |
|
| 10 |
try:
|
| 11 |
import torch
|
|
@@ -31,35 +31,35 @@ def _huggingface_device() -> int | str | None:
|
|
| 31 |
return "cpu"
|
| 32 |
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
def _initialize_whisper_pipeline():
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
_ASR_TYPHOON = None
|
| 61 |
-
|
| 62 |
-
_ASR_WHISPER = _initialize_whisper_pipeline()
|
| 63 |
|
| 64 |
|
| 65 |
def _transcribe_with_pipeline(audio_array: np.ndarray) -> str:
|
|
@@ -98,17 +98,17 @@ def transcribe_audio(audio_array: np.ndarray) -> str:
|
|
| 98 |
"""Transcribe user audio with the best available backend."""
|
| 99 |
if audio_array is None or not np.any(audio_array):
|
| 100 |
return ""
|
| 101 |
-
|
| 102 |
-
# try:
|
| 103 |
-
# transcriptions = _ASR_PIPELINE.transcribe(audio=audio_array)
|
| 104 |
-
# except Exception as exc:
|
| 105 |
-
# print(f"Typhoon ASR pipeline failed: {exc}")
|
| 106 |
-
if _ASR_WHISPER:
|
| 107 |
try:
|
| 108 |
-
|
| 109 |
-
return transcription
|
| 110 |
except Exception as exc:
|
| 111 |
print(f"Typhoon ASR pipeline failed: {exc}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
try:
|
| 114 |
return _transcribe_with_google(audio_array)
|
|
|
|
| 5 |
import numpy as np
|
| 6 |
|
| 7 |
from backend.utils import device
|
| 8 |
+
import nemo.collections.asr as nemo_asr
|
| 9 |
|
| 10 |
try:
|
| 11 |
import torch
|
|
|
|
| 31 |
return "cpu"
|
| 32 |
|
| 33 |
|
| 34 |
+
def _initialize_typhoon_pipeline():
|
| 35 |
+
if torch is None or pipeline is None:
|
| 36 |
+
return None
|
| 37 |
+
device = 'cuda' if torch.cuda.is_available() else 'mps'
|
| 38 |
+
print(f"Using device: {device}")
|
| 39 |
+
print("Initializing Typhoon ASR pipeline...")
|
| 40 |
+
asr_model = nemo_asr.models.ASRModel.from_pretrained(
|
| 41 |
+
model_name="scb10x/typhoon-asr-realtime",
|
| 42 |
+
map_location=device
|
| 43 |
+
)
|
| 44 |
+
print("Typhoon ASR pipeline initialized.")
|
| 45 |
+
return asr_model
|
| 46 |
+
|
| 47 |
+
# def _initialize_whisper_pipeline():
|
| 48 |
+
# pipe = pipeline(
|
| 49 |
+
# task="automatic-speech-recognition",
|
| 50 |
+
# model="nectec/Pathumma-whisper-th-medium",
|
| 51 |
+
# chunk_length_s=30,
|
| 52 |
+
# device=device,
|
| 53 |
+
# model_kwargs={"torch_dtype": torch.bfloat16},
|
| 54 |
+
# )
|
| 55 |
+
# pipe.model.config.forced_decoder_ids = pipe.tokenizer.get_decoder_prompt_ids(
|
| 56 |
+
# language='th',
|
| 57 |
+
# task="transcribe"
|
| 58 |
+
# )
|
| 59 |
+
# return pipe
|
| 60 |
+
# _ASR_TYPHOON = None
|
| 61 |
+
_ASR_TYPHOON = _initialize_typhoon_pipeline()
|
| 62 |
+
# _ASR_WHISPER = _initialize_whisper_pipeline()
|
| 63 |
|
| 64 |
|
| 65 |
def _transcribe_with_pipeline(audio_array: np.ndarray) -> str:
|
|
|
|
| 98 |
"""Transcribe user audio with the best available backend."""
|
| 99 |
if audio_array is None or not np.any(audio_array):
|
| 100 |
return ""
|
| 101 |
+
if _ASR_TYPHOON:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
try:
|
| 103 |
+
transcriptions = _ASR_TYPHOON.transcribe(audio=audio_array)
|
|
|
|
| 104 |
except Exception as exc:
|
| 105 |
print(f"Typhoon ASR pipeline failed: {exc}")
|
| 106 |
+
# if _ASR_WHISPER:
|
| 107 |
+
# try:
|
| 108 |
+
# transcription = _ASR_WHISPER(audio_array)["text"]
|
| 109 |
+
# return transcription
|
| 110 |
+
# except Exception as exc:
|
| 111 |
+
# print(f"Typhoon ASR pipeline failed: {exc}")
|
| 112 |
|
| 113 |
try:
|
| 114 |
return _transcribe_with_google(audio_array)
|