Arsala Grey
added temp param and restructured ui
c2d5ffe
raw
history blame
3.23 kB
const { createApp, ref, onMounted, computed } = Vue;
import { HfInference } from "https://cdn.skypack.dev/@huggingface/inference@latest";
const app = createApp({
setup() {
const token = ref(localStorage.getItem("token") || "");
const userPrompt = ref("Write about the difference between AI Engineering");
const currentGeneratedText = ref("");
const models = ref(["mistralai/Mistral-7B-v0.1",
"google/flan-t5-xxl"]);
const selectedModel = ref("mistralai/Mistral-7B-v0.1");
const isRunning = ref(false);
const responseLength = ref("150");
const temperature = ref("1.0");
const generating = ref(false);
let controller;
const statusMessage = computed(() => {
if (generating.value) return "Generating..."
return "Ready"
})
const createTextGenerationStream = (hfInstance, prompt, abortControllerSignal) => {
return hfInstance.textGenerationStream(
{
model: selectedModel.value,
inputs: prompt,
parameters: {
max_new_tokens: parseInt(responseLength.value),
temperature: parseFloat(temperature.value),
},
},
{
use_cache: false,
signal: abortControllerSignal,
}
);
};
const generateTextStream = async function* (hfInstance, abortSignal, prompt) {
let generatedText = ""
generating.value = true
for await (const output of createTextGenerationStream(hfInstance, prompt, abortSignal)) {
generatedText += output.token.text;
yield generatedText;
}
generating.value = false
};
const run = async () => {
isRunning.value = true;
currentGeneratedText.value = "";
controller = new AbortController();
localStorage.setItem("token", token.value);
const hfInstance = new HfInference(token.value);
try {
for await (const textStream of generateTextStream(
hfInstance,
controller.signal,
userPrompt.value
)) {
currentGeneratedText.value = textStream;
}
} catch (e) {
console.log(e);
}
};
const stop = () => {
if (controller) {
controller.abort();
}
isRunning.value = false;
};
onMounted(async () => {
const localStorageToken = localStorage.getItem("token")
if (localStorageToken) {
token.value = localStorageToken;
}
});
return {
token,
userPrompt,
currentGeneratedText,
run,
stop,
models,
selectedModel,
isRunning,
responseLength,
temperature,
statusMessage
};
},
});
app.mount("#app");