File size: 3,621 Bytes
e0d2132
2bcab4f
a60d562
 
 
 
2602bfb
a60d562
e0d2132
2602bfb
e0d2132
 
 
 
2602bfb
a60d562
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2bcab4f
 
a60d562
 
 
 
 
 
2bcab4f
 
a60d562
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2bcab4f
 
a60d562
2bcab4f
 
 
930d0f4
 
2bcab4f
 
a60d562
2bcab4f
 
 
 
 
 
 
 
 
a60d562
2bcab4f
a60d562
 
 
 
 
2602bfb
e0d2132
930d0f4
2bcab4f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<!DOCTYPE html>
<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Modelo de Preguntas y Respuestas sobre un PDF</title>
    <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.11.338/pdf.min.js"></script>
    <script>
        // Aseguramos que pdf.js esté cargado antes de configurarlo
        window.onload = function() {
            pdfjsLib.GlobalWorkerOptions.workerSrc = "https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.11.338/pdf.worker.min.js";
        }
    </script>
</head>
<body>
    <h1>Modelo de Preguntas y Respuestas sobre un PDF</h1>
    
    <input type="file" id="pdfInput" />
    <button onclick="procesarPDF()">Cargar PDF</button>

    <h2>Preguntar sobre el PDF</h2>
    <input type="text" id="inputPregunta" placeholder="Escribe tu pregunta aquí">
    <button onclick="responderPregunta()">Hacer pregunta</button>

    <h3>Respuesta:</h3>
    <div id="respuesta"></div>

    <script>
        // Variable global para almacenar el texto del PDF
        let textoPDF = "";

        // Cargar y procesar el archivo PDF
        async function procesarPDF() {
            const archivo = document.getElementById("pdfInput").files[0];
            if (archivo) {
                const archivoPDF = await leerPDF(archivo);
                textoPDF = archivoPDF.join(" ");
                alert("PDF cargado y procesado.");
            }
        }

        // Leer y extraer el texto del archivo PDF
        async function leerPDF(archivo) {
            const lector = new FileReader();
            return new Promise((resolve, reject) => {
                lector.onload = async function (e) {
                    const arrayBuffer = e.target.result;
                    const pdf = await pdfjsLib.getDocument(arrayBuffer).promise;
                    let texto = [];
                    for (let i = 1; i <= pdf.numPages; i++) {
                        const pagina = await pdf.getPage(i);
                        const contenido = await pagina.getTextContent();
                        const textoPagina = contenido.items.map(item => item.str).join(" ");
                        texto.push(textoPagina);
                    }
                    resolve(texto);
                };
                lector.onerror = reject;
                lector.readAsArrayBuffer(archivo);
            });
        }

        // Función para responder una pregunta utilizando el texto del PDF
        function responderPregunta() {
            const pregunta = document.getElementById("inputPregunta").value;
            if (!textoPDF) {
                alert("Por favor, cargue un PDF primero.");
                return;
            }

            // Tokenizar la pregunta en palabras clave
            const palabrasClave = pregunta.toLowerCase().split(" ");

            // Buscar frases que contengan las palabras clave
            const frases = textoPDF.split(".");
            const frasesRelevantes = frases.filter(frase => {
                return palabrasClave.some(palabra => frase.toLowerCase().includes(palabra));
            });

            if (frasesRelevantes.length > 0) {
                // Devolver la primera frase relevante
                document.getElementById("respuesta").innerText = "Respuesta: " + frasesRelevantes[0];
            } else {
                document.getElementById("respuesta").innerText = "No se encontraron respuestas relevantes.";
            }
        }
    </script>
</body>
</html>