streaming output and add keep alive arg
This commit is contained in:
@ -15,20 +15,14 @@ class TranslatorApp:
|
|||||||
|
|
||||||
def __init__(self, config_path: str = 'config.yaml'):
|
def __init__(self, config_path: str = 'config.yaml'):
|
||||||
self._config = parse_config(config_path)
|
self._config = parse_config(config_path)
|
||||||
self._lang_directions = {
|
|
||||||
'英->中': ('英文', '正體中文', '台灣地區用語'),
|
|
||||||
'中->英': ('正體中文', '英文', '自然通順'),
|
|
||||||
'中->日': ('正體中文', '日文', '自然通順'),
|
|
||||||
'日->中': ('日文', '正體中文', '自然通順')
|
|
||||||
}
|
|
||||||
self._chain = self._prepare_chain()
|
self._chain = self._prepare_chain()
|
||||||
|
|
||||||
def _prepare_chain(self) -> LLMChain:
|
def _prepare_chain(self) -> LLMChain:
|
||||||
"""Prepare the chain for translation"""
|
"""Prepare the chain for translation"""
|
||||||
template = (
|
template = (
|
||||||
'你是專業的翻譯人員,請判斷這段句子「{input_text}」的語言是否為{source_lang},若非的'
|
'你是專業的翻譯人員,請判斷這段句子「{input_text}」的語言是否為{source_lang},若非的'
|
||||||
'話則請回傳一模一樣的句子,若是的話則請將該句翻譯成{target_lang},並且符合{rule}(僅回'
|
'話則請回傳一模一樣的句子,若是的話則請將該句翻譯成{target_lang},並且符合{description}'
|
||||||
'傳翻譯結果即可)。'
|
'(僅回傳翻譯結果即可)。'
|
||||||
)
|
)
|
||||||
llm = ChatOllama(
|
llm = ChatOllama(
|
||||||
base_url=self._config['app']['ollama_url'],
|
base_url=self._config['app']['ollama_url'],
|
||||||
@ -36,59 +30,56 @@ class TranslatorApp:
|
|||||||
temperature=self._config['app']['ollama_temperature'],
|
temperature=self._config['app']['ollama_temperature'],
|
||||||
max_tokens=self._config['app']['ollama_max_tokens'],
|
max_tokens=self._config['app']['ollama_max_tokens'],
|
||||||
top_p=self._config['app']['ollama_top_p'],
|
top_p=self._config['app']['ollama_top_p'],
|
||||||
|
keep_alive=self._config['app']['ollama_keep_alive'],
|
||||||
stop=None
|
stop=None
|
||||||
)
|
)
|
||||||
prompt = PromptTemplate(
|
prompt = PromptTemplate(
|
||||||
input_variables=['input_text', 'source_lang', 'target_lang', 'rule'],
|
input_variables=['input_text', 'source_lang', 'target_lang', 'description'],
|
||||||
template=template
|
template=template
|
||||||
)
|
)
|
||||||
return prompt | llm
|
return prompt | llm
|
||||||
|
|
||||||
def run(self) -> None:
|
def run(self) -> None:
|
||||||
""" Run the Streamlit app """
|
""" Run the Streamlit app """
|
||||||
|
|
||||||
st.set_page_config(
|
st.set_page_config(
|
||||||
page_title=self._config['app']['page_title'],
|
page_title=self._config['app']['page_title'],
|
||||||
page_icon=self._config['app']['page_favicon_path'],
|
page_icon=self._config['app']['page_favicon_path'],
|
||||||
)
|
)
|
||||||
st.title(body=self._config['app']['page_title'])
|
st.title(body=self._config['app']['page_title'])
|
||||||
|
|
||||||
direction = st.radio(
|
direction = st.radio(
|
||||||
label='Direction',
|
label='語言',
|
||||||
options=list(self._lang_directions.keys()),
|
options=list(self._config['app']['lang_directions'].keys()),
|
||||||
index=0,
|
index=0,
|
||||||
key='lang_choice',
|
key='lang_choice',
|
||||||
horizontal=True
|
horizontal=True
|
||||||
)
|
)
|
||||||
|
|
||||||
input_text = st.text_area(
|
input_text = st.text_area(
|
||||||
label='Input',
|
label='輸入',
|
||||||
placeholder='請輸入文字',
|
placeholder='請輸入文字',
|
||||||
key='input_text'
|
key='input_text'
|
||||||
)
|
)
|
||||||
output_container = st.empty()
|
|
||||||
_ = output_container.text_area(
|
|
||||||
label='Output',
|
|
||||||
placeholder='',
|
|
||||||
disabled=True
|
|
||||||
)
|
|
||||||
|
|
||||||
if st.button('Go'):
|
output_container = st.empty()
|
||||||
|
|
||||||
|
if st.button('翻譯'):
|
||||||
if not input_text.strip():
|
if not input_text.strip():
|
||||||
st.warning("請輸入要翻譯的文字")
|
st.warning("請輸入要翻譯的文字")
|
||||||
return
|
return
|
||||||
|
|
||||||
with st.spinner('翻譯中...'):
|
with st.spinner('翻譯中...'):
|
||||||
|
|
||||||
source_lang, target_lang, rule = self._lang_directions[direction]
|
result = self._chain.stream({
|
||||||
result = self._chain.invoke({
|
|
||||||
'input_text': input_text,
|
'input_text': input_text,
|
||||||
'source_lang': source_lang,
|
'source_lang': self._config['app']['lang_directions'][direction]['source_lang'],
|
||||||
'target_lang': target_lang,
|
'target_lang': self._config['app']['lang_directions'][direction]['target_lang'],
|
||||||
'rule': rule
|
'description': self._config['app']['lang_directions'][direction]['description']
|
||||||
}).content
|
})
|
||||||
|
|
||||||
output_container.text_area(
|
output_container.write_stream(
|
||||||
label='Output',
|
stream=result
|
||||||
value=result,
|
|
||||||
disabled=True
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -6,3 +6,13 @@ app:
|
|||||||
ollama_max_tokens: 256
|
ollama_max_tokens: 256
|
||||||
ollama_temperature: 0.2
|
ollama_temperature: 0.2
|
||||||
ollama_top_p: 0.9
|
ollama_top_p: 0.9
|
||||||
|
ollama_keep_alive: 2m
|
||||||
|
lang_directions:
|
||||||
|
'英->中':
|
||||||
|
source_lang: '英文'
|
||||||
|
target_lang: '正體中文'
|
||||||
|
description: '台灣地區用語'
|
||||||
|
'中->英':
|
||||||
|
source_lang: '中文'
|
||||||
|
target_lang: '英文'
|
||||||
|
description: '自然通順'
|
Reference in New Issue
Block a user