Spaces:
Sleeping
Sleeping
usertea commited on
Commit ·
8ae702a
1
Parent(s): cc4eaac
EchoScript : Add application file
Browse files
app.py
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import tempfile
|
| 2 |
+
from pathlib import Path
|
| 3 |
+
|
| 4 |
+
import gradio as gr
|
| 5 |
+
from faster_whisper import WhisperModel
|
| 6 |
+
|
| 7 |
+
MODEL_SIZE = "base"
|
| 8 |
+
|
| 9 |
+
model = WhisperModel(
|
| 10 |
+
MODEL_SIZE,
|
| 11 |
+
device="cpu",
|
| 12 |
+
compute_type="int8"
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
LANGUAGE_NAMES = {
|
| 16 |
+
"fr": "French",
|
| 17 |
+
"en": "English",
|
| 18 |
+
"de": "German",
|
| 19 |
+
"fa": "Persian",
|
| 20 |
+
"es": "Spanish",
|
| 21 |
+
"it": "Italian",
|
| 22 |
+
"pt": "Portuguese",
|
| 23 |
+
"nl": "Dutch"
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def transcribe(audio_file):
|
| 28 |
+
|
| 29 |
+
if audio_file is None:
|
| 30 |
+
return "", "", None
|
| 31 |
+
|
| 32 |
+
segments, info = model.transcribe(
|
| 33 |
+
audio_file,
|
| 34 |
+
beam_size=5
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
transcript_lines = []
|
| 38 |
+
timestamp_lines = []
|
| 39 |
+
|
| 40 |
+
for segment in segments:
|
| 41 |
+
transcript_lines.append(segment.text)
|
| 42 |
+
egment.end:.2f}s] "
|
| 43 |
+
f"{segment.text}"
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
transcript = "\n".join(transcript_lines)
|
| 47 |
+
|
| 48 |
+
transcript_with_timestamps = "\n".join(
|
| 49 |
+
timestamp_lines
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
detected_language = LANGUAGE_NAMES.get(
|
| 53 |
+
info.language,
|
| 54 |
+
info.language
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
summary = (
|
| 58 |
+
f"Detected language: {detected_language}\n"
|
| 59 |
+
f"Confidence: {info.language_probability:.2%}"
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
output_file = Path(tempfile.gettempdir()) / "transcript.txt"
|
| 63 |
+
|
| 64 |
+
with open(
|
| 65 |
+
output_file,
|
| 66 |
+
"w",
|
| 67 |
+
encoding="utf-8"
|
| 68 |
+
) as f:
|
| 69 |
+
f.write(transcript_with_timestamps)
|
| 70 |
+
|
| 71 |
+
return (
|
| 72 |
+
summary,
|
| 73 |
+
transcript,
|
| 74 |
+
str(output_file)
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
with gr.Blocks(title="EchoScript") as demo:
|
| 79 |
+
|
| 80 |
+
gr.Markdown(
|
| 81 |
+
"""
|
| 82 |
+
# EchoScript
|
| 83 |
+
|
| 84 |
+
Upload an audio file and automatically
|
| 85 |
+
transcribe speech to text.
|
| 86 |
+
"""
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
audio_input = gr.Audio(
|
| 90 |
+
type="filepath",
|
| 91 |
+
label="Upload Audio"
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
transcribe_button = gr.Button(
|
| 95 |
+
"Transcribe"
|
| 96 |
+
)
|
| 97 |
+
|
| 98 |
+
language_output = gr.Textbox(
|
| 99 |
+
label="Language Information"
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
transcript_output = gr.Textbox(
|
| 103 |
+
label="Transcript",
|
| 104 |
+
lines=20
|
| 105 |
+
)
|
| 106 |
+
|
| 107 |
+
download_output = gr.File(
|
| 108 |
+
label="Download Transcript"
|
| 109 |
+
)
|
| 110 |
+
|
| 111 |
+
transcribe_button.click(
|
| 112 |
+
fn=transcribe,
|
| 113 |
+
inputs=audio_input,
|
| 114 |
+
outputs=[
|
| 115 |
+
language_output,
|
| 116 |
+
transcript_output,
|
| 117 |
+
download_output
|
| 118 |
+
]
|
| 119 |
+
)
|
| 120 |
+
|
| 121 |
+
demo.launch()
|
| 122 |
+
timestamp_lines.append(
|
| 123 |
+
f"[{segment.start:.2f}s �
|