Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
|
| 5 |
+
# -----------------------------
|
| 6 |
+
# 1. Load Whisper model on CPU
|
| 7 |
+
# -----------------------------
|
| 8 |
+
MODEL_NAME = "Phonepadith/whisper-3-large-turbo-lao-finetuned" # Change if needed
|
| 9 |
+
|
| 10 |
+
device = "cpu"
|
| 11 |
+
torch_dtype = torch.float32
|
| 12 |
+
|
| 13 |
+
asr_pipeline = pipeline(
|
| 14 |
+
"automatic-speech-recognition",
|
| 15 |
+
model=MODEL_NAME,
|
| 16 |
+
tokenizer=MODEL_NAME,
|
| 17 |
+
feature_extractor=MODEL_NAME,
|
| 18 |
+
device=-1, # Force CPU
|
| 19 |
+
torch_dtype=torch_dtype,
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
# -----------------------------
|
| 23 |
+
# 2. Transcription function
|
| 24 |
+
# -----------------------------
|
| 25 |
+
def transcribe(audio_file):
|
| 26 |
+
if audio_file is None:
|
| 27 |
+
return "❌ Please upload an audio file."
|
| 28 |
+
try:
|
| 29 |
+
result = asr_pipeline(audio_file)
|
| 30 |
+
text = result["text"]
|
| 31 |
+
return f"🧩 Lao Transcription:\n\n{text}"
|
| 32 |
+
except Exception as e:
|
| 33 |
+
return f"❌ Error: {str(e)}"
|
| 34 |
+
|
| 35 |
+
# -----------------------------
|
| 36 |
+
# 3. Gradio Interface
|
| 37 |
+
# -----------------------------
|
| 38 |
+
title = "🎧 Lao Whisper Test (CPU)"
|
| 39 |
+
description = """
|
| 40 |
+
ທົດສອບຮູບແບບຈຳລອງ Whisper ທີ່ປັບປຸງໃຫ້ຮັບຮູ້ພາສາລາວ 🇱🇦
|
| 41 |
+
Upload an audio file (`.wav`, `.mp3`, `.m4a`, `.flac`) and get the Lao transcription.
|
| 42 |
+
"""
|
| 43 |
+
|
| 44 |
+
iface = gr.Interface(
|
| 45 |
+
fn=transcribe,
|
| 46 |
+
inputs=gr.Audio(type="filepath", label="🎵 Upload Lao Speech Audio"),
|
| 47 |
+
outputs=gr.Textbox(label="🧾 Transcription Result", lines=8),
|
| 48 |
+
title=title,
|
| 49 |
+
description=description,
|
| 50 |
+
allow_flagging="never",
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
if __name__ == "__main__":
|
| 54 |
+
iface.launch()
|