, and Siri are the ever present voice assistants that serve a lot of the web linked inhabitants in the present day. For probably the most half, English is the dominant language used with these voice assistants. Nonetheless, for a voice assistant to be really useful, it should have the ability to perceive the person as they naturally communicate. In lots of elements of the world, particularly in a various nation like India, it is not uncommon for folks to be multilingual and to modify between a number of languages in a single dialog. A really sensible assistant ought to have the ability to deal with this.
Google Assistant affords the flexibility so as to add a second language; however its performance is proscribed to sure gadgets solely and affords this just for a restricted set of main languages. For instance, Google’s Nest Hub doesn’t but assist bilingual capabilities for Tamil, a language spoken by over 80 million folks. Alexa helps bilingual method so long as it’s supported in its inner language pair; once more this solely helps a restricted set of main languages. Siri doesn’t have bilingual functionality and permits just one language at a time.
On this article I’ll talk about the method taken to allow my Voice Assistant to have a bilingual functionality with English and Tamil because the languages. Utilizing this method, the voice assistant will have the ability to routinely detect the language an individual is talking by analyzing the audio immediately. By utilizing a “confidence rating”-based algorithm, the system will decide if English or Tamil is spoken and reply within the corresponding language.
Strategy to Bilingual Functionality
To make the assistant perceive each English and Tamil, there are a couple of potential options. The primary method can be to coach a customized Machine Studying mannequin from scratch, particularly on Tamil language knowledge, after which combine that mannequin into the Raspberry Pi. Whereas this could supply a excessive diploma of customization, it’s an extremely time-consuming and resource-intensive course of. Coaching a mannequin requires a large dataset and vital computational energy. Moreover, working a heavy customized mannequin would doubtless decelerate the Raspberry Pi, resulting in a poor person expertise.
fastText Strategy
A extra sensible answer is to make use of an present, pre-trained mannequin that’s already optimized for a selected job. For language identification, a fantastic choice is fastText.
fastText is an open-source library from Fb AI Analysis designed for environment friendly textual content classification and phrase illustration. It comes with pre-trained fashions that may shortly and precisely determine the language of a given piece of textual content from numerous languages. As a result of it’s light-weight and extremely optimized, it is a wonderful selection for working on a resource-constrained system like a Raspberry Pi with out inflicting vital efficiency points. The plan, subsequently, was to make use of fastText
to categorise the person’s spoken language.
To make use of fastText, you obtain the corresponding mannequin (lid.176.bin) and retailer it in your undertaking folder. Specify this because the MODEL_PATH and cargo the mannequin.
import fastText
import speech_recognition as sr
import fasttext
# --- Configuration ---
MODEL_PATH = "./lid.176.bin" # That is the mannequin file you downloaded and unzipped
# --- Important Utility Logic ---
print("Loading fastText language identification mannequin...")
strive:
# Load the pre-trained mannequin
mannequin = fasttext.load_model(MODEL_PATH)
besides Exception as e:
print(f"FATAL ERROR: Couldn't load the fastText mannequin. Error: {e}")
exit()
The following step can be to move the voice instructions, as recordings, to the mannequin and get the prediction again. This may be achieved by means of a devoted perform.
def identify_language(textual content, mannequin):
# The mannequin.predict() perform returns a tuple of labels and possibilities
predictions = mannequin.predict(textual content, ok=1)
language_code = predictions[0][0] # e.g., '__label__en'
return language_code
strive:
with microphone as supply:
recognizer.adjust_for_ambient_noise(supply, length=1)
print("nPlease communicate now...")
audio = recognizer.hear(supply, phrase_time_limit=8)
print("Transcribing audio...")
# Get a tough transcription with out specifying a language
transcription = recognizer.recognize_google(audio)
print(f"Heard: "{transcription}"")
# Establish the language from the transcribed textual content
language = identify_language(transcription, mannequin)
if language == '__label__en':
print("n---> End result: The detected language is English. <---")
elif language == '__label__ta':
print("n---> End result: The detected language is Tamil. <---")
else:
print(f"n---> End result: Detected a unique language: {language}")
besides sr.UnknownValueError:
print("Couldn't perceive the audio.")
besides sr.RequestError as e:
print(f"Speech recognition service error; {e}")
besides Exception as e:
print(f"An surprising error occurred: {e}")
The code block above follows a easy path. It makes use of the recognizer.recognize_google(audio) perform to transcribe the voice command after which passes this transcription to the fastText mannequin to get a prediction on the language. If the prediction is “__label__en” then English has been detected and if prediction is “__label_ta” then Tamil has been detected.
This method led to poor predictions although. The issue is that speech_recognition library defaults to English. So after I communicate one thing in Tamil, it finds the closest (and incorrect) equal sounding phrases in English and passes it to fastText.
For instance after I stated “En Peyar enna” (What’s my Title in Tamil), speech_recognition understood it as “Empire NA” and therefore fastText predicted the language as English. To beat this, I can hardcode the speech_recognition perform to detect solely Tamil. However this could defeat the concept of being really ‘sensible’ and ‘bilingual’. The assistant ought to have the ability to detect the language primarily based on what’s spoken; not primarily based on what is tough coded.
The ‘Confidence Rating’ methodology
What we’d like is a extra direct and data-driven methodology. The answer lies inside a characteristic of the speech_recognition library. The recognizer.recognize_google() perform is the Google Speech Recognition API and it could possibly transcribe audio from an enormous variety of languages, together with each English and Tamil. A key characteristic of this API is that for each transcription it offers, it could possibly additionally return a confidence rating — a numerical worth between 0 and 1, indicating how sure it’s that its transcription is right.
This characteristic permits for a way more elegant and dynamic method to language identification. Let’s check out the code.
def recognize_with_confidence(recognizer, audio_data):
tamil_text = None
tamil_confidence = 0.0
english_text = None
english_confidence = 0.0
# 1. Try to acknowledge as Tamil and get confidence
strive:
print("Making an attempt to transcribe as Tamil...")
# show_all=True returns a dictionary with transcription options
response_tamil = recognizer.recognize_google(audio_data, language='ta-IN', show_all=True)
# We solely take a look at the highest different
if response_tamil and 'different' in response_tamil:
top_alternative = response_tamil['alternative'][0]
tamil_text = top_alternative['transcript']
if 'confidence' in top_alternative:
tamil_confidence = top_alternative['confidence']
else:
tamil_confidence = 0.8 # Assign a default excessive confidence if not supplied
besides sr.UnknownValueError:
print("Couldn't perceive audio as Tamil.")
besides sr.RequestError as e:
print(f"Tamil recognition service error; {e}")
# 2. Try to acknowledge as English and get confidence
strive:
print("Making an attempt to transcribe as English...")
response_english = recognizer.recognize_google(audio_data, language='en-US', show_all=True)
if response_english and 'different' in response_english:
top_alternative = response_english['alternative'][0]
english_text = top_alternative['transcript']
if 'confidence' in top_alternative:
english_confidence = top_alternative['confidence']
else:
english_confidence = 0.8 # Assign a default excessive confidence
besides sr.UnknownValueError:
print("Couldn't perceive audio as English.")
besides sr.RequestError as e:
print(f"English recognition service error; {e}")
# 3. Examine confidence scores and return the winner
print(f"nConfidence Scores -> Tamil: {tamil_confidence:.2f}, English: {english_confidence:.2f}")
if tamil_confidence > english_confidence:
return tamil_text, "Tamil"
elif english_confidence > tamil_confidence:
return english_text, "English"
else:
# If scores are equal (or each zero), return neither
return None, None
The logic on this code block is straightforward. We move the audio to the recognize_google() perform and get the entire record of options and its scores. First we strive the language as Tamil and get the corresponding confidence rating. Then we strive the identical audio as English and get the corresponding confidence rating from the API. As soon as we have now each, we then examine the arrogance scores and select the one with the upper rating because the language detected by the system.
Under is the output of the perform after I communicate in English and after I communicate in Tamil.


The outcomes above present how the code is ready to perceive the language spoken dynamically, primarily based on the arrogance rating.
Placing all of it collectively — The Bilingual Assistant
The ultimate step can be to combine this method into the code for the Raspberry Pi primarily based Voice assistant. The complete code could be present in my GitHub. As soon as built-in the subsequent step can be to check the functioning of the Voice Assistant by talking in English and Tamil and seeing the way it responds for every language. The recordings beneath exhibit the working of the Bilingual Voice Assistant when requested a query in English and in Tamil.
Conclusion
On this article, we have now seen tips on how to efficiently improve a easy voice assistant into a very bilingual instrument. By implementing a “confidence rating” algorithm, the system could be made to find out whether or not a command is spoken in English or Tamil, permitting it to grasp and reply within the person’s chosen language for that particular question. This creates a extra pure and seamless conversational expertise.
The important thing benefit of this methodology is its reliability and scalability. Whereas this undertaking centered on simply two languages, the identical confidence rating logic might simply be prolonged to assist three, 4, or extra by merely including an API name for every new language and evaluating all the outcomes. The methods explored right here function a strong basis for creating extra superior and intuitive private AI instruments.
Reference:
[1] A. Joulin, E. Grave, P. Bojanowski, T. Mikolov, Bag of Tricks for Efficient Text Classification
[2] A. Joulin, E. Grave, P. Bojanowski, M. Douze, H. Jégou, T. Mikolov, FastText.zip: Compressing text classification models