dawnrennlabs commited on
Commit ·
d6abc0b
1
Parent(s): e4f67ec
chore: update
Browse files
app.py
CHANGED
|
@@ -1,8 +1,39 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import pipeline
|
| 3 |
import numpy as np
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
def transcribe_stream(stream, new_chunk, language):
|
| 8 |
sr, y = new_chunk
|
|
@@ -19,31 +50,13 @@ def transcribe_stream(stream, new_chunk, language):
|
|
| 19 |
else:
|
| 20 |
stream = y
|
| 21 |
|
| 22 |
-
# Limit input length to 30 seconds
|
| 23 |
max_length = min(len(stream), int(30 * sr))
|
| 24 |
stream = stream[:max_length]
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
def transcribe_file(audio, language):
|
| 30 |
-
if audio is None:
|
| 31 |
-
return ""
|
| 32 |
-
sr, y = audio
|
| 33 |
-
|
| 34 |
-
# Convert to mono if stereo
|
| 35 |
-
if y.ndim > 1:
|
| 36 |
-
y = y.mean(axis=1)
|
| 37 |
-
|
| 38 |
-
y = y.astype(np.float32)
|
| 39 |
-
y /= np.max(np.abs(y))
|
| 40 |
-
|
| 41 |
-
# Limit input length to 30 seconds (3000 mel input features)
|
| 42 |
-
max_length = min(len(y), int(30 * sr))
|
| 43 |
-
y = y[:max_length]
|
| 44 |
-
|
| 45 |
-
result = transcriber({"sampling_rate": sr, "raw": y}, return_timestamps=True, language=language)
|
| 46 |
-
return result["text"]
|
| 47 |
|
| 48 |
with gr.Blocks() as demo:
|
| 49 |
gr.Markdown("# Audio Transcription Demo (Vietnamese and English)")
|
|
@@ -60,7 +73,7 @@ with gr.Blocks() as demo:
|
|
| 60 |
audio_file = gr.Audio(type="numpy", label="Upload Audio")
|
| 61 |
text_output_file = gr.Textbox(label="Transcription")
|
| 62 |
transcribe_btn = gr.Button("Transcribe")
|
| 63 |
-
transcribe_btn.click(
|
| 64 |
|
| 65 |
gr.Markdown("Note: Audio input is limited to 30 seconds for transcription. Select 'vi' for Vietnamese or 'en' for English.")
|
| 66 |
gr.Markdown("Please note that using the medium model may increase processing time.")
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
+
import torch
|
| 4 |
+
from transformers import WhisperProcessor, WhisperForConditionalGeneration
|
| 5 |
|
| 6 |
+
# Initialize the Whisper model and processor
|
| 7 |
+
model_name = "openai/whisper-medium"
|
| 8 |
+
processor = WhisperProcessor.from_pretrained(model_name)
|
| 9 |
+
model = WhisperForConditionalGeneration.from_pretrained(model_name)
|
| 10 |
+
|
| 11 |
+
def transcribe_audio(audio, language):
|
| 12 |
+
if audio is None:
|
| 13 |
+
return ""
|
| 14 |
+
sr, y = audio
|
| 15 |
+
|
| 16 |
+
# Convert to mono if stereo
|
| 17 |
+
if y.ndim > 1:
|
| 18 |
+
y = y.mean(axis=1)
|
| 19 |
+
|
| 20 |
+
y = y.astype(np.float32)
|
| 21 |
+
y /= np.max(np.abs(y))
|
| 22 |
+
|
| 23 |
+
# Limit input length to 30 seconds
|
| 24 |
+
max_length = min(len(y), int(30 * sr))
|
| 25 |
+
y = y[:max_length]
|
| 26 |
+
|
| 27 |
+
# Process the audio
|
| 28 |
+
input_features = processor(y, sampling_rate=sr, return_tensors="pt").input_features
|
| 29 |
+
|
| 30 |
+
# Generate tokens using the model
|
| 31 |
+
forced_decoder_ids = processor.get_decoder_prompt_ids(language=language, task="transcribe")
|
| 32 |
+
generated_ids = model.generate(input_features, forced_decoder_ids=forced_decoder_ids)
|
| 33 |
+
|
| 34 |
+
# Decode the tokens to text
|
| 35 |
+
transcription = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 36 |
+
return transcription
|
| 37 |
|
| 38 |
def transcribe_stream(stream, new_chunk, language):
|
| 39 |
sr, y = new_chunk
|
|
|
|
| 50 |
else:
|
| 51 |
stream = y
|
| 52 |
|
| 53 |
+
# Limit input length to 30 seconds
|
| 54 |
max_length = min(len(stream), int(30 * sr))
|
| 55 |
stream = stream[:max_length]
|
| 56 |
|
| 57 |
+
# Transcribe the current stream
|
| 58 |
+
transcription = transcribe_audio((sr, stream), language)
|
| 59 |
+
return stream, transcription
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
with gr.Blocks() as demo:
|
| 62 |
gr.Markdown("# Audio Transcription Demo (Vietnamese and English)")
|
|
|
|
| 73 |
audio_file = gr.Audio(type="numpy", label="Upload Audio")
|
| 74 |
text_output_file = gr.Textbox(label="Transcription")
|
| 75 |
transcribe_btn = gr.Button("Transcribe")
|
| 76 |
+
transcribe_btn.click(transcribe_audio, inputs=[audio_file, language], outputs=[text_output_file])
|
| 77 |
|
| 78 |
gr.Markdown("Note: Audio input is limited to 30 seconds for transcription. Select 'vi' for Vietnamese or 'en' for English.")
|
| 79 |
gr.Markdown("Please note that using the medium model may increase processing time.")
|