Spaces:
Running
Running
Create test1.html
Browse files- test1.html +118 -0
test1.html
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 PDF</title>
|
| 7 |
+
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>
|
| 8 |
+
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.11.338/pdf.min.js"></script>
|
| 9 |
+
</head>
|
| 10 |
+
<body>
|
| 11 |
+
<h1>Modelo de Preguntas y Respuestas sobre un PDF</h1>
|
| 12 |
+
|
| 13 |
+
<input type="file" id="pdfInput" />
|
| 14 |
+
<button onclick="procesarPDF()">Cargar PDF</button>
|
| 15 |
+
|
| 16 |
+
<h2>Preguntar sobre el PDF</h2>
|
| 17 |
+
<input type="text" id="inputPregunta" placeholder="Escribe tu pregunta aqu铆">
|
| 18 |
+
<button onclick="responderPregunta()">Hacer pregunta</button>
|
| 19 |
+
|
| 20 |
+
<h3>Respuesta:</h3>
|
| 21 |
+
<div id="respuesta"></div>
|
| 22 |
+
|
| 23 |
+
<script>
|
| 24 |
+
// Variable global para almacenar el modelo
|
| 25 |
+
let modelo;
|
| 26 |
+
|
| 27 |
+
// Cargar y procesar el archivo PDF
|
| 28 |
+
async function procesarPDF() {
|
| 29 |
+
const archivo = document.getElementById("pdfInput").files[0];
|
| 30 |
+
if (archivo) {
|
| 31 |
+
const archivoPDF = await leerPDF(archivo);
|
| 32 |
+
const textoPDF = archivoPDF.join(" ");
|
| 33 |
+
|
| 34 |
+
// Entrenar el modelo con el texto extra铆do
|
| 35 |
+
modelo = await entrenarModelo(textoPDF);
|
| 36 |
+
|
| 37 |
+
// Almacenar el modelo en IndexedDB
|
| 38 |
+
almacenarModeloEnIndexedDB(modelo);
|
| 39 |
+
}
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
// Leer y extraer el texto del archivo PDF
|
| 43 |
+
async function leerPDF(archivo) {
|
| 44 |
+
const lector = new FileReader();
|
| 45 |
+
return new Promise((resolve, reject) => {
|
| 46 |
+
lector.onload = async function (e) {
|
| 47 |
+
const arrayBuffer = e.target.result;
|
| 48 |
+
const pdf = await pdfjsLib.getDocument(arrayBuffer).promise;
|
| 49 |
+
let texto = [];
|
| 50 |
+
for (let i = 1; i <= pdf.numPages; i++) {
|
| 51 |
+
const pagina = await pdf.getPage(i);
|
| 52 |
+
const contenido = await pagina.getTextContent();
|
| 53 |
+
const textoPagina = contenido.items.map(item => item.str).join(" ");
|
| 54 |
+
texto.push(textoPagina);
|
| 55 |
+
}
|
| 56 |
+
resolve(texto);
|
| 57 |
+
};
|
| 58 |
+
lector.onerror = reject;
|
| 59 |
+
lector.readAsArrayBuffer(archivo);
|
| 60 |
+
});
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
// Funci贸n para entrenar un modelo simple (esto es solo un ejemplo)
|
| 64 |
+
async function entrenarModelo(textoPDF) {
|
| 65 |
+
const tokenizer = new tf.tokenizers.BertTokenizer();
|
| 66 |
+
const tokens = tokenizer.encode(textoPDF);
|
| 67 |
+
const inputs = tf.tensor(tokens);
|
| 68 |
+
|
| 69 |
+
// Aqu铆 simplemente estamos creando un modelo muy simple para ilustrar
|
| 70 |
+
const modelo = tf.sequential();
|
| 71 |
+
modelo.add(tf.layers.dense({ units: 64, inputShape: [inputs.shape[1]] }));
|
| 72 |
+
modelo.add(tf.layers.dense({ units: 32 }));
|
| 73 |
+
modelo.add(tf.layers.dense({ units: 1, activation: 'sigmoid' }));
|
| 74 |
+
|
| 75 |
+
modelo.compile({ optimizer: 'adam', loss: 'binaryCrossentropy' });
|
| 76 |
+
await modelo.fit(inputs, inputs, { epochs: 10 });
|
| 77 |
+
|
| 78 |
+
return modelo;
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
// Almacenar el modelo en IndexedDB
|
| 82 |
+
function almacenarModeloEnIndexedDB(modelo) {
|
| 83 |
+
const request = indexedDB.open("ModeloPDF", 1);
|
| 84 |
+
request.onupgradeneeded = function (event) {
|
| 85 |
+
const db = event.target.result;
|
| 86 |
+
if (!db.objectStoreNames.contains("modelos")) {
|
| 87 |
+
db.createObjectStore("modelos");
|
| 88 |
+
}
|
| 89 |
+
};
|
| 90 |
+
request.onsuccess = function (event) {
|
| 91 |
+
const db = event.target.result;
|
| 92 |
+
const transaction = db.transaction(["modelos"], "readwrite");
|
| 93 |
+
const store = transaction.objectStore("modelos");
|
| 94 |
+
store.put(modelo, "modeloPDF");
|
| 95 |
+
transaction.oncomplete = function () {
|
| 96 |
+
console.log("Modelo almacenado en IndexedDB");
|
| 97 |
+
};
|
| 98 |
+
};
|
| 99 |
+
}
|
| 100 |
+
|
| 101 |
+
// Funci贸n para responder una pregunta utilizando el modelo
|
| 102 |
+
async function responderPregunta() {
|
| 103 |
+
const pregunta = document.getElementById("inputPregunta").value;
|
| 104 |
+
if (modelo && pregunta) {
|
| 105 |
+
// Simulaci贸n de un proceso de predicci贸n (esto debe ser ajustado seg煤n el modelo real)
|
| 106 |
+
const tokenizer = new tf.tokenizers.BertTokenizer();
|
| 107 |
+
const tokens = tokenizer.encode(pregunta);
|
| 108 |
+
const input = tf.tensor(tokens);
|
| 109 |
+
|
| 110 |
+
const prediccion = modelo.predict(input);
|
| 111 |
+
document.getElementById("respuesta").innerText = prediccion.toString();
|
| 112 |
+
} else {
|
| 113 |
+
alert("Por favor, cargue el PDF y entrene el modelo primero.");
|
| 114 |
+
}
|
| 115 |
+
}
|
| 116 |
+
</script>
|
| 117 |
+
</body>
|
| 118 |
+
</html>
|