Spaces:
Sleeping
Sleeping
Fix audio input parsing for browser/client variants
Browse files
app.py
CHANGED
|
@@ -2,16 +2,17 @@ import numpy as np
|
|
| 2 |
import torch
|
| 3 |
import gradio as gr
|
| 4 |
from transformers import WhisperForConditionalGeneration, WhisperProcessor
|
|
|
|
| 5 |
|
| 6 |
MODEL_ID = "openai/whisper-small"
|
|
|
|
| 7 |
|
| 8 |
processor = WhisperProcessor.from_pretrained(MODEL_ID)
|
| 9 |
model = WhisperForConditionalGeneration.from_pretrained(MODEL_ID)
|
| 10 |
model.eval()
|
| 11 |
|
| 12 |
|
| 13 |
-
def
|
| 14 |
-
sample_rate, data = audio
|
| 15 |
if data.ndim > 1:
|
| 16 |
data = data.mean(axis=1)
|
| 17 |
|
|
@@ -24,14 +25,35 @@ def _to_float32_audio(audio: tuple[int, np.ndarray]) -> tuple[int, np.ndarray]:
|
|
| 24 |
if peak > 1.0:
|
| 25 |
data = data / peak
|
| 26 |
|
| 27 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
|
| 30 |
def transcribe_audio(audio, task):
|
| 31 |
if audio is None:
|
| 32 |
return "Please upload or record audio first."
|
| 33 |
|
| 34 |
-
sample_rate, data =
|
| 35 |
|
| 36 |
inputs = processor(
|
| 37 |
data,
|
|
|
|
| 2 |
import torch
|
| 3 |
import gradio as gr
|
| 4 |
from transformers import WhisperForConditionalGeneration, WhisperProcessor
|
| 5 |
+
from transformers.pipelines.audio_utils import ffmpeg_read
|
| 6 |
|
| 7 |
MODEL_ID = "openai/whisper-small"
|
| 8 |
+
TARGET_SAMPLE_RATE = 16000
|
| 9 |
|
| 10 |
processor = WhisperProcessor.from_pretrained(MODEL_ID)
|
| 11 |
model = WhisperForConditionalGeneration.from_pretrained(MODEL_ID)
|
| 12 |
model.eval()
|
| 13 |
|
| 14 |
|
| 15 |
+
def _normalize_waveform(data: np.ndarray) -> np.ndarray:
|
|
|
|
| 16 |
if data.ndim > 1:
|
| 17 |
data = data.mean(axis=1)
|
| 18 |
|
|
|
|
| 25 |
if peak > 1.0:
|
| 26 |
data = data / peak
|
| 27 |
|
| 28 |
+
return data
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def _extract_audio(audio_input):
|
| 32 |
+
# Gradio may send audio as (sample_rate, np.ndarray), a filepath string, or a FileData-like dict.
|
| 33 |
+
if isinstance(audio_input, tuple) and len(audio_input) == 2:
|
| 34 |
+
sample_rate, data = audio_input
|
| 35 |
+
return int(sample_rate), _normalize_waveform(data)
|
| 36 |
+
|
| 37 |
+
path = None
|
| 38 |
+
if isinstance(audio_input, str):
|
| 39 |
+
path = audio_input
|
| 40 |
+
elif isinstance(audio_input, dict):
|
| 41 |
+
path = audio_input.get("path")
|
| 42 |
+
|
| 43 |
+
if path:
|
| 44 |
+
with open(path, "rb") as f:
|
| 45 |
+
audio_bytes = f.read()
|
| 46 |
+
data = ffmpeg_read(audio_bytes, TARGET_SAMPLE_RATE)
|
| 47 |
+
return TARGET_SAMPLE_RATE, _normalize_waveform(data)
|
| 48 |
+
|
| 49 |
+
raise ValueError("Unsupported audio input format. Please upload a valid audio file.")
|
| 50 |
|
| 51 |
|
| 52 |
def transcribe_audio(audio, task):
|
| 53 |
if audio is None:
|
| 54 |
return "Please upload or record audio first."
|
| 55 |
|
| 56 |
+
sample_rate, data = _extract_audio(audio)
|
| 57 |
|
| 58 |
inputs = processor(
|
| 59 |
data,
|