jasleen02's picture
Update index.html
74b3e32 verified
Raw History Blame Contribute Delete
3.25 kB
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Swedish → English Translator</title>
<script
type="module"
crossorigin
src="https://cdn.jsdelivr.net/npm/@gradio/lite/dist/lite.js">
</script>
<link
rel="stylesheet"
crossorigin
href="https://cdn.jsdelivr.net/npm/@gradio/lite/dist/lite.css">
<style>
body {
max-width: 900px;
margin: 0 auto;
padding: 24px;
font-family: Arial, sans-serif;
}
#status {
color: #555;
margin: 8px 0 18px;
}
#error {
color: #b00020;
white-space: pre-wrap;
margin-top: 12px;
}
textarea {
width: 100%;
min-height: 120px;
padding: 12px;
box-sizing: border-box;
font-size: 16px;
}
button {
margin: 14px 0;
padding: 10px 18px;
font-size: 16px;
cursor: pointer;
}
</style>
</head>
<body>
<h1>English → Swedish Translator</h1>
<p>
This translation model runs directly in your browser.
The first translation can take a little while because the model must download.
</p>
<p id="status">Loading translation model…</p>
<label for="input">Swedish text</label>
<textarea
id="input"
placeholder="Write an Swedish sentence here..."
disabled
></textarea>
<br>
<button id="translate-button" disabled>Translate</button>
<label for="output">English translation</label>
<textarea id="output" readonly></textarea>
<p id="error"></p>
<script type="module">
import { pipeline, env } from
"https://cdn.jsdelivr.net/npm/@huggingface/transformers@3.0.2";
const status = document.getElementById("status");
const input = document.getElementById("input");
const output = document.getElementById("output");
const button = document.getElementById("translate-button");
const errorBox = document.getElementById("error");
let translator;
async function loadModel() {
try {
env.allowLocalModels = false;
translator = await pipeline(
"translation",
"Xenova/opus-mt-sv-en"
);
status.textContent = "Model ready. Enter Swedish text and click Translate.";
input.disabled = false;
button.disabled = false;
} catch (error) {
console.error(error);
status.textContent = "The translation model could not load.";
errorBox.textContent = String(error);
}
}
button.addEventListener("click", async () => {
const text = input.value.trim();
if (!text) {
output.value = "Please write something first.";
return;
}
button.disabled = true;
status.textContent = "Translating…";
errorBox.textContent = "";
try {
const result = await translator(text);
output.value = result[0].translation_text;
status.textContent = "Done.";
} catch (error) {
console.error(error);
status.textContent = "Translation failed.";
errorBox.textContent = String(error);
} finally {
button.disabled = false;
}
});
loadModel();
</script>
</body>
</html>