karenlu653 commited on
Commit
1d674fa
·
1 Parent(s): 592b0b5

fixing audio upload

Browse files
Files changed (1) hide show
  1. app.py +18 -13
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 or not isinstance(audio_path, str):
80
  return "No audio provided"
81
 
82
- try:
83
- y, sr = sf.read(audio_path, dtype="float32")
84
- if len(y) == 0:
85
- return "No audio recorded, please try again."
86
-
87
- feats = extract_features(y, sr).to(device)
88
- with torch.no_grad():
89
- logits = model(feats)
90
- pred = int(logits.argmax(dim=1))
91
- return label_map.get(str(pred), str(pred))
92
- except Exception as e:
93
- return f"Error processing audio: {e}"
 
 
 
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 -----------------