Spaces:
Runtime error
Runtime error
Commit
·
95bc175
1
Parent(s):
a973dfb
Update static/main.js
Browse files- static/main.js +56 -9
static/main.js
CHANGED
|
@@ -1,38 +1,85 @@
|
|
|
|
|
| 1 |
const generateButton = document.getElementById('generate');
|
| 2 |
-
// console.log("Hello, world!");
|
| 3 |
generateButton.addEventListener('click', async () => {
|
|
|
|
|
|
|
| 4 |
const accountSelect = document.getElementById('account');
|
| 5 |
const account = accountSelect.options[accountSelect.selectedIndex].value;
|
| 6 |
const inputText = document.getElementById('input').value;
|
| 7 |
|
|
|
|
| 8 |
const outputTextArea = document.getElementById('output');
|
| 9 |
outputTextArea.value = 'Loading...';
|
| 10 |
|
|
|
|
| 11 |
try {
|
| 12 |
const response = await fetch('/api/generate', {
|
| 13 |
method: 'POST',
|
| 14 |
headers: { 'Content-Type': 'application/json' },
|
| 15 |
body: JSON.stringify({ account: account, text: inputText })
|
| 16 |
});
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
| 19 |
outputTextArea.value = 'Generating a reply...';
|
|
|
|
|
|
|
|
|
|
| 20 |
}
|
|
|
|
|
|
|
| 21 |
await new Promise(resolve => setTimeout(resolve, 1000));
|
|
|
|
|
|
|
| 22 |
if (!response.ok) {
|
|
|
|
| 23 |
throw new Error(`Network response was not ok. Status: ${response.status} - ${response.statusText}`);
|
| 24 |
}
|
| 25 |
-
|
| 26 |
-
// First step update: POST request successful
|
| 27 |
-
outputTextArea.value = 'Model is running...';
|
| 28 |
|
|
|
|
| 29 |
const data = await response.json();
|
| 30 |
const outputText = data.generated_text;
|
| 31 |
-
|
| 32 |
-
//
|
|
|
|
|
|
|
|
|
|
| 33 |
outputTextArea.value = outputText;
|
| 34 |
} catch (error) {
|
|
|
|
| 35 |
console.error('Error:', error);
|
| 36 |
outputTextArea.value = `Failed to fetch data. Reason: ${error.message}`;
|
| 37 |
}
|
| 38 |
-
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Get the generate button and add an event listener for the click event
|
| 2 |
const generateButton = document.getElementById('generate');
|
|
|
|
| 3 |
generateButton.addEventListener('click', async () => {
|
| 4 |
+
|
| 5 |
+
// Get the account select element and the input text
|
| 6 |
const accountSelect = document.getElementById('account');
|
| 7 |
const account = accountSelect.options[accountSelect.selectedIndex].value;
|
| 8 |
const inputText = document.getElementById('input').value;
|
| 9 |
|
| 10 |
+
// Set the output textarea to "Loading..."
|
| 11 |
const outputTextArea = document.getElementById('output');
|
| 12 |
outputTextArea.value = 'Loading...';
|
| 13 |
|
| 14 |
+
// Make a POST request to the /api/generate endpoint
|
| 15 |
try {
|
| 16 |
const response = await fetch('/api/generate', {
|
| 17 |
method: 'POST',
|
| 18 |
headers: { 'Content-Type': 'application/json' },
|
| 19 |
body: JSON.stringify({ account: account, text: inputText })
|
| 20 |
});
|
| 21 |
+
|
| 22 |
+
// Check the response status code
|
| 23 |
+
if (response.ok) {
|
| 24 |
+
// The request was successful
|
| 25 |
outputTextArea.value = 'Generating a reply...';
|
| 26 |
+
} else {
|
| 27 |
+
// The request failed
|
| 28 |
+
throw new Error(`Network response was not ok. Status: ${response.status} - ${response.statusText}`);
|
| 29 |
}
|
| 30 |
+
|
| 31 |
+
// Wait 1 second
|
| 32 |
await new Promise(resolve => setTimeout(resolve, 1000));
|
| 33 |
+
|
| 34 |
+
// Check the response status code again
|
| 35 |
if (!response.ok) {
|
| 36 |
+
// The request still failed
|
| 37 |
throw new Error(`Network response was not ok. Status: ${response.status} - ${response.statusText}`);
|
| 38 |
}
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
+
// The request was successful, so get the data
|
| 41 |
const data = await response.json();
|
| 42 |
const outputText = data.generated_text;
|
| 43 |
+
|
| 44 |
+
// Log the generated text
|
| 45 |
+
console.log("Generated text", outputText.toString());
|
| 46 |
+
|
| 47 |
+
// Set the output textarea to the generated text
|
| 48 |
outputTextArea.value = outputText;
|
| 49 |
} catch (error) {
|
| 50 |
+
// The request failed, so log the error
|
| 51 |
console.error('Error:', error);
|
| 52 |
outputTextArea.value = `Failed to fetch data. Reason: ${error.message}`;
|
| 53 |
}
|
| 54 |
+
});
|
| 55 |
+
|
| 56 |
+
document.getElementById("generate-summary").addEventListener("click", async () => {
|
| 57 |
+
try {
|
| 58 |
+
const response = await fetch("/api/generate_summary", {
|
| 59 |
+
method: "POST",
|
| 60 |
+
headers: { "Content-Type": "application/json" },
|
| 61 |
+
body: JSON.stringify({ text: ["text1", "text2", "text3"] }),
|
| 62 |
+
});
|
| 63 |
+
|
| 64 |
+
if (!response.ok) {
|
| 65 |
+
throw new Error(
|
| 66 |
+
`Network response was not ok. Status: ${response.status} - ${response.statusText}`
|
| 67 |
+
);
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
const data = await response.json();
|
| 71 |
+
console.log(data)
|
| 72 |
+
const summary = data.summary;
|
| 73 |
+
|
| 74 |
+
const outputSummaryArea = document.getElementById("output-summary");
|
| 75 |
+
|
| 76 |
+
// Set the output summary area to the generated summary
|
| 77 |
+
outputSummaryArea.value = summary;
|
| 78 |
+
|
| 79 |
+
// Log the output summary area value
|
| 80 |
+
console.log(outputSummaryArea.value);
|
| 81 |
+
} catch (error) {
|
| 82 |
+
console.error("Error:", error);
|
| 83 |
+
}
|
| 84 |
+
});
|
| 85 |
+
|