Update handler.py
Browse files- handler.py +10 -9
handler.py
CHANGED
|
@@ -1,25 +1,26 @@
|
|
| 1 |
# handler.py
|
| 2 |
-
import io
|
| 3 |
-
import torch
|
| 4 |
-
from omnilingual_asr.models.inference.pipeline import ASRInferencePipeline
|
| 5 |
|
| 6 |
class EndpointHandler:
|
| 7 |
def __init__(self, path=""):
|
| 8 |
-
#
|
|
|
|
|
|
|
|
|
|
| 9 |
self.pipeline = ASRInferencePipeline(model_card="facebook/omniASR-LLM-7B")
|
| 10 |
|
| 11 |
def __call__(self, data):
|
| 12 |
-
#
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
| 14 |
audio_bytes = data.get("inputs")
|
| 15 |
if not audio_bytes:
|
| 16 |
return {"error": "no audio provided"}
|
| 17 |
|
| 18 |
-
# convert bytes to temp file, path etc.
|
| 19 |
-
import soundfile as sf
|
| 20 |
f = io.BytesIO(audio_bytes)
|
| 21 |
audio, sr = sf.read(f)
|
| 22 |
|
| 23 |
-
#
|
| 24 |
result = self.pipeline.transcribe([audio], batch_size=1)
|
| 25 |
return {"text": result}
|
|
|
|
| 1 |
# handler.py
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
class EndpointHandler:
|
| 4 |
def __init__(self, path=""):
|
| 5 |
+
# lazy import only inside init
|
| 6 |
+
from omnilingual_asr.models.inference.pipeline import ASRInferencePipeline
|
| 7 |
+
|
| 8 |
+
# Load the omniASR pipeline
|
| 9 |
self.pipeline = ASRInferencePipeline(model_card="facebook/omniASR-LLM-7B")
|
| 10 |
|
| 11 |
def __call__(self, data):
|
| 12 |
+
# import here so it doesn’t trigger during module load
|
| 13 |
+
import io
|
| 14 |
+
import soundfile as sf
|
| 15 |
+
|
| 16 |
+
# read raw bytes
|
| 17 |
audio_bytes = data.get("inputs")
|
| 18 |
if not audio_bytes:
|
| 19 |
return {"error": "no audio provided"}
|
| 20 |
|
|
|
|
|
|
|
| 21 |
f = io.BytesIO(audio_bytes)
|
| 22 |
audio, sr = sf.read(f)
|
| 23 |
|
| 24 |
+
# run transcription
|
| 25 |
result = self.pipeline.transcribe([audio], batch_size=1)
|
| 26 |
return {"text": result}
|