Sarah Ciston
try inference version
cc195e8
raw
history blame
10.4 kB
// connect to API via module
// import { AutoTokenizer, env } from 'https://cdn.jsdelivr.net/npm/@xenova/transformers';
// import { pipeline, env } from 'https://cdn.jsdelivr.net/npm/@xenova/transformers';
/// AUTHORIZATION
import { oauthLoginUrl, oauthHandleRedirectIfPresent } from 'https://esm.sh/@huggingface/hub';
const oauthResult = await oauthHandleRedirectIfPresent();
if (!oauthResult) {
// If the user is not logged in, redirect to the login page
window.location.href = await oauthLoginUrl();
}
// You can use oauthResult.accessToken, oauthResult.accessTokenExpiresAt and oauthResult.userInfo
// console.log(oauthResult);
const HFAUTH = oauthResult.accessToken
console.log(HFAUTH)
import { HfInference } from 'https://esm.sh/@huggingface/inference';
const inference = new HfInference(HFAUTH);
// PIPELINE MODELS
// models('Xenova/gpt2', 'Xenova/gpt-3.5-turbo', 'mistralai/Mistral-7B-Instruct-v0.2', 'Xenova/llama-68m', 'meta-llama/Meta-Llama-3-8B', 'Xenova/bloom-560m', 'Xenova/distilgpt2')
// list of models by task: 'https://huggingface.co/docs/transformers.js/index#supported-tasksmodels'
// Since we will download the model from the Hugging Face Hub, we can skip the local model check
// env.allowLocalModels = false;
///////// VARIABLES
// establish global variables to reference later
var promptInput
var blanksArray = []
// pick a model (see list of models)
// INFERENCE MODELS
// let MODELNAME = "mistralai/Mistral-7B-Instruct-v0.2";
// models('Xenova/gpt2', 'Xenova/gpt-3.5-turbo', 'mistralai/Mistral-7B-Instruct-v0.2', 'Xenova/llama-68m', "meta-llama/Meta-Llama-3-70B-Instruct", 'meta-llama/Meta-Llama-3-8B', 'Xenova/bloom-560m', 'Xenova/distilgpt2', "meta-llama/Meta-Llama-3-70B-Instruct")
// const detector = await pipeline('text-generation', 'meta-llama/Meta-Llama-3-8B', 'Xenova/LaMini-Flan-T5-783M');
///// p5 STUFF
// create an instance of the p5 class as a workspace for all your p5.js code
new p5(function (p5) {
p5.setup = function(){
console.log('p5 loaded')
p5.noCanvas()
makeInterface()
}
p5.draw = function(){
//
}
window.onload = function(){
console.log('dom and js loaded')
}
let fieldsDiv = document.querySelector("#blanks")
function makeInterface(){
console.log('reached makeInterface')
let title = p5.createElement('h1', 'p5.js Critical AI Prompt Battle')
// title.position(0,50)
p5.createElement('p',`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]`)
// .position(0,100)
promptInput = p5.createInput("")
// promptInput.position(0,160)
promptInput.size(600);
promptInput.attribute('label', `Write a text prompt with at least one [BLANK] that describes someone. You can also write [FILL] where you want the bot to fill in a word on its own.`)
promptInput.value(`The [BLANK] works as a [FILL] but wishes for...`)
promptInput.addClass("prompt")
p5.createP(promptInput.attribute('label'))
// .position(0,100)
//make for loop to generate
//make a button to make another
//add them to the list of items
fieldsDiv = p5.createDiv()
fieldsDiv.id('fieldsDiv')
// fieldsDiv.position(0,250)
// initial code to make a single field
// blankA = p5.createInput("");
// blankA.position(0, 240);
// blankA.size(300);
// blankA.addClass("blank")
// blankA.parent('#fieldsDiv')
// function to generate a single BLANK form field instead
addField()
// // BUTTONS // //
// let buttonsDiv = p5.createDiv() // container to organize buttons
// buttonsDiv.id('buttonsDiv')
// send prompt to model
let submitButton = p5.createButton("SUBMIT")
// submitButton.position(0,500)
submitButton.size(170)
submitButton.class('submit');
// submitButton.parent('#buttonsDiv')
submitButton.mousePressed(getInputs)
// add more blanks to fill in
let addButton = p5.createButton("more blanks")
addButton.size(170)
// addButton.position(220,500)
// addButton.parent('#buttonsDiv')
addButton.mousePressed(addField)
// TO-DO a model drop down list?
// alt-text description
// p5.describe(`Pink and black text on a white background with form inputs and two buttons. The text describes a p5.js Critical AI Prompt Battle tool that 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. In the largest form input you can write a prompt to submit. In smaller inputs, you can write variables that will be inserted into that prompt as variations of the prompt when it is run through the model. There is a submit button, a button to add more variations, and when the model is run it adds text at the bottom showing the output results.`)
}
function addField(){
let f = p5.createInput("")
f.class("blank")
f.parent("#fieldsDiv")
// DOES THIS WORK???????????????????
blanksArray.push(f)
console.log("made field")
// Cap the number of fields, avoids token limit in prompt
let blanks = document.querySelectorAll(".blank")
if (blanks.length > 7){
console.log(blanks.length)
addButton.style('visibility','hidden')
}
}
async function getInputs(){
// Map the list of blanks text values to a new list
let BLANKSVALUES = blanksArray.map(i => i.value())
console.log(BLANKSVALUES)
// Do model stuff in this function instead of in general
let PROMPT = promptInput.value() // updated check of the prompt field
// BLANKS = inputValues // get ready to feed array list into model
let PREPROMPT = `In the sentence I provide, please fill in the [BLANK] with each word in the array ${BLANKSVALUES}, replace any [FILL] with a word of your choice. Here is the SAMPLE SENTENCE: `
// we pass PROMPT and PREPROMPT to the model function, don't need to pass BLANKSVALUES bc it's passed into the PREPROMPT already here
// Please return an array of sentences based on the sample sentence to follow. In each sentence,
let modelResult = await runModel(PREPROMPT, PROMPT)
await displayModel(modelResult)
}
async function displayModel(m){
let modelDisplay = p5.createElement("p", "Results:");
await modelDisplay.html(m)
}
});
///// MODEL STUFF
async function runModel(PREPROMPT, PROMPT){
// inference API version
let MODELNAME = "HuggingFaceH4/zephyr-7b-beta"
// let MODELNAME = "openai-community/gpt2"
// let MODELNAME = 'mistral_inference'
let out = await inference.textGeneration({
model: MODELNAME,
messages: [{
role: "system",
content: PREPROMPT
},{
role: "user",
content: PROMPT
}],
max_new_tokens: 128
});
console.log(out)
// modelResult = await out.messages[0].content
var modelResult = await out.choices[0].message.content
// var modelResult = await out[0].generated_text
console.log(modelResult);
return modelResult
}
//inference.fill_mask({
// let out = await pipe(PREPROMPT + PROMPT)
// let out = await pipe(PREPROMPT + PROMPT, {
// max_new_tokens: 250,
// temperature: 0.9,
// // return_full_text: False,
// repetition_penalty: 1.5,
// // no_repeat_ngram_size: 2,
// // num_beams: 2,
// num_return_sequences: 1
// });
// var PROMPT = `The [BLANK] works as a [blank] but wishes for [blank].`
// /// this needs to run on button click, use string variables to blank in the form
// var PROMPT = promptInput.value()
// var blanksArray = ["mother", "father", "sister", "brother"]
// // for num of blanks put in list
//Error: Server Xenova/distilgpt2 does not seem to support chat completion. Error: HfApiJson(Deserialize(Error("unknown variant `transformers.js`, expected one of `text-generation-inference`, `transformers`, `allennlp`, `flair`, `espnet`, `asteroid`, `speechbrain`, `timm`, `sentence-transformers`, `spacy`, `sklearn`, `stanza`, `adapter-transformers`, `fasttext`, `fairseq`, `pyannote-audio`, `doctr`, `nemo`, `fastai`, `k2`, `diffusers`, `paddlenlp`, `mindspore`, `open_clip`, `span-marker`, `bertopic`, `peft`, `setfit`", line: 1, column: 397)))
// async function runModel(PREPROMPT, PROMPT){
// // // pipeline version
// // let MODELNAME = 'mistralai/Mistral-Nemo-Instruct-2407'
// let MODELNAME = "HuggingFaceH4/zephyr-7b-beta"
// // HFAUTH
// // 'meta-llama/Meta-Llama-3-70B-Instruct'
// // 'openai-community/gpt2'
// // 'Xenova/gpt-3.5-turbo'
// // , 'Xenova/distilgpt2'
// // 'mistralai/Mistral-7B-Instruct-v0.2'
// // 'HuggingFaceH4/zephyr-7b-beta'
// // pipeline/transformers version
// let pipe = await pipeline('text-generation', {
// model: MODELNAME,
// accessToken: HFAUTH
// });
// // seems to work with default model distilgpt2 ugh
// // let out = await pipe(inputText, {
// // max_tokens: 250,
// // return_full_text: false
// // // repetition_penalty: 1.5,
// // // num_return_sequences: 1 //must be 1 for greedy search
// // })
// // let inputText = PREPROMPT + PROMPT
// // let out = await pipe(inputText)
// let out = await pipe({
// messages: [{
// role: "system",
// content: PREPROMPT
// },{
// role: "user",
// content: PROMPT
// }],
// max_new_tokens: 100
// });
// console.log(out)
// var modelResult = await out.choices[0].message.content
// // var modelResult = await out[0].generated_text
// console.log(modelResult)
// return modelResult
// }