250 lines
8.8 KiB
HTML
250 lines
8.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');
|
|
let currentModel = null;
|
|
|
|
async function initEngine() {
|
|
const modelSelect = document.getElementById('model-select');
|
|
const selectedModel = modelSelect.value;
|
|
|
|
// If already loaded and same model, skip
|
|
if (engine && currentModel === selectedModel) {
|
|
statusDiv.textContent = "✅ WebLLM engine already ready!";
|
|
return;
|
|
}
|
|
|
|
// Clear previous engine
|
|
if (engine) {
|
|
engine = null;
|
|
}
|
|
|
|
statusDiv.textContent = `Initializing ${selectedModel} (first time: downloading model)...`;
|
|
document.getElementById('translate-btn').disabled = true;
|
|
|
|
try {
|
|
engine = await CreateMLCEngine(selectedModel, {
|
|
initProgressCallback: (progress) => {
|
|
statusDiv.textContent = `Loading: ${progress.text}`;
|
|
}
|
|
});
|
|
|
|
currentModel = selectedModel;
|
|
statusDiv.textContent = `✅ ${selectedModel} ready!`;
|
|
document.getElementById('translate-btn').disabled = false;
|
|
} catch (error) {
|
|
statusDiv.textContent = `❌ Error: ${error.message}`;
|
|
}
|
|
}
|
|
|
|
async function clearCache() {
|
|
if (!confirm('This will delete all downloaded WebLLM models (~2-5GB). Continue?')) {
|
|
return;
|
|
}
|
|
|
|
try {
|
|
const databases = await indexedDB.databases();
|
|
for (const db of databases) {
|
|
if (db.name && (db.name.includes('webllm') || db.name.includes('mlc'))) {
|
|
indexedDB.deleteDatabase(db.name);
|
|
}
|
|
}
|
|
|
|
if ('caches' in window) {
|
|
const cacheNames = await caches.keys();
|
|
for (const name of cacheNames) {
|
|
if (name.includes('webllm') || name.includes('mlc')) {
|
|
await caches.delete(name);
|
|
}
|
|
}
|
|
}
|
|
|
|
alert('✅ Cache cleared! Refresh the page.');
|
|
location.reload();
|
|
} catch (error) {
|
|
alert('❌ 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', () => {
|
|
// Don't auto-init, let user choose model first
|
|
statusDiv.textContent = "Select a model and click 'Load Model' to start.";
|
|
});
|
|
|
|
window.translateText = translateText;
|
|
window.initEngine = initEngine;
|
|
window.clearCache = clearCache;
|
|
</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;
|
|
}
|
|
.btn-group {
|
|
display: grid;
|
|
grid-template-columns: 1fr 1fr;
|
|
gap: 10px;
|
|
margin-bottom: 15px;
|
|
}
|
|
#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> Runs entirely in your browser using WebGPU. Models are cached after first download.
|
|
</div>
|
|
|
|
<label for="model-select">Select Model:</label>
|
|
<select id="model-select">
|
|
<option value="Llama-3.2-3B-Instruct-q4f32_1-MLC">Llama 3.2 3B (~2GB) - Fast</option>
|
|
<option value="Llama-3.1-8B-Instruct-q4f32_1-MLC">Llama 3.1 8B (~4.5GB) - Accurate</option>
|
|
<option value="Phi-3.5-mini-instruct-q4f16_1-MLC">Phi 3.5 Mini (~2.5GB) - Balanced</option>
|
|
<option value="Mistral-7B-Instruct-v0.3-q4f16_1-MLC">Mistral 7B (~4.5GB) - High Quality</option>
|
|
<option value="gemma-2-2b-it-q4f16_1-MLC">Gemma 2 2B (~1.5GB) - Lightweight</option>
|
|
</select>
|
|
|
|
<div class="btn-group">
|
|
<button onclick="initEngine()" style="background: #059669;">Load Model</button>
|
|
<button onclick="clearCache()" style="background: #dc2626;">Clear Cache</button>
|
|
</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>
|