Spaces:
Sleeping
Sleeping
Murali Chandran commited on
Commit ·
8656a89
1
Parent(s): add0255
fixed AttributeError: 'int' object has no attribute 'ndim'
Browse files- app.py +20 -10
- requirements.txt +2 -0
app.py
CHANGED
|
@@ -5,27 +5,36 @@ 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 |
-
|
|
|
|
| 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
|
| 21 |
-
if isinstance(speech_array,
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
#
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
#
|
|
|
|
| 29 |
pred_ids = model.generate(**inputs)
|
| 30 |
text = processor.batch_decode(pred_ids, skip_special_tokens=True)[0]
|
| 31 |
return text.strip()
|
|
@@ -39,4 +48,5 @@ demo = gr.Interface(
|
|
| 39 |
)
|
| 40 |
|
| 41 |
if __name__ == "__main__":
|
| 42 |
-
|
|
|
|
|
|
| 5 |
|
| 6 |
MODEL_ID = "codeninja3d/whisper-small-ls100"
|
| 7 |
|
| 8 |
+
# Load model + processor
|
| 9 |
processor = WhisperProcessor.from_pretrained(MODEL_ID)
|
| 10 |
model = WhisperForConditionalGeneration.from_pretrained(MODEL_ID)
|
| 11 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 12 |
model = model.to(device)
|
| 13 |
|
| 14 |
def transcribe(audio):
|
| 15 |
+
# Handle no input or bad input
|
| 16 |
+
if not audio or not isinstance(audio, tuple):
|
| 17 |
return "⚠️ Please record or upload an audio clip."
|
| 18 |
|
|
|
|
| 19 |
speech_array, sampling_rate = audio
|
| 20 |
|
| 21 |
+
# Ensure it's a NumPy array of floats
|
| 22 |
+
if not isinstance(speech_array, np.ndarray):
|
| 23 |
+
try:
|
| 24 |
+
speech_array = np.array(speech_array, dtype=np.float32)
|
| 25 |
+
except Exception:
|
| 26 |
+
return "⚠️ Invalid audio input received."
|
| 27 |
|
| 28 |
+
# Convert to mono if stereo
|
| 29 |
+
if speech_array.ndim > 1:
|
| 30 |
+
speech_array = np.mean(speech_array, axis=-1)
|
| 31 |
+
|
| 32 |
+
# Normalize range to [-1, 1]
|
| 33 |
+
if np.abs(speech_array).max() > 1.0:
|
| 34 |
+
speech_array = speech_array / np.abs(speech_array).max()
|
| 35 |
|
| 36 |
+
# Run Whisper
|
| 37 |
+
inputs = processor(speech_array, sampling_rate=sampling_rate, return_tensors="pt").to(device)
|
| 38 |
pred_ids = model.generate(**inputs)
|
| 39 |
text = processor.batch_decode(pred_ids, skip_special_tokens=True)[0]
|
| 40 |
return text.strip()
|
|
|
|
| 48 |
)
|
| 49 |
|
| 50 |
if __name__ == "__main__":
|
| 51 |
+
# On Hugging Face Spaces, no need for share=True
|
| 52 |
+
demo.launch()
|
requirements.txt
CHANGED
|
@@ -2,3 +2,5 @@ torch
|
|
| 2 |
torchaudio
|
| 3 |
transformers>=4.45
|
| 4 |
gradio>=4.0
|
|
|
|
|
|
|
|
|
| 2 |
torchaudio
|
| 3 |
transformers>=4.45
|
| 4 |
gradio>=4.0
|
| 5 |
+
safetensors
|
| 6 |
+
numpy
|