VoiceGPT With Multilingual Facility

efy test

Earlier, we used the power of ChatGPT and converted it into VoiceGPT, making it the most advanced voice assistant that can talk. We ended the project with a promise to come up with a more advanced feature and release its next version. Today, we are excited to introduce the multilingual facility to our VoiceGPT.

That’s right, now you can talk to ChatGPT in your own language, as it supports more than 100 languages. Unlike Alexa, you can now have a smart assistant that can communicate with you in a wide range of languages. Fig. 1 showcases the author’s setup for testing the project. The components used in this project are listed in Table 1 for the Bill of Material. Simply gather all the components mentioned in Table 1 and interconnect them using the provided slots, eliminating the need for a circuit diagram.

POC Video Tutorial In English:

POC Video Tutorial In Hindi:

Author’s setup for testing the project
Fig. 1: Author’s setup for testing the project

ChatGPT voice control setup

To begin, you need to perform the initial setup of OpenAI and obtain the API key for OpenAI, as described in previous issue of this magazine. Once you have set up an AI account, you may choose to add billing based on your usage and requirements. With that complete, you can now delve into the code.

We have developed two versions of the multilingual code: a manual version where you enter the desired language code for your voice assistant, and an automatic version that detects the language you input and uses that same language in VoiceGPT to communicate with you. Let’s proceed with the coding.

Code for talking to ChatGPT

First, you need to import the speech recognition for NLP as well as GTTS. Alternatively, OpenAI Whisper can be used. Next, set the model for ChatGPT that you are going to utilise. Since we are supporting multiple languages, we will be using the “text-davinchi-003” model. You then set the OpenAI API.

Bill Of Materials
Components Quantity Description
Raspberry Pi 4 1 1GB
USB MIC 1 Microphone
Speaker 1 For audio out
5V DC adaptor 1 5V 2A

We have created the chatGPT function, where we define the chatGPT query prompt, model to use, temperature, and other relevant settings. You can modify these values as per your requirements. Fig. 2 showcases a code snippet that sets the API and GPT model name.

Code snippet setting the API and GPT model name
Fig. 2: Code snippet setting the API and GPT model name

Next, you create the loop function, which continuously checks the microphone and captures the human voice while filtering out background noise. Here comes the important part where you set the language code, enabling NLP to recognise the voice spoken in your chosen language and transfer it to ChatGPT to provide an answer in that same language. NLP then processes the answer and converts it into voice, enabling you to hear the response in your preferred language.

In the code, we set the language code to our desired language; for example, if you want to use Hindi, you set the language code as “hi.” This way, NLP will recognise whatever you say in Hindi, and ChatGPT will respond in Hindi, with NLP processing the answer as Hindi voice output. Fig. 3 illustrates a code snippet that sets the language code to be used.

 Code snippet setting the language code to use
Fig. 3: Code snippet setting the language code to use

Now that our code is ready, you can run it and speak in Hindi or any other language code you have set in the code. VoiceGPT will recognise and respond accordingly using ChatGPT. Fig. 4 showcases VoiceGPT recognising and responding in Hindi, providing both text and voice output.

VoiceGPT recognising Hindi and talking in Hindi with text as well as voice as output
Fig. 4: VoiceGPT recognising Hindi and talking in Hindi with text as well as voice as output

Multilingual voice chat with ChatGPT

But wait, we promised to make it multilingual, which means it will automatically detect the language and change the language for communication. You only need to run the code once; there is no need to manually change the language code for communication. Our next step is to modify the above code and create the next version that detects the language automatically. For this, we need another module called “languagedetect.” To install the module, run the following command in the terminal:

sudo pip3 install langdetect

Now, in the code, we import the “langdetect” Python module along with OpenAI and another Python module. We then create another function. Fig. 5 illustrates the process of importing the language detection module.

 Importing language detection module
Fig. 5: Importing language detection module

Following this, we create another function named “detect_language” that prompts for text input and automatically detects the language in the input query. You can input text in any language, such as Korean, Devanagari, Marlin, Latin, etc, and the function will automatically detect the language. Fig. 6 demonstrates the creation of the language detection function.

Creating language detection function
Fig. 6: Creating language detection function

After language detection, we set the language code to the detected language, using the value determined by the “detect_language” function. Within the while loop, we capture audio, extract the human voice, and use NLP in the detected language to convert it into text.

This text is then sent to ChatGPT as a query, and NLP converts the response into voice in the same language. Congratulations, your code is now ready, and the VoiceGPT speaker with multilingual support is prepared.

Testing ChatGPT voice control system

You can now run the code. It will prompt you to input a query in any language, and it will automatically detect and communicate with ChatGPT in that language. So, now you can talk to ChatGPT in any language; it will understand and reply using the same interface. Fig. 7 shows a testing code used to test this project.

 Code used for testing
Fig. 7: Code used for testing

Ashwini Kumar Sinha is a tech journalist at EFY