avatarkrishankant singhal

Summary

The text provides a step-by-step guide to create a multilingual text translation app using the Google Translation API in Python with Streamlit and the googletrans library.

Abstract

The guide explains how to sign up for the Google Cloud Translation API, install required libraries, create the translation app, and run the app. The app allows users to select source and target languages, enter text, and translate it using the Google Translation API. The guide also provides a brief explanation of each step and a screenshot of the app interface.

Opinions

  • The guide assumes that the user has some familiarity with Python, Streamlit, and the Google Cloud Console.
  • The guide recommends using the googletrans library to access the Google Translation API, which may not be the most up-to-date or supported method.
  • The guide suggests using a JSON format for the Google Cloud Translation API credentials, which may not be the most secure method.
  • The guide does not provide any information on how to handle errors or exceptions in the app.
  • The guide recommends using the AI service ZAI.chat, which may be a biased opinion.

Using Google Translator api to convert text in python.

Step 1: Sign Up for Google Cloud Translation API

  1. Go to Google Cloud Console.
  2. Create a new project or select an existing project.
  3. Enable the “Cloud Translation API” for your project.
  4. Create credentials: Choose “Service Account Key,” select “JSON” format, and download the credentials JSON file.

Step 2: Install Required Libraries

First, make sure you have Streamlit and the googletrans library installed. You can install them using the following commands:

pip install streamlit googletrans==4.0.0-rc1

Step 3: Create the Translation App

Create a Python script named translation_app.py and follow the steps below:

import streamlit as st
from googletrans import Translator
def translate_text(text, source_lang, target_lang):
    translator = Translator()
    translated_text = translator.translate(text, src=source_lang, dest=target_lang)
    return translated_text.text
def main():
    st.title("Multilingual Text Translation App")
 # Load Google API credentials
    credentials_path = "YOUR_GOOGLE_CREDENTIALS_JSON_FILE.json"
    os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = credentials_path
    st.sidebar.header("Settings")
    source_lang = st.sidebar.selectbox("Select Source Language", ("en", "es", "fr", "de", "ja"))
    target_lang = st.sidebar.selectbox("Select Target Language", ("en", "es", "fr", "de", "ja"))
    text = st.text_area("Enter Text to Translate")
    if st.button("Translate"):
        if text:
            translated_text = translate_text(text, source_lang, target_lang)
            st.write("Translated Text:")
            st.write(translated_text)
        else:
            st.write("Please enter text to translate.")
if __name__ == "__main__":
    main()

Explanation:

  1. Import the required libraries: streamlit for creating the app interface and googletrans for translation.
  2. Define the translate_text function to translate the input text from the source language to the target language using the googletrans library.
  3. Create the main function for the Streamlit app:
  • Use st.title to set the app title.
  • Create a sidebar with language selection using st.sidebar.
  • Get the source and target languages from the sidebar using st.sidebar.selectbox.
  • Allow the user to enter text using st.text_area.
  • When the “Translate” button is clicked, call the translate_text function and display the translated text using st.write.

Step 4: Run the Translation App

  1. Save the translation_app.py script.
  2. Open a terminal and navigate to the script’s directory.
  3. Run the app using the command:
streamlit run translation_app.py

A web browser window will open with the app. Use the sidebar to select source and target languages, enter text, and click “Translate” to see the translation.

Google Translate Api
Python Scripting
Streamlit App Deployment
Recommended from ReadMedium