mrmuminov's picture
Update app.py
31a57d8 verified
raw
history blame
2.34 kB
import torch
import gradio as gr
import yt_dlp as youtube_dl
from transformers import pipeline
from transformers.pipelines.audio_utils import ffmpeg_read
from urllib.parse import urlparse, parse_qs
import tempfile
import time
import os
import numpy as np
# Constants
MODEL_NAME = "dataprizma/whisper-large-v3-turbo"
BATCH_SIZE = 8
FILE_LIMIT_MB = 1000
YT_LENGTH_LIMIT_S = 3600 # 1 hour limit
# Device selection
device = 0 if torch.cuda.is_available() else "cpu"
# Load Whisper pipeline
pipe = pipeline(
task="automatic-speech-recognition",
model=MODEL_NAME,
chunk_length_s=9,
device=device,
model_kwargs={
# "torch_dtype": torch.float16,
"attn_implementation": "eager"
},
)
# Transcription function (Fix applied)
def transcribe(audio_file, task):
if audio_file is None:
raise gr.Error("No audio file submitted! Please upload or record an audio file before submitting.")
# Open file as binary to ensure correct data type
with open(audio_file, "rb") as f:
audio_data = f.read()
# Read audio using ffmpeg_read (correcting input format)
audio_array = ffmpeg_read(audio_data, pipe.feature_extractor.sampling_rate)
duration = len(audio_array) / pipe.feature_extractor.sampling_rate
print(f"Audio duration: {duration:.2f} seconds")
# Convert to proper format
inputs = {
"array": np.array(audio_array),
"sampling_rate": pipe.feature_extractor.sampling_rate
}
generate_kwargs = {
"task": task,
"no_speech_threshold": 0.3,
"logprob_threshold": -1.0,
"compression_ratio_threshold": 2.4
}
# Perform transcription
result = pipe(
inputs,
batch_size=BATCH_SIZE,
generate_kwargs=generate_kwargs,
return_timestamps=False
)
return result["text"]
# Gradio UI
demo = gr.Blocks()
file_transcribe = gr.Interface(
fn=transcribe,
inputs=[
gr.Audio(type="filepath", label="Audio file"),
gr.Radio(["transcribe", "translate"], label="Task"),
],
outputs="text",
title="Whisper Large V3: Transcribe Audio",
description="Whisper Large V3 fine-tuned for Uzbek language by Dataprizma",
flagging_mode="never",
)
with demo:
gr.TabbedInterface([file_transcribe], ["Audio file"])
demo.launch()