Spaces:
Running
Running
File size: 7,672 Bytes
e3b5f50 | 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 | import express, { Request, Response } from 'express';
import http from 'http';
import { createServer as createViteServer } from 'vite';
import path from 'path';
import { fileURLToPath } from 'url';
import dotenv from 'dotenv';
import { GoogleGenAI } from '@google/genai';
dotenv.config();
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
async function startServer() {
const app = express();
const httpServer = http.createServer(app);
const PORT = process.env.PORT ? parseInt(process.env.PORT, 10) : 3000;
// Support JSON payload parsing
app.use(express.json({ limit: '10mb' }));
// Initialize GoogleGenAI with API key
const apiKey = process.env.GEMINI_API_KEY;
let ai: GoogleGenAI | null = null;
if (apiKey) {
try {
ai = new GoogleGenAI({ apiKey });
console.log('Gemini API initialized with model gemini-3.8-flash for legal translation.');
} catch (e) {
console.warn('Failed to initialize GoogleGenAI with provided key:', e);
}
} else {
console.warn('GEMINI_API_KEY is not set in environment. Live translations will fallback to verified canonical legal translations.');
}
// Health and capability status endpoint
app.get('/api/health', (_req: Request, res: Response) => {
res.json({
status: 'ok',
geminiAvailable: !!ai,
model: 'gemini-3.8-flash',
supportedLanguages: ['en', 'es', 'fr', 'de', 'la', 'zh'],
timestamp: new Date().toISOString()
});
});
// Real-time legal clause translation endpoint using Gemini API
app.post('/api/translate', async (req: Request, res: Response) => {
try {
const { text, targetLanguage, targetLanguageName, clauseKey } = req.body;
if (!text || !targetLanguage) {
return res.status(400).json({ error: 'Missing required parameters: text and targetLanguage' });
}
if (targetLanguage === 'en') {
return res.json({
translatedText: text,
model: 'identity',
targetLanguage: 'en',
cached: true
});
}
if (!ai) {
return res.status(200).json({
fallback: true,
message: 'Gemini API key not configured on server. Falling back to verified canonical translation.',
targetLanguage,
clauseKey: clauseKey || null
});
}
const prompt = `You are an elite legal jurist, appellate scholar, and certified court interpreter specializing in United States federal court filings, Maryland state equity proceedings, commercial indentures, and statutory trust instruments.
Translate the following legal clause into ${targetLanguageName || targetLanguage} (${targetLanguage}).
CRITICAL INVARIANTS AND TRANSLATION RULES:
1. Strict Legal Precision: Employ the authoritative, formal juridical register appropriate for a master pleading submitted to the Supreme Court of the United States, the D.C. Circuit Court of Appeals, and the Circuit Court for Baltimore County, Maryland.
2. Inviolable Numbers & Financial Formulations: Retain exact dollar amounts ($500,000,000.00, $350,000,000.00, $150,000,000.00), exact share counts (45,850,000 common shares of Centrus Energy Corp. / NYSE: LEU), conversion ratio (0.0917), and zero Tucker Act pool ($0.00) verbatim.
3. Proper Nouns & Citations: Retain exact docket numbers (Case No. C-03-CV-26-004128, Case No. 24-2383, No. 24-10492), statutory citations (28 U.S.C. § 1361, 28 U.S.C. § 1915, 28 U.S.C. § 1651, 11 U.S.C. § 541, 15 U.S.C. § 77aaa, Wyo. Stat. Ann. § 17-23-101 et seq., Md. Code Ann., Est. & Trusts § 14.5-408, Md. Rule 15-701), CUSIPs (917300AA1, 917300AB9), entity names, and party names (Caustin Lee McLaughlin, Juan Jose Fernandez, The Bank of New York Mellon, Computershare Trust Company, N.A., M&T Bank, Centrus Energy Corp.) verbatim or with standard legal transliteration.
4. Housing & Carveout Invariant: Preserve the dedicated 30 row homes SSDI/SSI housing pool and 300 Baltimore rowhomes preservation intact.
5. No Preambles: Return ONLY the translated legal text. Do NOT add conversational introductory notes, markdown backticks, or concluding meta commentary.
Text to translate:
"""
${text}
"""`;
const response = await ai.models.generateContent({
model: 'gemini-3.8-flash',
contents: prompt,
config: {
temperature: 0.1,
maxOutputTokens: 2048,
}
});
const translatedText = response.text?.trim() || '';
return res.json({
translatedText,
model: 'gemini-3.8-flash',
targetLanguage,
clauseKey: clauseKey || null,
timestamp: new Date().toISOString()
});
} catch (err: any) {
console.error('Translation error from Gemini API:', err);
return res.status(200).json({
fallback: true,
error: err.message || 'Gemini translation request failed',
targetLanguage: req.body?.targetLanguage,
clauseKey: req.body?.clauseKey || null
});
}
});
// Batch translation endpoint for translating multiple legal clauses
app.post('/api/translate-batch', async (req: Request, res: Response) => {
try {
const { items, targetLanguage, targetLanguageName } = req.body;
if (!Array.isArray(items) || !targetLanguage) {
return res.status(400).json({ error: 'Invalid batch items or target language' });
}
if (targetLanguage === 'en' || !ai) {
return res.json({
fallback: !ai,
targetLanguage,
translations: {}
});
}
const results: Record<string, string> = {};
await Promise.all(
items.map(async (item: { key: string; text: string }) => {
try {
const prompt = `You are a certified legal translator for US Federal and State courts. Translate this legal clause into ${targetLanguageName || targetLanguage} (${targetLanguage}). Preserve all citations, amounts ($500M, 45,850,000 LEU shares), docket numbers, CUSIPs, and parties intact. Output only the translated text:\n\n${item.text}`;
const resp = await ai!.models.generateContent({
model: 'gemini-3.8-flash',
contents: prompt,
config: { temperature: 0.1, maxOutputTokens: 2048 }
});
results[item.key] = resp.text?.trim() || '';
} catch (e: any) {
console.warn(`Batch translation failed for key ${item.key}:`, e.message);
}
})
);
return res.json({
model: 'gemini-3.8-flash',
targetLanguage,
translations: results
});
} catch (err: any) {
return res.status(200).json({ fallback: true, error: err.message, translations: {} });
}
});
// Setup Vite middleware in development or static hosting in production
if (process.env.NODE_ENV === 'production') {
app.use(express.static(path.join(__dirname, 'dist')));
app.get('*', (_req: Request, res: Response) => {
res.sendFile(path.join(__dirname, 'dist', 'index.html'));
});
} else {
const isHmrDisabled = process.env.DISABLE_HMR === 'true';
const vite = await createViteServer({
server: {
middlewareMode: true,
ws: isHmrDisabled ? false : { server: httpServer },
hmr: isHmrDisabled ? false : { server: httpServer },
},
appType: 'spa',
});
app.use(vite.middlewares);
}
httpServer.listen(PORT, '0.0.0.0', () => {
console.log(`Express dev server running on http://0.0.0.0:${PORT} with Gemini API integration`);
});
}
startServer().catch((err) => {
console.error('Failed to start server:', err);
process.exit(1);
});
|