Spaces:
Running
Running
Upload script.js
Browse files- static/script.js +70 -0
static/script.js
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const imageInput = document.getElementById('imageInput');
|
| 2 |
+
const previewImg = document.getElementById('previewImg');
|
| 3 |
+
const classifyBtn = document.getElementById('classifyBtn');
|
| 4 |
+
const resultDiv = document.getElementById('result');
|
| 5 |
+
|
| 6 |
+
imageInput.addEventListener('change', (event) => {
|
| 7 |
+
const file = event.target.files[0];
|
| 8 |
+
if (file) {
|
| 9 |
+
const reader = new FileReader();
|
| 10 |
+
reader.onload = function(e) {
|
| 11 |
+
previewImg.src = e.target.result;
|
| 12 |
+
previewImg.style.display = 'block';
|
| 13 |
+
classifyBtn.disabled = false;
|
| 14 |
+
resultDiv.innerHTML = '';
|
| 15 |
+
resultDiv.className = 'result';
|
| 16 |
+
};
|
| 17 |
+
reader.readAsDataURL(file);
|
| 18 |
+
} else {
|
| 19 |
+
previewImg.style.display = 'none';
|
| 20 |
+
classifyBtn.disabled = true;
|
| 21 |
+
}
|
| 22 |
+
});
|
| 23 |
+
|
| 24 |
+
classifyBtn.addEventListener('click', async () => {
|
| 25 |
+
const file = imageInput.files[0];
|
| 26 |
+
if (!file) return;
|
| 27 |
+
|
| 28 |
+
classifyBtn.disabled = true;
|
| 29 |
+
classifyBtn.innerText = '🔍 Analizando...';
|
| 30 |
+
resultDiv.innerHTML = '<em>Procesando imagen...</em>';
|
| 31 |
+
|
| 32 |
+
const formData = new FormData();
|
| 33 |
+
formData.append('image', file);
|
| 34 |
+
|
| 35 |
+
try {
|
| 36 |
+
const response = await fetch('/predict', {
|
| 37 |
+
method: 'POST',
|
| 38 |
+
body: formData
|
| 39 |
+
});
|
| 40 |
+
|
| 41 |
+
if (!response.ok) {
|
| 42 |
+
const errorData = await response.json();
|
| 43 |
+
throw new Error(errorData.error || 'Error del servidor');
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
const data = await response.json();
|
| 47 |
+
const genero = data.genero;
|
| 48 |
+
const confianza = data.confianza; // Capturamos la nueva variable
|
| 49 |
+
|
| 50 |
+
// Actualizamos el HTML para mostrar tanto el género como la confianza
|
| 51 |
+
resultDiv.innerHTML = `
|
| 52 |
+
🧬 La persona es <strong>${genero}</strong>.<br>
|
| 53 |
+
🎯 Precisión: <strong>${confianza}</strong>
|
| 54 |
+
`;
|
| 55 |
+
|
| 56 |
+
// Removemos clases anteriores por si acaso
|
| 57 |
+
resultDiv.classList.remove('mujer', 'hombre', 'error');
|
| 58 |
+
// Añadimos la clase correspondiente
|
| 59 |
+
resultDiv.classList.add(genero === 'mujer' ? 'mujer' : 'hombre');
|
| 60 |
+
|
| 61 |
+
} catch (error) {
|
| 62 |
+
console.error(error);
|
| 63 |
+
resultDiv.innerHTML = `❌ Error: ${error.message}`;
|
| 64 |
+
resultDiv.classList.remove('mujer', 'hombre'); // Limpiar estilos previos
|
| 65 |
+
resultDiv.classList.add('error');
|
| 66 |
+
} finally {
|
| 67 |
+
classifyBtn.disabled = false;
|
| 68 |
+
classifyBtn.innerText = '🔍 Clasificar';
|
| 69 |
+
}
|
| 70 |
+
});
|