hsuwill000 commited on
Commit
94d98a0
·
verified ·
1 Parent(s): da45380

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -93
app.py CHANGED
@@ -1,8 +1,8 @@
1
  #!/usr/bin/env python3
2
- # qwen_style_gui.py ── 相容 Gradio 4.x+,Qwen 風格,重整清空
3
 
4
  import gradio as gr
5
- import requests, json, re, time
6
  from threading import Thread
7
 
8
  URL = "http://localhost:8000/v1/chat/completions"
@@ -12,11 +12,13 @@ def sanitize(text: str):
12
  return re.sub(r"[\ud800-\udfff]", "", text)
13
 
14
  # ======================
15
- # 全域狀態(每次重啟或重整頁面會重設)
16
  # ======================
17
- history_openai = [] # OpenAI 格式 [{"role": "user", "content": "..."}, ...]
18
- is_streaming = False
19
  assistant_buffer = ""
 
 
20
 
21
  # ======================
22
  # Streaming 主執行緒
@@ -44,120 +46,64 @@ def stream_to_buffer(user_input: str):
44
  continue
45
  data = line[6:]
46
  if data == "[DONE]":
 
47
  break
48
  try:
49
  tok = json.loads(data)["choices"][0]["delta"].get("content")
50
- except (json.JSONDecodeError, KeyError, IndexError):
 
51
  continue
52
  if tok:
53
  assistant_buffer += tok
54
  # 結束後更新歷史
55
- final_content = sanitize(assistant_buffer)
56
- history_openai.append({"role": "assistant", "content": final_content})
57
  except Exception as e:
58
  assistant_buffer = f"请求失败: {e}"
59
- finally:
60
  is_streaming = False
61
 
 
62
  # ======================
63
  # 使用者送出訊息
64
  # ======================
65
- def user_submit(user_msg: str):
66
- global assistant_buffer, is_streaming
67
- if not user_msg.strip():
68
- return [], "", ""
69
- # 清空 buffer,啟動 streaming
70
  assistant_buffer = ""
71
  is_streaming = True
72
  Thread(target=stream_to_buffer, args=(user_msg,), daemon=True).start()
73
- # 回傳初始對話(user + 空 assistant)
74
- return [{"role": "user", "content": user_msg}], "", user_msg
75
 
76
  # ======================
77
  # Timer 定時刷新
78
  # ======================
79
- def flush_buffer(current_messages):
80
- global assistant_buffer, is_streaming
81
- if not current_messages:
82
- return current_messages
83
-
84
- # 如果最後一條是 user,就加上 streaming 中的 assistant 回覆
85
- if current_messages[-1]["role"] == "user":
86
- return current_messages + [{"role": "assistant", "content": assistant_buffer}]
87
- elif current_messages[-1]["role"] == "assistant":
88
- # 更新最後一條 assistant 的內容
89
- updated = current_messages.copy()
90
- updated[-1]["content"] = assistant_buffer
91
- return updated
92
- return current_messages
93
 
94
- # ======================
95
- # 清除對話
96
- # ======================
97
- def clear_conversation():
98
- global history_openai, assistant_buffer, is_streaming
99
- history_openai = []
100
- assistant_buffer = ""
101
- is_streaming = False
102
- return [], "", ""
103
 
104
- # ======================
105
- # Qwen 風格 CSS
106
- # ======================
107
- QWEN_CSS = """
108
- #chatbot {
109
- height: 600px;
110
- overflow-y: auto;
111
- }
112
- #chatbot .user {
113
- background-color: #e6f0ff;
114
- border-radius: 12px;
115
- padding: 12px;
116
- margin: 8px 0;
117
- text-align: right;
118
- }
119
- #chatbot .bot {
120
- background-color: #f0f0f0;
121
- border-radius: 12px;
122
- padding: 12px;
123
- margin: 8px 0;
124
- text-align: left;
125
- }
126
- .gradio-container {
127
- font-family: 'Segoe UI', 'PingFang SC', 'Microsoft YaHei', sans-serif;
128
- }
129
- """
130
 
131
  # ======================
132
- # Gradio 介面(使用 type='messages')
133
  # ======================
134
- with gr.Blocks(css=QWEN_CSS, title="Qwen Chat") as demo:
135
- gr.Markdown("## 💬 Qwen Chat Service")
136
- chatbot = gr.Chatbot(elem_id="chatbot", type="messages") # ✅ 關鍵:type="messages"
137
-
138
- with gr.Row():
139
- msg = gr.Textbox(
140
- show_label=False,
141
- placeholder="輸入訊息後按 Enter",
142
- container=False,
143
- scale=8
144
- )
145
- clear_btn = gr.Button("🗑️ 清除", scale=1)
146
-
147
- # 提交時:更新 chatbot 為 [{"role": "user", "content": "..."}]
148
- msg.submit(
149
- fn=user_submit,
150
- inputs=msg,
151
- outputs=[chatbot, msg, gr.State()], # 第三個 output 是為了觸發 timer(可選)
152
- queue=False
153
- )
154
-
155
- clear_btn.click(clear_conversation, outputs=[chatbot, msg, gr.State()])
156
-
157
- # Timer 每 0.3 秒刷新
158
- timer = gr.Timer(value=0.3)
159
- timer.tick(flush_buffer, inputs=chatbot, outputs=chatbot)
160
 
161
  if __name__ == "__main__":
162
- # ⚠️ 不要加 reload=...,這不是 launch() 的參數
163
  demo.launch(server_name="0.0.0.0", server_port=7860)
 
1
  #!/usr/bin/env python3
2
+ # gradio_timer_buffer_final.py ── 0.3 秒刷新 + 完成後停止刷新 + 可重整重啟
3
 
4
  import gradio as gr
5
+ import requests, json, re, sys, time
6
  from threading import Thread
7
 
8
  URL = "http://localhost:8000/v1/chat/completions"
 
12
  return re.sub(r"[\ud800-\udfff]", "", text)
13
 
14
  # ======================
15
+ # 全域狀態(每次重整會重設)
16
  # ======================
17
+ history_openai = []
18
+ current_chatbot = []
19
  assistant_buffer = ""
20
+ is_streaming = False
21
+
22
 
23
  # ======================
24
  # Streaming 主執行緒
 
46
  continue
47
  data = line[6:]
48
  if data == "[DONE]":
49
+ is_streaming = False
50
  break
51
  try:
52
  tok = json.loads(data)["choices"][0]["delta"].get("content")
53
+ except json.JSONDecodeError:
54
+ byte_buf = line_bytes + b"\n" + byte_buf
55
  continue
56
  if tok:
57
  assistant_buffer += tok
58
  # 結束後更新歷史
59
+ history_openai.append({"role": "assistant", "content": sanitize(assistant_buffer)})
60
+ is_streaming = False
61
  except Exception as e:
62
  assistant_buffer = f"请求失败: {e}"
 
63
  is_streaming = False
64
 
65
+
66
  # ======================
67
  # 使用者送出訊息
68
  # ======================
69
+ def user_submit(user_msg, chatbot):
70
+ global current_chatbot, assistant_buffer, is_streaming
71
+ current_chatbot = chatbot + [[user_msg, ""]]
 
 
72
  assistant_buffer = ""
73
  is_streaming = True
74
  Thread(target=stream_to_buffer, args=(user_msg,), daemon=True).start()
75
+ return "", current_chatbot
76
+
77
 
78
  # ======================
79
  # Timer 定時刷新
80
  # ======================
81
+ def flush_buffer():
82
+ """每 0.3 秒刷新 buffer,若已完成就不再更新"""
83
+ global current_chatbot, assistant_buffer, is_streaming
84
+ if not current_chatbot:
85
+ return current_chatbot
 
 
 
 
 
 
 
 
 
86
 
87
+ # 更新目前的內容
88
+ current_chatbot[-1][1] = assistant_buffer
89
+
90
+ # 若結束則停止 timer 更新
91
+ if not is_streaming:
92
+ time.sleep(0.1) # 確保最後一次更新
93
+ return current_chatbot
 
 
94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95
 
96
  # ======================
97
+ # Gradio 介面
98
  # ======================
99
+ with gr.Blocks() as demo:
100
+ gr.Markdown("## Chat8000 Lab3 - 每 0.3 秒刷新 Buffer(完成自動停止)")
101
+ chatbot = gr.Chatbot()
102
+ msg = gr.Textbox(show_label=False, placeholder="輸入訊息後按 Enter")
103
+ msg.submit(user_submit, [msg, chatbot], [msg, chatbot], queue=False)
104
+
105
+ timer = gr.Timer(value=0.3, active=True)
106
+ timer.tick(flush_buffer, outputs=chatbot)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
107
 
108
  if __name__ == "__main__":
 
109
  demo.launch(server_name="0.0.0.0", server_port=7860)