File size: 1,228 Bytes
c0ad4b9
 
4564178
bff9622
c0ad4b9
bff9622
c0ad4b9
 
 
 
 
 
 
 
 
 
4564178
c0ad4b9
 
bff9622
 
c0ad4b9
4564178
c0ad4b9
bff9622
4564178
bff9622
c0ad4b9
 
4564178
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
document.addEventListener("DOMContentLoaded", () => {
  const textGenForm = document.querySelector(".text-gen-form");

  const translateText = async (question, context) => {
      try {
          const response = await fetch(`generate-text?question=${encodeURIComponent(question)}&context=${encodeURIComponent(context)}`);
          if (!response.ok) {
              throw new Error('Network response was not ok.');
          }
          const data = await response.json();
          return data.output;
      } catch (error) {
          console.error("Error:", error);
          return "Failed to generate text. Please try again later.";
      }
  };

  textGenForm.addEventListener("submit", async (event) => {
      event.preventDefault();
      const textGenContext = document.getElementById("text-gen-context");
      const textGenQuestion = document.getElementById("text-gen-question");
      const textGenParagraph = document.querySelector(".text-gen-output");

      // Display a loading message or similar feedback
      textGenParagraph.textContent = "Generating answer...";

      const output = await translateText(textGenQuestion.value, textGenContext.value);
      textGenParagraph.textContent = output;
  });
});