Spaces:
Sleeping

emotion-ui / src /utils /api.js
Aya07's picture
Upload api.js
ee09ce0 verified
Raw History Blame Contribute Delete
879 Bytes
const API_URL = "https://aya07-emotion-det.hf.space/predict";
export const predictEmotion = async (file) => {
const formData = new FormData();
// 🔥 FIX : S'assurer que le fichier a un nom valide
let fileToSend = file;
if (file instanceof Blob && !file.name) {
// Créer un File object avec un nom valide
fileToSend = new File([file], `recording_${Date.now()}.wav`, { type: 'audio/wav' });
console.log('Fixed file for API:', fileToSend.name, fileToSend.type);
}
formData.append("audio_file", fileToSend);
const res = await fetch(API_URL, {
method: "POST",
body: formData,
});
const data = await res.json();
if (!res.ok) {
console.error('API Error:', data);
throw new Error(data.detail || `Server error: ${res.status}`);
}
if (!data.success) throw new Error(data.detail || "Prediction failed");
return data;
};