Spaces:
Runtime error
Runtime error
Add autodetect language button
Browse files- document-authoring.js +109 -10
document-authoring.js
CHANGED
|
@@ -7,6 +7,7 @@ app.innerHTML = `
|
|
| 7 |
<span class="spinner"></span> Translation services starting...
|
| 8 |
</div>
|
| 9 |
<div id="translationControls">
|
|
|
|
| 10 |
<select id="sourceLanguageSelect">
|
| 11 |
<option value="English" selected>English</option>
|
| 12 |
<option value="Spanish">Spanish</option>
|
|
@@ -15,8 +16,7 @@ app.innerHTML = `
|
|
| 15 |
</select>
|
| 16 |
<span style="margin: 0 10px;">→</span>
|
| 17 |
<select id="targetLanguageSelect">
|
| 18 |
-
<option value="
|
| 19 |
-
<option value="Spanish">Spanish</option>
|
| 20 |
<option value="French">French</option>
|
| 21 |
<option value="German">German</option>
|
| 22 |
</select>
|
|
@@ -204,21 +204,36 @@ script.onload = async () => {
|
|
| 204 |
}
|
| 205 |
}
|
| 206 |
|
| 207 |
-
// Add
|
| 208 |
-
const translateButton = document.getElementById('translateButton');
|
| 209 |
const sourceLanguageSelect = document.getElementById('sourceLanguageSelect');
|
| 210 |
const targetLanguageSelect = document.getElementById('targetLanguageSelect');
|
| 211 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 212 |
translateButton.addEventListener('click', async () => {
|
| 213 |
try {
|
| 214 |
const sourceLanguage = sourceLanguageSelect.value;
|
| 215 |
const targetLanguage = targetLanguageSelect.value;
|
| 216 |
|
| 217 |
-
if (sourceLanguage === targetLanguage) {
|
| 218 |
-
alert('Please select different languages for source and target');
|
| 219 |
-
return;
|
| 220 |
-
}
|
| 221 |
-
|
| 222 |
translateButton.disabled = true;
|
| 223 |
translateButton.innerHTML = '<span class="spinner"></span> Translating...';
|
| 224 |
|
|
@@ -233,4 +248,88 @@ script.onload = async () => {
|
|
| 233 |
translateButton.innerHTML = 'Translate Document';
|
| 234 |
}
|
| 235 |
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 236 |
};
|
|
|
|
| 7 |
<span class="spinner"></span> Translation services starting...
|
| 8 |
</div>
|
| 9 |
<div id="translationControls">
|
| 10 |
+
<button id="detectLanguageButton" style="margin-right: 10px;">Detect Language</button>
|
| 11 |
<select id="sourceLanguageSelect">
|
| 12 |
<option value="English" selected>English</option>
|
| 13 |
<option value="Spanish">Spanish</option>
|
|
|
|
| 16 |
</select>
|
| 17 |
<span style="margin: 0 10px;">→</span>
|
| 18 |
<select id="targetLanguageSelect">
|
| 19 |
+
<option value="Spanish" selected>Spanish</option>
|
|
|
|
| 20 |
<option value="French">French</option>
|
| 21 |
<option value="German">German</option>
|
| 22 |
</select>
|
|
|
|
| 204 |
}
|
| 205 |
}
|
| 206 |
|
| 207 |
+
// Add language selection handlers
|
|
|
|
| 208 |
const sourceLanguageSelect = document.getElementById('sourceLanguageSelect');
|
| 209 |
const targetLanguageSelect = document.getElementById('targetLanguageSelect');
|
| 210 |
+
|
| 211 |
+
// Update target language options when source language changes
|
| 212 |
+
sourceLanguageSelect.addEventListener('change', () => {
|
| 213 |
+
const selectedSource = sourceLanguageSelect.value;
|
| 214 |
+
const languages = ['English', 'Spanish', 'French', 'German'];
|
| 215 |
+
|
| 216 |
+
// Clear existing options
|
| 217 |
+
targetLanguageSelect.innerHTML = '';
|
| 218 |
+
|
| 219 |
+
// Add all languages except the selected source language
|
| 220 |
+
languages.forEach(lang => {
|
| 221 |
+
if (lang !== selectedSource) {
|
| 222 |
+
const option = document.createElement('option');
|
| 223 |
+
option.value = lang;
|
| 224 |
+
option.textContent = lang;
|
| 225 |
+
targetLanguageSelect.appendChild(option);
|
| 226 |
+
}
|
| 227 |
+
});
|
| 228 |
+
});
|
| 229 |
+
|
| 230 |
+
// Update translate button handler (removed validation)
|
| 231 |
+
const translateButton = document.getElementById('translateButton');
|
| 232 |
translateButton.addEventListener('click', async () => {
|
| 233 |
try {
|
| 234 |
const sourceLanguage = sourceLanguageSelect.value;
|
| 235 |
const targetLanguage = targetLanguageSelect.value;
|
| 236 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 237 |
translateButton.disabled = true;
|
| 238 |
translateButton.innerHTML = '<span class="spinner"></span> Translating...';
|
| 239 |
|
|
|
|
| 248 |
translateButton.innerHTML = 'Translate Document';
|
| 249 |
}
|
| 250 |
});
|
| 251 |
+
|
| 252 |
+
// Add language detection function
|
| 253 |
+
async function detectLanguage(text) {
|
| 254 |
+
try {
|
| 255 |
+
const response = await fetch('/client/api/v1/chat/completions', {
|
| 256 |
+
method: 'POST',
|
| 257 |
+
headers: {
|
| 258 |
+
'Content-Type': 'application/json',
|
| 259 |
+
},
|
| 260 |
+
body: JSON.stringify({
|
| 261 |
+
messages: [
|
| 262 |
+
{
|
| 263 |
+
role: "system",
|
| 264 |
+
content: `You are a language detection expert. Analyze the following text and determine if it's English, Spanish, French, or German.
|
| 265 |
+
|
| 266 |
+
Key indicators:
|
| 267 |
+
- French: Uses accents like é, è, ê, à, ç. Common words: je, suis, le, la, les, et, dans
|
| 268 |
+
- Spanish: Uses ñ, á, é, í, ó, ú. Common words: el, la, los, las, es, en, y, que
|
| 269 |
+
- German: Uses ä, ö, ü, ß. Common words: der, die, das, und, ist, in, ich
|
| 270 |
+
- English: Generally no accents. Common words: the, is, are, and, in, of, to
|
| 271 |
+
|
| 272 |
+
Respond with ONLY ONE of these exact options: "English", "Spanish", "French", or "German". Do not add any other text.`
|
| 273 |
+
},
|
| 274 |
+
{
|
| 275 |
+
role: "user",
|
| 276 |
+
content: text
|
| 277 |
+
}
|
| 278 |
+
],
|
| 279 |
+
model: "gemma-2b",
|
| 280 |
+
stream: false
|
| 281 |
+
})
|
| 282 |
+
});
|
| 283 |
+
|
| 284 |
+
if (!response.ok) {
|
| 285 |
+
throw new Error('Language detection failed');
|
| 286 |
+
}
|
| 287 |
+
|
| 288 |
+
const data = await response.json();
|
| 289 |
+
const detectedLanguage = data.choices[0].message.content.trim();
|
| 290 |
+
|
| 291 |
+
// Validate the response is one of our supported languages
|
| 292 |
+
const supportedLanguages = ['English', 'Spanish', 'French', 'German'];
|
| 293 |
+
if (!supportedLanguages.includes(detectedLanguage)) {
|
| 294 |
+
throw new Error('Invalid language detection response');
|
| 295 |
+
}
|
| 296 |
+
|
| 297 |
+
return detectedLanguage;
|
| 298 |
+
} catch (error) {
|
| 299 |
+
console.error("Language detection error:", error);
|
| 300 |
+
throw new Error("Failed to detect language");
|
| 301 |
+
}
|
| 302 |
+
}
|
| 303 |
+
|
| 304 |
+
// Add detect language button handler
|
| 305 |
+
const detectLanguageButton = document.getElementById('detectLanguageButton');
|
| 306 |
+
detectLanguageButton.addEventListener('click', async () => {
|
| 307 |
+
try {
|
| 308 |
+
detectLanguageButton.disabled = true;
|
| 309 |
+
detectLanguageButton.innerHTML = '<span class="spinner"></span> Detecting...';
|
| 310 |
+
|
| 311 |
+
// Get document content
|
| 312 |
+
const jsonDoc = await editor.currentDocument().saveDocument();
|
| 313 |
+
const textRuns = flattenTextRuns(jsonDoc.container.document);
|
| 314 |
+
|
| 315 |
+
// Get a sample of text (first 1000 characters)
|
| 316 |
+
const sampleText = textRuns
|
| 317 |
+
.map(run => run.text)
|
| 318 |
+
.join(' ')
|
| 319 |
+
.slice(0, 1000);
|
| 320 |
+
|
| 321 |
+
// Detect language
|
| 322 |
+
const detectedLanguage = await detectLanguage(sampleText);
|
| 323 |
+
|
| 324 |
+
// Update source language dropdown
|
| 325 |
+
const sourceLanguageSelect = document.getElementById('sourceLanguageSelect');
|
| 326 |
+
sourceLanguageSelect.value = detectedLanguage;
|
| 327 |
+
|
| 328 |
+
} catch (error) {
|
| 329 |
+
alert('Language detection failed: ' + error.message);
|
| 330 |
+
} finally {
|
| 331 |
+
detectLanguageButton.disabled = false;
|
| 332 |
+
detectLanguageButton.innerHTML = 'Detect Language';
|
| 333 |
+
}
|
| 334 |
+
});
|
| 335 |
};
|