Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
import os,
|
| 2 |
import torch
|
| 3 |
import gradio as gr
|
| 4 |
from transformers import pipeline
|
|
@@ -13,7 +13,7 @@ HAS_CUDA = torch.cuda.is_available()
|
|
| 13 |
DEVICE = 0 if HAS_CUDA else "cpu"
|
| 14 |
DTYPE = torch.float16 if HAS_CUDA else torch.float32
|
| 15 |
|
| 16 |
-
#
|
| 17 |
asr_pipe = pipeline(
|
| 18 |
task="automatic-speech-recognition",
|
| 19 |
model=ASR_MODEL,
|
|
@@ -23,45 +23,45 @@ asr_pipe = pipeline(
|
|
| 23 |
)
|
| 24 |
|
| 25 |
def _save_text_file(text: str, suffix: str = ".txt") -> str:
|
| 26 |
-
"""บันทึกไฟล์ชั่วคราวแล้วคืน path (เพื่อให้ Gradio สร้างปุ่มดาวน์โหลด)"""
|
| 27 |
fd, path = tempfile.mkstemp(suffix=suffix)
|
| 28 |
with os.fdopen(fd, "w", encoding="utf-8") as f:
|
| 29 |
f.write(text)
|
| 30 |
return path
|
| 31 |
|
| 32 |
-
def _transcribe_from_path(
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
# อ่านเป็น bytes แล้วให้ ffmpeg แปลงเป็น waveform (float32 mono)
|
| 38 |
-
with open(audio_path, "rb") as f:
|
| 39 |
payload = f.read()
|
| 40 |
audio = ffmpeg_read(payload, asr_pipe.feature_extractor.sampling_rate)
|
| 41 |
inputs = {"array": audio, "sampling_rate": asr_pipe.feature_extractor.sampling_rate}
|
| 42 |
|
| 43 |
-
# task = "transcribe" (คงภาษาเดิม) หรือ "translate" (ให้แปลเป็นอังกฤษ)
|
| 44 |
out = asr_pipe(
|
| 45 |
inputs,
|
| 46 |
batch_size=BATCH_SIZE,
|
| 47 |
-
generate_kwargs={"task": task},
|
| 48 |
return_timestamps=True,
|
| 49 |
)
|
| 50 |
text = out["text"]
|
| 51 |
-
|
| 52 |
-
return text, txt_path
|
| 53 |
|
|
|
|
| 54 |
def transcribe_mic(mic_path: str, task: str):
|
| 55 |
return _transcribe_from_path(mic_path, task)
|
| 56 |
|
| 57 |
-
def
|
| 58 |
return _transcribe_from_path(file_path, task)
|
| 59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
# -----------------------------
|
| 61 |
# UI
|
| 62 |
# -----------------------------
|
| 63 |
-
with gr.Blocks(title="Whisper V3 – Transcriber") as demo:
|
| 64 |
-
gr.Markdown("## 🎙️ Whisper V3 – Upload
|
| 65 |
|
| 66 |
with gr.Tab("🎤 Microphone"):
|
| 67 |
mic_audio = gr.Audio(sources="microphone", type="filepath", label="Record")
|
|
@@ -75,6 +75,13 @@ with gr.Blocks(title="Whisper V3 – Transcriber") as demo:
|
|
| 75 |
up_task = gr.Radio(["transcribe", "translate"], value="transcribe", label="Task")
|
| 76 |
up_text = gr.Textbox(label="Transcript", lines=10)
|
| 77 |
up_file = gr.File(label="Download Transcript (.txt)")
|
| 78 |
-
gr.Button("Run").click(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
demo.queue().launch()
|
|
|
|
| 1 |
+
import os, tempfile
|
| 2 |
import torch
|
| 3 |
import gradio as gr
|
| 4 |
from transformers import pipeline
|
|
|
|
| 13 |
DEVICE = 0 if HAS_CUDA else "cpu"
|
| 14 |
DTYPE = torch.float16 if HAS_CUDA else torch.float32
|
| 15 |
|
| 16 |
+
# โหลดโมเดลครั้งเดียว
|
| 17 |
asr_pipe = pipeline(
|
| 18 |
task="automatic-speech-recognition",
|
| 19 |
model=ASR_MODEL,
|
|
|
|
| 23 |
)
|
| 24 |
|
| 25 |
def _save_text_file(text: str, suffix: str = ".txt") -> str:
|
|
|
|
| 26 |
fd, path = tempfile.mkstemp(suffix=suffix)
|
| 27 |
with os.fdopen(fd, "w", encoding="utf-8") as f:
|
| 28 |
f.write(text)
|
| 29 |
return path
|
| 30 |
|
| 31 |
+
def _transcribe_from_path(path: str, task: str):
|
| 32 |
+
if not path:
|
| 33 |
+
raise gr.Error("โปรดอัปโหลดไฟล์ก่อน")
|
| 34 |
+
# อ่านเป็น bytes แล้วให้ ffmpeg แปลงเป็น waveform (mono float32)
|
| 35 |
+
with open(path, "rb") as f:
|
|
|
|
|
|
|
| 36 |
payload = f.read()
|
| 37 |
audio = ffmpeg_read(payload, asr_pipe.feature_extractor.sampling_rate)
|
| 38 |
inputs = {"array": audio, "sampling_rate": asr_pipe.feature_extractor.sampling_rate}
|
| 39 |
|
|
|
|
| 40 |
out = asr_pipe(
|
| 41 |
inputs,
|
| 42 |
batch_size=BATCH_SIZE,
|
| 43 |
+
generate_kwargs={"task": task}, # 'transcribe' = คงภาษาเดิม, 'translate' = แปลเป็นอังกฤษ
|
| 44 |
return_timestamps=True,
|
| 45 |
)
|
| 46 |
text = out["text"]
|
| 47 |
+
return text, _save_text_file(text, ".txt")
|
|
|
|
| 48 |
|
| 49 |
+
# ---- entry points สำหรับ UI สามแท็บ ----
|
| 50 |
def transcribe_mic(mic_path: str, task: str):
|
| 51 |
return _transcribe_from_path(mic_path, task)
|
| 52 |
|
| 53 |
+
def transcribe_audio(file_path: str, task: str):
|
| 54 |
return _transcribe_from_path(file_path, task)
|
| 55 |
|
| 56 |
+
def transcribe_video(video_path: str, task: str):
|
| 57 |
+
# ffmpeg_read รองรับไฟล์วิดีโอได้ (จะดึงเสียงออกมาให้)
|
| 58 |
+
return _transcribe_from_path(video_path, task)
|
| 59 |
+
|
| 60 |
# -----------------------------
|
| 61 |
# UI
|
| 62 |
# -----------------------------
|
| 63 |
+
with gr.Blocks(title="Whisper V3 – Transcriber (Audio + MP4)") as demo:
|
| 64 |
+
gr.Markdown("## 🎙️ Whisper V3 – Record/Upload Audio or MP4 → Transcript → Download (.txt)")
|
| 65 |
|
| 66 |
with gr.Tab("🎤 Microphone"):
|
| 67 |
mic_audio = gr.Audio(sources="microphone", type="filepath", label="Record")
|
|
|
|
| 75 |
up_task = gr.Radio(["transcribe", "translate"], value="transcribe", label="Task")
|
| 76 |
up_text = gr.Textbox(label="Transcript", lines=10)
|
| 77 |
up_file = gr.File(label="Download Transcript (.txt)")
|
| 78 |
+
gr.Button("Run").click(transcribe_audio, inputs=[up_audio, up_task], outputs=[up_text, up_file])
|
| 79 |
+
|
| 80 |
+
with gr.Tab("🎬 Video MP4"):
|
| 81 |
+
up_video = gr.Video(sources=["upload"], format="mp4", label="Upload MP4")
|
| 82 |
+
vd_task = gr.Radio(["transcribe", "translate"], value="transcribe", label="Task")
|
| 83 |
+
vd_text = gr.Textbox(label="Transcript", lines=10)
|
| 84 |
+
vd_file = gr.File(label="Download Transcript (.txt)")
|
| 85 |
+
gr.Button("Run").click(transcribe_video, inputs=[up_video, vd_task], outputs=[vd_text, vd_file])
|
| 86 |
|
| 87 |
demo.queue().launch()
|