Spaces:
Running
Running
Create test5.html
Browse files- test5.html +125 -0
test5.html
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html lang="es">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="UTF-8">
|
| 5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 6 |
+
<title>Preguntas sobre Documentos</title>
|
| 7 |
+
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>
|
| 8 |
+
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.10.377/pdf.min.js"></script>
|
| 9 |
+
<script src="https://cdnjs.cloudflare.com/ajax/libs/mammoth/1.4.2/mammoth.browser.min.js"></script>
|
| 10 |
+
</head>
|
| 11 |
+
<body>
|
| 12 |
+
<h1>Sube un archivo y haz preguntas en espa帽ol o catal谩n</h1>
|
| 13 |
+
|
| 14 |
+
<input type="file" id="file-input" />
|
| 15 |
+
<br><br>
|
| 16 |
+
|
| 17 |
+
<label for="question">Pregunta:</label>
|
| 18 |
+
<input type="text" id="question" placeholder="Escribe tu pregunta aqu铆">
|
| 19 |
+
<button onclick="askQuestion()">Hacer Pregunta</button>
|
| 20 |
+
|
| 21 |
+
<h3>Respuesta:</h3>
|
| 22 |
+
<div id="response"></div>
|
| 23 |
+
|
| 24 |
+
<script>
|
| 25 |
+
let documentText = ''; // Para almacenar el texto extra铆do de los documentos
|
| 26 |
+
|
| 27 |
+
// Funci贸n para leer archivos PDF
|
| 28 |
+
async function readPDF(file) {
|
| 29 |
+
const reader = new FileReader();
|
| 30 |
+
reader.onload = async function(event) {
|
| 31 |
+
const loadingTask = pdfjsLib.getDocument(event.target.result);
|
| 32 |
+
loadingTask.promise.then(function(pdf) {
|
| 33 |
+
let text = '';
|
| 34 |
+
const numPages = pdf.numPages;
|
| 35 |
+
for (let pageNum = 1; pageNum <= numPages; pageNum++) {
|
| 36 |
+
pdf.getPage(pageNum).then(function(page) {
|
| 37 |
+
page.getTextContent().then(function(textContent) {
|
| 38 |
+
textContent.items.forEach(function(item) {
|
| 39 |
+
text += item.str + ' ';
|
| 40 |
+
});
|
| 41 |
+
documentText = text; // Guardamos el texto
|
| 42 |
+
});
|
| 43 |
+
});
|
| 44 |
+
}
|
| 45 |
+
});
|
| 46 |
+
};
|
| 47 |
+
reader.readAsArrayBuffer(file);
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
// Funci贸n para leer archivos de texto (TXT)
|
| 51 |
+
function readTXT(file) {
|
| 52 |
+
const reader = new FileReader();
|
| 53 |
+
reader.onload = function(event) {
|
| 54 |
+
documentText = event.target.result; // Guardamos el texto
|
| 55 |
+
};
|
| 56 |
+
reader.readAsText(file);
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
// Funci贸n para leer archivos de Word (DOCX)
|
| 60 |
+
function readWord(file) {
|
| 61 |
+
const reader = new FileReader();
|
| 62 |
+
reader.onload = function(event) {
|
| 63 |
+
mammoth.convertToHtml({ arrayBuffer: event.target.result })
|
| 64 |
+
.then(function(result) {
|
| 65 |
+
documentText = result.value; // Guardamos el texto
|
| 66 |
+
})
|
| 67 |
+
.catch(function(err) {
|
| 68 |
+
console.log(err);
|
| 69 |
+
});
|
| 70 |
+
};
|
| 71 |
+
reader.readAsArrayBuffer(file);
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
// L贸gica para cargar el archivo seleccionado
|
| 75 |
+
document.getElementById('file-input').addEventListener('change', function(event) {
|
| 76 |
+
const file = event.target.files[0];
|
| 77 |
+
const fileType = file.type;
|
| 78 |
+
|
| 79 |
+
if (fileType === 'application/pdf') {
|
| 80 |
+
readPDF(file);
|
| 81 |
+
} else if (fileType === 'text/plain') {
|
| 82 |
+
readTXT(file);
|
| 83 |
+
} else if (fileType === 'application/vnd.openxmlformats-officedocument.wordprocessingml.document') {
|
| 84 |
+
readWord(file);
|
| 85 |
+
} else {
|
| 86 |
+
alert('Tipo de archivo no soportado');
|
| 87 |
+
}
|
| 88 |
+
});
|
| 89 |
+
|
| 90 |
+
// Funci贸n para hacer preguntas al modelo de Hugging Face
|
| 91 |
+
async function askQuestion() {
|
| 92 |
+
const question = document.getElementById('question').value;
|
| 93 |
+
if (!documentText || !question) {
|
| 94 |
+
alert('Aseg煤rate de que se haya cargado un archivo y que hayas ingresado una pregunta.');
|
| 95 |
+
return;
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
// Usamos la API de Hugging Face para el modelo multiling眉e de preguntas y respuestas
|
| 99 |
+
const modelUrl = 'https://api-inference.huggingface.co/models/distilbert-base-multilingual-cased-distilled-squad';
|
| 100 |
+
|
| 101 |
+
// Preparamos los datos para la consulta
|
| 102 |
+
const data = {
|
| 103 |
+
inputs: {
|
| 104 |
+
question: question,
|
| 105 |
+
context: documentText
|
| 106 |
+
}
|
| 107 |
+
};
|
| 108 |
+
|
| 109 |
+
// Hacemos la petici贸n a la API de Hugging Face
|
| 110 |
+
const response = await fetch(modelUrl, {
|
| 111 |
+
method: 'POST',
|
| 112 |
+
headers: {
|
| 113 |
+
'Authorization': 'Bearer YOUR_HUGGINGFACE_API_KEY', // Sustituye por tu API key de Hugging Face
|
| 114 |
+
'Content-Type': 'application/json'
|
| 115 |
+
},
|
| 116 |
+
body: JSON.stringify(data)
|
| 117 |
+
});
|
| 118 |
+
|
| 119 |
+
const result = await response.json();
|
| 120 |
+
const answer = result?.answer || 'No pude encontrar una respuesta.';
|
| 121 |
+
document.getElementById('response').innerHTML = answer;
|
| 122 |
+
}
|
| 123 |
+
</script>
|
| 124 |
+
</body>
|
| 125 |
+
</html>
|