Spaces:
				
			
			
	
			
			
					
		Running
		
	
	
	
			
			
	
	
	
	
		
		
					
		Running
		
	Create test4.html
Browse files- test4.html +109 -0
    	
        test4.html
    ADDED
    
    | @@ -0,0 +1,109 @@ | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | 
|  | |
| 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>Preguntas y Respuestas desde PDFs</title>
         | 
| 7 | 
            +
                <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>
         | 
| 8 | 
            +
                <script src="https://cdn.jsdelivr.net/npm/@tensorflow-models/qna"></script>
         | 
| 9 | 
            +
                <script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.11.338/pdf.min.js"></script>
         | 
| 10 | 
            +
                <script>
         | 
| 11 | 
            +
                    pdfjsLib.GlobalWorkerOptions.workerSrc = 'https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.11.338/pdf.worker.min.js';
         | 
| 12 | 
            +
                </script>
         | 
| 13 | 
            +
            </head>
         | 
| 14 | 
            +
            <body>
         | 
| 15 | 
            +
                <h1>Modelo de Preguntas y Respuestas a partir de PDFs</h1>
         | 
| 16 | 
            +
             | 
| 17 | 
            +
                <!-- Carga de PDFs -->
         | 
| 18 | 
            +
                <h2>Cargar PDFs</h2>
         | 
| 19 | 
            +
                <input type="file" id="pdfInput" multiple accept=".pdf" />
         | 
| 20 | 
            +
                <button onclick="cargarPDFs()">Cargar PDFs</button>
         | 
| 21 | 
            +
                <div id="statusCarga"></div>
         | 
| 22 | 
            +
             | 
| 23 | 
            +
                <!-- Preguntas -->
         | 
| 24 | 
            +
                <h2>Realizar una pregunta</h2>
         | 
| 25 | 
            +
                <input type="text" id="preguntaInput" placeholder="Escribe tu pregunta aqu铆" />
         | 
| 26 | 
            +
                <button onclick="realizarPregunta()">Preguntar</button>
         | 
| 27 | 
            +
                <div>
         | 
| 28 | 
            +
                    <h3>Respuesta:</h3>
         | 
| 29 | 
            +
                    <p id="respuesta"></p>
         | 
| 30 | 
            +
                </div>
         | 
| 31 | 
            +
             | 
| 32 | 
            +
                <script>
         | 
| 33 | 
            +
                    // Variable global para almacenar el texto extra铆do de los PDFs
         | 
| 34 | 
            +
                    let contextoGlobal = "";
         | 
| 35 | 
            +
             | 
| 36 | 
            +
                    // Carga y procesamiento de PDFs
         | 
| 37 | 
            +
                    async function cargarPDFs() {
         | 
| 38 | 
            +
                        const archivos = document.getElementById("pdfInput").files;
         | 
| 39 | 
            +
                        if (archivos.length === 0) {
         | 
| 40 | 
            +
                            alert("Por favor, selecciona uno o m谩s archivos PDF.");
         | 
| 41 | 
            +
                            return;
         | 
| 42 | 
            +
                        }
         | 
| 43 | 
            +
             | 
| 44 | 
            +
                        const textos = [];
         | 
| 45 | 
            +
                        for (const archivo of archivos) {
         | 
| 46 | 
            +
                            const texto = await extraerTextoPDF(archivo);
         | 
| 47 | 
            +
                            textos.push(texto);
         | 
| 48 | 
            +
                        }
         | 
| 49 | 
            +
             | 
| 50 | 
            +
                        // Unir todo el texto en un contexto global
         | 
| 51 | 
            +
                        contextoGlobal = textos.join(" ");
         | 
| 52 | 
            +
                        document.getElementById("statusCarga").innerText = "PDFs cargados y procesados correctamente.";
         | 
| 53 | 
            +
                    }
         | 
| 54 | 
            +
             | 
| 55 | 
            +
                    // Funci贸n para extraer texto de un archivo PDF
         | 
| 56 | 
            +
                    async function extraerTextoPDF(archivo) {
         | 
| 57 | 
            +
                        const lector = new FileReader();
         | 
| 58 | 
            +
                        return new Promise((resolve, reject) => {
         | 
| 59 | 
            +
                            lector.onload = async function (e) {
         | 
| 60 | 
            +
                                const arrayBuffer = e.target.result;
         | 
| 61 | 
            +
                                const pdf = await pdfjsLib.getDocument(arrayBuffer).promise;
         | 
| 62 | 
            +
                                let textoCompleto = "";
         | 
| 63 | 
            +
             | 
| 64 | 
            +
                                for (let i = 1; i <= pdf.numPages; i++) {
         | 
| 65 | 
            +
                                    const pagina = await pdf.getPage(i);
         | 
| 66 | 
            +
                                    const contenido = await pagina.getTextContent();
         | 
| 67 | 
            +
                                    const textoPagina = contenido.items.map(item => item.str).join(" ");
         | 
| 68 | 
            +
                                    textoCompleto += textoPagina + "\n";
         | 
| 69 | 
            +
                                }
         | 
| 70 | 
            +
                                resolve(textoCompleto);
         | 
| 71 | 
            +
                            };
         | 
| 72 | 
            +
                            lector.onerror = reject;
         | 
| 73 | 
            +
                            lector.readAsArrayBuffer(archivo);
         | 
| 74 | 
            +
                        });
         | 
| 75 | 
            +
                    }
         | 
| 76 | 
            +
             | 
| 77 | 
            +
                    // Modelo de Preguntas y Respuestas usando TensorFlow.js
         | 
| 78 | 
            +
                    let modelo = null;
         | 
| 79 | 
            +
             | 
| 80 | 
            +
                    // Funci贸n para realizar una pregunta
         | 
| 81 | 
            +
                    async function realizarPregunta() {
         | 
| 82 | 
            +
                        const pregunta = document.getElementById("preguntaInput").value;
         | 
| 83 | 
            +
                        if (!contextoGlobal) {
         | 
| 84 | 
            +
                            alert("Primero debes cargar los PDFs para generar el contexto.");
         | 
| 85 | 
            +
                            return;
         | 
| 86 | 
            +
                        }
         | 
| 87 | 
            +
                        if (!pregunta) {
         | 
| 88 | 
            +
                            alert("Por favor, escribe una pregunta.");
         | 
| 89 | 
            +
                            return;
         | 
| 90 | 
            +
                        }
         | 
| 91 | 
            +
             | 
| 92 | 
            +
                        if (!modelo) {
         | 
| 93 | 
            +
                            document.getElementById("respuesta").innerText = "Cargando modelo, por favor espera...";
         | 
| 94 | 
            +
                            modelo = await qna.load(); // Cargar el modelo QnA basado en DistilBERT
         | 
| 95 | 
            +
                        }
         | 
| 96 | 
            +
             | 
| 97 | 
            +
                        // Realizar la pregunta
         | 
| 98 | 
            +
                        const respuestas = await modelo.findAnswers(pregunta, contextoGlobal);
         | 
| 99 | 
            +
             | 
| 100 | 
            +
                        // Mostrar la mejor respuesta o un mensaje si no se encontr贸 ninguna
         | 
| 101 | 
            +
                        if (respuestas.length > 0) {
         | 
| 102 | 
            +
                            document.getElementById("respuesta").innerText = respuestas[0].text;
         | 
| 103 | 
            +
                        } else {
         | 
| 104 | 
            +
                            document.getElementById("respuesta").innerText = "No se encontr贸 una respuesta adecuada.";
         | 
| 105 | 
            +
                        }
         | 
| 106 | 
            +
                    }
         | 
| 107 | 
            +
                </script>
         | 
| 108 | 
            +
            </body>
         | 
| 109 | 
            +
            </html>
         | 
