Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import tempfile
|
| 3 |
+
import uuid
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
import gradio as gr
|
| 6 |
+
import ffmpeg
|
| 7 |
+
from faster_whisper import WhisperModel
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
# --- Helper functions ---
|
| 11 |
+
def _format_timestamp(seconds: float) -> str:
|
| 12 |
+
ms = int(round(seconds * 1000))
|
| 13 |
+
hours = ms // 3600000
|
| 14 |
+
ms_rem = ms % 3600000
|
| 15 |
+
minutes = ms_rem // 60000
|
| 16 |
+
ms_rem = ms_rem % 60000
|
| 17 |
+
secs = ms_rem // 1000
|
| 18 |
+
millis = ms_rem % 1000
|
| 19 |
+
return f"{hours:02d}:{minutes:02d}:{secs:02d},{millis:03d}"
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def segments_to_srt(segments: list) -> str:
|
| 25 |
+
lines = []
|
| 26 |
+
for i, seg in enumerate(segments, start=1):
|
| 27 |
+
start_ts = _format_timestamp(seg['start'])
|
| 28 |
+
end_ts = _format_timestamp(seg['end'])
|
| 29 |
+
text = seg['text'].replace('\n', ' ').strip()
|
| 30 |
+
if not text:
|
| 31 |
+
continue
|
| 32 |
+
block = f"{i}\n{start_ts} --> {end_ts}\n{text}\n"
|
| 33 |
+
lines.append(block)
|
| 34 |
+
return "\n".join(lines)
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
# --- Configuration ---
|
| 40 |
+
MODEL_NAME = "Systran/faster-whisper-small" # small model for HF free CPU
|
| 41 |
+
DEVICE = "cpu"
|
| 42 |
+
OUTPUT_DIR = Path("outputs/subtitles")
|
| 43 |
+
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
print(f"Loading model {MODEL_NAME} on {DEVICE} ...")
|
| 47 |
+
model = WhisperModel(MODEL_NAME, device=DEVICE)
|
| 48 |
+
print("Model loaded.")
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
def extract_audio(input_path: str, out_path: str):
|
| 54 |
+
try:
|
| 55 |
+
_ = (
|
| 56 |
+
ffmpeg
|
| 57 |
+
.input(input_path)
|
| 58 |
+
.output(out_path, format='wav', acodec='pcm_s16le', ac=1, ar='16000')
|
| 59 |
+
.overwrite_output()
|
| 60 |
+
.run(quiet=True)
|
| 61 |
+
)
|
| 62 |
+
except ffmpeg.Error as e:
|
| 63 |
+
stderr = getattr(e, 'stderr', None)
|
| 64 |
+
msg = stderr.decode() if stderr else str(e)
|
| 65 |
+
raise RuntimeError(f"ffmpeg error: {msg}")
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
def transcribe_file_to_srt(file_obj, language: str = "en"):
|
| 71 |
+
filename = getattr(file_obj, 'name', getattr(file_obj, 'filename', f"upload_{uuid.uuid4()}.bin"))
|
| 72 |
+
input_filepath = Path(filename)
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
tmp_dir = Path(tempfile.mkdtemp(prefix="subgen_"))
|
| 76 |
+
saved_input = tmp_dir / (str(uuid.uuid4()) + input_filepath.suffix)
|
| 77 |
+
demo.launch(share=True)
|