Spaces:
Runtime error
Runtime error
File size: 2,843 Bytes
95bc175 8158335 95bc175 8158335 95bc175 8158335 acf96b5 7020549 95bc175 8158335 acf96b5 95bc175 acf96b5 95bc175 acf96b5 95bc175 acf96b5 95bc175 8158335 95bc175 8158335 95bc175 acf96b5 8158335 95bc175 acf96b5 8158335 95bc175 8158335 95bc175 |
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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
// Get the generate button and add an event listener for the click event
const generateButton = document.getElementById('generate');
generateButton.addEventListener('click', async () => {
// Get the account select element and the input text
const accountSelect = document.getElementById('account');
const account = accountSelect.options[accountSelect.selectedIndex].value;
const inputText = document.getElementById('input').value;
// Set the output textarea to "Loading..."
const outputTextArea = document.getElementById('output');
outputTextArea.value = 'Loading...';
// Make a POST request to the /api/generate endpoint
try {
const response = await fetch('/api/generate', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ account: account, text: inputText })
});
// Check the response status code
if (response.ok) {
// The request was successful
outputTextArea.value = 'Generating a reply...';
} else {
// The request failed
throw new Error(`Network response was not ok. Status: ${response.status} - ${response.statusText}`);
}
// Wait 1 second
await new Promise(resolve => setTimeout(resolve, 1000));
// Check the response status code again
if (!response.ok) {
// The request still failed
throw new Error(`Network response was not ok. Status: ${response.status} - ${response.statusText}`);
}
// The request was successful, so get the data
const data = await response.json();
const outputText = data.generated_text;
// Log the generated text
console.log("Generated text", outputText.toString());
// Set the output textarea to the generated text
outputTextArea.value = outputText;
} catch (error) {
// The request failed, so log the error
console.error('Error:', error);
outputTextArea.value = `Failed to fetch data. Reason: ${error.message}`;
}
});
document.getElementById("generate-summary").addEventListener("click", async () => {
try {
const response = await fetch("/api/generate_summary", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ text: ["text1", "text2", "text3"] }),
});
if (!response.ok) {
throw new Error(
`Network response was not ok. Status: ${response.status} - ${response.statusText}`
);
}
const data = await response.json();
console.log(data)
const summary = data.summary;
const outputSummaryArea = document.getElementById("output-summary");
// Set the output summary area to the generated summary
outputSummaryArea.value = summary;
// Log the output summary area value
console.log(outputSummaryArea.value);
} catch (error) {
console.error("Error:", error);
}
});
|