|
|
|
const translateText = async (text) => { |
|
console.log(text) |
|
const inferResponse = await fetch(`infer_t5?input=${text}`); |
|
const inferJson = await inferResponse.json(); |
|
console.log(inferJson.output) |
|
return inferJson.output; |
|
}; |
|
|
|
|
|
function generatePrompterAssistantText(inputString) { |
|
|
|
const sentences = inputString.split('<|endoftext|>'); |
|
|
|
|
|
let prompterText = []; |
|
let assistantText = []; |
|
|
|
|
|
for (let i = 0; i < sentences.length; i++) { |
|
|
|
if (sentences[i].includes('<|prompter|>')) { |
|
|
|
const prompterSentence = sentences[i].replace(/<\|prompter\|>/g, ''); |
|
prompterText.push(prompterSentence); |
|
} else if (sentences[i].includes('<|assistant|>')) { |
|
const assistantSentence = sentences[i].replace(/<\|assistant\|>/g, ''); |
|
|
|
assistantText.push(assistantSentence); |
|
} |
|
} |
|
|
|
|
|
return [prompterText, assistantText]; |
|
} |
|
|
|
const submitButton = document.querySelector('#submit') |
|
const outPutElement = document.querySelector('#output') |
|
const inputElement = document.querySelector('input') |
|
const historyElement = document.querySelector('.history') |
|
const buttonElement = document.querySelector('button') |
|
|
|
|
|
function changeInput(value) |
|
{ |
|
console.log(value) |
|
const inputElement = document.querySelector('input') |
|
inputElement.value = value |
|
} |
|
async function getMessage(){ |
|
|
|
const options = { |
|
method: "POST", |
|
headers: { |
|
Authorization: `Bearer ${API_KEY}`, |
|
"Content-Type": "application/json" |
|
}, |
|
body: JSON.stringify({ |
|
inputs: "<|prompter|>" + inputElement.value + "<|endoftext|><|assistant|>", |
|
parameters: {"max_new_tokens": 200, "temperature": 0.9} |
|
}) |
|
} |
|
try{ |
|
const response = await fetch("https://api-inference.huggingface.co/models/OpenAssistant/oasst-sft-4-pythia-12b-epoch-3.5", options); |
|
const data = await response.json() |
|
|
|
|
|
if(inputElement.value && data && data[0].generated_text){ |
|
const [prompterText, assistantText] = generatePrompterAssistantText(data[0].generated_text); |
|
|
|
|
|
|
|
|
|
outPutElement.textContent = await translateText(assistantText[0]); |
|
const pElement = document.createElement('p') |
|
pElement.textContent = inputElement.value |
|
pElement.addEventListener('click', () => changeInput(pElement.textContent)) |
|
historyElement.append(pElement) |
|
} |
|
} catch(error) { |
|
console.log(error) |
|
} |
|
} |
|
|
|
submitButton.addEventListener('click', getMessage) |
|
|
|
function clearInput(){ |
|
inputElement.value = '' |
|
} |
|
buttonElement.addEventListener('click', clearInput) |