File size: 1,048 Bytes
8df7109
ec0f7db
acb5575
 
335d62d
 
ec0f7db
 
 
 
 
 
 
acb5575
 
 
8df7109
acb5575
 
 
 
 
 
 
 
 
 
 
 
 
 
335d62d
acb5575
335d62d
acb5575
335d62d
acb5575
335d62d
acb5575
335d62d
ec0f7db
 
 
acb5575
 
 
 
ec0f7db
 
335d62d
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
import spaces
import gradio as gr
from transformers import pipeline
from pydub import AudioSegment
import tempfile
import os

pipe = pipeline(
    "automatic-speech-recognition",
    model="openai/whisper-large-v3",
    device_map="auto"
)

CHUNK_LENGTH_MS = 30 * 1000  # 30 seconds


@spaces.GPU
def transcribe(audio_path):
    audio = AudioSegment.from_file(audio_path)

    transcript = []

    for i in range(0, len(audio), CHUNK_LENGTH_MS):
        chunk = audio[i:i + CHUNK_LENGTH_MS]

        temp_file = tempfile.NamedTemporaryFile(
            suffix=".wav",
            delete=False
        )

        chunk.export(temp_file.name, format="wav")

        result = pipe(temp_file.name)

        transcript.append(result["text"])

        os.remove(temp_file.name)

    return "\n".join(transcript)


demo = gr.Interface(
    fn=transcribe,
    inputs=gr.Audio(type="filepath"),
    outputs=gr.Textbox(lines=20),
    title="Whisper Large V3 - Long Audio Support",
    description="Upload long audio files for transcription."
)

demo.launch()