Build your own AI Language translator with Lyzr Automata and Streamlit

Harshit
3 min readMar 21, 2024

--

In today’s interconnected world, effective translation plays a pivotal role in breaking down language barriers and fostering communication across diverse cultures. With the advancements in AI and Natural Language Processing (NLP), creating a text translation tool has become more accessible than ever. In this blog, we’ll explore how to build a powerful text translator using Lyzr Automata, a versatile framework for creating AI-driven conversational agents, and Streamlit, a popular Python library for building interactive web applications.

Introduction to Lyzr Automata

Lyzr Automata is a robust framework designed to streamline the development of conversational AI agents and intricate pipelines. Offering a comprehensive suite of tools, Lyzr Automata empowers developers to effortlessly define agents, tasks, and pipelines, facilitating the orchestration of complex AI workflows. With its intuitive interface and flexible architecture, Lyzr Automata caters to a wide range of applications, from natural language understanding to task automation and beyond. By abstracting away the complexities of AI development, Lyzr Automata accelerates the creation process, allowing developers to focus on crafting innovative solutions and delivering exceptional user experiences.

Building the Text Translation Agent

Our goal is to create a language translator agent capable of translating sentences from one language to another using Lyzr Automata and Streamlit.

Let’s break down the key components of our implementation:

Import Libraries:

The code begins by importing necessary libraries:

  • streamlit : for building the web app interface.
  • lyzr_automata libraries: for defining AI models, agents, and tasks.
  • dotenv: for loading environment variables (API key).
pip install lyzr_automata python-dotenv streamlit 
import streamlit as st
from lyzr_automata.ai_models.openai import OpenAIModel
from lyzr_automata import Agent,Task
from lyzr_automata.pipelines.linear_sync_pipeline import LinearSyncPipeline
from dotenv import load_dotenv
import os

Load API Key:

API key for OpenAI is loaded from a .env file using load_dotenv and os.getenv. Make sure you have an OpenAI account and API key set up before running the app.

load_dotenv()
api = os.getenv("OPENAI_API_KEY")#insert your API key here

User Input:

  • st.text_input creates two text boxes for users to enter a sentence and the target language.
sentence = st.text_input("Enter a sentence:")
language = st.text_input("Enter Language:")

Define OpenAI Model:

  • An OpenAIModel object is created using the loaded API key.
  • Model parameters like “gpt-4-turbo-preview” (replace with your desired model), temperature, and maximum output length are specified.
open_ai_text_completion_model = OpenAIModel(
api_key=api,
parameters={
"model": "gpt-4-turbo-preview",
"temperature": 0.2,
"max_tokens": 1500,
},
)

Define Text Translator Function:

This function handles the language translation process:

  • A prompt defining the translator’s role and expertise is set.
  • An Agent object is created representing the "Translation expert".
  • A Task object is defined with the model, agent, instructions for translation, and the provided sentence and language.
  • A LinearSyncPipeline is created to execute the language translation task.
  • The function runs the pipeline and returns the translated text from the task output.
def text_translator(topic,language):
translator_prompt = "You are an Expert LINGUIST and TRANSLATOR. Your task is to PROVIDE GUIDANCE on how to EFFECTIVELY translate texts between languages."
mcq_agent = Agent(
role="Translation expert",
prompt_persona=translator_prompt
)

mcq_task = Task(
name="Text Translation",
model=open_ai_text_completion_model,
agent=mcq_agent,
instructions=f"your task is to translate {topic} in {language} and only give translated text",
)

output = LinearSyncPipeline(
name="Translation Details",
completion_message="pipeline completed",
tasks=[
mcq_task
],
).run()

return output[0]['task_output']

Translation Output

  • A button labeled “Translate” triggers the translation process when clicked.
  • Clicking the button calls text_translator with the user-provided sentence and language.
  • The translated text is displayed using st.markdown.
if st.button("Translate"):
translation = text_translator(sentence,language)
st.markdown(translation)

Running the App:

Run command in cmd

streamlit run <app_name.py>

The Lyzr Text Translator Agent demonstrates the power of Streamlit and OpenAI for building user-friendly AI applications.

This blog post provides a starting point for exploring AI-powered translator for text with Lyzr Automata,Streamlit and OpenAI. Feel free to experiment and enhance the app based on your specific needs!

try it now:https://lyzr-text-translator.streamlit.app/

For more information explore the website: Lyzr

--

--