hfjsdemoA / translation.html
boazchung's picture
Update translation.html
08a1859 verified
raw
history blame
4.2 kB
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script src="https://cdn.tailwindcss.com"></script>
<!-- polyfill for firefox + import maps -->
<script src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
<script type="importmap">
{
"imports": {
"@huggingface/inference": "https://cdn.jsdelivr.net/npm/@huggingface/[email protected]/+esm"
}
}
</script>
</head>
<body>
<form class="w-[90%] mx-auto pt-8" onsubmit="launch(); return false;">
<h1 class="text-3xl font-bold">
<span
class="bg-clip-text text-transparent bg-gradient-to-r from-pink-500 to-violet-500"
>
Translation demo with
<a href="https://github.com/huggingface/huggingface.js">
<kbd>@huggingface/inference</kbd>
</a>
</span>
</h1>
<p class="mt-8">
First, input your token if you have one! Otherwise, you may encounter
rate limiting. You can create a token for free at
<a
target="_blank"
href="https://huggingface.co/settings/tokens"
class="underline text-blue-500"
>hf.co/settings/tokens</a
>
</p>
<input
type="text"
id="token"
class="rounded border-2 border-blue-500 shadow-md px-3 py-2 w-96 mt-6"
placeholder="token (optional)"
/>
<p class="mt-8">
Pick the model you want to run. Check out over 10k models for text to
text generation
<a
href="https://huggingface.co/models?pipeline_tag=text2text-generation&sort=likes"
class="underline text-blue-500"
target="_blank"
>
here</a
>
</p>
<!-- Default model: https://huggingface.co/google/flan-t5-xxl -->
<input
type="text"
id="model"
class="rounded border-2 border-blue-500 shadow-md px-3 py-2 w-96 mt-6"
value="t5-base"
required
/>
<p class="mt-8">Finally the prompt</p>
<textarea
class="rounded border-blue-500 shadow-md px-3 py-2 w-96 mt-6 block"
rows="5"
id="prompt"
>My name is Wolfgang and I live in Amsterdam
</textarea>
<button
id="submit"
class="my-8 bg-green-500 rounded py-3 px-5 text-white shadow-md disabled:bg-slate-300"
>
Run
</button>
<p class="text-gray-400 text-sm">Translation</p>
<div id="logs" class="bg-gray-100 rounded p-3 mb-8 text-sm">
Output will be here
</div>
<p>Check out the <a class="underline text-blue-500" href="https://huggingface.co/spaces/huggingfacejs/streaming-text-generation/blob/main/index.html" target="_blank">source code</a></p>
</form>
<script type="module">
import { HfInference } from "@huggingface/inference";
let running = false;
async function launch() {
if (running) {
return;
}
running = true;
try {
const hf = new HfInference(
document.getElementById("token").value.trim() || undefined
);
const model = document.getElementById("model").value.trim();
const prompt = document.getElementById("prompt").value.trim();
document.getElementById("logs").textContent = "";
//await (let result of hf.tanslation({
let result = await hf.translation({
model,
inputs: prompt
}, {
use_cache: false
}); //{
//document.getElementById("logs").textContent += output.token.text;
document.getElementById("logs").innerText = JSON.stringify(result, null, 2);
//}
} catch (err) {
alert("Error: " + err.message);
} finally {
running = false;
}
}
window.launch = launch;
/*
async function translateText() {
const textFieldValue = document.getElementById("translationText").value.trim();
let result = await translator(textFieldValue, {
src_lang: 'hin_Deva', // Hindi
tgt_lang: 'fra_Latn', // French
});
document.getElementById("outputArea").innerText = JSON.stringify(result, null, 2);
}
await inference.translation({
model: 't5-base',
inputs: 'My name is Wolfgang and I live in Amsterdam'
})
*/
</script>
</body>
</html>