npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

translator-nllb

v1.0.1

Published

integrates NLLB models for accurate language translation, employing JavaScript for process management and Python for executing complex educational models efficiently.

Downloads

12

Readme

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.

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