mrmuminov's picture
Update app.py
51d1944 verified
raw
history blame
1.65 kB
import torch
import gradio as gr
from transformers import pipeline
from transformers.pipelines.audio_utils import ffmpeg_read
import numpy as np
MODEL_NAME = "dataprizma/whisper-large-v3-turbo"
BATCH_SIZE = 8
device = 0 if torch.cuda.is_available() else "cpu"
pipe = pipeline(
task="automatic-speech-recognition",
model=MODEL_NAME,
chunk_length_s=9,
device=device,
model_kwargs={
"attn_implementation": "eager"
},
)
def transcribe(audio_file):
if audio_file is None:
raise gr.Error("No audio file submitted! Please upload or record an audio file before submitting.")
with open(audio_file, "rb") as f:
audio_data = f.read()
audio_array = ffmpeg_read(audio_data, sampling_rate=pipe.feature_extractor.sampling_rate)
duration = len(audio_array) / pipe.feature_extractor.sampling_rate
print(f"Audio duration: {duration:.2f} seconds")
result = pipe(
inputs=audio_array,
batch_size=BATCH_SIZE,
return_timestamps=False,
generate_kwargs={
"task": "transcribe",
"no_speech_threshold": 0.4,
"logprob_threshold": -1.0,
"compression_ratio_threshold": 2.4
}
)
return result["text"] if isinstance(result, dict) else result
demo = gr.Blocks()
file_transcribe = gr.Interface(
fn=transcribe,
inputs=gr.Audio(type="filepath", label="Audio file"),
outputs="text",
title="Whisper Large V3: Transcribe Audio",
description="Whisper Large V3 fine-tuned for Uzbek language by Dataprizma",
)
with demo:
gr.TabbedInterface([file_transcribe], ["Audio file"])
demo.launch()