zawhtetpaing commited on
Commit
2b33a52
·
verified ·
1 Parent(s): 6a13a31

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -0
app.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import torch
3
+ import gradio as gr
4
+ from transformers import pipeline
5
+
6
+ # Model configuration
7
+ MODEL_NAME = "chuuhtetnaing/whisper-large-v3-myanmar"
8
+
9
+ # Automatically use GPU if available on HF Space, fallback to CPU
10
+ device = "cuda:0" if torch.cuda.is_available() else "cpu"
11
+ torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
12
+
13
+ print(f"Loading pipeline on {device}...")
14
+
15
+ # Load model pipeline ONCE when the Space starts up
16
+ pipe = pipeline(
17
+ "automatic-speech-recognition",
18
+ model=MODEL_NAME,
19
+ torch_dtype=torch_dtype,
20
+ device=device,
21
+ chunk_length_s=30, # Splice long audio into 30s chunks automatically
22
+ )
23
+
24
+ def transcribe_audio(audio_file):
25
+ if audio_file is None:
26
+ return "Please upload or record an audio snippet."
27
+
28
+ # Run transcription
29
+ outputs = pipe(
30
+ audio_file,
31
+ generate_kwargs={
32
+ "language": "burmese",
33
+ "task": "transcribe"
34
+ },
35
+ return_timestamps=True,
36
+ )
37
+
38
+ return outputs["text"]
39
+
40
+ # Define Gradio Interface
41
+ demo = gr.Interface(
42
+ fn=transcribe_audio,
43
+ inputs=gr.Audio(
44
+ type="filepath",
45
+ label="Record or Upload Audio",
46
+ sources=["microphone", "upload"]
47
+ ),
48
+ outputs=gr.Textbox(
49
+ label="Myanmar Transcription Result",
50
+ lines=6,
51
+ show_copy_button=True
52
+ ),
53
+ title="🇲🇲 Myanmar Speech-to-Text (Whisper Large v3)",
54
+ description="Upload an audio file (`.wav`, `.mp3`, `.m4a`) or record directly from your microphone to get Myanmar transcriptions.",
55
+ )
56
+
57
+ if __name__ == "__main__":
58
+ demo.launch()