adamtobegreat commited on
Commit
a3bae88
·
verified ·
1 Parent(s): 8d741eb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -19
app.py CHANGED
@@ -219,14 +219,15 @@ with gr.Blocks(
219
  border-radius: 10px !important;
220
  }
221
 
222
- /* ✅ 桌機版比例:輸入框 9、按鈕 1 */
223
- #chat-row > .gr-textbox { flex: 9 !important; }
224
- #chat-row > .gr-button { flex: 1 !important; }
225
 
226
- /* ✅ 手機版比例:輸入框 9、按鈕 1 */
227
  @media (max-width: 768px) {
228
- #chat-row > .gr-textbox { flex: 9 !important; }
229
- #chat-row > .gr-button { flex: 1 !important; }
 
230
  }
231
  """
232
  ) as demo:
@@ -249,7 +250,7 @@ with gr.Blocks(
249
  with gr.Column(scale=4, min_width=300):
250
  chatbot = gr.Chatbot(label="💬 對話紀錄", type="messages", height=500)
251
 
252
- # ✅ 輸入框與送出鍵同行排列(桌機 8:2、手機 9:1)
253
  with gr.Row(elem_id="chat-row"):
254
  user_input = gr.Textbox(
255
  placeholder="請輸入您的問題(Enter 送出 / Shift+Enter 換行)...",
@@ -280,28 +281,34 @@ with gr.Blocks(
280
  return history, gr.update(value="")
281
 
282
  # ✅ 綁定事件(Enter送出、Shift+Enter換行)
283
- user_input.submit(handle_input, [user_input, chatbot], [chatbot, user_input]) # Enter 觸發送出
284
- send_btn.click(handle_input, [user_input, chatbot], [chatbot, user_input]) # 按鈕觸發送出
285
 
286
- # ✅ 修正版 JS:適用桌機與手機
287
  gr.HTML("""
288
  <script>
289
  document.addEventListener("DOMContentLoaded", function() {
290
- const textareas = document.querySelectorAll("textarea");
291
- textareas.forEach((ta) => {
292
- ta.addEventListener("keydown", function(e) {
293
- if (e.key === "Enter" && !e.shiftKey) {
294
- e.preventDefault();
295
- const sendBtn = document.querySelector('#send-btn button, #send-btn');
296
- if (sendBtn) sendBtn.click();
 
 
 
 
 
297
  }
298
  });
299
  });
 
300
  });
301
  </script>
302
  """)
303
 
304
- # 右側:常見問題 + 整理畫面
305
  with gr.Column(scale=1, min_width=200):
306
  gr.Markdown("### 🔍 常見問題")
307
  examples = [
@@ -323,7 +330,7 @@ with gr.Blocks(
323
  memory.clear()
324
  return [], gr.update(value="")
325
  gr.Markdown("---")
326
- gr.Button("🧹 整理畫面").click(clear_all, outputs=[chatbot, user_input])
327
 
328
  gr.HTML("<div id='footer'>© Fintech Assistant — 僅業務使用,非官方授權</div>")
329
 
 
219
  border-radius: 10px !important;
220
  }
221
 
222
+ /* ✅ 桌機版比例:輸入框 8、按鈕 2 */
223
+ #chat-row .gr-textbox, #chat-row textarea { flex: 9 !important; width: 90% !important; }
224
+ #chat-row .gr-button, #chat-row button { flex: 1 !important; width: 10% !important; }
225
 
226
+ /* ✅ 手機版比例:輸入框 9、按鈕 1(強制套用到 Hugging Face 結構) */
227
  @media (max-width: 768px) {
228
+ #chat-row .gr-textbox, #chat-row textarea { flex: 9 !important; width: 90% !important; }
229
+ #chat-row .gr-button, #chat-row button { flex: 1 !important; width: 10% !important; max-width: 80px !important; min-width: 60px !important; }
230
+ #send-btn button { padding: 0 10px !important; }
231
  }
232
  """
233
  ) as demo:
 
250
  with gr.Column(scale=4, min_width=300):
251
  chatbot = gr.Chatbot(label="💬 對話紀錄", type="messages", height=500)
252
 
253
+ # ✅ 輸入框與送出鍵同行排列(桌機、手機 9:1)
254
  with gr.Row(elem_id="chat-row"):
255
  user_input = gr.Textbox(
256
  placeholder="請輸入您的問題(Enter 送出 / Shift+Enter 換行)...",
 
281
  return history, gr.update(value="")
282
 
283
  # ✅ 綁定事件(Enter送出、Shift+Enter換行)
284
+ user_input.submit(handle_input, [user_input, chatbot], [chatbot, user_input])
285
+ send_btn.click(handle_input, [user_input, chatbot], [chatbot, user_input])
286
 
287
+ # ✅ JS 修正版:支援桌機 / 手機 / HuggingFace IFrame
288
  gr.HTML("""
289
  <script>
290
  document.addEventListener("DOMContentLoaded", function() {
291
+ const observer = new MutationObserver(() => {
292
+ const textareas = document.querySelectorAll("textarea");
293
+ textareas.forEach((ta) => {
294
+ if (!ta.dataset.bound) {
295
+ ta.dataset.bound = "true";
296
+ ta.addEventListener("keydown", function(e) {
297
+ if (e.key === "Enter" && !e.shiftKey) {
298
+ e.preventDefault();
299
+ const sendBtn = document.querySelector('#send-btn button, #send-btn');
300
+ if (sendBtn) sendBtn.click();
301
+ }
302
+ });
303
  }
304
  });
305
  });
306
+ observer.observe(document.body, { childList: true, subtree: true });
307
  });
308
  </script>
309
  """)
310
 
311
+ # 右側:常見問題 + 清除對話
312
  with gr.Column(scale=1, min_width=200):
313
  gr.Markdown("### 🔍 常見問題")
314
  examples = [
 
330
  memory.clear()
331
  return [], gr.update(value="")
332
  gr.Markdown("---")
333
+ gr.Button("🧹 清除對話").click(clear_all, outputs=[chatbot, user_input])
334
 
335
  gr.HTML("<div id='footer'>© Fintech Assistant — 僅業務使用,非官方授權</div>")
336