Spaces:
Sleeping
Sleeping
Murali Chandran commited on
Commit ·
add0255
1
Parent(s): 51d1560
fixed the ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
Browse files
app.py
CHANGED
|
@@ -1,29 +1,41 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
|
|
|
| 3 |
from transformers import WhisperProcessor, WhisperForConditionalGeneration
|
| 4 |
|
| 5 |
MODEL_ID = "codeninja3d/whisper-small-ls100"
|
| 6 |
|
| 7 |
processor = WhisperProcessor.from_pretrained(MODEL_ID)
|
| 8 |
model = WhisperForConditionalGeneration.from_pretrained(MODEL_ID)
|
| 9 |
-
|
|
|
|
| 10 |
|
| 11 |
def transcribe(audio):
|
| 12 |
-
# audio is (numpy array, sample_rate)
|
| 13 |
if audio is None:
|
| 14 |
-
return "⚠️
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
pred_ids = model.generate(**inputs)
|
| 18 |
text = processor.batch_decode(pred_ids, skip_special_tokens=True)[0]
|
| 19 |
-
return text
|
| 20 |
|
| 21 |
demo = gr.Interface(
|
| 22 |
fn=transcribe,
|
| 23 |
inputs=gr.Audio(sources=["upload", "microphone"], type="numpy"),
|
| 24 |
outputs="text",
|
| 25 |
-
title="Whisper Small LS100 - Fine-tuned English ASR",
|
| 26 |
-
description="
|
| 27 |
)
|
| 28 |
|
| 29 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
+
import numpy as np
|
| 4 |
from transformers import WhisperProcessor, WhisperForConditionalGeneration
|
| 5 |
|
| 6 |
MODEL_ID = "codeninja3d/whisper-small-ls100"
|
| 7 |
|
| 8 |
processor = WhisperProcessor.from_pretrained(MODEL_ID)
|
| 9 |
model = WhisperForConditionalGeneration.from_pretrained(MODEL_ID)
|
| 10 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 11 |
+
model = model.to(device)
|
| 12 |
|
| 13 |
def transcribe(audio):
|
|
|
|
| 14 |
if audio is None:
|
| 15 |
+
return "⚠️ Please record or upload an audio clip."
|
| 16 |
+
|
| 17 |
+
# Gradio gives (np.ndarray, sample_rate)
|
| 18 |
+
speech_array, sampling_rate = audio
|
| 19 |
+
|
| 20 |
+
# Ensure correct format
|
| 21 |
+
if isinstance(speech_array, tuple):
|
| 22 |
+
speech_array = np.array(speech_array[0], dtype=np.float32)
|
| 23 |
+
|
| 24 |
+
# Whisper expects float32 mono audio
|
| 25 |
+
speech_array = np.mean(speech_array, axis=-1) if speech_array.ndim > 1 else speech_array
|
| 26 |
+
inputs = processor(speech_array, sampling_rate=sampling_rate, return_tensors="pt").to(device)
|
| 27 |
+
|
| 28 |
+
# Generate transcription
|
| 29 |
pred_ids = model.generate(**inputs)
|
| 30 |
text = processor.batch_decode(pred_ids, skip_special_tokens=True)[0]
|
| 31 |
+
return text.strip()
|
| 32 |
|
| 33 |
demo = gr.Interface(
|
| 34 |
fn=transcribe,
|
| 35 |
inputs=gr.Audio(sources=["upload", "microphone"], type="numpy"),
|
| 36 |
outputs="text",
|
| 37 |
+
title="🎙 Whisper Small LS100 - Fine-tuned English ASR",
|
| 38 |
+
description="Upload or record an English audio sample (16 kHz recommended) to transcribe it using your fine-tuned Whisper model."
|
| 39 |
)
|
| 40 |
|
| 41 |
if __name__ == "__main__":
|