const generateText = async () => { const textInput = document.getElementById("textInput").value; const maxTokens = document.getElementById("maxTokens").value; const doSample = document.getElementById("doSample").checked; const mode = document.querySelector('input[name="mode"]:checked').value; // Show loading state const outputElement = document.getElementById("output"); outputElement.innerText = "Processing..."; outputElement.classList.add("loading"); try { const response = await fetch("/generate", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ text: textInput, max_new_tokens: parseInt(maxTokens), do_sample: doSample, mode: mode, }), }); if (response.ok) { const data = await response.json(); outputElement.innerText = data.generated_text; outputElement.classList.remove("loading"); } else { outputElement.innerText = "Error: Unable to process request."; outputElement.classList.remove("loading"); } } catch (error) { outputElement.innerText = "Error: " + error.message; outputElement.classList.remove("loading"); } }; const predictNext = async () => { const textInput = document.getElementById("textInput").value; if (!textInput.trim()) { alert("Please enter some text first!"); return; } // Show loading state const predictionsElement = document.getElementById("predictions"); predictionsElement.innerHTML = "Loading predictions..."; try { const response = await fetch("/predict_next", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ text: textInput, }), }); if (response.ok) { const data = await response.json(); displayPredictions(data.predictions); } else { predictionsElement.innerHTML = "Error: Unable to get predictions."; } } catch (error) { predictionsElement.innerHTML = "Error: " + error.message; } }; const displayPredictions = (predictions) => { const predictionsElement = document.getElementById("predictions"); if (predictions.length === 0) { predictionsElement.innerHTML = "No predictions available."; return; } let html = '