@@ -69,18 +69,24 @@ async def run_shell() -> None:
6969def run_web () -> None :
7070 import gradio as gr
7171
72+ check_law_chain = get_check_law_chain (config )
73+ chain = get_law_chain (config , out_callback = None )
74+
7275 async def chat (message , history ):
73- check_law_chain = get_check_law_chain (config )
74- out_callback = OutCallbackHandler ()
75- chain = get_law_chain (config , out_callback = out_callback )
7676
77+ out_callback = OutCallbackHandler ()
7778 is_law = check_law_chain .invoke ({"question" : message })
7879 if not is_law :
7980 yield "不好意思,我是法律AI助手,请提问和法律有关的问题。"
8081 return
8182
8283 task = asyncio .create_task (
83- chain .ainvoke ({"question" : message }))
84+ chain .ainvoke ({"question" : message }, config = {"callbacks" : [out_callback ]}))
85+
86+ async for new_token in out_callback .aiter ():
87+ pass
88+
89+ out_callback .done .clear ()
8490
8591 response = ""
8692 async for new_token in out_callback .aiter ():
@@ -92,12 +98,14 @@ async def chat(message, history):
9298 response += new_token
9399 yield response
94100
101+ out_callback .done .clear ()
102+
95103 demo = gr .ChatInterface (
96104 fn = chat , examples = ["故意杀了一个人,会判几年?" , "杀人自首会减刑吗?" ], title = "法律AI小助手" )
97105
106+ demo .queue ()
98107 demo .launch (
99108 server_name = config .WEB_HOST , server_port = config .WEB_PORT ,
100- enable_queue = True ,
101109 auth = (config .WEB_USERNAME , config .WEB_PASSWORD ),
102110 auth_message = "默认用户名密码: username / password" )
103111
0 commit comments