Translator / app.py
teganmosi's picture
Update app.py
16369f3
# -*- coding: utf-8 -*-
"""Any Language Translator.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1cJmy7eHRvwkQJDWOg775L2iSMjfR4dFx
"""
import os
import openai
import gradio as gr
from dotenv import load_dotenv, find_dotenv
_ = load_dotenv(find_dotenv()) # read local .env file
os.environ["OPENAI_API_KEY"] = "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"
openai.api_key = os.environ['OPENAI_API_KEY']
from langchain.chat_models import ChatOpenAI
from langchain.chains import LLMChain
from langchain.prompts import PromptTemplate
llm = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0)
translation_template = "Translate the following text from {source_language} to {target_language}: {text}"
translation_prompt = PromptTemplate(input_variables=["source_language", "target_language", "text"], template=translation_template)
translation_chain = LLMChain(llm=llm, prompt=translation_prompt)
def translate(source_language: str, target_language: str, text: str) -> str:
translated_text = translation_chain.predict(source_language=source_language, target_language=target_language, text=text)
return translated_text
inputs = [
gr.inputs.Textbox(label="Source Language"),
gr.inputs.Textbox(label="Target Language"),
gr.inputs.Textbox(label="Text to Translate")
]
output = gr.outputs.Textbox(label="Translated Text")
gr.Interface(fn=translate, inputs=inputs, outputs=output).launch()