File size: 1,734 Bytes
a04eea2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
document.addEventListener("DOMContentLoaded", () => {
    const textClassForm = document.querySelector(".text-class-form");
  
    const classifyText = async (text) => {
        try {
            // Ensure the endpoint matches your FastAPI setup, including the base URL if needed
            const response = await fetch(`/classify_text?input=${encodeURIComponent(text)}`);
            if (!response.ok) {
                throw new Error('Network response was not ok.');
            }
            const data = await response.json();
            return data; // Return the entire response data
        } catch (error) {
            console.error("Error:", error);
            return { sentiment: "Failed to classify text. Please try again later." };
        }
    };
  
    textClassForm.addEventListener("submit", async (event) => {
        event.preventDefault();
        const textClassInput = document.getElementById("text-class-input");
        const sentimentParagraph = document.querySelector(".text-class-output");
        const probabilityParagraph = document.querySelector(".probability-output"); // Add a selector for displaying probabilities
  
        // Display a loading message or similar feedback
        sentimentParagraph.textContent = "Classifying text...";
  
        const { sentiment, positive_probability, negative_probability } = await classifyText(textClassInput.value);
        
        // Update the text content with the classification result and probabilities
        sentimentParagraph.textContent = `Sentiment: ${sentiment}`;
        probabilityParagraph.textContent = `Positive Probability: ${positive_probability.toFixed(2)}, Negative Probability: ${negative_probability.toFixed(2)}`;
    });
  });