VinayHajare
commited on
Commit
·
bce3574
1
Parent(s):
2b813f1
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,127 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from transformers import pipeline
|
3 |
+
from transformers.pipelines.audio_utils import ffmpeg_read
|
4 |
+
import gradio as gr
|
5 |
+
import pytube as pt
|
6 |
+
|
7 |
+
MODEL_NAME = "VinayHajare/whisper-small-finetuned-common-voice-mr"
|
8 |
+
BATCH_SIZE = 8
|
9 |
+
LANG = "mr"
|
10 |
+
device = 0 if torch.cuda.is_available() else "cpu"
|
11 |
+
|
12 |
+
pipe = pipeline(
|
13 |
+
task="automatic-speech-recognition",
|
14 |
+
model=MODEL_NAME,
|
15 |
+
chunk_length_s=30,
|
16 |
+
device=device,
|
17 |
+
)
|
18 |
+
|
19 |
+
# pipe.model.config.forced_decoder_ids = pipe.tokenizer.get_decoder_prompt_ids(language=lang)
|
20 |
+
|
21 |
+
# Copied from https://github.com/openai/whisper/blob/c09a7ae299c4c34c5839a76380ae407e7d785914/whisper/utils.py#L50
|
22 |
+
def format_timestamp(seconds: float, always_include_hours: bool = False, decimal_marker: str = "."):
|
23 |
+
if seconds is not None:
|
24 |
+
milliseconds = round(seconds * 1000.0)
|
25 |
+
|
26 |
+
hours = milliseconds // 3_600_000
|
27 |
+
milliseconds -= hours * 3_600_000
|
28 |
+
|
29 |
+
minutes = milliseconds // 60_000
|
30 |
+
milliseconds -= minutes * 60_000
|
31 |
+
|
32 |
+
seconds = milliseconds // 1_000
|
33 |
+
milliseconds -= seconds * 1_000
|
34 |
+
|
35 |
+
hours_marker = f"{hours:02d}:" if always_include_hours or hours > 0 else ""
|
36 |
+
return f"{hours_marker}{minutes:02d}:{seconds:02d}{decimal_marker}{milliseconds:03d}"
|
37 |
+
else:
|
38 |
+
# we have a malformed timestamp so just return it as is
|
39 |
+
return seconds
|
40 |
+
|
41 |
+
|
42 |
+
def transcribe(file, task, return_timestamps):
|
43 |
+
outputs = pipe(file, batch_size=BATCH_SIZE, generate_kwargs={"task": task}, return_timestamps=return_timestamps)
|
44 |
+
text = outputs["text"]
|
45 |
+
if return_timestamps:
|
46 |
+
timestamps = outputs["chunks"]
|
47 |
+
timestamps = [
|
48 |
+
f"[{format_timestamp(chunk['timestamp'][0])} -> {format_timestamp(chunk['timestamp'][1])}] {chunk['text']}"
|
49 |
+
for chunk in timestamps
|
50 |
+
]
|
51 |
+
text = "\n".join(str(feature) for feature in timestamps)
|
52 |
+
return text
|
53 |
+
|
54 |
+
def yt_transcribe(yt_url):
|
55 |
+
yt = pt.YouTube(yt_url)
|
56 |
+
html_embed_str = _return_yt_html_embed(yt_url)
|
57 |
+
stream = yt.streams.filter(only_audio=True)[0]
|
58 |
+
stream.download(filename="audio.mp3")
|
59 |
+
|
60 |
+
text = pipe("audio.mp3")["text"]
|
61 |
+
|
62 |
+
return html_embed_str, text
|
63 |
+
|
64 |
+
demo = gr.Blocks()
|
65 |
+
|
66 |
+
mic_transcribe = gr.Interface(
|
67 |
+
fn=transcribe,
|
68 |
+
inputs=[
|
69 |
+
gr.inputs.Audio(source="microphone", type="filepath", optional=True),
|
70 |
+
gr.inputs.Radio(["transcribe", "translate"], label="Task", default="transcribe"),
|
71 |
+
gr.inputs.Checkbox(default=False, label="Return timestamps"),
|
72 |
+
],
|
73 |
+
outputs="text",
|
74 |
+
layout="horizontal",
|
75 |
+
theme="huggingface",
|
76 |
+
title="Whisper Demo: Transcribe Marathi Audio",
|
77 |
+
description=(
|
78 |
+
"Transcribe long-form microphone or audio inputs with the click of a button! Demo uses the"
|
79 |
+
f" checkpoint [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and 🤗 Transformers to transcribe audio files"
|
80 |
+
" of arbitrary length."
|
81 |
+
),
|
82 |
+
allow_flagging="never",
|
83 |
+
)
|
84 |
+
|
85 |
+
file_transcribe = gr.Interface(
|
86 |
+
fn=transcribe,
|
87 |
+
inputs=[
|
88 |
+
gr.inputs.Audio(source="upload", optional=True, label="Audio file", type="filepath"),
|
89 |
+
gr.inputs.Radio(["transcribe", "translate"], label="Task", default="transcribe"),
|
90 |
+
gr.inputs.Checkbox(default=False, label="Return timestamps"),
|
91 |
+
],
|
92 |
+
outputs="text",
|
93 |
+
layout="horizontal",
|
94 |
+
theme="huggingface",
|
95 |
+
title="Whisper Demo: Transcribe Marathi Audio",
|
96 |
+
description=(
|
97 |
+
"Transcribe long-form microphone or audio inputs with the click of a button! Demo uses the"
|
98 |
+
f" checkpoint [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and 🤗 Transformers to transcribe audio files"
|
99 |
+
" of arbitrary length."
|
100 |
+
),
|
101 |
+
examples=[
|
102 |
+
["./example.flac", "transcribe", False],
|
103 |
+
["./example.flac", "transcribe", True],
|
104 |
+
],
|
105 |
+
cache_examples=True,
|
106 |
+
allow_flagging="never",
|
107 |
+
)
|
108 |
+
|
109 |
+
yt_transcribe = gr.Interface(
|
110 |
+
fn=yt_transcribe,
|
111 |
+
inputs=[gr.inputs.Textbox(lines=1, placeholder="Paste the URL to a YouTube video here", label="YouTube URL")],
|
112 |
+
outputs=["html", "text"],
|
113 |
+
layout="horizontal",
|
114 |
+
theme="huggingface",
|
115 |
+
title="Whisper Demo: Transcribe Marathi YouTube Video",
|
116 |
+
description=(
|
117 |
+
"Transcribe long-form YouTube videos with the click of a button! Demo uses the the fine-tuned checkpoint:"
|
118 |
+
f" [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and 🤗 Transformers to transcribe audio files of"
|
119 |
+
" arbitrary length."
|
120 |
+
),
|
121 |
+
allow_flagging="never",
|
122 |
+
)
|
123 |
+
|
124 |
+
with demo:
|
125 |
+
gr.TabbedInterface([mic_transcribe, file_transcribe,yt_transcribe], ["Transcribe Microphone", "Transcribe Audio File", "Transcribe YouTube Video"])
|
126 |
+
|
127 |
+
demo.launch(enable_queue=True)
|