Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import subprocess
|
3 |
+
import os
|
4 |
+
import tempfile
|
5 |
+
import sys
|
6 |
+
|
7 |
+
HF_REPO = "kyutai/stt-2.6b-en" # โมเดลที่จะโหลด
|
8 |
+
|
9 |
+
def transcribe(audio_path: str) -> str:
|
10 |
+
"""
|
11 |
+
รับพาธไฟล์เสียง (.wav/.mp3 ฯลฯ) แล้วเรียก moshi CLI
|
12 |
+
คืนค่าเป็น text transcript
|
13 |
+
"""
|
14 |
+
if audio_path is None:
|
15 |
+
return ""
|
16 |
+
|
17 |
+
# moshi CLI: python -m moshi.run_inference --hf-repo <repo> <wav> :contentReference[oaicite:2]{index=2}
|
18 |
+
cmd = [
|
19 |
+
sys.executable, "-m", "moshi.run_inference",
|
20 |
+
"--hf-repo", HF_REPO,
|
21 |
+
audio_path
|
22 |
+
]
|
23 |
+
# เก็บ stdout ทั้งหมดไว้ อ่านบรรทัดสุดท้ายเป็น transcription
|
24 |
+
result = subprocess.run(cmd, capture_output=True, text=True)
|
25 |
+
if result.returncode != 0:
|
26 |
+
raise RuntimeError(result.stderr)
|
27 |
+
|
28 |
+
# moshi จะพิมพ์ผลทีละบรรทัด บรรทัดสุดท้ายคือคำถอดเสียงสมบูรณ์
|
29 |
+
lines = [l for l in result.stdout.splitlines() if l.strip()]
|
30 |
+
return lines[-1] if lines else "(no output)"
|
31 |
+
|
32 |
+
demo = gr.Interface(
|
33 |
+
fn=transcribe,
|
34 |
+
inputs=gr.Audio(type="filepath", sources=["upload", "microphone"],
|
35 |
+
label="Audio (16-32 kHz)"),
|
36 |
+
outputs=gr.Textbox(label="Transcription"),
|
37 |
+
title="Kyutai STT-2.6B (Streaming ASR)",
|
38 |
+
description=(
|
39 |
+
"อัปโหลดหรืออัดเสียงภาษาอังกฤษ แล้วกด Submit เพื่อถอดเสียงด้วยโมเดลขนาด 2.6 B "
|
40 |
+
"(ใช้ CLI ของ moshi ภายใน Space)"
|
41 |
+
),
|
42 |
+
)
|
43 |
+
|
44 |
+
if __name__ == "__main__":
|
45 |
+
# share=True จะสร้าง public URL ให้อัตโนมัติหากเปิด “Community GPU” Space
|
46 |
+
demo.launch()
|