-
Notifications
You must be signed in to change notification settings - Fork 17
/
llm_chain_translator_app.py
63 lines (51 loc) · 1.96 KB
/
llm_chain_translator_app.py
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import streamlit as st
from dotenv import find_dotenv, load_dotenv
from langchain import LLMChain
from langchain.chains import LLMChain
from langchain.chat_models import ChatOpenAI
from langchain.prompts.chat import (ChatPromptTemplate,
HumanMessagePromptTemplate,
SystemMessagePromptTemplate)
from loguru import logger
_ = load_dotenv(find_dotenv())
# os.getenv('OPENAI_API_KEY')
class App:
def __init__(self) -> None:
self.lang_list = [
"English",
"German",
"Simplified Chinese",
"Traditional Chinese",
]
self.sys_msg_tmpl = SystemMessagePromptTemplate.from_template(
"You're a machine language translator. You can translate language from {src_lang} to {dist_lang}. Optimize the result is {optimising}."
)
self.human_msg_tmpl = HumanMessagePromptTemplate.from_template(
"Translate this sentence: {sentence}"
)
self.chat_prompt = ChatPromptTemplate.from_messages(
[self.sys_msg_tmpl, self.human_msg_tmpl]
)
self.llm_chain = LLMChain(llm=ChatOpenAI(), prompt=self.chat_prompt)
def __call__(self):
st.write("# Translator")
text = st.text_input("Your text")
col1, col2 = st.columns(2)
with col1:
src_lang = st.selectbox("Source Language", self.lang_list)
with col2:
dist_lang = st.selectbox("Destination Language", self.lang_list)
is_opt = st.checkbox("Optimize result", value=False)
optimising = "necessary" if is_opt else "unnecessary"
if text:
answer = self.llm_chain.run(
src_lang=src_lang,
dist_lang=dist_lang,
optimising=optimising,
sentence=text,
)
logger.info(f"Answer: {answer}")
st.write(answer)
if __name__ == "__main__":
app = App()
app()