File size: 1,744 Bytes
c8d4343
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77e5ef6
c8d4343
77e5ef6
36cffb4
 
77e5ef6
c8d4343
 
 
 
 
 
b0b7514
1651123
c8d4343
 
 
 
1651123
 
c8d4343
 
 
 
 
 
77e5ef6
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
47
48
49
50
51
52
53
54
55
56
57
import gradio as gr
import torch
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline

# Check if CUDA is available, and choose device accordingly
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 tokenizer
model_id = "openai/whisper-large-v3"
model = AutoModelForSpeechSeq2Seq.from_pretrained(
    model_id, torch_dtype=torch_dtype, use_safetensors=True
)
model.to(device)
processor = AutoProcessor.from_pretrained(model_id)

# Define a function to transcribe audio
pipe = pipeline(
    "automatic-speech-recognition",
    model=model,
    tokenizer=processor.tokenizer,
    feature_extractor=processor.feature_extractor,
    max_new_tokens=128,
    chunk_length_s=30,
    batch_size=16,
    return_timestamps=True,
    torch_dtype=torch_dtype,
    device=device,
)

def transcribe_audio(audio_file):
    # Check if audio file is None
    #if audio_file is None:
    #    raise ValueError("Input audio file is None.")
    
    # Use the pipeline to transcribe audio
    result = pipe(audio_file, generate_kwargs={"language": "english"})
    transcribed_text = result["text"]
    return transcribed_text

# Create a Gradio interface
audio_input = gr.Audio(label="Upload Audio", type="filepath")
output_text = gr.Textbox(label="Transcribed Text")

# Instantiate the Gradio interface
app = gr.Interface(
    fn=transcribe_audio,
    inputs=audio_input, 
    outputs=output_text,    
    title="Audio Transcription with Whisper Model",
    description="Upload an audio file to transcribe it into text using the Whisper model.",
    theme="compact"
)

# Launch the Gradio interface
app.launch(debug=True, inline=False)