Spaces:
Running
Running
Create sketch.js
Browse files
sketch.js
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Import the pipeline function from the transformers library via CDN link
|
| 2 |
+
//import { pipeline } from 'https://cdn.jsdelivr.net/npm/@xenova/[email protected]';
|
| 3 |
+
|
| 4 |
+
import { pipeline, env } from "https://cdn.jsdelivr.net/npm/@xenova/[email protected]";
|
| 5 |
+
// Declare variables to hold the instances of the summarizer, generator, and classifier
|
| 6 |
+
let summarizer, generator, classifier;
|
| 7 |
+
env.allowLocalModels = false;
|
| 8 |
+
// Add an event listener to the window object that will execute the following function once the window is fully loaded
|
| 9 |
+
window.addEventListener('load', () => {
|
| 10 |
+
// Add click event listeners to buttons with specific IDs, binding them to their respective functions
|
| 11 |
+
|
| 12 |
+
document.getElementById('generate').addEventListener('click', generate);
|
| 13 |
+
|
| 14 |
+
});
|
| 15 |
+
|
| 16 |
+
// Grab DOM elements for updating status and output messages to the user
|
| 17 |
+
const statusElement = document.getElementById('status');
|
| 18 |
+
const outputElement = document.getElementById('output');
|
| 19 |
+
|
| 20 |
+
// Define an asynchronous function to generate text
|
| 21 |
+
async function generate() {
|
| 22 |
+
console.log("hello")
|
| 23 |
+
// Retrieve the input text from the DOM
|
| 24 |
+
let text = document.getElementById('inputText').value;
|
| 25 |
+
// If the generator model has not been loaded yet, load it and update the status
|
| 26 |
+
if (!generator) {
|
| 27 |
+
console.log("loading generate")
|
| 28 |
+
updateStatus('Loading generation model...');
|
| 29 |
+
generator = await pipeline(
|
| 30 |
+
'text2text-generation',
|
| 31 |
+
'Xenova/LaMini-Flan-T5-783M'
|
| 32 |
+
);
|
| 33 |
+
}
|
| 34 |
+
// Update status and run the generator model on the input text
|
| 35 |
+
updateStatus('Generating...');
|
| 36 |
+
console.log("generate")
|
| 37 |
+
let output = await generator(text, {
|
| 38 |
+
max_new_tokens: 100, // Set the maximum number of new tokens for the generated text
|
| 39 |
+
});
|
| 40 |
+
// Log and display the generation results
|
| 41 |
+
console.log(output);
|
| 42 |
+
updateOutput(output[0]);
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
// Function to update the status text in the DOM
|
| 46 |
+
function updateStatus(message) {
|
| 47 |
+
statusElement.textContent = 'Status: ' + message;
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
// Function to update the output text in the DOM and set the status back to 'Ready' once done
|
| 51 |
+
function updateOutput(message) {
|
| 52 |
+
outputElement.innerHTML = message;
|
| 53 |
+
updateStatus('Ready');
|
| 54 |
+
}
|