File size: 2,046 Bytes
1a50ea3
9e5d6d7
1a50ea3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
58
59
60
61
62
63
64
65
66
import gradio as gr
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline
import torch

# Set up device and data type for torch based on GPU availability
device = "cuda" if torch.cuda.is_available() else "cpu"
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32

print(f"Using device: {device}, torch_dtype: {torch_dtype}")



# Correct the model_id if using from the Hugging Face Model Hub
model_id = "distil-whisper/distil-large-v3"
model = AutoModelForSpeechSeq2Seq.from_pretrained(model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True, use_safetensors=True)
processor = AutoProcessor.from_pretrained(model_id)

model.to(device)

print(f"Model and processor loaded successfully: {model_id}")



def transcribe_speech(file_info):
    filepath = file_info['path']
    sample = processor(filepath, return_tensors="pt")
    input_features = sample.input_features.to(device)

    # Check audio length to decide on chunking
    audio_length_seconds = sample.input_values.shape[1] / processor.feature_extractor.sampling_rate
    if audio_length_seconds > 30:
        chunk_length_s = 15
        batch_size = 2
    else:
        chunk_length_s = None
        batch_size = 1

    # Use the model and processor directly in the pipeline function
    pipe = pipeline(
        "automatic-speech-recognition",
        model=model,
        feature_extractor=processor.feature_extractor,
        tokenizer=processor.tokenizer,
        device=device,
        max_new_tokens=128,
        chunk_length_s=chunk_length_s,
        batch_size=batch_size,
        torch_dtype=torch_dtype
    )

    result = pipe(input_features)
    return result["text"]




with gr.Blocks() as demo:
    with gr.Tab("Transcribe Audio"):
        with gr.Row():
            audio_input = gr.Audio(label="Upload audio file or record")
        with gr.Row():
            audio_output = gr.Textbox(label="Transcription")
        demo.add_callback(transcribe_speech, inputs=[audio_input], outputs=[audio_output])

demo.launch(share=True)