Spaces:
Sleeping
Sleeping
import gradio as gr | |
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline | |
import torch | |
import requests | |
import os | |
model_id = "distil-whisper/distil-large-v2" | |
device = "cuda:0" if torch.cuda.is_available() else "cpu" | |
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32 | |
model = AutoModelForSpeechSeq2Seq.from_pretrained( | |
model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True, use_safetensors=True | |
) | |
model.to(device) | |
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32 | |
processor = AutoProcessor.from_pretrained(model_id) | |
pipe = pipeline( | |
"automatic-speech-recognition", | |
model=model, | |
tokenizer=processor.tokenizer, | |
feature_extractor=processor.feature_extractor, | |
max_new_tokens=128, | |
torch_dtype=torch_dtype, | |
device=device, | |
) | |
def transcribe_audio(audio_file): | |
recorded_filename = audio_file.name | |
if os.path.exists(recorded_filename): | |
results = pipe(recorded_filename) | |
return results["text"] | |
else: | |
return "Error: No audio file uploaded." | |
inputs = gr.Audio(sources="upload", type="filepath") | |
outputs = gr.Textbox() | |
interface = gr.Interface(fn=transcribe_audio, inputs=inputs, outputs=outputs, title="Audio Transcription App") | |
interface.launch() |