Spaces:
Running
Running
Create test2.html
Browse files- test2.html +93 -0
test2.html
ADDED
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
<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 |
+
<script src="https://cdn.jsdelivr.net/npm/@huggingface/transformers"></script>
|
10 |
+
</head>
|
11 |
+
<body>
|
12 |
+
<h1>Modelo de Preguntas y Respuestas sobre un PDF</h1>
|
13 |
+
|
14 |
+
<input type="file" id="pdfInput" />
|
15 |
+
<button onclick="procesarPDF()">Cargar PDF</button>
|
16 |
+
|
17 |
+
<h2>Preguntar sobre el PDF</h2>
|
18 |
+
<input type="text" id="inputPregunta" placeholder="Escribe tu pregunta aqu铆">
|
19 |
+
<button onclick="responderPregunta()">Hacer pregunta</button>
|
20 |
+
|
21 |
+
<h3>Respuesta:</h3>
|
22 |
+
<div id="respuesta"></div>
|
23 |
+
|
24 |
+
<script>
|
25 |
+
// Variable global para almacenar el texto del PDF
|
26 |
+
let textoPDF = "";
|
27 |
+
|
28 |
+
// Cargar y procesar el archivo PDF
|
29 |
+
async function procesarPDF() {
|
30 |
+
const archivo = document.getElementById("pdfInput").files[0];
|
31 |
+
if (archivo) {
|
32 |
+
const archivoPDF = await leerPDF(archivo);
|
33 |
+
textoPDF = archivoPDF.join(" ");
|
34 |
+
alert("PDF cargado y procesado.");
|
35 |
+
}
|
36 |
+
}
|
37 |
+
|
38 |
+
// Leer y extraer el texto del archivo PDF
|
39 |
+
async function leerPDF(archivo) {
|
40 |
+
const lector = new FileReader();
|
41 |
+
return new Promise((resolve, reject) => {
|
42 |
+
lector.onload = async function (e) {
|
43 |
+
const arrayBuffer = e.target.result;
|
44 |
+
const pdf = await pdfjsLib.getDocument(arrayBuffer).promise;
|
45 |
+
let texto = [];
|
46 |
+
for (let i = 1; i <= pdf.numPages; i++) {
|
47 |
+
const pagina = await pdf.getPage(i);
|
48 |
+
const contenido = await pagina.getTextContent();
|
49 |
+
const textoPagina = contenido.items.map(item => item.str).join(" ");
|
50 |
+
texto.push(textoPagina);
|
51 |
+
}
|
52 |
+
resolve(texto);
|
53 |
+
};
|
54 |
+
lector.onerror = reject;
|
55 |
+
lector.readAsArrayBuffer(archivo);
|
56 |
+
});
|
57 |
+
}
|
58 |
+
|
59 |
+
// Funci贸n para responder una pregunta utilizando el texto del PDF
|
60 |
+
async function responderPregunta() {
|
61 |
+
const pregunta = document.getElementById("inputPregunta").value;
|
62 |
+
if (!textoPDF) {
|
63 |
+
alert("Por favor, cargue un PDF primero.");
|
64 |
+
return;
|
65 |
+
}
|
66 |
+
|
67 |
+
// Tokenizar la pregunta y el contexto
|
68 |
+
const question = pregunta;
|
69 |
+
const context = textoPDF;
|
70 |
+
|
71 |
+
// Usar un modelo preentrenado como BERT o T5 de HuggingFace
|
72 |
+
const response = await obtenerRespuestaDeModelo(question, context);
|
73 |
+
|
74 |
+
// Mostrar la respuesta
|
75 |
+
document.getElementById("respuesta").innerText = "Respuesta: " + response;
|
76 |
+
}
|
77 |
+
|
78 |
+
// Funci贸n para obtener respuesta utilizando el modelo de Hugging Face
|
79 |
+
async function obtenerRespuestaDeModelo(question, context) {
|
80 |
+
const model = await transformers.BertForQuestionAnswering.from_pretrained('bert-large-uncased-whole-word-masking-finetuned-squad');
|
81 |
+
const tokenizer = await transformers.BertTokenizer.from_pretrained('bert-large-uncased-whole-word-masking-finetuned-squad');
|
82 |
+
|
83 |
+
const inputs = tokenizer.encode_plus(question, context, { add_special_tokens: true, return_tensors: 'pt' });
|
84 |
+
const output = await model(inputs);
|
85 |
+
const answer_start = output.start_logits.argmax();
|
86 |
+
const answer_end = output.end_logits.argmax();
|
87 |
+
const answer = tokenizer.decode(inputs.input_ids.slice(answer_start, answer_end + 1));
|
88 |
+
|
89 |
+
return answer;
|
90 |
+
}
|
91 |
+
</script>
|
92 |
+
</body>
|
93 |
+
</html>
|