Spaces:
Running
Running
Update test3.html
Browse files- test3.html +96 -65
test3.html
CHANGED
@@ -3,90 +3,121 @@
|
|
3 |
<head>
|
4 |
<meta charset="UTF-8">
|
5 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
6 |
-
<title>Modelo de
|
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 |
-
<
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
-
<
|
20 |
-
|
21 |
-
|
|
|
|
|
22 |
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
<script>
|
27 |
-
|
28 |
-
let
|
29 |
-
|
30 |
-
//
|
31 |
-
async function
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
}
|
|
|
38 |
}
|
39 |
|
40 |
-
//
|
41 |
-
async function
|
42 |
-
const
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
|
|
|
|
|
|
59 |
}
|
60 |
|
61 |
-
//
|
62 |
-
async function
|
63 |
-
const
|
64 |
-
if (!
|
65 |
-
|
66 |
return;
|
67 |
}
|
68 |
|
69 |
-
//
|
70 |
-
const
|
|
|
71 |
|
72 |
-
//
|
73 |
-
document.getElementById(
|
74 |
}
|
75 |
|
76 |
-
//
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
// Devolver la respuesta
|
88 |
-
return respuesta;
|
89 |
-
}
|
90 |
</script>
|
|
|
91 |
</body>
|
92 |
</html>
|
|
|
|
3 |
<head>
|
4 |
<meta charset="UTF-8">
|
5 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
6 |
+
<title>Modelo de BERT con TensorFlow.js</title>
|
|
|
7 |
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>
|
8 |
<script src="https://cdn.jsdelivr.net/npm/@tensorflow-models/bert"></script>
|
9 |
+
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.10.377/pdf.min.js"></script>
|
10 |
+
<style>
|
11 |
+
body {
|
12 |
+
font-family: Arial, sans-serif;
|
13 |
+
}
|
14 |
+
.container {
|
15 |
+
margin: 20px;
|
16 |
+
}
|
17 |
+
.file-input {
|
18 |
+
margin: 10px 0;
|
19 |
+
}
|
20 |
+
.query-input {
|
21 |
+
margin: 10px 0;
|
22 |
+
}
|
23 |
+
.response {
|
24 |
+
margin-top: 20px;
|
25 |
+
font-size: 1.2em;
|
26 |
+
}
|
27 |
+
</style>
|
28 |
</head>
|
29 |
<body>
|
|
|
|
|
|
|
|
|
30 |
|
31 |
+
<div class="container">
|
32 |
+
<h1>Modelo de BERT con TensorFlow.js</h1>
|
33 |
+
|
34 |
+
<!-- Subir archivo PDF -->
|
35 |
+
<input type="file" id="pdf-file" class="file-input" accept=".pdf" />
|
36 |
|
37 |
+
<!-- Entrada para consulta -->
|
38 |
+
<input type="text" id="query" class="query-input" placeholder="Escribe tu consulta..." />
|
39 |
+
|
40 |
+
<button onclick="handleQuery()">Consultar modelo</button>
|
41 |
+
|
42 |
+
<div class="response" id="response"></div>
|
43 |
+
</div>
|
44 |
|
45 |
<script>
|
46 |
+
let model;
|
47 |
+
let trainingData = [];
|
48 |
+
|
49 |
+
// Funci贸n para cargar el modelo DistilBERT
|
50 |
+
async function loadModel() {
|
51 |
+
model = await tf.loadLayersModel('https://cdn.jsdelivr.net/npm/@tensorflow-models/bert/dist/bert_model.json');
|
52 |
+
console.log("Modelo cargado");
|
53 |
+
}
|
54 |
+
|
55 |
+
// Funci贸n para procesar PDF y extraer texto
|
56 |
+
async function extractTextFromPDF(file) {
|
57 |
+
const pdf = await pdfjsLib.getDocument(URL.createObjectURL(file)).promise;
|
58 |
+
let textContent = '';
|
59 |
+
for (let pageNum = 1; pageNum <= pdf.numPages; pageNum++) {
|
60 |
+
const page = await pdf.getPage(pageNum);
|
61 |
+
const content = await page.getTextContent();
|
62 |
+
content.items.forEach(item => {
|
63 |
+
textContent += item.str + ' ';
|
64 |
+
});
|
65 |
}
|
66 |
+
return textContent;
|
67 |
}
|
68 |
|
69 |
+
// Funci贸n para agregar el texto de los PDFs y entrenar el modelo
|
70 |
+
async function trainModel(file) {
|
71 |
+
const text = await extractTextFromPDF(file);
|
72 |
+
trainingData.push(text);
|
73 |
+
|
74 |
+
// Preprocesar el texto para BERT (esto es un ejemplo b谩sico)
|
75 |
+
const inputText = text.split(' ').slice(0, 512).join(' '); // Limitar el tama帽o del texto
|
76 |
+
|
77 |
+
// Predecir con DistilBERT (aqu铆 solo mostramos un ejemplo b谩sico)
|
78 |
+
const tokenizedInput = tokenizeInput(inputText);
|
79 |
+
const prediction = await model.predict(tokenizedInput);
|
80 |
+
|
81 |
+
console.log(prediction); // Aqu铆 deber铆as implementar m谩s l贸gica para entrenar el modelo
|
82 |
+
}
|
83 |
+
|
84 |
+
// Tokenizar el texto de entrada
|
85 |
+
function tokenizeInput(inputText) {
|
86 |
+
// Aseg煤rate de usar una correcta tokenizaci贸n basada en BERT
|
87 |
+
// Este es un ejemplo b谩sico, puede requerir una implementaci贸n completa seg煤n TensorFlow.js y BERT
|
88 |
+
const tokens = inputText.split(' ');
|
89 |
+
const inputTensor = tf.tensor([tokens.map(token => token.charCodeAt(0))]); // Tokenizaci贸n b谩sica
|
90 |
+
return inputTensor;
|
91 |
}
|
92 |
|
93 |
+
// Manejar la consulta del modelo
|
94 |
+
async function handleQuery() {
|
95 |
+
const query = document.getElementById('query').value;
|
96 |
+
if (!query) {
|
97 |
+
document.getElementById('response').innerText = "Por favor, escribe una consulta.";
|
98 |
return;
|
99 |
}
|
100 |
|
101 |
+
// Tokenizar y hacer una predicci贸n con el modelo
|
102 |
+
const tokenizedQuery = tokenizeInput(query);
|
103 |
+
const queryPrediction = await model.predict(tokenizedQuery);
|
104 |
|
105 |
+
// Aqu铆 deber铆as implementar la l贸gica para dar una respuesta basada en la predicci贸n
|
106 |
+
document.getElementById('response').innerText = `Respuesta del modelo: ${queryPrediction}`;
|
107 |
}
|
108 |
|
109 |
+
// Escuchar el archivo PDF y entrenar el modelo
|
110 |
+
document.getElementById('pdf-file').addEventListener('change', function(event) {
|
111 |
+
const file = event.target.files[0];
|
112 |
+
if (file) {
|
113 |
+
trainModel(file);
|
114 |
+
}
|
115 |
+
});
|
116 |
+
|
117 |
+
// Cargar el modelo cuando la p谩gina se carga
|
118 |
+
loadModel();
|
|
|
|
|
|
|
|
|
119 |
</script>
|
120 |
+
|
121 |
</body>
|
122 |
</html>
|
123 |
+
|