CoolFace
Apppublic

eduardmtz/www

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
test1.html96 linesDownload Raw Back to root
1<!DOCTYPE html>2<!DOCTYPE html>3<html lang="es">4<head>5    <meta charset="UTF-8">6    <meta name="viewport" content="width=device-width, initial-scale=1.0">7    <title>Modelo de Preguntas y Respuestas sobre un PDF</title>8    <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>9    <script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.11.338/pdf.min.js"></script>10    <script>11        // Aseguramos que pdf.js esté cargado antes de configurarlo12        window.onload = function() {13            pdfjsLib.GlobalWorkerOptions.workerSrc = "https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.11.338/pdf.worker.min.js";14        }15    </script>16</head>17<body>18    <h1>Modelo de Preguntas y Respuestas sobre un PDF</h1>19    20    <input type="file" id="pdfInput" />21    <button onclick="procesarPDF()">Cargar PDF</button>22 23    <h2>Preguntar sobre el PDF</h2>24    <input type="text" id="inputPregunta" placeholder="Escribe tu pregunta aquí">25    <button onclick="responderPregunta()">Hacer pregunta</button>26 27    <h3>Respuesta:</h3>28    <div id="respuesta"></div>29 30    <script>31        // Variable global para almacenar el texto del PDF32        let textoPDF = "";33 34        // Cargar y procesar el archivo PDF35        async function procesarPDF() {36            const archivo = document.getElementById("pdfInput").files[0];37            if (archivo) {38                const archivoPDF = await leerPDF(archivo);39                textoPDF = archivoPDF.join(" ");40                alert("PDF cargado y procesado.");41            }42        }43 44        // Leer y extraer el texto del archivo PDF45        async function leerPDF(archivo) {46            const lector = new FileReader();47            return new Promise((resolve, reject) => {48                lector.onload = async function (e) {49                    const arrayBuffer = e.target.result;50                    const pdf = await pdfjsLib.getDocument(arrayBuffer).promise;51                    let texto = [];52                    for (let i = 1; i <= pdf.numPages; i++) {53                        const pagina = await pdf.getPage(i);54                        const contenido = await pagina.getTextContent();55                        const textoPagina = contenido.items.map(item => item.str).join(" ");56                        texto.push(textoPagina);57                    }58                    resolve(texto);59                };60                lector.onerror = reject;61                lector.readAsArrayBuffer(archivo);62            });63        }64 65        // Función para responder una pregunta utilizando el texto del PDF66        function responderPregunta() {67            const pregunta = document.getElementById("inputPregunta").value;68            if (!textoPDF) {69                alert("Por favor, cargue un PDF primero.");70                return;71            }72 73            // Tokenizar la pregunta en palabras clave74            const palabrasClave = pregunta.toLowerCase().split(" ");75 76            // Buscar frases que contengan las palabras clave77            const frases = textoPDF.split(".");78            const frasesRelevantes = frases.filter(frase => {79                return palabrasClave.some(palabra => frase.toLowerCase().includes(palabra));80            });81 82            if (frasesRelevantes.length > 0) {83                // Devolver la primera frase relevante84                document.getElementById("respuesta").innerText = "Respuesta: " + frasesRelevantes[0];85            } else {86                document.getElementById("respuesta").innerText = "No se encontraron respuestas relevantes.";87            }88        }89    </script>90</body>91</html>92 93 94 95 96