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)}`; }); });