|
import torch |
|
import torchaudio |
|
import gradio as gr |
|
import time |
|
import numpy as np |
|
import scipy.io.wavfile |
|
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline |
|
|
|
|
|
device = "cpu" |
|
torch_dtype = torch.float32 |
|
MODEL_NAME = "openai/whisper-tiny" |
|
|
|
|
|
model = AutoModelForSpeechSeq2Seq.from_pretrained( |
|
MODEL_NAME, torch_dtype=torch_dtype, use_safetensors=True |
|
) |
|
model.to(device) |
|
|
|
|
|
processor = AutoProcessor.from_pretrained(MODEL_NAME) |
|
|
|
pipe = pipeline( |
|
task="automatic-speech-recognition", |
|
model=model, |
|
tokenizer=processor.tokenizer, |
|
feature_extractor=processor.feature_extractor, |
|
chunk_length_s=2, |
|
torch_dtype=torch_dtype, |
|
device=device, |
|
sampling_rate=16000, |
|
) |
|
|
|
|
|
def stream_transcribe(stream, new_chunk): |
|
start_time = time.time() |
|
try: |
|
sr, y = new_chunk |
|
|
|
|
|
if y.ndim > 1: |
|
y = y.mean(axis=1) |
|
|
|
y = y.astype(np.float32) |
|
y /= np.max(np.abs(y)) |
|
|
|
|
|
|