Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline | |
| import torch | |
| model_id = "distil-whisper/distil-large-v3" | |
| 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) | |
| def transcribe_audio(audio_file): | |
| 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, | |
| ) | |
| results = pipe(audio_file) | |
| return results["text"] | |
| 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() |