File size: 1,594 Bytes
7b60995
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import torch
import gradio as gr
from transformers import pipeline

# Model configuration
MODEL_NAME = "chuuhtetnaing/whisper-large-v3-myanmar"

# Automatically use GPU if available on HF Space, fallback to CPU
device = "cuda:0" if torch.cuda.is_available() else "cpu"
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32

print(f"Loading pipeline on {device}...")

# Load model pipeline ONCE when the Space starts up
pipe = pipeline(
    "automatic-speech-recognition",
    model=MODEL_NAME,
    torch_dtype=torch_dtype,
    device=device,
    chunk_length_s=30,  # Splice long audio into 30s chunks automatically
)

def transcribe_audio(audio_file):
    if audio_file is None:
        return "Please upload or record an audio snippet."

    # Run transcription
    outputs = pipe(
        audio_file,
        generate_kwargs={
            "language": "burmese", 
            "task": "transcribe"
        },
        return_timestamps=True,
    )
    
    return outputs["text"]

# Define Gradio Interface
demo = gr.Interface(
    fn=transcribe_audio,
    inputs=gr.Audio(
        type="filepath", 
        label="Record or Upload Audio",
        sources=["microphone", "upload"]
    ),
    outputs=gr.Textbox(
        label="Myanmar Transcription Result", 
        lines=6, 
        show_copy_button=True
    ),
    title="🇲🇲 Myanmar Speech-to-Text (Whisper Large v3)",
    description="Upload an audio file (`.wav`, `.mp3`, `.m4a`) or record directly from your microphone to get Myanmar transcriptions.",
)

if __name__ == "__main__":
    demo.launch()