Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -15,19 +15,24 @@ model = Wav2Vec2ForCTC.from_pretrained("jonatasgrosman/wav2vec2-large-xlsr-53-it
|
|
15 |
# Function to perform ASR on audio data
|
16 |
def transcribe_audio(audio_data):
|
17 |
print("Received audio data:", audio_data) # Debug print
|
18 |
-
|
|
|
|
|
19 |
return "Invalid audio data format."
|
20 |
|
21 |
-
|
22 |
-
# Extract sample rate and audio waveform from the tuple
|
23 |
-
sample_rate, waveform = audio_data
|
24 |
|
|
|
|
|
|
|
|
|
|
|
25 |
# Convert audio data to mono and normalize
|
26 |
audio_data = torchaudio.transforms.Resample(sample_rate, 16000)(waveform)
|
27 |
audio_data = torchaudio.functional.gain(audio_data, gain_db=5.0)
|
28 |
|
29 |
# Apply custom preprocessing to the audio data if needed
|
30 |
-
input_values = processor(
|
31 |
|
32 |
# Perform ASR
|
33 |
with torch.no_grad():
|
|
|
15 |
# Function to perform ASR on audio data
|
16 |
def transcribe_audio(audio_data):
|
17 |
print("Received audio data:", audio_data) # Debug print
|
18 |
+
|
19 |
+
# Check if audio_data is None or not a tuple of length 2
|
20 |
+
if audio_data is None or not isinstance(audio_data, tuple) or len(audio_data) != 2:
|
21 |
return "Invalid audio data format."
|
22 |
|
23 |
+
sample_rate, waveform = audio_data
|
|
|
|
|
24 |
|
25 |
+
# Check if waveform is None or not a NumPy array
|
26 |
+
if waveform is None or not isinstance(waveform, torch.Tensor):
|
27 |
+
return "Invalid audio data format."
|
28 |
+
|
29 |
+
try:
|
30 |
# Convert audio data to mono and normalize
|
31 |
audio_data = torchaudio.transforms.Resample(sample_rate, 16000)(waveform)
|
32 |
audio_data = torchaudio.functional.gain(audio_data, gain_db=5.0)
|
33 |
|
34 |
# Apply custom preprocessing to the audio data if needed
|
35 |
+
input_values = processor(audio_data[0], return_tensors="pt").input_values
|
36 |
|
37 |
# Perform ASR
|
38 |
with torch.no_grad():
|