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'):
|
||||
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}(僅回'
|
||||
'傳翻譯結果即可)。'
|
||||
'話則請回傳一模一樣的句子,若是的話則請將該句翻譯成{target_lang},並且符合{description}'
|
||||
'(僅回傳翻譯結果即可)。'
|
||||
)
|
||||
llm = ChatOllama(
|
||||
base_url=self._config['app']['ollama_url'],
|
||||
@ -36,59 +30,56 @@ class TranslatorApp:
|
||||
temperature=self._config['app']['ollama_temperature'],
|
||||
max_tokens=self._config['app']['ollama_max_tokens'],
|
||||
top_p=self._config['app']['ollama_top_p'],
|
||||
keep_alive=self._config['app']['ollama_keep_alive'],
|
||||
stop=None
|
||||
)
|
||||
prompt = PromptTemplate(
|
||||
input_variables=['input_text', 'source_lang', 'target_lang', 'rule'],
|
||||
input_variables=['input_text', 'source_lang', 'target_lang', 'description'],
|
||||
template=template
|
||||
)
|
||||
return prompt | llm
|
||||
|
||||
def run(self) -> None:
|
||||
""" Run the Streamlit app """
|
||||
|
||||
st.set_page_config(
|
||||
page_title=self._config['app']['page_title'],
|
||||
page_icon=self._config['app']['page_favicon_path'],
|
||||
)
|
||||
st.title(body=self._config['app']['page_title'])
|
||||
|
||||
direction = st.radio(
|
||||
label='Direction',
|
||||
options=list(self._lang_directions.keys()),
|
||||
label='語言',
|
||||
options=list(self._config['app']['lang_directions'].keys()),
|
||||
index=0,
|
||||
key='lang_choice',
|
||||
horizontal=True
|
||||
)
|
||||
|
||||
input_text = st.text_area(
|
||||
label='Input',
|
||||
label='輸入',
|
||||
placeholder='請輸入文字',
|
||||
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():
|
||||
st.warning("請輸入要翻譯的文字")
|
||||
return
|
||||
|
||||
with st.spinner('翻譯中...'):
|
||||
|
||||
source_lang, target_lang, rule = self._lang_directions[direction]
|
||||
result = self._chain.invoke({
|
||||
result = self._chain.stream({
|
||||
'input_text': input_text,
|
||||
'source_lang': source_lang,
|
||||
'target_lang': target_lang,
|
||||
'rule': rule
|
||||
}).content
|
||||
'source_lang': self._config['app']['lang_directions'][direction]['source_lang'],
|
||||
'target_lang': self._config['app']['lang_directions'][direction]['target_lang'],
|
||||
'description': self._config['app']['lang_directions'][direction]['description']
|
||||
})
|
||||
|
||||
output_container.text_area(
|
||||
label='Output',
|
||||
value=result,
|
||||
disabled=True
|
||||
output_container.write_stream(
|
||||
stream=result
|
||||
)
|
||||
|
||||
|
||||
|
@ -5,4 +5,14 @@ app:
|
||||
ollama_model_name: gemma3:12b-it-qat
|
||||
ollama_max_tokens: 256
|
||||
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