first usable type
This commit is contained in:
94
translator/app.py
Normal file
94
translator/app.py
Normal file
@ -0,0 +1,94 @@
|
||||
# app.py
|
||||
#
|
||||
# author: deng
|
||||
# date : 20250604
|
||||
|
||||
import streamlit as st
|
||||
from langchain.chains import LLMChain
|
||||
from langchain.prompts import PromptTemplate
|
||||
from langchain_community.chat_models import ChatOllama
|
||||
from utils import parse_config
|
||||
|
||||
|
||||
class TranslatorApp:
|
||||
"""Streamlit App for Language Translation"""
|
||||
|
||||
def __init__(self, config_path: str = 'config.yaml'):
|
||||
self._config = parse_config(config_path)
|
||||
self._lang_directions = {
|
||||
'英->中': ('英文', '正體中文', '台灣地區用語'),
|
||||
'中->英': ('正體中文', '英文', '自然通順'),
|
||||
'中->日': ('正體中文', '日文', '自然通順'),
|
||||
'日->中': ('日文', '正體中文', '自然通順')
|
||||
}
|
||||
self._chain = self._prepare_chain()
|
||||
|
||||
def _prepare_chain(self) -> LLMChain:
|
||||
"""Prepare the chain for translation"""
|
||||
template = (
|
||||
'你是專業的翻譯人員,請判斷這段句子「{input_text}」的語言是否為{source_lang},若非的'
|
||||
'話則請回傳一模一樣的句子,若是的話則請將該句翻譯成{target_lang},並且符合{rule}(僅回'
|
||||
'傳翻譯結果即可)。'
|
||||
)
|
||||
llm = ChatOllama(
|
||||
base_url=self._config['app']['ollama_url'],
|
||||
model=self._config['app']['ollama_model_name'],
|
||||
temperature=self._config['app']['ollama_temperature'],
|
||||
max_tokens=self._config['app']['ollama_max_tokens'],
|
||||
top_p=self._config['app']['ollama_top_p'],
|
||||
stop=None
|
||||
)
|
||||
prompt = PromptTemplate(
|
||||
input_variables=['input_text', 'source_lang', 'target_lang', 'rule'],
|
||||
template=template
|
||||
)
|
||||
return LLMChain(llm=llm, prompt=prompt)
|
||||
|
||||
def run(self) -> None:
|
||||
""" Run the Streamlit app """
|
||||
|
||||
st.title(body='跨過語言的黑水溝')
|
||||
direction = st.radio(
|
||||
label='Direction',
|
||||
options=list(self._lang_directions.keys()),
|
||||
index=0,
|
||||
key='lang_choice',
|
||||
horizontal=True
|
||||
)
|
||||
input_text = st.text_area(
|
||||
label='Input',
|
||||
placeholder='請輸入文字',
|
||||
key='input_text'
|
||||
)
|
||||
output_container = st.empty()
|
||||
_ = output_container.text_area(
|
||||
label='Output',
|
||||
placeholder='',
|
||||
disabled=True
|
||||
)
|
||||
|
||||
if st.button('Go'):
|
||||
if not input_text.strip():
|
||||
st.warning("請輸入要翻譯的文字")
|
||||
return
|
||||
|
||||
with st.spinner('翻譯中...'):
|
||||
|
||||
source_lang, target_lang, rule = self._lang_directions[direction]
|
||||
result = self._chain.run({
|
||||
'input_text': input_text,
|
||||
'source_lang': source_lang,
|
||||
'target_lang': target_lang,
|
||||
'rule': rule
|
||||
}).strip()
|
||||
|
||||
output_container.text_area(
|
||||
label='Output',
|
||||
value=result,
|
||||
disabled=True
|
||||
)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app = TranslatorApp()
|
||||
app.run()
|
||||
Reference in New Issue
Block a user