Spaces:
Sleeping
Sleeping
File size: 9,143 Bytes
e7c6240 9a98204 9702737 8304957 9702737 8304957 b778f8d 8c66f34 b778f8d 8c66f34 b778f8d 1fa5b10 b778f8d 8304957 b778f8d 8304957 b778f8d 8304957 b778f8d 8c66f34 b778f8d 8304957 b778f8d 8304957 9702737 e7c6240 bd58e89 e7c6240 dbe735d e7c6240 9702737 8304957 2c5a9d9 8304957 71aa48d e7c6240 8c66f34 e7c6240 8c66f34 e7c6240 b778f8d 2c5a9d9 a52ade4 2c5a9d9 a52ade4 2c5a9d9 a52ade4 2c5a9d9 b778f8d e7c6240 8c66f34 1fa5b10 e7c6240 9702737 8304957 71aa48d 8304957 71aa48d 8304957 e7c6240 9702737 71aa48d 73585c7 9702737 73585c7 9702737 73585c7 9702737 73585c7 e7c6240 71aa48d e7c6240 71aa48d e7c6240 d4f99c0 e7c6240 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 |
// Initialize the app container.
const app = document.getElementById('app');
// Add retry count with 600 second timeout (checking every 2 seconds = 300 attempts).
let retryCount = 0;
const MAX_RETRIES = 300; // 600 seconds / 2 second interval = 300 attempts
// Add function to get JWT token
async function getAuthToken() {
try {
const response = await fetch('/api/auth-token');
if (!response.ok) {
throw new Error('Failed to fetch auth token');
}
const { token } = await response.json();
return token;
} catch (error) {
console.error('Error getting auth token:', error);
throw error;
}
}
async function checkServicesStatus() {
try {
const token = await getAuthToken();
const response = await fetch('/inference/api/v1/chat/completions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Token token=${token}`
},
body: JSON.stringify({
model: 'gemma-2b',
messages: [{role: 'user', content: 'hi'}],
nutrient_dont_send_defaults: true
})
});
if (response.ok) {
window.servicesReady = true;
const translationControls = document.getElementById('translationControls');
const statusIndicator = document.getElementById('statusIndicator');
const loadingOverlay = document.getElementById('loadingOverlay');
if (translationControls) {
translationControls.style.display = 'block';
}
if (statusIndicator) {
statusIndicator.style.display = 'none';
}
if (loadingOverlay) {
loadingOverlay.classList.add('hidden');
}
return true;
}
throw new Error('Services not ready');
} catch (error) {
retryCount++;
const statusIndicator = document.getElementById('statusIndicator');
if (statusIndicator) {
if (retryCount >= MAX_RETRIES) {
statusIndicator.innerHTML = '❌ Failed to initialize AI services. Try restarting the space.';
statusIndicator.style.color = '#dc3545';
} else {
statusIndicator.innerHTML = `<span class="spinner"></span> Initializing AI services...`;
// Schedule next check only if we haven't exceeded retries
setTimeout(checkServicesStatus, 2000);
}
}
console.log('Waiting for services...', error);
return false;
}
}
// Start the first check
checkServicesStatus();
// Load Document Authoring SDK.
const script = document.createElement('script');
script.src = 'https://document-authoring-cdn.pspdfkit.com/releases/document-authoring-1.0.26-umd.js';
script.crossOrigin = true;
document.head.appendChild(script);
script.onload = async () => {
// Fetch license key from nginx endpoint.
const licenseKeyResponse = await fetch('/api/license-key');
if (!licenseKeyResponse.ok) {
throw new Error('Failed to fetch license key');
}
const { licenseKey } = await licenseKeyResponse.json();
const docAuthSystem = await DocAuth.createDocAuthSystem({licenseKey});
const editor = await docAuthSystem.createEditor(
document.getElementById("editor"),
{
document: await docAuthSystem.importDOCX(fetch('./Sample.docx')),
}
);
// Helper function to extract text runs with their paths.
function flattenTextRuns(obj, path = [], result = []) {
for (let key in obj) {
const newPath = [...path, key];
if (typeof obj[key] === "string" && key === "text" && obj[key].length > 0) {
// Trim any whitespace between text and delimiters.
result.push({ text: obj[key].trim(), path: newPath });
} else if (typeof obj[key] === "object") {
flattenTextRuns(obj[key], newPath, result);
}
}
return result;
}
// Helper function to group adjacent text runs
function groupAdjacentRuns(textRuns, maxChunkSize = 1000) {
const groups = [];
let currentGroup = [];
let currentSize = 0;
for (let run of textRuns) {
if (currentSize + run.text.length > maxChunkSize && currentGroup.length > 0) {
groups.push([...currentGroup]);
currentGroup = [];
currentSize = 0;
}
currentGroup.push(run);
currentSize += run.text.length;
}
if (currentGroup.length > 0) {
groups.push(currentGroup);
}
return groups;
}
async function translate(content, targetLang, sourceLang = 'English') {
try {
const token = await getAuthToken();
const response = await fetch('/inference/api/v1/chat/completions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Token token=${token}`
},
body: JSON.stringify({
messages: [
{
role: "user",
content: `You are a translator. Translate ONLY the exact text between ||| delimiters. Each segment must be translated separately and maintain its own delimiters. The word "Nutrient" must stay EXACTLY as "Nutrient".
Rules:
1. Do NOT translate proper nouns
2. Keep original capitalization and punctuation
3. Preserve ALL spaces exactly as they appear in the original text
4. For Spanish, use proper inverted punctuation (¡ ¿)
5. Keep each segment separate - do not combine segments
Examples of correct translations:
Complex sentence split across segments:
|||John's report shows that|||
|||John muestra que|||
|||the project will be|||
|||el proyecto estará|||
|||completed by Friday.|||
|||terminado el viernes.|||
Punctuation and spacing:
|||Hello, world!|||
|||¡Hola, mundo!|||
Mixed content with numbers:
|||Room 101 is empty.|||
|||La habitación 101 está vacía.|||
Now translate this text from ${sourceLang} to ${targetLang}:
${content}`
}
],
model: "gemma-2b",
nutrient_dont_send_defaults: true
})
});
if (!response.ok) {
throw new Error('Translation request failed');
}
const data = await response.json();
return data.choices[0].message.content;
} catch (error) {
console.error("Translation error:", error);
throw new Error("Failed to generate translation");
}
}
// Function to update nested object value by path.
function setNestedValue(obj, path, value) {
let current = obj;
for (let i = 0; i < path.length - 1; i++) {
current = current[path[i]];
}
current[path[path.length - 1]] = value;
}
async function translateRecursive(docJson, targetLang, sourceLang = 'English') {
const textRuns = flattenTextRuns(docJson);
const groups = groupAdjacentRuns(textRuns);
for (let group of groups) {
const content = group.map(run => `|||${run.text}|||`).join('\n');
const translatedContent = await translate(content, targetLang, sourceLang);
const translations = translatedContent
.split('|||')
.filter(t => t.trim())
.map(t => t.trim());
group.forEach((run, index) => {
if (translations[index]) {
const originalText = run.text;
let translatedText = translations[index];
if (originalText.startsWith(' ')) {
translatedText = ' ' + translatedText;
}
if (originalText.endsWith(' ')) {
translatedText = translatedText + ' ';
}
setNestedValue(docJson, run.path, translatedText);
}
});
}
}
// Add language selection handlers.
const sourceLanguageSelect = document.getElementById('sourceLanguageSelect');
const targetLanguageSelect = document.getElementById('targetLanguageSelect');
// Update target language options when source language changes.
sourceLanguageSelect.addEventListener('change', () => {
const selectedSource = sourceLanguageSelect.value;
const languages = ['English', 'Spanish', 'French', 'German'];
// Clear existing options.
targetLanguageSelect.innerHTML = '';
// Add all languages except the selected source language.
languages.forEach(lang => {
if (lang !== selectedSource) {
const option = document.createElement('option');
option.value = lang;
option.textContent = lang;
targetLanguageSelect.appendChild(option);
}
});
});
const translateButton = document.getElementById('translateButton');
translateButton.addEventListener('click', async () => {
try {
const sourceLanguage = sourceLanguageSelect.value;
const targetLanguage = targetLanguageSelect.value;
translateButton.disabled = true;
translateButton.innerHTML = '<span class="spinner"></span> Translating...';
const jsonDoc = await editor.currentDocument().saveDocument();
await translateRecursive(jsonDoc.container.document, targetLanguage, sourceLanguage);
const translatedDoc = await docAuthSystem.loadDocument(jsonDoc);
editor.setCurrentDocument(translatedDoc);
} catch (error) {
alert('Translation failed: ' + error.message);
} finally {
translateButton.disabled = false;
translateButton.innerHTML = 'Translate Document';
}
});
};
|