File size: 1,285 Bytes
9232ed6
 
 
 
 
 
c7fc9c2
9232ed6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36a83db
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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()