|
import gradio as gr |
|
from transformers import pipeline |
|
import torch |
|
import os |
|
import tempfile |
|
import soundfile as sf |
|
|
|
|
|
device = 0 if torch.cuda.is_available() else -1 |
|
asr_pipeline = pipeline(model="openai/whisper-small", device=device) |
|
|
|
|
|
def transcribe_audio(audio_file): |
|
|
|
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_audio_file: |
|
temp_audio_file.write(audio_file.read()) |
|
temp_file_path = temp_audio_file.name |
|
|
|
|
|
transcription = asr_pipeline(temp_file_path) |
|
|
|
|
|
os.remove(temp_file_path) |
|
|
|
|
|
return transcription['text'] |
|
|
|
|
|
interface = gr.Interface( |
|
fn=transcribe_audio, |
|
inputs=gr.Audio(source="upload", type="file"), |
|
outputs="text", |
|
title="Whisper Audio Transcription", |
|
description="Upload an audio file to get a transcription using OpenAI's Whisper model" |
|
) |
|
|
|
|
|
interface.launch() |