MeysamSh commited on
Commit
4472515
·
verified ·
1 Parent(s): 20f2542

Update Web/script.js

Browse files
Files changed (1) hide show
  1. Web/script.js +8 -18
Web/script.js CHANGED
@@ -182,34 +182,24 @@ async function analyzeAudio(blob) {
182
 
183
  responseBox.innerHTML = "<em>Analyzing...</em>";
184
 
185
- // 1. Prepare the file
186
- // const file = new File([blob], blob.name || "audio.wav", { type: blob.type });
 
187
  const formData = new FormData();
188
- // const filename = 'recorded-audio.wav'; // Nom du fichier
189
- formData.append('files', blob, "audio.wav"); // Utiliser 'files' comme nom de champ
190
 
191
  try {
192
- // 3. CRITICAL FIX: Use relative path AND add the trailing slash
193
- // Your app.py defines: @app.post("/predict/")
194
- const res = await fetch("/predict/", {
195
  method: "POST",
196
  body: formData,
197
  });
198
 
199
- if (!res.ok) {
200
- // Read the error message from the server if available
201
- const errorText = await res.text();
202
- throw new Error(`Server Error ${res.status}: ${errorText}`);
203
- }
204
-
205
  const data = await res.json();
206
- responseBox.innerHTML = `<pre>${JSON.stringify(data, null, 2)}</pre>`;
207
 
 
208
  } catch (err) {
 
209
  console.error(err);
210
- responseBox.innerHTML = `<div class="alert alert-danger">
211
- <strong>Analysis failed.</strong><br/>
212
- <small>${err.message}</small>
213
- </div>`;
214
  }
215
  }
 
182
 
183
  responseBox.innerHTML = "<em>Analyzing...</em>";
184
 
185
+ // Convert Blob to File
186
+ const file = new File([blob], blob.name || "audio.wav", { type: blob.type });
187
+
188
  const formData = new FormData();
189
+ formData.append("file", file);
 
190
 
191
  try {
192
+ // Replace the URL with your real backend endpoint
193
+ const res = await fetch("/predict", {
 
194
  method: "POST",
195
  body: formData,
196
  });
197
 
 
 
 
 
 
 
198
  const data = await res.json();
 
199
 
200
+ responseBox.innerHTML = `<pre>${JSON.stringify(data, null, 2)}</pre>`;
201
  } catch (err) {
202
+ responseBox.innerHTML = `<span style="color:red;">Analysis failed.</span>`;
203
  console.error(err);
 
 
 
 
204
  }
205
  }