lm_translator

1.0.7 • Public • Published

lm_translator Module

The lm_translator Module is a versatile tool for language translation in your Node.js applications. It leverages the powerful Xenova module for the NLLB model pipeline, providing a simple and convenient way to integrate translation functionality into your projects. This integration makes it easier to create multilingual applications with advanced translation capabilities.

Pre-requis

  • RAM: 4Gb or more
  • CPU: 2Ghz or more
  • Memory: 500 Mb

Note: For the first time you translate a text, it need to download the model from the Xenova repository.

Features

  • Simplified API: Easily translate text between 204 different languages.
  • Customizable Configuration: Tailor the translation settings to your specific needs.

Installation

You can install lm_translator Module using either of the following methods:

Clone from GitLab:

git clone https://gitlab.com/machutvladimir/module_traducteur.git
cd module_traducteur
npm install

Install via npm

npm install lm_translator

Usage

Here's a code snippet demonstrating how to use lm_translator Module, highlighting its capabilities and necessary configurations:

Using call function API

import translateText, { getLanguageOptions } from "lm_translator";

async function exampleTranslation() {
  const textToTranslate = "Translate this text into Malagasy";
  const targetLanguage = "plt_Latn"; // Target language code
  const sourceLanguage = "eng_Latn"; // Source language code

  try {
    // Verify if the source and target languages are supported
    const languageOptions = await getLanguageOptions();
    const validLanguages = languageOptions.map(option => option.value);

    if (!validLanguages.includes(targetLanguage)) {
      console.error(`Error: Invalid target language code - ${targetLanguage}`);
      return;
    }
    if (!validLanguages.includes(sourceLanguage)) {
      console.error(`Error: Invalid source language code - ${sourceLanguage}`);
      return;
    }

    // Define necessary arguments
    const translationResult = await translateText(textToTranslate, targetLanguage, sourceLanguage);
    console.log(translationResult);
  } catch (error) {
    console.error("An error occurred:", error);
  }
}

exampleTranslation();

In the code snippet, we use translateText to perform the translation. It checks for valid language codes, configures the translation task, and then translates the specified text. You can customize it further as needed.

Using the API express

To run the server you can launch it manualy in another server nodejs

cd node_modules/lm_translator
npm run server

Get Supported Languages:

To get a list of supported languages, you can make a GET request to the /getall endpoint:

const fetch = require('node-fetch');

fetch('http://localhost:3210/getall')
  .then(response => response.json())
  .then(data => {
    console.log('Supported Languages:', data.languages);
  })
  .catch(error => {
    console.error('Error fetching supported languages:', error);
  });

Translate Text:

To translate text, you can make a GET request to the /translate/text/source/target endpoint:

const fetch = require('node-fetch');

const textToTranslate = 'Translate this text';
const sourceLanguage = 'eng_Latn';
const targetLanguage = 'fra_Latn';

fetch(`http://localhost:3210/translate/${textToTranslate}/${sourceLanguage}/${targetLanguage}`)
  .then(response => response.json())
  .then(data => {
    console.log('Translated Text:', data.translation);
  })
  .catch(error => {3
    console.error('Error translating text:', error);
  });

In these examples, we use the node-fetch library to make HTTP requests. You can adapt the code to your preferred HTTP request library if you are using a different one.

UI Test:

There's an UI to test the module created in python using Gradio.

Create an virtual environment

pip install venv
python -m venv .env

Activate the virtual environment

source ./.env/bin/activate

Excecute the code for UI

Before launching the following command, make sure that the server API(NodeJS server) is already launched

python UI.py

Then open the link

Feel free to adapt and integrate this code into your Node.js applications for seamless language translation capabilities.

Note: The commented-out code at the bottom is not necessary and can be removed. It's provided only for reference.

Package Sidebar

Install

npm i lm_translator

Weekly Downloads

18

Version

1.0.7

License

ISC

Unpacked Size

391 MB

Total Files

13145

Last publish

Collaborators

  • machut