Spaces:
Sleeping
Sleeping
Commit ·
1d674fa
1
Parent(s): 592b0b5
fixing audio upload
Browse files
app.py
CHANGED
|
@@ -75,22 +75,27 @@ def extract_features(y, sr):
|
|
| 75 |
|
| 76 |
# ----------------- Prediction function -----------------
|
| 77 |
|
|
|
|
|
|
|
| 78 |
def predict(audio_path):
|
| 79 |
-
if not audio_path
|
| 80 |
return "No audio provided"
|
| 81 |
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
|
| 96 |
# ----------------- Gradio Interface -----------------
|
|
|
|
| 75 |
|
| 76 |
# ----------------- Prediction function -----------------
|
| 77 |
|
| 78 |
+
import tempfile, shutil
|
| 79 |
+
|
| 80 |
def predict(audio_path):
|
| 81 |
+
if not audio_path:
|
| 82 |
return "No audio provided"
|
| 83 |
|
| 84 |
+
# Copy to a safe temp file
|
| 85 |
+
tmp_path = tempfile.mktemp(suffix=".wav")
|
| 86 |
+
shutil.copy(audio_path, tmp_path)
|
| 87 |
+
|
| 88 |
+
import soundfile as sf
|
| 89 |
+
y, sr = sf.read(tmp_path, dtype="float32")
|
| 90 |
+
if len(y) == 0:
|
| 91 |
+
return "No audio detected, please try again."
|
| 92 |
+
|
| 93 |
+
feats = extract_features(y, sr).to(device)
|
| 94 |
+
with torch.no_grad():
|
| 95 |
+
logits = model(feats)
|
| 96 |
+
pred = int(logits.argmax(dim=1))
|
| 97 |
+
|
| 98 |
+
return label_map.get(str(pred), str(pred))
|
| 99 |
|
| 100 |
|
| 101 |
# ----------------- Gradio Interface -----------------
|