eduardmtz commited on
Commit
3524a2f
verified
1 Parent(s): 42dc88d

Update test2.html

Browse files
Files changed (1) hide show
  1. test2.html +38 -14
test2.html CHANGED
@@ -68,24 +68,48 @@
68
  return;
69
  }
70
 
71
- // Tokenizar la pregunta y el contexto
72
- const question = pregunta;
73
- const context = textoPDF;
74
-
75
- // Usar un modelo preentrenado como BERT o T5 de HuggingFace
76
- const response = await obtenerRespuestaDeModelo(question, context);
77
 
78
  // Mostrar la respuesta
79
- document.getElementById("respuesta").innerText = "Respuesta: " + response;
80
  }
81
 
82
- // Funci贸n para obtener respuesta utilizando el modelo de Hugging Face
83
- async function obtenerRespuestaDeModelo(question, context) {
84
- // En este ejemplo, aqu铆 puedes integrar una API de preguntas y respuestas
85
- // como Hugging Face o TensorFlow.js con BERT.
86
- // En esta versi贸n solo devolveremos una frase que coincida con el contexto.
87
- const fraseRespuesta = "Aqu铆 ir铆a la respuesta generada por el modelo (por ejemplo, usando BERT o T5)";
88
- return fraseRespuesta;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
  }
90
  </script>
91
  </body>
 
68
  return;
69
  }
70
 
71
+ // Enviar la pregunta y el texto del PDF a la API de Hugging Face para obtener la respuesta
72
+ const respuesta = await obtenerRespuestaDeModelo(pregunta, textoPDF);
 
 
 
 
73
 
74
  // Mostrar la respuesta
75
+ document.getElementById("respuesta").innerText = "Respuesta: " + respuesta;
76
  }
77
 
78
+ // Funci贸n para obtener respuesta utilizando el modelo BERT de Hugging Face
79
+ async function obtenerRespuestaDeModelo(pregunta, contexto) {
80
+ const endpoint = "https://api-inference.huggingface.co/models/deepset/roberta-large-squad2"; // Usar el modelo BERT o RoBERTa preentrenado
81
+ const token = 'tu_token_de_huggingface'; // Sustituye con tu token de Hugging Face
82
+
83
+ const requestData = {
84
+ inputs: {
85
+ question: pregunta,
86
+ context: contexto
87
+ }
88
+ };
89
+
90
+ try {
91
+ const response = await fetch(endpoint, {
92
+ method: 'POST',
93
+ headers: {
94
+ 'Authorization': `Bearer ${token}`,
95
+ 'Content-Type': 'application/json'
96
+ },
97
+ body: JSON.stringify(requestData)
98
+ });
99
+
100
+ const data = await response.json();
101
+
102
+ // Extraer la respuesta del modelo
103
+ if (data && data.answer) {
104
+ return data.answer;
105
+ } else {
106
+ return "No se pudo encontrar una respuesta.";
107
+ }
108
+
109
+ } catch (error) {
110
+ console.error("Error al obtener la respuesta de Hugging Face:", error);
111
+ return "Error al obtener la respuesta.";
112
+ }
113
  }
114
  </script>
115
  </body>