Update app.py
Browse files
app.py
CHANGED
@@ -23,7 +23,7 @@ CSS = """
|
|
23 |
|
24 |
def format_message(content):
|
25 |
if not content:
|
26 |
-
return ""
|
27 |
|
28 |
# 查找 <think> 标签并替换为折叠内容
|
29 |
think_pattern = re.compile(r"<think>(.*?)</think>", re.DOTALL)
|
@@ -46,8 +46,10 @@ def format_message(content):
|
|
46 |
# 去掉 </details> 后面多余的 <br>
|
47 |
formatted_content = re.sub(r"(</details>)(<br>)+", r"\1", formatted_content)
|
48 |
|
49 |
-
#
|
50 |
-
return formatted_content
|
|
|
|
|
51 |
|
52 |
def user_input(user_message, history):
|
53 |
if history is None:
|
@@ -90,26 +92,31 @@ def predict(message, chat_history, system_msg, temperature, top_p, repetition_pe
|
|
90 |
)
|
91 |
|
92 |
buffer = ""
|
93 |
-
history = chat_history.copy()
|
94 |
|
95 |
-
#
|
96 |
for chunk in response.iter_lines():
|
97 |
if chunk:
|
98 |
chunk_str = chunk.decode("utf-8").replace("data: ", "")
|
99 |
try:
|
100 |
chunk_data = json.loads(chunk_str)
|
101 |
if "choices" in chunk_data:
|
102 |
-
delta = chunk_data["choices"][0]
|
103 |
if "content" in delta:
|
|
|
104 |
buffer += delta["content"]
|
105 |
-
|
106 |
-
|
107 |
-
|
|
|
|
|
108 |
except Exception as e:
|
109 |
print(f"Error in processing chunk: {e}")
|
110 |
continue
|
111 |
|
112 |
-
|
|
|
|
|
113 |
|
114 |
# JavaScript to ensure the details tag is working properly
|
115 |
js_script = """
|
@@ -230,7 +237,7 @@ NPC只有四个,分别是:有狐娘,蜘蛛娘,猫娘,狼女。
|
|
230 |
with gr.Accordion("高级设置", open=False):
|
231 |
temperature = gr.Slider(0, 2, value=0.7, label="温度")
|
232 |
top_p = gr.Slider(0, 1, value=0.7, label="Top-p")
|
233 |
-
repetition_penalty = gr.Slider(1, 2, value=1.
|
234 |
|
235 |
# Ensure that the user input is submitted correctly
|
236 |
msg.submit(
|
|
|
23 |
|
24 |
def format_message(content):
|
25 |
if not content:
|
26 |
+
return gr.HTML("")
|
27 |
|
28 |
# 查找 <think> 标签并替换为折叠内容
|
29 |
think_pattern = re.compile(r"<think>(.*?)</think>", re.DOTALL)
|
|
|
46 |
# 去掉 </details> 后面多余的 <br>
|
47 |
formatted_content = re.sub(r"(</details>)(<br>)+", r"\1", formatted_content)
|
48 |
|
49 |
+
# 返回 gr.HTML 对象以确保正确渲染
|
50 |
+
return gr.HTML(formatted_content)
|
51 |
+
|
52 |
+
|
53 |
|
54 |
def user_input(user_message, history):
|
55 |
if history is None:
|
|
|
92 |
)
|
93 |
|
94 |
buffer = ""
|
95 |
+
history = chat_history.copy() + [[message, None]] # 初始化新对话
|
96 |
|
97 |
+
# 流式处理关键修改点
|
98 |
for chunk in response.iter_lines():
|
99 |
if chunk:
|
100 |
chunk_str = chunk.decode("utf-8").replace("data: ", "")
|
101 |
try:
|
102 |
chunk_data = json.loads(chunk_str)
|
103 |
if "choices" in chunk_data:
|
104 |
+
delta = chunk_data["choices"][0].get("delta", {})
|
105 |
if "content" in delta:
|
106 |
+
# 增量更新缓冲区
|
107 |
buffer += delta["content"]
|
108 |
+
|
109 |
+
# 只更新当前消息的最新状态
|
110 |
+
new_history = history.copy()
|
111 |
+
new_history[-1][1] = format_message(buffer).value # 提取 HTML 字符串
|
112 |
+
yield new_history
|
113 |
except Exception as e:
|
114 |
print(f"Error in processing chunk: {e}")
|
115 |
continue
|
116 |
|
117 |
+
# 最终确认更新
|
118 |
+
history[-1][1] = format_message(buffer).value
|
119 |
+
yield history
|
120 |
|
121 |
# JavaScript to ensure the details tag is working properly
|
122 |
js_script = """
|
|
|
237 |
with gr.Accordion("高级设置", open=False):
|
238 |
temperature = gr.Slider(0, 2, value=0.7, label="温度")
|
239 |
top_p = gr.Slider(0, 1, value=0.7, label="Top-p")
|
240 |
+
repetition_penalty = gr.Slider(1, 2, value=1.1, label="重复惩罚")
|
241 |
|
242 |
# Ensure that the user input is submitted correctly
|
243 |
msg.submit(
|