File size: 1,872 Bytes
ad3bc83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from datasets import load_dataset
import soundfile as sf

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

# Define the ASR pipeline
asr_pipeline = 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,
)

# Function to process audio in chunks and return the combined text
def process_audio(file_info):
    path = file_info["path"]
    audio_stream = sf.SoundFile(path, 'r')
    results = []
    while True:
        data = audio_stream.read(dtype='float32')
        if len(data) == 0:
            break
        result = asr_pipeline(data)
        results.append(result)
    audio_stream.close()
    combined_text = " ".join([r["text"] for r in results])
    return combined_text

# Create the Gradio interface
iface = gr.Interface(
    fn=process_audio,
    inputs=gr.inputs.Audio(source="upload", type="file", label="Upload your audio file"),
    outputs="text",
    title="👋🏻Welcome To 🙋🏻‍♂️Patrick's Whisper🌬️",
    description="Upload a large audio file to transcribe it into text using [Whisper3Large](https://huggingface.co/openai/whisper-large-v3) !",
)

# Launch the application
iface.launch()