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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!doctype html>
|
| 2 |
+
<html lang="en">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="utf-8" />
|
| 5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover" />
|
| 6 |
+
<title>Simple Browser Translator — NLLB</title>
|
| 7 |
+
<style>
|
| 8 |
+
:root{
|
| 9 |
+
--bg:#0b1220; --bg2:#0f1628; --panel:#121a2b; --border:#223050;
|
| 10 |
+
--text:#e8eefc; --muted:#8ea2c7; --accent:#6ba5ff; --accent2:#2d7dff;
|
| 11 |
+
--radius:14px; --shadow:0 10px 30px rgba(0,0,0,.35);
|
| 12 |
+
}
|
| 13 |
+
*{box-sizing:border-box}
|
| 14 |
+
html,body{height:100%}
|
| 15 |
+
body{
|
| 16 |
+
margin:0;
|
| 17 |
+
background:linear-gradient(180deg,#0b1220 0%, #0b1220 40%, #0f1525 100%);
|
| 18 |
+
color:var(--text);
|
| 19 |
+
font:16px/1.45 system-ui,-apple-system,Segoe UI,Roboto,Helvetica,Arial,Apple Color Emoji,Segoe UI Emoji;
|
| 20 |
+
}
|
| 21 |
+
.wrap{max-width:1100px;margin:28px auto 72px;padding:0 16px}
|
| 22 |
+
|
| 23 |
+
header{display:flex;align-items:center;justify-content:space-between;margin-bottom:18px}
|
| 24 |
+
h1{margin:0;font-size:28px;font-weight:800;letter-spacing:.2px}
|
| 25 |
+
h1 .brand{color:var(--accent)}
|
| 26 |
+
#backend-pill{
|
| 27 |
+
padding:6px 10px; border:1px solid var(--border); border-radius:999px;
|
| 28 |
+
background:#0e1628; color:var(--muted); font-size:12px
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
.grid-2{display:grid;grid-template-columns:1fr 1fr;gap:16px}
|
| 32 |
+
@media (max-width:860px){ .grid-2{grid-template-columns:1fr} }
|
| 33 |
+
.card{background:var(--panel);border:1px solid var(--border);border-radius:var(--radius);box-shadow:var(--shadow);padding:16px}
|
| 34 |
+
.card h3{margin:0 0 8px;font-size:14px;font-weight:700;color:#b9c6e4}
|
| 35 |
+
|
| 36 |
+
select,textarea,button{
|
| 37 |
+
width:100%;border-radius:12px;border:1px solid var(--border);outline:none;
|
| 38 |
+
background:#0e1628;color:var(--text)
|
| 39 |
+
}
|
| 40 |
+
select{padding:12px;font-size:14px;font-weight:600}
|
| 41 |
+
textarea{
|
| 42 |
+
min-height:180px;padding:14px 16px;
|
| 43 |
+
font:15px/1.5 ui-monospace,SFMono-Regular,Menlo,Consolas,monospace
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
.translate-row{display:flex;align-items:center;gap:12px;margin-top:10px}
|
| 47 |
+
#go{
|
| 48 |
+
flex:1;padding:12px 18px;font-weight:800;border-radius:999px;cursor:pointer;
|
| 49 |
+
background:linear-gradient(90deg,var(--accent) 0%,var(--accent2) 100%);
|
| 50 |
+
border:none;color:white;box-shadow:var(--shadow)
|
| 51 |
+
}
|
| 52 |
+
.meta{font-size:12px;color:var(--muted);white-space:nowrap}
|
| 53 |
+
|
| 54 |
+
.status{margin-top:8px;font-size:12px;color:var(--muted)}
|
| 55 |
+
.output{
|
| 56 |
+
min-height:180px;padding:14px 16px;white-space:pre-wrap;overflow:auto;
|
| 57 |
+
border:1px solid var(--border);background:#0e1628;border-radius:12px
|
| 58 |
+
}
|
| 59 |
+
footer{margin-top:8px;font-size:12px;color:var(--muted)}
|
| 60 |
+
</style>
|
| 61 |
+
</head>
|
| 62 |
+
<body>
|
| 63 |
+
<div class="wrap">
|
| 64 |
+
<header>
|
| 65 |
+
<h1>Simple Browser Translator — <span class="brand">NLLB</span></h1>
|
| 66 |
+
<span id="backend-pill">backend: …</span>
|
| 67 |
+
</header>
|
| 68 |
+
|
| 69 |
+
<!-- selectors -->
|
| 70 |
+
<div class="grid-2">
|
| 71 |
+
<div class="card">
|
| 72 |
+
<h3>Source language</h3>
|
| 73 |
+
<select id="src"></select>
|
| 74 |
+
</div>
|
| 75 |
+
<div class="card">
|
| 76 |
+
<h3>Target language</h3>
|
| 77 |
+
<select id="tgt"></select>
|
| 78 |
+
</div>
|
| 79 |
+
</div>
|
| 80 |
+
|
| 81 |
+
<!-- input -->
|
| 82 |
+
<div class="card" style="margin-top:16px">
|
| 83 |
+
<h3>Input text</h3>
|
| 84 |
+
<textarea id="inp" placeholder="Type or paste text here…">Hello world, this is a test.</textarea>
|
| 85 |
+
|
| 86 |
+
<div class="translate-row">
|
| 87 |
+
<button id="go">Translate</button>
|
| 88 |
+
<span class="meta">Model: <code>Xenova/nllb-200-distilled-600M</code></span>
|
| 89 |
+
</div>
|
| 90 |
+
|
| 91 |
+
<div class="status" id="status">Ready.</div>
|
| 92 |
+
</div>
|
| 93 |
+
|
| 94 |
+
<!-- output -->
|
| 95 |
+
<div class="card" style="margin-top:16px">
|
| 96 |
+
<h3>Output</h3>
|
| 97 |
+
<div class="output" id="out">—</div>
|
| 98 |
+
<footer>Arabic/Urdu outputs render RTL automatically.</footer>
|
| 99 |
+
</div>
|
| 100 |
+
</div>
|
| 101 |
+
|
| 102 |
+
<!-- === LOGIC (unchanged from your working code) === -->
|
| 103 |
+
<script type="module">
|
| 104 |
+
import { pipeline, env } from "https://cdn.jsdelivr.net/npm/@xenova/[email protected]";
|
| 105 |
+
|
| 106 |
+
let chosenBackend='auto';
|
| 107 |
+
try{
|
| 108 |
+
if('gpu' in navigator && navigator.gpu){env.backends.onnx='webgpu';chosenBackend='webgpu';}
|
| 109 |
+
else{env.backends.onnx='wasm';chosenBackend='wasm';}
|
| 110 |
+
}catch(_){env.backends.onnx='wasm';chosenBackend='wasm';}
|
| 111 |
+
document.getElementById('backend-pill').textContent=`backend: ${chosenBackend}`;
|
| 112 |
+
|
| 113 |
+
const LANGS=[
|
| 114 |
+
["English (eng_Latn)","eng_Latn"],
|
| 115 |
+
["French (fra_Latn)","fra_Latn"],
|
| 116 |
+
["Hindi (hin_Deva)","hin_Deva"],
|
| 117 |
+
["Urdu (urd_Arab)","urd_Arab"],
|
| 118 |
+
["Arabic (arb_Arab)","arb_Arab"],
|
| 119 |
+
["Spanish (spa_Latn)","spa_Latn"],
|
| 120 |
+
["German (deu_Latn)","deu_Latn"],
|
| 121 |
+
["Bengali (ben_Beng)","ben_Beng"],
|
| 122 |
+
["Punjabi (pan_Guru)","pan_Guru"]
|
| 123 |
+
];
|
| 124 |
+
const srcSel=document.getElementById('src');
|
| 125 |
+
const tgtSel=document.getElementById('tgt');
|
| 126 |
+
const inp=document.getElementById('inp');
|
| 127 |
+
const out=document.getElementById('out');
|
| 128 |
+
const status=document.getElementById('status');
|
| 129 |
+
const goBtn=document.getElementById('go');
|
| 130 |
+
|
| 131 |
+
for(const [label,code] of LANGS){
|
| 132 |
+
srcSel.add(new Option(label,code));
|
| 133 |
+
tgtSel.add(new Option(label,code));
|
| 134 |
+
}
|
| 135 |
+
srcSel.value="eng_Latn";
|
| 136 |
+
tgtSel.value="arb_Arab";
|
| 137 |
+
|
| 138 |
+
function refreshDirection(){
|
| 139 |
+
const rtl=(srcSel.value==="arb_Arab"||srcSel.value==="urd_Arab");
|
| 140 |
+
inp.setAttribute("dir",rtl?"rtl":"ltr");
|
| 141 |
+
inp.style.textAlign=rtl?"right":"left";
|
| 142 |
+
}
|
| 143 |
+
srcSel.addEventListener('change',refreshDirection);
|
| 144 |
+
refreshDirection();
|
| 145 |
+
|
| 146 |
+
let translator=null,loading=false;
|
| 147 |
+
async function getTranslator(){
|
| 148 |
+
if(translator) return translator;
|
| 149 |
+
if(loading){while(!translator) await new Promise(r=>setTimeout(r,120));return translator;}
|
| 150 |
+
loading=true;
|
| 151 |
+
status.innerHTML="Loading model… (first time will download weights)";
|
| 152 |
+
const progress=(p)=>{
|
| 153 |
+
if(!p)return;
|
| 154 |
+
const{status:st,file,loaded,total}=p;
|
| 155 |
+
if(st){
|
| 156 |
+
const pct=(loaded&&total)?` ${(100*loaded/total).toFixed(1)}%`:"";
|
| 157 |
+
status.innerHTML=`<b>${st}</b>${file?` — ${file}`:""}${pct}`;
|
| 158 |
+
}
|
| 159 |
+
};
|
| 160 |
+
translator=await pipeline("translation","Xenova/nllb-200-distilled-600M",{progress_callback:progress});
|
| 161 |
+
status.innerHTML="Model ready ✅";
|
| 162 |
+
return translator;
|
| 163 |
+
}
|
| 164 |
+
function assertCode(code){
|
| 165 |
+
if(!code||typeof code!=="string") throw new Error("Invalid language code");
|
| 166 |
+
const ok=LANGS.some(([,c])=>c===code);
|
| 167 |
+
if(!ok) throw new Error(`Unsupported language code: ${code}`);
|
| 168 |
+
return code;
|
| 169 |
+
}
|
| 170 |
+
async function doTranslate(){
|
| 171 |
+
try{
|
| 172 |
+
goBtn.disabled=true;
|
| 173 |
+
out.textContent="—";
|
| 174 |
+
status.innerHTML="Preparing…";
|
| 175 |
+
const text=(inp.value||"").trim();
|
| 176 |
+
if(!text){status.innerHTML="Enter some text to translate.";return;}
|
| 177 |
+
const src=assertCode(srcSel.value);
|
| 178 |
+
const tgt=assertCode(tgtSel.value);
|
| 179 |
+
if(src===tgt){status.innerHTML="Source and target are the same — translating anyway.";}
|
| 180 |
+
const tr=await getTranslator();
|
| 181 |
+
const output=await tr(text,{src_lang:src,tgt_lang:tgt});
|
| 182 |
+
const translated=Array.isArray(output)?(output[0]?.translation_text||""):(output?.translation_text||"");
|
| 183 |
+
out.textContent=translated||"(empty result)";
|
| 184 |
+
status.innerHTML="Done.";
|
| 185 |
+
}catch(err){
|
| 186 |
+
console.error(err);
|
| 187 |
+
status.innerHTML=`❌ ${err.message||err}`;
|
| 188 |
+
}finally{goBtn.disabled=false;}
|
| 189 |
+
}
|
| 190 |
+
goBtn.addEventListener('click',doTranslate);
|
| 191 |
+
inp.addEventListener('keydown',e=>{if(e.key==='Enter'&&!e.shiftKey){e.preventDefault();doTranslate();}});
|
| 192 |
+
</script>
|
| 193 |
+
</body>
|
| 194 |
+
</html>
|