Code Monkey home page Code Monkey logo

translator-nllb's Introduction

translator-nllb

The "translator-nllb" project is an automated translation system utilizing NLLB (Natural Language Longtail Benchmark) models from Facebook for accurate and efficient language translation. The system leverages JavaScript for process management and environment setup, alongside Python for executing and loading complex educational models.

translator-nllb

Installation

Install the package from npm:

npm install translator-nllb

Usage

import Translator from 'translator-nllb';

(async function() {
  const translator = new Translator();
  await translator.setup(false);

  const text = 'ロックヒッド・マーティン社は,米海兵隊に初回5Gテストベッドを配達し,モバイルネットワーク実験を開始した.';
  const to = 'ara'; // Target language code "kor_Hang", "kor", "eng_Latn", "eng", "ara", "fra" .....
  const model = 0; // Model index
  // 0: 'facebook/nllb-200-distilled-600M' <= default
  // 1: 'facebook/nllb-200-distilled-1.3B'
  // 2: 'facebook/nllb-200-1.3B'
  // 3: 'facebook/nllb-200-3.3B'
  const result = await translator.exec(text, to, model);
  console.log(result);

  // Output example =>
  // ara: شركة "لوكهيد مارتين" ، بدأت أول تجربة لتقديم أسلحة تجريبية 5G للقوات البحرية الأمريكية ، وتجربة شبكة المحمول. 
  // eng: Lockheed Martin Corporation, the first 5G test beds to be delivered to the US Marine Corps, has started mobile network experimentation.
  // zho_Hans: 洛克希德·马丁公司向美海军陆战队发送了5G测试床,开始了移动网络实验.

})();

Integration Examples

Integration with Express.js

You can integrate the translator into an Express.js server to handle translation requests via API endpoints.

import express from 'express';
import Translator from 'translator-nllb';

const app = express();
const translator = new Translator();

// Middleware to setup translator
app.use(async (req, res, next) => {
  await translator.setup(false);
  next();
});

// Example route for translation
app.get('/translate', async (req, res) => {
  const { text, to, model } = req.query;

  try {
    const result = await translator.exec(text, to, model);
    res.json({ translatedText: result });
  } catch (err) {
    res.status(500).json({ error: err.message });
  }
});

const PORT = 3000;
app.listen(PORT, () => {
  console.log(`Server is running on http://localhost:${PORT}`);
});

Integration with Telegraf (Telegram Bot)

Use the translator within a Telegraf-powered Telegram bot to provide real-time translation of messages.

import { Telegraf } from 'telegraf';
import Translator from 'translator-nllb';

const bot = new Telegraf('YOUR_TELEGRAM_BOT_TOKEN');
const translator = new Translator();

// Middleware to setup translator
bot.use(async (ctx, next) => {
  await translator.setup(false);
  ctx.translator = translator;
  next();
});

// Command to handle translation
bot.command('translate', async (ctx) => {
  const text = ctx.message.text.split(' ').slice(1).join(' ');
  const to = 'ara'; // Target language code
  const model = 0; // Model index

  try {
    const result = await ctx.translator.exec(text, to, model);
    ctx.reply(`Translated: ${result}`);
  } catch (err) {
    ctx.reply(`Translation failed: ${err.message}`);
  }
});

bot.launch();

Integration with WhatsApp using whatsapp-web.js

Integrate translator-nllb with whatsapp-web.js to automate translation tasks within WhatsApp.

import { Client } from 'whatsapp-web.js';
import Translator from 'translator-nllb';

const client = new Client();
const translator = new Translator();

client.on('qr', (qr) => {
  console.log('QR Received:', qr);
});

client.on('ready', async () => {
  console.log('WhatsApp Client is ready!');

  // Example: send translated message
  const chatId = '[email protected]';
  const text = 'Hello';
  const to = 'fra'; // Target language code
  const model = 0; // Model index

  try {
    const result = await translator.exec(text, to, model);
    client.sendMessage(chatId, `Translated: ${result}`);
  } catch (err) {
    console.error('Translation error:', err);
  }
});

client.initialize();

License

MIT License

translator-nllb's People

Contributors

rn0x avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.