p5tutorial2 / index.js
sarahciston's picture
test prompt input value
bc4bca9 verified
raw
history blame
3.33 kB
// IMPORT LIBRARIES TOOLS
import { pipeline, env } from 'https://cdn.jsdelivr.net/npm/@xenova/[email protected]';
// skip local model check
env.allowLocalModels = false;
// GLOBAL VARIABLES
var PROMPT_INPUT = `The vice president [MASK] after returning from war.` // a field for writing or changing a text value
var OUTPUT_LIST = [] // a blank array to store the results from the model
// RUN MODEL
async function fillInTask(){
console.log('fill-in task initiated')
const pipe = await pipeline('fill-mask', 'Xenova/bert-base-uncased');
var out = await pipe(PROMPT_INPUT);
console.log(await out) // yields { score, sequence, token, token_str } for each result
await out.forEach(o => {
console.log(o) // yields { score, sequence, token, token_str } for each result
OUTPUT_LIST.push(o.sequence) // put only the full sequence in a list
})
console.log(OUTPUT_LIST)
displayResults(OUTPUT_LIST)
console.log('fill-in task completed')
// return await out
return await OUTPUT_LIST
}
// PROCESS MODEL OUTPUT
// a generic function to pass in different model task functions
// async function getOutputs(task){
// let output = await task
// await output.forEach(o => {
// OUTPUT_LIST.push(o.sequence) // put only the full sequence in a list
// })
// console.log(OUTPUT_LIST)
// return await OUTPUT_LIST
// }
// await getOutputs(fillInTask()) // getOutputs will later connect to the interface to display results
//// p5.js Instance
new p5(function (p5){
p5.setup = function(){
p5.noCanvas()
console.log('p5 instance loaded')
makeTextDisplay()
makeFields()
makeButtons()
}
p5.draw = function(){
//
}
function makeTextDisplay(){
let title = p5.createElement('h1','p5.js Critical AI Prompt Battle')
let intro = p5.createP(`This tool lets you run several AI chat prompts at once and compare their results. Use it to explore what models 'know' about various concepts, communities, and cultures. For more information on prompt programming and critical AI, see [Tutorial & extra info][TO-DO][XXX]`)
}
function makeFields(){
PROMPT_INPUT = p5.createInput(PROMPT_INPUT).value(PROMPT_INPUT) // turns the string into an input; now access the text via PROMPT_INPUT.value()
PROMPT_INPUT.size(700)
PROMPT_INPUT.attribute('label', `Write a text prompt with at least one [MASK] that the model will fill in.`)
p5.createP(PROMPT_INPUT.attribute('label'))
PROMPT_INPUT.addClass("prompt")
console.log(PROMPT_INPUT.value())
}
function makeButtons(){
let submitButton = p5.createButton("SUBMIT")
submitButton.size(170)
submitButton.class('submit')
submitButton.mousePressed(fillInTask)
}
function displayResults(list){
console.log('displayed or pressed')
let outHead = p5.createElement('h4',"Results:")
let outText = p5.createP('')
outText.html(list)
}
// async function makeOutputDisplay(){
// console.log('button pressed')
// let outHead = p5.createElement('h4',"Results:")
// let out = await fillInTask() //just model no parsing
// // let out = await getOutputs(fillInTask()) // model and parsing
// out = str(await out)
// console.log(out)
// let outText = p5.createP('')
// await outText.html(out)
// }
});