File size: 1,491 Bytes
74d018c
 
 
 
 
 
 
 
 
439cf62
74d018c
 
 
 
 
 
 
16369f3
74d018c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5ac9eda
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# -*- 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()