Spaces:
Sleeping
Sleeping
usertea commited on
Commit ·
5c9196d
1
Parent(s): b352be4
\EchoScript : Multi-language transcription, Automatic language detection, Translation to English, Batch processing (multiple audio files), TXT export, SRT export, VTT export, CPU-only execution, Faster-Whisper (Model Initialization Improvement)
Browse files
app.py
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
|
|
| 1 |
import tempfile
|
| 2 |
from pathlib import Path
|
|
|
|
| 3 |
|
| 4 |
import gradio as gr
|
| 5 |
from faster_whisper import WhisperModel
|
|
@@ -9,7 +11,8 @@ MODEL_SIZE = "base"
|
|
| 9 |
model = WhisperModel(
|
| 10 |
MODEL_SIZE,
|
| 11 |
device="cpu",
|
| 12 |
-
compute_type="int8"
|
|
|
|
| 13 |
)
|
| 14 |
|
| 15 |
LANGUAGE_NAMES = {
|
|
@@ -20,104 +23,220 @@ LANGUAGE_NAMES = {
|
|
| 20 |
"es": "Spanish",
|
| 21 |
"it": "Italian",
|
| 22 |
"pt": "Portuguese",
|
| 23 |
-
"nl": "Dutch"
|
|
|
|
|
|
|
|
|
|
| 24 |
}
|
| 25 |
|
| 26 |
|
| 27 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
-
if audio_file is None:
|
| 30 |
-
return "", "", None
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
)
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
for segment in segments:
|
| 41 |
-
transcript_lines.append(segment.text)
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
| 46 |
)
|
| 47 |
|
| 48 |
-
|
| 49 |
|
| 50 |
-
transcript_with_timestamps = "\n".join(
|
| 51 |
-
timestamp_lines
|
| 52 |
-
)
|
| 53 |
|
| 54 |
-
|
| 55 |
-
info.language,
|
| 56 |
-
info.language
|
| 57 |
-
)
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
)
|
|
|
|
|
|
|
| 63 |
|
| 64 |
-
|
| 65 |
|
| 66 |
-
with
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
return (
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
str(output_file)
|
| 77 |
)
|
| 78 |
|
| 79 |
|
| 80 |
-
with gr.Blocks(
|
|
|
|
|
|
|
| 81 |
|
| 82 |
gr.Markdown(
|
| 83 |
"""
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
-
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
"""
|
| 89 |
)
|
| 90 |
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
label="Upload Audio"
|
| 94 |
)
|
| 95 |
|
| 96 |
-
|
| 97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
)
|
| 99 |
|
| 100 |
-
|
| 101 |
-
|
| 102 |
)
|
| 103 |
|
| 104 |
-
|
| 105 |
-
label="
|
| 106 |
-
lines=
|
| 107 |
)
|
| 108 |
|
| 109 |
download_output = gr.File(
|
| 110 |
-
label="Download
|
| 111 |
)
|
| 112 |
|
| 113 |
-
|
| 114 |
-
fn=
|
| 115 |
-
inputs=
|
|
|
|
|
|
|
|
|
|
| 116 |
outputs=[
|
| 117 |
-
|
| 118 |
-
transcript_output,
|
| 119 |
download_output
|
| 120 |
]
|
| 121 |
)
|
| 122 |
|
| 123 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
```python
|
| 2 |
import tempfile
|
| 3 |
from pathlib import Path
|
| 4 |
+
from zipfile import ZipFile
|
| 5 |
|
| 6 |
import gradio as gr
|
| 7 |
from faster_whisper import WhisperModel
|
|
|
|
| 11 |
model = WhisperModel(
|
| 12 |
MODEL_SIZE,
|
| 13 |
device="cpu",
|
| 14 |
+
compute_type="int8",
|
| 15 |
+
download_root="/tmp/whisper_models"
|
| 16 |
)
|
| 17 |
|
| 18 |
LANGUAGE_NAMES = {
|
|
|
|
| 23 |
"es": "Spanish",
|
| 24 |
"it": "Italian",
|
| 25 |
"pt": "Portuguese",
|
| 26 |
+
"nl": "Dutch",
|
| 27 |
+
"ar": "Arabic",
|
| 28 |
+
"ru": "Russian",
|
| 29 |
+
"tr": "Turkish"
|
| 30 |
}
|
| 31 |
|
| 32 |
|
| 33 |
+
def format_srt_timestamp(seconds):
|
| 34 |
+
hours = int(seconds // 3600)
|
| 35 |
+
minutes = int((seconds % 3600) // 60)
|
| 36 |
+
secs = int(seconds % 60)
|
| 37 |
+
millis = int((seconds - int(seconds)) * 1000)
|
| 38 |
+
|
| 39 |
+
return (
|
| 40 |
+
f"{hours:02}:{minutes:02}:{secs:02},{millis:03}"
|
| 41 |
+
)
|
| 42 |
|
|
|
|
|
|
|
| 43 |
|
| 44 |
+
def format_vtt_timestamp(seconds):
|
| 45 |
+
hours = int(seconds // 3600)
|
| 46 |
+
minutes = int((seconds % 3600) // 60)
|
| 47 |
+
secs = int(seconds % 60)
|
| 48 |
+
millis = int((seconds - int(seconds)) * 1000)
|
| 49 |
+
|
| 50 |
+
return (
|
| 51 |
+
f"{hours:02}:{minutes:02}:{secs:02}.{millis:03}"
|
| 52 |
)
|
| 53 |
|
| 54 |
+
|
| 55 |
+
def generate_srt(segments):
|
| 56 |
+
lines = []
|
| 57 |
+
|
| 58 |
+
for idx, segment in enumerate(segments, start=1):
|
| 59 |
+
|
| 60 |
+
start = format_srt_timestamp(segment.start)
|
| 61 |
+
end = format_srt_timestamp(segment.end)
|
| 62 |
+
|
| 63 |
+
lines.append(
|
| 64 |
+
f"{idx}\n"
|
| 65 |
+
f"{start} --> {end}\n"
|
| 66 |
+
f"{segment.text.strip()}\n"
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
return "\n".join(lines)
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
def generate_vtt(segments):
|
| 73 |
+
lines = ["WEBVTT\n"]
|
| 74 |
|
| 75 |
for segment in segments:
|
|
|
|
| 76 |
|
| 77 |
+
start = format_vtt_timestamp(segment.start)
|
| 78 |
+
end = format_vtt_timestamp(segment.end)
|
| 79 |
+
|
| 80 |
+
lines.append(
|
| 81 |
+
f"{start} --> {end}\n"
|
| 82 |
+
f"{segment.text.strip()}\n"
|
| 83 |
)
|
| 84 |
|
| 85 |
+
return "\n".join(lines)
|
| 86 |
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
+
def process_files(files, mode):
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
+
if not files:
|
| 91 |
+
return "", None
|
| 92 |
+
|
| 93 |
+
tmp_dir = Path(tempfile.mkdtemp())
|
| 94 |
+
|
| 95 |
+
summary_lines = []
|
| 96 |
|
| 97 |
+
zip_path = tmp_dir / "echoscript_results.zip"
|
| 98 |
|
| 99 |
+
with ZipFile(zip_path, "w") as zipf:
|
| 100 |
+
|
| 101 |
+
for uploaded_file in files:
|
| 102 |
+
|
| 103 |
+
audio_path = uploaded_file
|
| 104 |
+
|
| 105 |
+
stem = Path(audio_path).stem
|
| 106 |
+
|
| 107 |
+
task = (
|
| 108 |
+
"transcribe"
|
| 109 |
+
if mode == "Transcribe"
|
| 110 |
+
else "translate"
|
| 111 |
+
)
|
| 112 |
+
|
| 113 |
+
segments_generator, info = model.transcribe(
|
| 114 |
+
audio_path,
|
| 115 |
+
task=task,
|
| 116 |
+
beam_size=5
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
segments = list(segments_generator)
|
| 120 |
+
|
| 121 |
+
transcript = "\n".join(
|
| 122 |
+
segment.text.strip()
|
| 123 |
+
for segment in segments
|
| 124 |
+
)
|
| 125 |
+
|
| 126 |
+
srt_content = generate_srt(segments)
|
| 127 |
+
vtt_content = generate_vtt(segments)
|
| 128 |
+
|
| 129 |
+
language = LANGUAGE_NAMES.get(
|
| 130 |
+
info.language,
|
| 131 |
+
info.language
|
| 132 |
+
)
|
| 133 |
+
|
| 134 |
+
summary_lines.append(
|
| 135 |
+
f"{stem}\n"
|
| 136 |
+
f"Language: {language}\n"
|
| 137 |
+
f"Confidence: {info.language_probability:.2%}\n"
|
| 138 |
+
)
|
| 139 |
+
|
| 140 |
+
txt_file = tmp_dir / f"{stem}.txt"
|
| 141 |
+
srt_file = tmp_dir / f"{stem}.srt"
|
| 142 |
+
vtt_file = tmp_dir / f"{stem}.vtt"
|
| 143 |
+
|
| 144 |
+
txt_file.write_text(
|
| 145 |
+
transcript,
|
| 146 |
+
encoding="utf-8"
|
| 147 |
+
)
|
| 148 |
+
|
| 149 |
+
srt_file.write_text(
|
| 150 |
+
srt_content,
|
| 151 |
+
encoding="utf-8"
|
| 152 |
+
)
|
| 153 |
+
|
| 154 |
+
vtt_file.write_text(
|
| 155 |
+
vtt_content,
|
| 156 |
+
encoding="utf-8"
|
| 157 |
+
)
|
| 158 |
+
|
| 159 |
+
zipf.write(
|
| 160 |
+
txt_file,
|
| 161 |
+
arcname=txt_file.name
|
| 162 |
+
)
|
| 163 |
+
|
| 164 |
+
zipf.write(
|
| 165 |
+
srt_file,
|
| 166 |
+
arcname=srt_file.name
|
| 167 |
+
)
|
| 168 |
+
|
| 169 |
+
zipf.write(
|
| 170 |
+
vtt_file,
|
| 171 |
+
arcname=vtt_file.name
|
| 172 |
+
)
|
| 173 |
|
| 174 |
return (
|
| 175 |
+
"\n\n".join(summary_lines),
|
| 176 |
+
str(zip_path)
|
|
|
|
| 177 |
)
|
| 178 |
|
| 179 |
|
| 180 |
+
with gr.Blocks(
|
| 181 |
+
title="EchoScript"
|
| 182 |
+
) as demo:
|
| 183 |
|
| 184 |
gr.Markdown(
|
| 185 |
"""
|
| 186 |
+
# EchoScript
|
| 187 |
+
|
| 188 |
+
Transcribe audio files using Faster-Whisper.
|
| 189 |
+
|
| 190 |
+
### Features
|
| 191 |
|
| 192 |
+
- Automatic language detection
|
| 193 |
+
- Multi-language transcription
|
| 194 |
+
- Translation to English
|
| 195 |
+
- Batch processing
|
| 196 |
+
- TXT export
|
| 197 |
+
- SRT subtitle export
|
| 198 |
+
- VTT subtitle export
|
| 199 |
"""
|
| 200 |
)
|
| 201 |
|
| 202 |
+
files_input = gr.Files(
|
| 203 |
+
label="Upload Audio Files"
|
|
|
|
| 204 |
)
|
| 205 |
|
| 206 |
+
mode_input = gr.Dropdown(
|
| 207 |
+
choices=[
|
| 208 |
+
"Transcribe",
|
| 209 |
+
"Translate to English"
|
| 210 |
+
],
|
| 211 |
+
value="Transcribe",
|
| 212 |
+
label="Mode"
|
| 213 |
)
|
| 214 |
|
| 215 |
+
run_button = gr.Button(
|
| 216 |
+
"Start Processing"
|
| 217 |
)
|
| 218 |
|
| 219 |
+
summary_output = gr.Textbox(
|
| 220 |
+
label="Results",
|
| 221 |
+
lines=12
|
| 222 |
)
|
| 223 |
|
| 224 |
download_output = gr.File(
|
| 225 |
+
label="Download ZIP"
|
| 226 |
)
|
| 227 |
|
| 228 |
+
run_button.click(
|
| 229 |
+
fn=process_files,
|
| 230 |
+
inputs=[
|
| 231 |
+
files_input,
|
| 232 |
+
mode_input
|
| 233 |
+
],
|
| 234 |
outputs=[
|
| 235 |
+
summary_output,
|
|
|
|
| 236 |
download_output
|
| 237 |
]
|
| 238 |
)
|
| 239 |
|
| 240 |
+
if __name__ == "__main__":
|
| 241 |
+
demo.launch()
|
| 242 |
+
```
|