Translate English to Japanese Using Streamlit and Google Translate API

Jiro Ishida
The Startup
Published in
3 min readFeb 22, 2021
Photo by Manuel Cosentino on Unsplash

I created a simple translation app using streamlit to highlight the simplicity of using the platform to quickly create apps. The app takes English input from a user which is then translated to Japanese.

Translate user input to Japanese

The app I created borrowed ideas from combination of different resources with the main influence from this video from YouTuber clueple c .

I added some extra features from the original app by adding in a text-to-speech feature, which creates an audio file that reads out the final translation. I also created a feature which allows a user to upload audio that can be transcribed and translated. Additional resources used included the following:

The initial set up of the code involved importing the relevant libraries needed for the app. The gtts library is Google’s text-to-speech API to read out the final translation, while the speech_recognition library was used to transcribe the audio. Google translator was then used to convert English text to Japanese. I had a few issues initially installing the streamlit library so there may be some issues depending on your version of python.

import streamlit as st
import datetime as dt
from gtts import gTTS
import speech_recognition as sr
from google_trans_new import google_translator
from PIL import Image

The next block of code involves setting up the main displays and buttons. This involved showing the date using the datetime package and displaying text using the write() function.

#image for top of the screen 
image = Image.open('translate.png')
st.image(image)
#date display
now = dt.date.today()
#Text Display
st.write(f"Today is {now}")
st.write(f"Translate your thoughts.")
input_text = st.text_input('Enter whatever')

The following code then gets the input text from the user and passes it into the translate function to convert to Japanese. After this, the result is also passed into the gtts function to convert to audio and then loaded to enable playback.

#Translates user input and creates text to speech audio

if st.button('Translate'):
result = trans.translate(input_text, lang_tgt = 'ja')
st.success(result)
speech = gTTS(text = result, lang = 'ja', slow = False)
speech.save('user_trans.mp3')
audio_file = open('user_trans.mp3', 'rb')
audio_bytes = audio_file.read()
st.audio(audio_bytes, format='audio/ogg',start_time=0)

The final block of code allows the user to upload an audio file then converts it to text. This text is then converted to Japanese by following a similar process as the user text input code block above.

if st.button('Translate audio'):     
# Initialize recognizer class (for recognizing the speech)
r = sr.Recognizer()

# Reading Audio file as source
# listening the audio file and store in audio_text variable

with sr.AudioFile(path) as source:
audio_text = r.listen(source)
# recoginize_() method will throw a request error if the API is unreachable, hence using exception handling

try:
# using google speech recognition
text = r.recognize_google(audio_text)
print('Converting audio transcripts into text ...')
input_text = st.write(text)
result = trans.translate(text, lang_tgt = 'ja')
st.success(result)
speech = gTTS(text = result, lang = 'ja', slow = False)
speech.save('trans.mp3')
audio_file = open('trans.mp3', 'rb')
audio_bytes = audio_file.read()
st.audio(audio_bytes, format='audio/ogg',start_time=0)
except:

st.write('Sorry.. run again...')

The final app should resemble something like the gif below.

Convert audio to text and then to Japanese

Hopefully this article has show how easy it is to create user friendly apps with streamlit. If you encounter any issues let me know!

--

--

Jiro Ishida
The Startup

Budding Data Scientist by day, mediocre Olympic Weightlifter/Hip-hop Producer by night.