AI-Powered Language Translation: A Guide to Building a Discord Bot with MindsDB and HuggingFace

AI-Powered Language Translation: A Guide to Building a Discord Bot with MindsDB and HuggingFace

Introduction

Language translation is an important application of artificial intelligence (AI) that has revolutionized the way we communicate and connect with people from different cultures and backgrounds. Discord is a popular chat platform that allows users to communicate with each other in real-time using text, voice, and video.

In this guide, we will explore how to build a language translation bot for Discord using MindsDB and Hugging Face, two powerful AI libraries. MindsDB is an open-source, easy-to-use machine learning tool that allows you to create predictive models without requiring extensive knowledge of machine learning. Hugging Face is a popular natural language processing (NLP) library that provides access to pre-trained models for various NLP tasks, including language translation.

By the end of this guide, you will have learned how to build a language translation bot that can translate messages from one language to another in real-time. This bot can be added to any Discord server, allowing users to communicate with each other in their preferred language.

About MindsDb

MindsDB is an open-source machine learning tool that allows users to build and deploy predictive models with ease. It provides a simple interface for users to create machine learning models using SQL queries, making it accessible to those without a deep understanding of machine learning algorithms.

MindsDB supports a range of machine learning tasks, including regression, classification, time-series forecasting, and anomaly detection. It uses a technique called automated machine learning (AutoML) to automate the process of model selection, feature engineering, and hyperparameter tuning.

MindsDB is a powerful tool for anyone who wants to leverage the power of machine learning without having to become an expert in the field. It can be used for a wide range of applications, from fraud detection to natural language processing, and can help businesses and individuals make data-driven decisions.

Create and Train Language Translation Huggingface Model

We can create a Hugging Face model for language translation using the below command in MindsDBCloud Editor.

We will be Training Three Language Translation Model using Hugging Face Library

CREATE MODEL MODEL_NAME
PREDICT PRED
USING
engine = 'ENGINE',
task = 'translation',
model_name = 'MODEL_NAME',
input_column = 'COLUMN_NAME',
lang_input = 'INPUT_LANG',
lang_output = 'OUTPUT_LANF';

Before we proceed, let's take a look at all of the parameters we're passing in the USING statement.

  • engine = 'huggingface': This specifies that we're using the Hugging Face engine.

  • task = 'translation': This specifies that we're using the Hugging Face T5 model for language translation.

  • model_name = 't5-base': This specifies the name of the Hugging Face T5 model we're using. In this case, we're using the base T5 model.

  • input_column = 'text': This specifies the name of the input column in our dataset. Our model will be trained to translate text from this column.

  • lang_input = 'en': This specifies the input language. In this case, we're using English as the input language.

  • lang_output = 'fr': This specifies the output language. In this case, we're translating from English to French.

    1. Language Translation Model - English to French

       CREATE MODEL mindsdb.hf_t5_en_fr
       PREDICT PRED
       USING
       engine = 'huggingface',
       task = 'translation',
       model_name = 't5-base',
       input_column = 'text',
       lang_input = 'en',
       lang_output = 'fr';
      

    2. Language Translation Model - French to English

       CREATE MODEL hf_fren_translator
       PREDICT english
       USING
       engine = 'huggingface',
       task = 'translation',
       model_name = 'Helsinki-NLP/opus-mt-fr-en',
       input_column = 'text',
       lang_input = 'fr',
       lang_output = 'en';
      

    3. Language Translation Model - Spanish to English

       CREATE MODEL hf_spain_translator
       PREDICT english
       USING
       engine = 'huggingface',
       task = 'translation',
       model_name = 'Helsinki-NLP/opus-mt-es-en',
       input_column = 'text',
       lang_input = 'es',
       lang_output = 'en';
      

Status of the Model

The model might take a while to be ready for use. In the meantime, we can check its status with the query below.

  1. English to French

     SELECT status FROM models
     WHERE name = 'hf_t5_en_fr';
    

  2. French to English

     SELECT status FROM models
     WHERE name = 'hf_fren_translator';
    

  3. Spanish to English

     SELECT status FROM models
     WHERE name = 'hf_spain_translator';
    

Create Discord Bot using Node.js

Setup Discord Application

  1. Open the Discord app and create a new Discord server.

  2. Create a new application on the Discord Developer Portal (discord.com/developers/applications).

  3. Give the application a name and click "Create".

  4. Click on the "Bot" section and then click "Add Bot" and give the bot a username and click "Save Changes".

  5. Click on the "OAuth2" section and select the "bot" checkbox under "Scopes" and select the permissions the bot should have under "Bot Permissions".

  6. Copy the generated URL and paste it into a new browser window.

  7. Select the server where the bot should be added and click "Authorize"

    Create Express application

    1. Run the command "npm init" to create a new Node.js project.

    2. Install all the necessary libraries by running the below command:

      • npm install discord.js

      • npm install express

      • npm install dotenv

      • npm install mindsdb-js-sdk

    3. Create a new JavaScript file in the folder and name it "index.js" and paste the following code

       require('dotenv').config();
       const express = require('express');
       const app = express();
       const Discord = require('discord.js');
       const bodyParser = require("body-parser");
      
       const { connectToMindsDBCloud,analyzeLanguageEngToFreTranslation,analyzeLanguageFreToEngTranslation, analyzeLanguageSpaToEngTranslation} = require("./dispatcher/mindsdb.js")
      
       const client = new Discord.Client({ intents: [
         Discord.GatewayIntentBits.Guilds,
         Discord.GatewayIntentBits.GuildMessages,
         Discord.GatewayIntentBits.MessageContent
       ]})
       app.use(bodyParser.urlencoded({ extended: true }));
       app.use(bodyParser.json());
       app.get('/', (req, res) => {
         res.send('Discord bot is running!');
       });
      
       client.on('ready', () => {
         console.log(`Logged in as ${client.user.tag}!`);
       });
      
       client.on('messageCreate', async (message) => {
         if (message.author.bot) return;
      
         if (message.content.startsWith('!EnglishToFrench')) {
           const query = message.content.slice(16).trim();
           await connectToMindsDBCloud();
           const response = await analyzeLanguageEngToFreTranslation(query);    
      
           console.log("result----->",response);
      
           message.reply(JSON.stringify(response.rows[0]));
         }  
           console.log("result----->",response);
           message.reply(JSON.stringify(response.rows[0]));
         } 
       });
      
       client.login(process.env.TOKEN);
       const PORT = process.env.PORT || 3000;
       app.listen(PORT, () => {
         console.log(`Express app is listening on port ${PORT}!`);
       });
      
    4. Maintain a separate .env file to store the secret key and password.

    5. Create a new file (mindsdb.js) where we maintain code for Maintaining connection with MindsDB Cloud and Function to interact to fetch the result of the trained Model to Predict the Queries.

       async function connectToMindsDBCloud() {
         try {
           await MindsDBCloud.connect({
             user: process.env.MINDSDB_USER,
             password: process.env.MINDSDB_PASS,
           });
           console.log("Suceesfully connected to MindsDB Cloud");
         } catch (error) {
           console.log("Problem connecting to MindsDB Cloud:", error);
           throw error;
         }
       }
      
    6. Create an async function that attempts to run a SQL query using the MindsDBCloud.SQL.runQuery method. The method takes in a query string that selects data from a specified table (languageModel) where the text column matches the message parameter. The results of the query are stored in a variable called LanguageTranslationResponse.

       async function analyzeLanguageEngToFreTranslation(message, language) {
           let retries = 3; // Maximum number of retries
           while (retries > 0) {
             try {
               const text = `SELECT * FROM ${languageEnglishToFrench} WHERE text='${message}'`;
               console.log("test---->",text)
               const LanguageTranslationResponse = await MindsDBCloud.SQL.runQuery(text);
               console.log("response--->",LanguageTranslationResponse)
               if (!LanguageTranslationResponse.rows) {
                 throw new Error("Invalid response from MindsDB");
               }
                 return LanguageTranslationResponse;
      
             } catch (error) {
               console.log("Error detecting Language Transaltion:", error);
               retries--;
               if (retries === 0) {
                   throw new Error("Maximum number of retries reached");
               }
               await new Promise((resolve) => setTimeout(resolve, 1000)); // Wait for 1 second before retrying
             }
           }
         }
      
    7. Save the file and run the command "npm start" in the terminal to start the discord bot.

    8. Open Discord and send a message containing "!EnglishToFrench " and then pass the word in English which you want to convert into the French language, to the server where the bot is located.

    9. Now send a message containing "!FrenchToEnglish " and then pass the word in French which you want to convert into English language

    10. Now send a message containing "!SpanishToEnglish " and then pass the word in Spanish that you want to convert into the English language.

Congratulations, you have created a basic Discord bot using Node.js! From here, you can continue to add features and functionality to your bot to make it more useful and interactive.

Deployment using Railway.app

  1. Create a new project in railway.app: First, create a new project in railway.app by logging in to your account and clicking on "New Project." Choose a name and select the "Node.js" template.

  2. Connect your Github or Gitlab repository: If your Discord bot code is already hosted on Github or Gitlab, you can connect your repository to railway.app by clicking on the "Connect Repository" button in your project dashboard. Otherwise, you can upload your code directly to railway.app.

  3. Set up environment variables: Set up environment variables for your Discord bot, such as the Discord bot token and any API keys or credentials required for your bot's functionality. You can do this by clicking on the "Environment Variables" tab in your project dashboard and adding the necessary variables.

  4. Install dependencies: Install the necessary dependencies for your Discord bot by running "npm install" in the terminal or command prompt.

  5. Start your bot: Start your Discord bot by running the command "node index.js" in the terminal or command prompt. Make sure your bot is functioning as expected by testing its functionality.

  6. Configure a Discord webhook: In order to keep your bot running on railway.app, you'll need to configure a Discord webhook to ping your bot periodically. You can do this by adding a new "Scheduled Task" in your railway.app project dashboard and specifying the URL for your webhook.

  7. Expose your application: Go to the setting tab and check the Domain section, Click on Generate url . Now your application is exposed on the internet.

    URL: discordbotmindsdb-production.up.railway.app

That's it! Your Discord bot should now be up and running on railway.app.

Conclusion

In conclusion, you have successfully created a Discord bot using Node.js that interacts with a MindsCloud predictive model using the Hugging Face library. This means that users can now use the bot to receive predictions from the MindsCloud model within Discord.

The process likely involved setting up a Discord bot account, installing and configuring the necessary libraries for Node.js and Hugging Face, integrating the MindsCloud model into the bot, and setting up appropriate commands for users to interact with the bot.

Overall, this project demonstrates how machine learning models can be integrated into chat platforms like Discord, providing users with powerful predictive capabilities and enhancing their user experience.

MindsDB has recently organized a Hackathon in collaboration with Hashnode. You can check all the details by clicking on the banner below.