Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,7 @@ import numpy as np
|
|
| 3 |
import torch
|
| 4 |
from transformers import pipeline, AutoModelForSpeechSeq2Seq, AutoProcessor
|
| 5 |
|
|
|
|
| 6 |
model_id = 'openai/whisper-large-v3'
|
| 7 |
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
| 8 |
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
|
|
@@ -11,19 +12,24 @@ processor = AutoProcessor.from_pretrained(model_id)
|
|
| 11 |
|
| 12 |
pipe_asr = pipeline("automatic-speech-recognition", model=model, tokenizer=processor.tokenizer, feature_extractor=processor.feature_extractor, max_new_tokens=128, chunk_length_s=15, batch_size=16, torch_dtype=torch_dtype, device=device, return_timestamps=True)
|
| 13 |
|
| 14 |
-
base_audio_drive = "/data/audio"
|
| 15 |
-
|
| 16 |
def transcribe_function(audio):
|
| 17 |
-
sr, y = audio
|
| 18 |
y = y.astype(np.float32) / np.max(np.abs(y))
|
| 19 |
result = pipe_asr({"array": y, "sampling_rate": sr}, return_timestamps=False)
|
| 20 |
full_text = result.get("text", "")
|
| 21 |
return full_text
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
|
| 28 |
|
| 29 |
-
|
|
|
|
| 3 |
import torch
|
| 4 |
from transformers import pipeline, AutoModelForSpeechSeq2Seq, AutoProcessor
|
| 5 |
|
| 6 |
+
# Initialize the model and processor
|
| 7 |
model_id = 'openai/whisper-large-v3'
|
| 8 |
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
| 9 |
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
|
|
|
|
| 12 |
|
| 13 |
pipe_asr = pipeline("automatic-speech-recognition", model=model, tokenizer=processor.tokenizer, feature_extractor=processor.feature_extractor, max_new_tokens=128, chunk_length_s=15, batch_size=16, torch_dtype=torch_dtype, device=device, return_timestamps=True)
|
| 14 |
|
|
|
|
|
|
|
| 15 |
def transcribe_function(audio):
|
| 16 |
+
sr, y = audio
|
| 17 |
y = y.astype(np.float32) / np.max(np.abs(y))
|
| 18 |
result = pipe_asr({"array": y, "sampling_rate": sr}, return_timestamps=False)
|
| 19 |
full_text = result.get("text", "")
|
| 20 |
return full_text
|
| 21 |
|
| 22 |
+
with gr.Blocks() as demo:
|
| 23 |
+
gr.Markdown("# Voice to Text Transcription\nTranscribe your voice input to text using a pre-trained Whisper model.")
|
| 24 |
+
|
| 25 |
+
with gr.Row():
|
| 26 |
+
with gr.Column():
|
| 27 |
+
audio_input = gr.Audio(source="microphone", type="numpy", label="Speak Here")
|
| 28 |
+
transcribe_button = gr.Button("Transcribe")
|
| 29 |
+
|
| 30 |
+
with gr.Column():
|
| 31 |
+
output_text = gr.Textbox(label="Transcription")
|
| 32 |
|
| 33 |
+
transcribe_button.click(transcribe_function, inputs=audio_input, outputs=output_text)
|
| 34 |
|
| 35 |
+
demo.launch()
|