increase init loading speed
This commit is contained in:
@ -5,7 +5,6 @@
|
||||
|
||||
import streamlit as st
|
||||
from langchain.prompts import ChatPromptTemplate
|
||||
from langchain_core.runnables import Runnable
|
||||
from langchain_ollama import ChatOllama
|
||||
from langchain_openai import ChatOpenAI
|
||||
from utils import parse_config
|
||||
@ -16,10 +15,13 @@ class TranslatorApp:
|
||||
|
||||
def __init__(self, config_path: str = 'config.yaml'):
|
||||
self._config = parse_config(config_path)
|
||||
self._chain = self._prepare_chain()
|
||||
self._chain = None
|
||||
|
||||
def _prepare_chain(self) -> Runnable:
|
||||
def _prepare_chain(self) -> None:
|
||||
"""Prepare the chain for translation"""
|
||||
if self._chain is not None:
|
||||
return
|
||||
|
||||
system_template = (
|
||||
'你是專業的翻譯人員,請判斷接下來句子的語言是否為{source_lang},若是的話則請將該句翻譯成{target_lang},'
|
||||
'並且符合{description}(僅回傳翻譯結果即可),若非的話則請回傳一模一樣的句子。'
|
||||
@ -50,11 +52,12 @@ class TranslatorApp:
|
||||
('system', system_template),
|
||||
('human', user_template)
|
||||
])
|
||||
return prompt | llm
|
||||
self._chain = prompt | llm
|
||||
|
||||
def run(self) -> None:
|
||||
""" Run the Streamlit app """
|
||||
|
||||
# Interface
|
||||
st.set_page_config(
|
||||
page_title=self._config['app']['page_title'],
|
||||
page_icon=self._config['app']['page_favicon_path'],
|
||||
@ -76,20 +79,21 @@ class TranslatorApp:
|
||||
translate_button = st.button('翻譯')
|
||||
output_container = st.empty()
|
||||
|
||||
|
||||
# Prepare chain
|
||||
self._prepare_chain()
|
||||
|
||||
# Action
|
||||
if input_text or translate_button:
|
||||
if not input_text.strip():
|
||||
st.warning("請輸入要翻譯的文字")
|
||||
else:
|
||||
with st.spinner('翻譯中...'):
|
||||
|
||||
result = self._chain.stream({
|
||||
'input_text': input_text,
|
||||
'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.write_stream(
|
||||
stream=result
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user