178 lines
5.8 KiB
HTML
178 lines
5.8 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>WebLLM Translation Demo</title>
|
|
<script type="module">
|
|
import { CreateMLCEngine } from "https://esm.run/@mlc-ai/web-llm";
|
|
|
|
let engine = null;
|
|
const statusDiv = document.getElementById('status');
|
|
const outputDiv = document.getElementById('output');
|
|
|
|
async function initEngine() {
|
|
statusDiv.textContent = "Initializing WebLLM engine (first time: ~2GB download)...";
|
|
|
|
try {
|
|
engine = await CreateMLCEngine("Llama-3.1-8B-Instruct-q4f32_1-MLC", {
|
|
initProgressCallback: (progress) => {
|
|
statusDiv.textContent = `Loading: ${progress.text}`;
|
|
}
|
|
});
|
|
|
|
statusDiv.textContent = "✅ WebLLM engine ready!";
|
|
document.getElementById('translate-btn').disabled = false;
|
|
} catch (error) {
|
|
statusDiv.textContent = `❌ Error: ${error.message}`;
|
|
}
|
|
}
|
|
|
|
async function translateText() {
|
|
const inputText = document.getElementById('input-text').value;
|
|
const targetLang = document.getElementById('target-lang').value;
|
|
|
|
if (!inputText) {
|
|
alert('Please enter text to translate');
|
|
return;
|
|
}
|
|
|
|
if (!engine) {
|
|
alert('Engine not ready. Please wait for initialization.');
|
|
return;
|
|
}
|
|
|
|
statusDiv.textContent = "Translating...";
|
|
outputDiv.textContent = "";
|
|
|
|
const prompt = `Translate the following text to ${targetLang}. Return ONLY the translation:\n\n${inputText}`;
|
|
|
|
try {
|
|
const reply = await engine.chat.completions.create({
|
|
messages: [{ role: "user", content: prompt }],
|
|
temperature: 0.3,
|
|
max_tokens: 1000,
|
|
});
|
|
|
|
const translation = reply.choices[0].message.content;
|
|
outputDiv.textContent = translation;
|
|
statusDiv.textContent = "✅ Translation complete!";
|
|
} catch (error) {
|
|
statusDiv.textContent = `❌ Translation error: ${error.message}`;
|
|
}
|
|
}
|
|
|
|
// Auto-init on page load
|
|
window.addEventListener('DOMContentLoaded', initEngine);
|
|
window.translateText = translateText;
|
|
</script>
|
|
<style>
|
|
body {
|
|
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
|
|
max-width: 800px;
|
|
margin: 50px auto;
|
|
padding: 20px;
|
|
background: #f5f7fa;
|
|
}
|
|
.container {
|
|
background: white;
|
|
padding: 30px;
|
|
border-radius: 8px;
|
|
box-shadow: 0 1px 3px rgba(0,0,0,0.1);
|
|
}
|
|
h1 {
|
|
color: #1a202c;
|
|
margin-bottom: 10px;
|
|
}
|
|
.info {
|
|
background: #e0f2ff;
|
|
padding: 12px;
|
|
border-radius: 6px;
|
|
margin-bottom: 20px;
|
|
font-size: 14px;
|
|
color: #1e40af;
|
|
}
|
|
textarea {
|
|
width: 100%;
|
|
padding: 12px;
|
|
border: 1px solid #cbd5e0;
|
|
border-radius: 6px;
|
|
font-size: 14px;
|
|
min-height: 150px;
|
|
margin-bottom: 15px;
|
|
}
|
|
select {
|
|
width: 100%;
|
|
padding: 10px;
|
|
border: 1px solid #cbd5e0;
|
|
border-radius: 6px;
|
|
font-size: 14px;
|
|
margin-bottom: 15px;
|
|
}
|
|
button {
|
|
background: #2563eb;
|
|
color: white;
|
|
padding: 10px 24px;
|
|
border: none;
|
|
border-radius: 6px;
|
|
font-size: 14px;
|
|
cursor: pointer;
|
|
width: 100%;
|
|
}
|
|
button:hover:not(:disabled) {
|
|
background: #1e40af;
|
|
}
|
|
button:disabled {
|
|
opacity: 0.5;
|
|
cursor: not-allowed;
|
|
}
|
|
#status {
|
|
margin-top: 15px;
|
|
padding: 12px;
|
|
background: #f7fafc;
|
|
border-radius: 6px;
|
|
font-size: 14px;
|
|
min-height: 20px;
|
|
}
|
|
#output {
|
|
margin-top: 15px;
|
|
padding: 15px;
|
|
background: #f0fdf4;
|
|
border: 1px solid #10b981;
|
|
border-radius: 6px;
|
|
white-space: pre-wrap;
|
|
min-height: 100px;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="container">
|
|
<h1>WebLLM Translation Demo</h1>
|
|
<div class="info">
|
|
<strong>Info:</strong> This demo runs entirely in your browser using WebGPU. First load will download ~2GB model.
|
|
</div>
|
|
|
|
<label for="input-text">Text to translate:</label>
|
|
<textarea id="input-text" placeholder="Enter text here...">Hello, how are you today?</textarea>
|
|
|
|
<label for="target-lang">Target language:</label>
|
|
<select id="target-lang">
|
|
<option value="Spanish">Spanish</option>
|
|
<option value="French">French</option>
|
|
<option value="German">German</option>
|
|
<option value="Italian">Italian</option>
|
|
<option value="Portuguese">Portuguese</option>
|
|
<option value="Chinese">Chinese</option>
|
|
<option value="Japanese">Japanese</option>
|
|
<option value="Korean">Korean</option>
|
|
<option value="Arabic">Arabic</option>
|
|
</select>
|
|
|
|
<button id="translate-btn" onclick="translateText()" disabled>Translate</button>
|
|
|
|
<div id="status">Initializing...</div>
|
|
<div id="output"></div>
|
|
</div>
|
|
</body>
|
|
</html>
|