import DocAuth from "@pspdfkit/document-authoring"; const docAuthSystem = await DocAuth.createDocAuthSystem({ // assets: { base: '' }, // licenseKey: '', }); const editor = await docAuthSystem.createEditor( document.getElementById("editor"), { document: await docAuthSystem.importDOCX(fetch("./Spanish.docx")), } ); // Translation function. async function translate(content, lang = "English") { try { const response = await fetch('/api/aia/v1/chat/completions', { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': 'Token token=eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE3MzMyNjQ2MDcsImV4cCI6MTczMzUyMzgwN30.WWyks6NOU43_2OcZX82z3P4QUwQiUA61SeTZsNobIGoVg3uDTkedXWqISIB7QqTnRyf_D92t_pGr5g_GzqBaOTqaXYJJPco9mtZjtAyUpdWr1UHu4jx-zoKUpCeguZQZbHG6vqYzu1BNV1iPdij2OIoUrr_DY--G-6m5-31E0LcDZb7ysUyZL1Ai_q7PPRwTS8mEtvuF4QO6Cw_a77X7Qh9x_V8a_WwNMFwQMRkIUAsRx3IOI10q0hdwP-k8xYtTbHQO-z66pRl6I71ATclR7FS5FraoLLmWlq8lqNKfzeOgER8Kcf8uB87zHQbfk0QR8AfJGmZDE8pG5F3QG29MeA' }, body: JSON.stringify({ messages: [ { role: "system", content: "You are a translator. Only provide the translation, no explanations or additional text." }, { role: "user", content: `Translate the following text to ${lang}: ${content}` } ], model: "gemma-2b", stream: false }) }); if (!response.ok) { throw new Error('Translation request failed'); } const data = await response.json(); console.log('Translation API response:', data); return data.choices[0].message.content; } catch (error) { console.error("Translation error:", error); throw new Error("Failed to generate translation"); } } // Function to recursively translate text in the JSON structure. async function translateRecursive(obj) { for (let key in obj) { if (typeof obj[key] === "string" && key === "text" && obj[key].length > 0) { obj[key] = await translate(obj[key]); } else if (typeof obj[key] === "object") { await translateRecursive(obj[key]); } } } // Function to translate the document. async function translateDocument() { try { translateButton.disabled = true; translateButton.innerHTML = ' Translating...'; const jsonDoc = await editor.currentDocument().saveDocument(); await translateRecursive(jsonDoc.container.document); const translatedDoc = await docAuthSystem.loadDocument(jsonDoc); editor.setCurrentDocument(translatedDoc); } catch (error) { console.error("Translation error:", error); } finally { translateButton.disabled = false; translateButton.innerHTML = 'Translate to English'; } } // Add click event listener to the translate button. const translateButton = document.getElementById("translateButton"); translateButton.addEventListener("click", translateDocument); // Dev exports. window.DocAuth = DocAuth; window.docAuthSystem = docAuthSystem; window.editor = editor;