Spaces:
Running
Running
thread lock in chatglm
Browse files
request_llm/bridge_chatglm.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
|
| 2 |
from transformers import AutoModel, AutoTokenizer
|
| 3 |
import time
|
|
|
|
| 4 |
import importlib
|
| 5 |
from toolbox import update_ui, get_conf
|
| 6 |
from multiprocessing import Process, Pipe
|
|
@@ -18,6 +19,7 @@ class GetGLMHandle(Process):
|
|
| 18 |
self.success = True
|
| 19 |
self.check_dependency()
|
| 20 |
self.start()
|
|
|
|
| 21 |
|
| 22 |
def check_dependency(self):
|
| 23 |
try:
|
|
@@ -72,6 +74,7 @@ class GetGLMHandle(Process):
|
|
| 72 |
|
| 73 |
def stream_chat(self, **kwargs):
|
| 74 |
# 主进程执行
|
|
|
|
| 75 |
self.parent.send(kwargs)
|
| 76 |
while True:
|
| 77 |
res = self.parent.recv()
|
|
@@ -79,7 +82,7 @@ class GetGLMHandle(Process):
|
|
| 79 |
yield res
|
| 80 |
else:
|
| 81 |
break
|
| 82 |
-
|
| 83 |
|
| 84 |
global glm_handle
|
| 85 |
glm_handle = None
|
|
@@ -145,10 +148,13 @@ def predict(inputs, llm_kwargs, plugin_kwargs, chatbot, history=[], system_promp
|
|
| 145 |
history_feedin.append([history[2*i], history[2*i+1]] )
|
| 146 |
|
| 147 |
# 开始接收chatglm的回复
|
|
|
|
| 148 |
for response in glm_handle.stream_chat(query=inputs, history=history_feedin, max_length=llm_kwargs['max_length'], top_p=llm_kwargs['top_p'], temperature=llm_kwargs['temperature']):
|
| 149 |
chatbot[-1] = (inputs, response)
|
| 150 |
yield from update_ui(chatbot=chatbot, history=history)
|
| 151 |
|
| 152 |
# 总结输出
|
|
|
|
|
|
|
| 153 |
history.extend([inputs, response])
|
| 154 |
yield from update_ui(chatbot=chatbot, history=history)
|
|
|
|
| 1 |
|
| 2 |
from transformers import AutoModel, AutoTokenizer
|
| 3 |
import time
|
| 4 |
+
import threading
|
| 5 |
import importlib
|
| 6 |
from toolbox import update_ui, get_conf
|
| 7 |
from multiprocessing import Process, Pipe
|
|
|
|
| 19 |
self.success = True
|
| 20 |
self.check_dependency()
|
| 21 |
self.start()
|
| 22 |
+
self.threadLock = threading.Lock()
|
| 23 |
|
| 24 |
def check_dependency(self):
|
| 25 |
try:
|
|
|
|
| 74 |
|
| 75 |
def stream_chat(self, **kwargs):
|
| 76 |
# 主进程执行
|
| 77 |
+
self.threadLock.acquire()
|
| 78 |
self.parent.send(kwargs)
|
| 79 |
while True:
|
| 80 |
res = self.parent.recv()
|
|
|
|
| 82 |
yield res
|
| 83 |
else:
|
| 84 |
break
|
| 85 |
+
self.threadLock.release()
|
| 86 |
|
| 87 |
global glm_handle
|
| 88 |
glm_handle = None
|
|
|
|
| 148 |
history_feedin.append([history[2*i], history[2*i+1]] )
|
| 149 |
|
| 150 |
# 开始接收chatglm的回复
|
| 151 |
+
response = "[Local Message]: 等待ChatGLM响应中 ..."
|
| 152 |
for response in glm_handle.stream_chat(query=inputs, history=history_feedin, max_length=llm_kwargs['max_length'], top_p=llm_kwargs['top_p'], temperature=llm_kwargs['temperature']):
|
| 153 |
chatbot[-1] = (inputs, response)
|
| 154 |
yield from update_ui(chatbot=chatbot, history=history)
|
| 155 |
|
| 156 |
# 总结输出
|
| 157 |
+
if response == "[Local Message]: 等待ChatGLM响应中 ...":
|
| 158 |
+
response = "[Local Message]: ChatGLM响应异常 ..."
|
| 159 |
history.extend([inputs, response])
|
| 160 |
yield from update_ui(chatbot=chatbot, history=history)
|