• ☆ Yσɠƚԋσʂ ☆@lemmygrad.mlOP
    link
    fedilink
    arrow-up
    2
    arrow-down
    1
    ·
    12 days ago

    I’ve found DeepSeek works pretty well for translating content, their API access is pretty cheap. The main limitation comes from the context size, smaller models can handle less text, so you’d have to feed it content in smaller chunks. That said, locally running models are pretty capable of doing these types of translations.

    Here’s an example node script you could use to call DeepSeek to translate a document:

    const fs = require('fs');
    const axios = require('axios');
    
    async function translateFile(filePath) {
        try {
            // Check if API key is set
            if (!process.env.DEEPSEEK_API_KEY) {
                throw new Error('DEEPSEEK_API_KEY environment variable is not set');
            }
    
            // Read the file content
            const content = fs.readFileSync(filePath, 'utf8');
            
            // Call Deepseek API for translation using chat completion
            const response = await axios.post(
                'https://api.deepseek.com/v1/chat/completions',
                {
                    model: "deepseek-chat",
                    messages: [
                        {
                            role: "system",
                            content: "You are a professional translator. Translate the following text to English while preserving formatting and meaning."
                        },
                        {
                            role: "user",
                            content: content
                        }
                    ],
                    temperature: 0.3
                },
                {
                    headers: {
                        'Authorization': `Bearer ${process.env.DEEPSEEK_API_KEY}`,
                        'Content-Type': 'application/json'
                    }
                }
            );
    
            return response.data.choices[0].message.content;
        } catch (error) {
            console.error('Translation failed:', error.message);
            process.exit(1);
        }
    }
    
    // Usage: node src/index.js <input-file> <output-file>
    async function main() {
        if (process.argv.length < 4) {
            console.log('Usage: node src/index.js <input-file> <output-file>');
            process.exit(1);
        }
    
        const inputFile = process.argv[2];
        const outputFile = process.argv[3];
        const translatedText = await translateFile(inputFile);
        
        fs.writeFileSync(outputFile, translatedText);
        console.log(`Translation saved to ${outputFile}`);
    }
    
    main();
    

    Similarly, if you wanted to use a model like qwen3 with ollama for translations, you could do something like this:

    const fs = require('fs');
    const axios = require('axios');
    
    async function translateText(text) {
        try {
            const response = await axios.post('http://localhost:11434/api/generate', {
                model: 'qwen3:32b',
                prompt: `Translate the following text to English:\n\n${text}`,
                stream: false,
                options: {
                    num_ctx: 16384,  // Larger context window
                    temperature: 0.3,  // More deterministic output
                    top_k: 40,       // Balance between quality and speed
                    top_p: 0.9        // Controls diversity of output
                }
            });
            return response.data.response;
        } catch (error) {
            console.error('Translation error:', error);
            throw error;
        }
    }
    
    async function translateFile(inputPath, outputPath) {
        try {
            // Read input file
            const inputText = fs.readFileSync(inputPath, 'utf8');
            
            // Translate text
            const translatedText = await translateText(inputText);
            
            // Write output file
            fs.writeFileSync(outputPath, translatedText);
            console.log(`Translation complete. Output written to ${outputPath}`);
        } catch (error) {
            console.error('File processing error:', error);
        }
    }
    
    // Usage: node src/index.js input.txt output.txt
    if (process.argv.length >= 4) {
        const inputFile = process.argv[2];
        const outputFile = process.argv[3];
        translateFile(inputFile, outputFile);
    } else {
        console.log('Usage: node src/index.js <input-file> <output-file>');
    }