cafe3310 commited on
Commit
0d2c9df
·
1 Parent(s): a339ccc

feat: Implement colored output for thinking tokens

Browse files
Files changed (3) hide show
  1. GEMINI.md +2 -1
  2. app.py +9 -11
  3. comp.py +1 -1
GEMINI.md CHANGED
@@ -30,10 +30,11 @@
30
 
31
  # Todolist
32
  ## 未完成
33
- - [ ] (进行中) 实现调试模式以观察“思考”和“正文” token 的区别。
34
  - [ ] (已暂停) 搜索 `huggingface_hub` 文档,确认是否存在用于重启 Space 的 API。
35
 
36
  ## 已完成
 
37
  - [x] 修改 `app.py`,移除 `Ling-flash-2.0` 模型,只保留 `Ring-mini-2.0`。
38
  - [x] **(用户决策)** 确认 `Ling-flash-2.0` 模型过大,暂时移除,仅使用 `Ring-mini-2.0`。
39
  - [x] 搭建 LangGraph 基础架构并重构 `app.py`。
 
30
 
31
  # Todolist
32
  ## 未完成
33
+ - [ ] (进行中) 为“思考”和“正文” token 实现不同的颜色显示。
34
  - [ ] (已暂停) 搜索 `huggingface_hub` 文档,确认是否存在用于重启 Space 的 API。
35
 
36
  ## 已完成
37
+ - [x] 实现调试模式以观察“思考”和“正文” token 的区别。
38
  - [x] 修改 `app.py`,移除 `Ling-flash-2.0` 模型,只保留 `Ring-mini-2.0`。
39
  - [x] **(用户决策)** 确认 `Ling-flash-2.0` 模型过大,暂时移除,仅使用 `Ring-mini-2.0`。
40
  - [x] 搭建 LangGraph 基础架构并重构 `app.py`。
app.py CHANGED
@@ -3,13 +3,11 @@ from comp import generate_response
3
 
4
  # --- Gradio UI ---
5
 
6
- with gr.Blocks() as demo:
7
  gr.Markdown("# Ling Playground")
8
  chatbot = gr.Chatbot()
9
- with gr.Row():
10
- msg = gr.Textbox()
11
- token_debugger = gr.Textbox(label="Token Debugger", interactive=False)
12
- clear = gr.ClearButton([msg, chatbot, token_debugger])
13
 
14
  def user(user_message, history):
15
  return "", history + [[user_message, None]]
@@ -17,14 +15,14 @@ with gr.Blocks() as demo:
17
  def bot(history):
18
  user_message = history[-1][0]
19
  history[-1][1] = ""
20
- raw_token_text = ""
21
- for response, new_text in generate_response(user_message, history[:-1]):
22
- history[-1][1] = response
23
- raw_token_text += new_text
24
- yield history, raw_token_text
25
 
26
  msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
27
- bot, chatbot, [chatbot, token_debugger]
28
  )
29
  clear.click(lambda: None, None, chatbot, queue=False)
30
 
 
3
 
4
  # --- Gradio UI ---
5
 
6
+ with gr.Blocks(css=".thinking { color: gray; font-style: italic; }") as demo:
7
  gr.Markdown("# Ling Playground")
8
  chatbot = gr.Chatbot()
9
+ msg = gr.Textbox()
10
+ clear = gr.ClearButton([msg, chatbot])
 
 
11
 
12
  def user(user_message, history):
13
  return "", history + [[user_message, None]]
 
15
  def bot(history):
16
  user_message = history[-1][0]
17
  history[-1][1] = ""
18
+ for response in generate_response(user_message, history[:-1]):
19
+ html_response = response.replace("<think>", "<div class='thinking'>")
20
+ html_response = html_response.replace("</think>", "</div>")
21
+ history[-1][1] = html_response
22
+ yield history
23
 
24
  msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
25
+ bot, chatbot, chatbot
26
  )
27
  clear.click(lambda: None, None, chatbot, queue=False)
28
 
comp.py CHANGED
@@ -62,7 +62,7 @@ def generate_response(message, history):
62
  response = ""
63
  for new_text in streamer:
64
  response += new_text
65
- yield response, new_text
66
 
67
  # wait for the generation thread to finish
68
  thread.join()
 
62
  response = ""
63
  for new_text in streamer:
64
  response += new_text
65
+ yield response
66
 
67
  # wait for the generation thread to finish
68
  thread.join()