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

lm_translator

v1.0.7

Published

large model translator

Downloads

51

Readme

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.