Update app.py
Browse files
app.py
CHANGED
@@ -50,6 +50,42 @@ max_new_tokens = 2048
|
|
50 |
# for token in model.generate(prompt=prompt, temp=temperature, top_k = top_k, top_p = top_p, max_tokens = max_new_tokens, streaming=True):
|
51 |
# outputs.append(token)
|
52 |
# yield "".join(outputs)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
async def generater(message, history, temperature, top_p, top_k):
|
54 |
# 构建prompt
|
55 |
prompt = ""
|
@@ -59,31 +95,76 @@ async def generater(message, history, temperature, top_p, top_k):
|
|
59 |
prompt += model.config["promptTemplate"].format(message)
|
60 |
|
61 |
# Debug: 打印最终的prompt以验证其正确性
|
62 |
-
print(f"Final prompt: {prompt}")
|
63 |
cmd = [
|
64 |
-
|
65 |
-
"-m",os.path.join(model_path, model_name),
|
66 |
"--prompt", prompt
|
67 |
]
|
68 |
|
69 |
-
#
|
70 |
-
process =
|
71 |
-
cmd,
|
|
|
|
|
|
|
72 |
)
|
73 |
|
74 |
# 初始占位符输出
|
75 |
yield "Generating response..."
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
|
88 |
|
89 |
def vote(data: gr.LikeData):
|
|
|
50 |
# for token in model.generate(prompt=prompt, temp=temperature, top_k = top_k, top_p = top_p, max_tokens = max_new_tokens, streaming=True):
|
51 |
# outputs.append(token)
|
52 |
# yield "".join(outputs)
|
53 |
+
|
54 |
+
# async def generater(message, history, temperature, top_p, top_k):
|
55 |
+
# # 构建prompt
|
56 |
+
# prompt = ""
|
57 |
+
# for user_message, assistant_message in history:
|
58 |
+
# prompt += model.config["promptTemplate"].format(user_message)
|
59 |
+
# prompt += assistant_message
|
60 |
+
# prompt += model.config["promptTemplate"].format(message)
|
61 |
+
|
62 |
+
# # Debug: 打印最终的prompt以验证其正确性
|
63 |
+
# print(f"Final prompt: {prompt}")
|
64 |
+
# cmd = [
|
65 |
+
# main_path,
|
66 |
+
# "-m",os.path.join(model_path, model_name),
|
67 |
+
# "--prompt", prompt
|
68 |
+
# ]
|
69 |
+
|
70 |
+
# # 使用subprocess.Popen调用./main并流式读取输出
|
71 |
+
# process = subprocess.Popen(
|
72 |
+
# cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True
|
73 |
+
# )
|
74 |
+
|
75 |
+
# # 初始占位符输出
|
76 |
+
# yield "Generating response..."
|
77 |
+
# # 异步等待并处理输出
|
78 |
+
# try:
|
79 |
+
# while True:
|
80 |
+
# line = process.stdout.readline()
|
81 |
+
# if not line:
|
82 |
+
# break # 如果没有更多的输出,结束循环
|
83 |
+
# print(f"Generated line: {line.strip()}") # Debug: 打印生成的每行
|
84 |
+
# yield line
|
85 |
+
# except Exception as e:
|
86 |
+
# print(f"Error during generation: {e}")
|
87 |
+
# yield "Sorry, an error occurred while generating the response."
|
88 |
+
|
89 |
async def generater(message, history, temperature, top_p, top_k):
|
90 |
# 构建prompt
|
91 |
prompt = ""
|
|
|
95 |
prompt += model.config["promptTemplate"].format(message)
|
96 |
|
97 |
# Debug: 打印最终的prompt以验证其正确性
|
98 |
+
print(f"Final prompt: {prompt}\n\n\n\n\n\n\n\n")
|
99 |
cmd = [
|
100 |
+
"./main", # 确保这个是可执行文件的正确路径
|
101 |
+
"-m", os.path.join(model_path, model_name),
|
102 |
"--prompt", prompt
|
103 |
]
|
104 |
|
105 |
+
# 创建异步子进程
|
106 |
+
process = await asyncio.create_subprocess_exec(
|
107 |
+
*cmd,
|
108 |
+
stdout=asyncio.subprocess.PIPE,
|
109 |
+
stderr=asyncio.subprocess.PIPE,
|
110 |
+
#text=True, # 这里设置text=True使得输出以字符串形式处理
|
111 |
)
|
112 |
|
113 |
# 初始占位符输出
|
114 |
yield "Generating response..."
|
115 |
+
|
116 |
+
# # 异步等待并逐字处理输出
|
117 |
+
# while True:
|
118 |
+
# char = await process.stdout.read(1) # 读取1字节
|
119 |
+
# if not char:
|
120 |
+
# break # 如果没有更多的输出,结束循环
|
121 |
+
# # 直接输出字符,这里假设输出是文本形式
|
122 |
+
# print(char, end='', flush=True) # 使用print来立即输出每个字符
|
123 |
+
# yield char
|
124 |
+
|
125 |
+
# while True:
|
126 |
+
# char = await process.stdout.read(1) # 读取1字节
|
127 |
+
# if not char:
|
128 |
+
# break # 如果没有更多的输出,结束循环
|
129 |
+
# # 将字节解码为字符串
|
130 |
+
# char_decoded = char.decode('utf-8')
|
131 |
+
# print(char_decoded, end='') # 使用print来立即输出每个字符
|
132 |
+
# yield "1"
|
133 |
+
|
134 |
+
# # 等待子进程结束
|
135 |
+
# await process.wait()
|
136 |
+
# 初始化一个空字节串用作缓冲区
|
137 |
+
buffer = b""
|
138 |
+
# 初始化一个空字符串用于累积解码的输出
|
139 |
+
accumulated_output = ""
|
140 |
+
|
141 |
+
while True:
|
142 |
+
# 尝试从stdout中读取更多的字节
|
143 |
+
more_bytes = await process.stdout.read(1)
|
144 |
+
if not more_bytes:
|
145 |
+
break # 没有更多的字节可以读取,结束循环
|
146 |
+
buffer += more_bytes # 将新读取的字节添加到缓冲区
|
147 |
+
|
148 |
+
try:
|
149 |
+
# 尝试解码整个缓冲区
|
150 |
+
decoded = buffer.decode('utf-8')
|
151 |
+
# 将成功解码的内容添加到累积的输出中
|
152 |
+
accumulated_output += decoded
|
153 |
+
# 输出累积的内容到屏幕上
|
154 |
+
print(f'\r{accumulated_output}', end='', flush=True)
|
155 |
+
yield accumulated_output
|
156 |
+
buffer = b"" # 清空缓冲区以接受新的输入
|
157 |
+
except UnicodeDecodeError:
|
158 |
+
# 解码失败,可能是因为字节不完整
|
159 |
+
# 继续循环,读取更多的字节
|
160 |
+
continue
|
161 |
+
|
162 |
+
# 循环结束后,处理缓冲区中剩余的字节
|
163 |
+
if buffer:
|
164 |
+
# 这里忽略解码错误,因为最后的字节可能不完整
|
165 |
+
remaining_output = buffer.decode('utf-8', errors='ignore')
|
166 |
+
accumulated_output += remaining_output
|
167 |
+
print(f'\r{accumulated_output}', end='', flush=True)
|
168 |
|
169 |
|
170 |
def vote(data: gr.LikeData):
|