Spaces:
Runtime error
Runtime error
carlfeynman
commited on
Commit
•
b58c329
1
Parent(s):
00a87e1
Update app.py
Browse files
app.py
CHANGED
@@ -1,101 +1,45 @@
|
|
1 |
-
import torch
|
2 |
from transformers import pipeline
|
3 |
-
from transformers.pipelines.audio_utils import ffmpeg_read
|
4 |
-
import gradio as gr
|
5 |
-
|
6 |
-
MODEL_NAME = "openai/whisper-small"
|
7 |
-
BATCH_SIZE = 8
|
8 |
-
|
9 |
-
device = 0 if torch.cuda.is_available() else "cpu"
|
10 |
-
|
11 |
-
pipe = pipeline(
|
12 |
-
task="automatic-speech-recognition",
|
13 |
-
model=MODEL_NAME,
|
14 |
-
chunk_length_s=30,
|
15 |
-
device=device,
|
16 |
-
)
|
17 |
|
|
|
18 |
|
19 |
-
|
20 |
-
def format_timestamp(seconds: float, always_include_hours: bool = False, decimal_marker: str = "."):
|
21 |
-
if seconds is not None:
|
22 |
-
milliseconds = round(seconds * 1000.0)
|
23 |
|
24 |
-
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
-
minutes = milliseconds // 60_000
|
28 |
-
milliseconds -= minutes * 60_000
|
29 |
|
30 |
-
|
31 |
-
milliseconds -= seconds * 1_000
|
32 |
-
|
33 |
-
hours_marker = f"{hours:02d}:" if always_include_hours or hours > 0 else ""
|
34 |
-
return f"{hours_marker}{minutes:02d}:{seconds:02d}{decimal_marker}{milliseconds:03d}"
|
35 |
-
else:
|
36 |
-
# we have a malformed timestamp so just return it as is
|
37 |
-
return seconds
|
38 |
-
|
39 |
-
|
40 |
-
def transcribe(file, task, return_timestamps):
|
41 |
-
outputs = pipe(file, batch_size=BATCH_SIZE, generate_kwargs={"task": task}, return_timestamps=return_timestamps)
|
42 |
-
text = outputs["text"]
|
43 |
-
if return_timestamps:
|
44 |
-
timestamps = outputs["chunks"]
|
45 |
-
timestamps = [
|
46 |
-
f"[{format_timestamp(chunk['timestamp'][0])} -> {format_timestamp(chunk['timestamp'][1])}] {chunk['text']}"
|
47 |
-
for chunk in timestamps
|
48 |
-
]
|
49 |
-
text = "\n".join(str(feature) for feature in timestamps)
|
50 |
-
return text
|
51 |
|
52 |
|
53 |
demo = gr.Blocks()
|
54 |
|
55 |
mic_transcribe = gr.Interface(
|
56 |
-
fn=
|
57 |
-
inputs=
|
58 |
-
|
59 |
-
gr.inputs.Radio(["transcribe", "translate"], label="Task", default="transcribe"),
|
60 |
-
gr.inputs.Checkbox(default=False, label="Return timestamps"),
|
61 |
-
],
|
62 |
-
outputs="text",
|
63 |
-
layout="horizontal",
|
64 |
-
theme="huggingface",
|
65 |
-
title="Whisper Demo: Transcribe Audio",
|
66 |
-
description=(
|
67 |
-
"Transcribe long-form microphone or audio inputs with the click of a button! Demo uses the"
|
68 |
-
f" checkpoint [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and 🤗 Transformers to transcribe audio files"
|
69 |
-
" of arbitrary length."
|
70 |
-
),
|
71 |
-
allow_flagging="never",
|
72 |
)
|
73 |
|
74 |
-
file_transcribe =
|
75 |
-
fn=
|
76 |
-
inputs=
|
77 |
-
|
78 |
-
gr.inputs.Radio(["transcribe", "translate"], label="Task", default="transcribe"),
|
79 |
-
gr.inputs.Checkbox(default=False, label="Return timestamps"),
|
80 |
-
],
|
81 |
-
outputs="text",
|
82 |
-
layout="horizontal",
|
83 |
-
theme="huggingface",
|
84 |
-
title="Whisper Demo: Transcribe Audio",
|
85 |
-
description=(
|
86 |
-
"Transcribe long-form microphone or audio inputs with the click of a button! Demo uses the"
|
87 |
-
f" checkpoint [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and 🤗 Transformers to transcribe audio files"
|
88 |
-
" of arbitrary length."
|
89 |
-
),
|
90 |
-
examples=[
|
91 |
-
["./example.flac", "transcribe", False],
|
92 |
-
["./example.flac", "transcribe", True],
|
93 |
-
],
|
94 |
-
cache_examples=True,
|
95 |
-
allow_flagging="never",
|
96 |
)
|
97 |
|
98 |
with demo:
|
99 |
-
|
|
|
|
|
|
|
|
|
|
|
100 |
|
101 |
-
demo.launch(enable_queue=True)
|
|
|
|
|
1 |
from transformers import pipeline
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
+
model_id = 'carlfeynman/whisper-small-tamil'
|
4 |
|
5 |
+
pipe = pipeline('automatic-speech-recognition', model=model_id)
|
|
|
|
|
|
|
6 |
|
7 |
+
def transcribe_speech(filepath):
|
8 |
+
pred = pipe(
|
9 |
+
filepath,
|
10 |
+
max_new_tokens=256,
|
11 |
+
generate_kwargs={
|
12 |
+
"task": "transcribe",
|
13 |
+
"language": "tamil",
|
14 |
+
},
|
15 |
+
chunk_length_s=30,
|
16 |
+
batch_size=8,
|
17 |
+
)
|
18 |
+
return pred['text']
|
19 |
|
|
|
|
|
20 |
|
21 |
+
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
|
24 |
demo = gr.Blocks()
|
25 |
|
26 |
mic_transcribe = gr.Interface(
|
27 |
+
fn=transcribe_speech,
|
28 |
+
inputs=gr.Audio(source='microphone',type='filepath'),
|
29 |
+
outputs="textbox"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
)
|
31 |
|
32 |
+
file_transcribe = gr.Interface(
|
33 |
+
fn=transcribe_speech,
|
34 |
+
inputs=gr.Audio(source='upload', type='filepath'),
|
35 |
+
outputs="textbox"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
)
|
37 |
|
38 |
with demo:
|
39 |
+
gr.TabbedInterface(
|
40 |
+
[mic_transcribe, file_transcribe],
|
41 |
+
["Transcribe Microphone", "Transcribe Audio File"],
|
42 |
+
)
|
43 |
+
|
44 |
+
demo.launch()
|
45 |
|
|