Spaces:
Running
Running
Create test3.html
Browse files- test3.html +92 -0
test3.html
ADDED
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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>Modelo de Preguntas y Respuestas sobre un PDF</title>
|
7 |
+
|
8 |
+
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>
|
9 |
+
<script src="https://cdn.jsdelivr.net/npm/@tensorflow-models/bert"></script>
|
10 |
+
|
11 |
+
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.11.338/pdf.min.js"></script>
|
12 |
+
</head>
|
13 |
+
<body>
|
14 |
+
<h1>Modelo de Preguntas y Respuestas sobre un PDF</h1>
|
15 |
+
|
16 |
+
<input type="file" id="pdfInput" />
|
17 |
+
<button onclick="procesarPDF()">Cargar PDF</button>
|
18 |
+
|
19 |
+
<h2>Preguntar sobre el PDF</h2>
|
20 |
+
<input type="text" id="inputPregunta" placeholder="Escribe tu pregunta aqu铆">
|
21 |
+
<button onclick="responderPregunta()">Hacer pregunta</button>
|
22 |
+
|
23 |
+
<h3>Respuesta:</h3>
|
24 |
+
<div id="respuesta"></div>
|
25 |
+
|
26 |
+
<script>
|
27 |
+
// Variable global para almacenar el texto del PDF
|
28 |
+
let textoPDF = "";
|
29 |
+
|
30 |
+
// Cargar y procesar el archivo PDF
|
31 |
+
async function procesarPDF() {
|
32 |
+
const archivo = document.getElementById("pdfInput").files[0];
|
33 |
+
if (archivo) {
|
34 |
+
const archivoPDF = await leerPDF(archivo);
|
35 |
+
textoPDF = archivoPDF.join(" ");
|
36 |
+
alert("PDF cargado y procesado.");
|
37 |
+
}
|
38 |
+
}
|
39 |
+
|
40 |
+
// Leer y extraer el texto del archivo PDF
|
41 |
+
async function leerPDF(archivo) {
|
42 |
+
const lector = new FileReader();
|
43 |
+
return new Promise((resolve, reject) => {
|
44 |
+
lector.onload = async function (e) {
|
45 |
+
const arrayBuffer = e.target.result;
|
46 |
+
const pdf = await pdfjsLib.getDocument(arrayBuffer).promise;
|
47 |
+
let texto = [];
|
48 |
+
for (let i = 1; i <= pdf.numPages; i++) {
|
49 |
+
const pagina = await pdf.getPage(i);
|
50 |
+
const contenido = await pagina.getTextContent();
|
51 |
+
const textoPagina = contenido.items.map(item => item.str).join(" ");
|
52 |
+
texto.push(textoPagina);
|
53 |
+
}
|
54 |
+
resolve(texto);
|
55 |
+
};
|
56 |
+
lector.onerror = reject;
|
57 |
+
lector.readAsArrayBuffer(archivo);
|
58 |
+
});
|
59 |
+
}
|
60 |
+
|
61 |
+
// Funci贸n para responder una pregunta utilizando el texto del PDF
|
62 |
+
async function responderPregunta() {
|
63 |
+
const pregunta = document.getElementById("inputPregunta").value;
|
64 |
+
if (!textoPDF) {
|
65 |
+
alert("Por favor, cargue un PDF primero.");
|
66 |
+
return;
|
67 |
+
}
|
68 |
+
|
69 |
+
// Enviar la pregunta y el texto del PDF al modelo para obtener la respuesta
|
70 |
+
const respuesta = await obtenerRespuestaConBERT(pregunta, textoPDF);
|
71 |
+
|
72 |
+
// Mostrar la respuesta
|
73 |
+
document.getElementById("respuesta").innerText = "Respuesta: " + respuesta;
|
74 |
+
}
|
75 |
+
|
76 |
+
// Funci贸n para obtener respuesta utilizando un modelo de TensorFlow.js con BERT
|
77 |
+
async function obtenerRespuestaConBERT(pregunta, contexto) {
|
78 |
+
// Cargar el modelo preentrenado de BERT usando TensorFlow.js
|
79 |
+
const modelo = await tf.loadGraphModel('https://tensorflowjs-models.s3.us-east-2.amazonaws.com/distilbert_squad/model.json');
|
80 |
+
|
81 |
+
// Procesar la pregunta y contexto para usar en el modelo (esto var铆a seg煤n el modelo espec铆fico)
|
82 |
+
const respuesta = await modelo.predict({
|
83 |
+
question: pregunta,
|
84 |
+
context: contexto
|
85 |
+
});
|
86 |
+
|
87 |
+
// Devolver la respuesta
|
88 |
+
return respuesta;
|
89 |
+
}
|
90 |
+
</script>
|
91 |
+
</body>
|
92 |
+
</html>
|