Spaces:
Runtime error
Runtime error
Upload any_language_translator.py
Browse files- any_language_translator.py +46 -0
any_language_translator.py
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
2 |
+
"""Any Language Translator.ipynb
|
3 |
+
|
4 |
+
Automatically generated by Colaboratory.
|
5 |
+
|
6 |
+
Original file is located at
|
7 |
+
https://colab.research.google.com/drive/1cJmy7eHRvwkQJDWOg775L2iSMjfR4dFx
|
8 |
+
"""
|
9 |
+
|
10 |
+
!pip install gradio
|
11 |
+
!pip install langchain==0.0.208 deeplake openai tiktoken
|
12 |
+
!pip install python-dotenv
|
13 |
+
|
14 |
+
import os
|
15 |
+
import openai
|
16 |
+
import gradio as gr
|
17 |
+
|
18 |
+
from dotenv import load_dotenv, find_dotenv
|
19 |
+
_ = load_dotenv(find_dotenv()) # read local .env file
|
20 |
+
os.environ["OPENAI_API_KEY"] = "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"
|
21 |
+
|
22 |
+
openai.api_key = os.environ['OPENAI_API_KEY']
|
23 |
+
|
24 |
+
from langchain.chat_models import ChatOpenAI
|
25 |
+
from langchain.chains import LLMChain
|
26 |
+
from langchain.prompts import PromptTemplate
|
27 |
+
|
28 |
+
llm = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0)
|
29 |
+
|
30 |
+
translation_template = "Translate the following text from {source_language} to {target_language}: {text}"
|
31 |
+
translation_prompt = PromptTemplate(input_variables=["source_language", "target_language", "text"], template=translation_template)
|
32 |
+
translation_chain = LLMChain(llm=llm, prompt=translation_prompt)
|
33 |
+
|
34 |
+
def translate(source_language: str, target_language: str, text: str) -> str:
|
35 |
+
translated_text = translation_chain.predict(source_language=source_language, target_language=target_language, text=text)
|
36 |
+
return translated_text
|
37 |
+
|
38 |
+
inputs = [
|
39 |
+
gr.inputs.Textbox(label="Source Language"),
|
40 |
+
gr.inputs.Textbox(label="Target Language"),
|
41 |
+
gr.inputs.Textbox(label="Text to Translate")
|
42 |
+
]
|
43 |
+
|
44 |
+
output = gr.outputs.Textbox(label="Translated Text")
|
45 |
+
|
46 |
+
gr.Interface(fn=translate, inputs=inputs, outputs=output).launch(share=True)
|