ValueFX9507 commited on
Commit
0a2992f
·
verified ·
1 Parent(s): a16b829

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -42
app.py CHANGED
@@ -27,59 +27,45 @@ def format_message(content):
27
  formatted = []
28
  current_pos = 0
29
 
30
- # 处理 <think> 标签
31
  for match in re.finditer(r"<think>(.*?)</think>", content, re.DOTALL):
32
  start, end = match.span()
33
  think_content = match.group(1).strip()
34
  think_id = f"think-{hash(think_content)}"
35
 
36
- # 添加前面的内容
37
  if current_pos < start:
38
  formatted.append(content[current_pos:start])
39
 
40
- # 添加思考内容
41
  formatted.append(f'''
42
  <details class="think-container" open>
43
- <summary class="think-summary">🤔 思考过程(点击展开)</summary>
44
  <div class="think-content">{think_content}</div>
45
  </details>
46
  ''')
47
  current_pos = end
48
 
49
- # 添加剩余内容
50
  formatted.append(content[current_pos:])
51
-
52
- # 合并内容
53
  final_content = "".join(formatted)
54
-
55
- # 处理代码块
56
- final_content = re.sub(r"```([\s\S]*?)```",
57
- r'<div class="code-block">\1</div>',
58
- final_content)
59
-
60
- # 处理换行
61
  final_content = final_content.replace("\n", "<br>")
62
-
63
- # 清理多余换行
64
  final_content = re.sub(r"(<br>)*</details>(<br>)*", "</details>", final_content)
65
  final_content = re.sub(r"(<br>)*(<div class=[^>]+>)(<br>)*", r"\2", final_content)
66
 
67
  return final_content
68
 
69
- def predict(history, system_msg, temperature, top_p, repetition_penalty):
70
- # 初始化 history 如果为 None
71
- if history is None:
72
- history = []
73
-
74
  headers = {
75
  "Content-Type": "application/json",
76
  "Authorization": f"Bearer {API_KEY}"
77
  }
78
 
79
  messages = [{"role": "system", "content": system_msg}]
80
- for msg in history:
81
  messages.append({"role": "user", "content": msg[0]})
82
- messages.append({"role": "assistant", "content": msg[1]})
 
83
 
84
  data = {
85
  "model": "mini",
@@ -98,6 +84,8 @@ def predict(history, system_msg, temperature, top_p, repetition_penalty):
98
  )
99
 
100
  buffer = ""
 
 
101
  for chunk in response.iter_lines():
102
  if chunk:
103
  chunk_str = chunk.decode("utf-8").replace("data: ", "")
@@ -107,9 +95,12 @@ def predict(history, system_msg, temperature, top_p, repetition_penalty):
107
  delta = chunk_data["choices"][0]["delta"]
108
  if "content" in delta:
109
  buffer += delta["content"]
110
- yield format_message(buffer)
 
111
  except:
112
- pass
 
 
113
 
114
  with gr.Blocks(css=CSS, title="Tifa-Deepsex-COT-14B") as demo:
115
  gr.HTML(f"<h1 id='gpt-title'>Tifa-Deepsex-COT-14B</h1>")
@@ -121,10 +112,9 @@ with gr.Blocks(css=CSS, title="Tifa-Deepsex-COT-14B") as demo:
121
  elem_id="chatbot",
122
  bubble_full_width=False,
123
  avatar_images=(
124
- "https://i.imgur.com/8O4ZSgh.png", # 用户头像
125
- "https://i.imgur.com/hUAXKg0.png" # 机器人头像
126
  ),
127
- value=[] # 初始化 Chatbot 的值
128
  )
129
  msg = gr.Textbox(
130
  placeholder="输入消息...",
@@ -146,29 +136,29 @@ with gr.Blocks(css=CSS, title="Tifa-Deepsex-COT-14B") as demo:
146
  top_p = gr.Slider(0, 1, value=0.7, label="Top-p")
147
  repetition_penalty = gr.Slider(1, 2, value=1.17, label="重复惩罚")
148
 
149
- submit_event = msg.submit(
150
- fn=lambda user_msg, history: history + [[user_msg, None]],
151
- inputs=[msg, chatbot],
152
- outputs=chatbot,
153
- show_progress="hidden"
154
  ).then(
155
  predict,
156
- inputs=[chatbot, system_msg, temperature, top_p, repetition_penalty],
157
- outputs=chatbot
158
  )
159
 
160
  submit_btn.click(
161
- fn=lambda user_msg, history: history + [[user_msg, None]],
162
- inputs=[msg, chatbot],
163
- outputs=chatbot,
164
- show_progress="hidden"
165
  ).then(
166
  predict,
167
- inputs=[chatbot, system_msg, temperature, top_p, repetition_penalty],
168
- outputs=chatbot
169
  )
170
 
171
- clear_btn.click(lambda: [], None, chatbot, queue=False)
172
 
173
  if __name__ == "__main__":
174
  demo.queue().launch(share=True)
 
27
  formatted = []
28
  current_pos = 0
29
 
 
30
  for match in re.finditer(r"<think>(.*?)</think>", content, re.DOTALL):
31
  start, end = match.span()
32
  think_content = match.group(1).strip()
33
  think_id = f"think-{hash(think_content)}"
34
 
 
35
  if current_pos < start:
36
  formatted.append(content[current_pos:start])
37
 
 
38
  formatted.append(f'''
39
  <details class="think-container" open>
40
+ <summary class="think-summary">🤔 思考过程(点击展开)</summary>
41
  <div class="think-content">{think_content}</div>
42
  </details>
43
  ''')
44
  current_pos = end
45
 
 
46
  formatted.append(content[current_pos:])
 
 
47
  final_content = "".join(formatted)
48
+ final_content = re.sub(r"```([\s\S]*?)```", r'<div class="code-block">\1</div>', final_content)
 
 
 
 
 
 
49
  final_content = final_content.replace("\n", "<br>")
 
 
50
  final_content = re.sub(r"(<br>)*</details>(<br>)*", "</details>", final_content)
51
  final_content = re.sub(r"(<br>)*(<div class=[^>]+>)(<br>)*", r"\2", final_content)
52
 
53
  return final_content
54
 
55
+ def user_input(user_message, history):
56
+ return "", history + [[user_message, None]]
57
+
58
+ def predict(message, chat_history, system_msg, temperature, top_p, repetition_penalty):
 
59
  headers = {
60
  "Content-Type": "application/json",
61
  "Authorization": f"Bearer {API_KEY}"
62
  }
63
 
64
  messages = [{"role": "system", "content": system_msg}]
65
+ for msg in chat_history:
66
  messages.append({"role": "user", "content": msg[0]})
67
+ if msg[1]:
68
+ messages.append({"role": "assistant", "content": msg[1]})
69
 
70
  data = {
71
  "model": "mini",
 
84
  )
85
 
86
  buffer = ""
87
+ history = chat_history.copy()
88
+
89
  for chunk in response.iter_lines():
90
  if chunk:
91
  chunk_str = chunk.decode("utf-8").replace("data: ", "")
 
95
  delta = chunk_data["choices"][0]["delta"]
96
  if "content" in delta:
97
  buffer += delta["content"]
98
+ history[-1][1] = format_message(buffer)
99
+ yield history
100
  except:
101
+ continue
102
+
103
+ return history
104
 
105
  with gr.Blocks(css=CSS, title="Tifa-Deepsex-COT-14B") as demo:
106
  gr.HTML(f"<h1 id='gpt-title'>Tifa-Deepsex-COT-14B</h1>")
 
112
  elem_id="chatbot",
113
  bubble_full_width=False,
114
  avatar_images=(
115
+ "https://i.imgur.com/8O4ZSgh.png",
116
+ "https://i.imgur.com/hUAXKg0.png"
117
  ),
 
118
  )
119
  msg = gr.Textbox(
120
  placeholder="输入消息...",
 
136
  top_p = gr.Slider(0, 1, value=0.7, label="Top-p")
137
  repetition_penalty = gr.Slider(1, 2, value=1.17, label="重复惩罚")
138
 
139
+ msg.submit(
140
+ user_input,
141
+ [msg, chatbot],
142
+ [msg, chatbot],
143
+ queue=False
144
  ).then(
145
  predict,
146
+ [msg, chatbot, system_msg, temperature, top_p, repetition_penalty],
147
+ chatbot
148
  )
149
 
150
  submit_btn.click(
151
+ user_input,
152
+ [msg, chatbot],
153
+ [msg, chatbot],
154
+ queue=False
155
  ).then(
156
  predict,
157
+ [msg, chatbot, system_msg, temperature, top_p, repetition_penalty],
158
+ chatbot
159
  )
160
 
161
+ clear_btn.click(lambda: None, None, chatbot, queue=False)
162
 
163
  if __name__ == "__main__":
164
  demo.queue().launch(share=True)