Spaces:
Running
Running
| import { TRANSLATION_PROMPT } from '../../../prompts/translation.js'; | |
| import { Type } from '@google/genai'; | |
| import { tryModels, getPrompt, cleanJson, DEFAULT_SAFETY_SETTINGS } from '@/backend/services/ai/utils'; | |
| export async function translate(text, targetLanguage, options, apiKey, isOwnApi = false) { | |
| const models = ['gemini-3-flash-preview', 'gemini-flash-lite-latest']; | |
| const isBurmese = targetLanguage.toLowerCase().includes('burm') || targetLanguage.includes('မြန်မာ'); | |
| const finalPrompt = TRANSLATION_PROMPT(targetLanguage, options); | |
| return await tryModels(apiKey, models, async (ai, model) => { | |
| const response = await ai.models.generateContent({ | |
| model: model, | |
| contents: [{ parts: [{ text: `CONTENT: ${text}` }] }], | |
| config: { | |
| temperature: 0.7, | |
| systemInstruction: finalPrompt, | |
| responseMimeType: "application/json", | |
| safetySettings: DEFAULT_SAFETY_SETTINGS, | |
| responseSchema: { | |
| type: Type.OBJECT, | |
| properties: { | |
| translation: { type: Type.STRING }, | |
| deepMeaning: { type: Type.STRING, nullable: true }, | |
| suggestions: { type: Type.OBJECT, nullable: true, properties: { videoTitles: { type: Type.ARRAY, items: { type: Type.STRING } }, thumbnailTexts: { type: Type.ARRAY, items: { type: Type.STRING } } } } | |
| }, | |
| required: ['translation'] | |
| } | |
| } | |
| }); | |
| return JSON.parse(cleanJson(response.text)); | |
| }); | |
| } | |