File size: 1,370 Bytes
80f8097
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85a9111
80f8097
 
 
 
 
 
 
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
43
44
45
46
import gradio as gr
import torch
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline

# Set up the device (GPU or CPU)
device = "cuda:0" if torch.cuda.is_available() else "cpu"
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32

# Load the model and processor
model_id = "ylacombe/whisper-large-v3-turbo"
model = AutoModelForSpeechSeq2Seq.from_pretrained(
    model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True, use_safetensors=True
)
model.to(device)
processor = AutoProcessor.from_pretrained(model_id)

# Create a pipeline for speech recognition
pipe = pipeline(
    "automatic-speech-recognition",
    model=model,
    tokenizer=processor.tokenizer,
    feature_extractor=processor.feature_extractor,
    torch_dtype=torch_dtype,
    device=device,
)

def transcribe_audio(audio):
    # Preprocess the audio
    audio_input = processor(audio, return_tensors="pt", sampling_rate=16000)
    audio_input = audio_input.to(device)
    
    # Run the pipeline to get the transcription
    result = pipe(audio_input)
    return result["text"]

# Create a Gradio interface
demo = gr.Interface(
    transcribe_audio,
    inputs=gr.Audio(type="file"),
    outputs="text",
    title="Speech-to-Text Transcription",
    description="Upload an audio file to transcribe its content.",
)

# Launch the Gradio app
demo.launch()