hsuwill000 commited on
Commit
3920413
·
verified ·
1 Parent(s): c50d9ac

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -14
app.py CHANGED
@@ -5,7 +5,7 @@ from transformers import AutoTokenizer, pipeline
5
 
6
  # Load the model and tokenizer
7
  model_id = "hsuwill000/DeepSeek-R1-Distill-Qwen-1.5B-openvino"
8
- model = OVModelForCausalLM.from_pretrained(model_id, device="CPU") # 明確指定设备
9
  tokenizer = AutoTokenizer.from_pretrained(model_id)
10
 
11
  # Create generation pipeline
@@ -15,17 +15,17 @@ def respond(message):
15
  try:
16
  start_time = time.time()
17
 
18
- # 強化 Prompt 讓模型輸出更合理
19
  instruction = (
20
- "請用簡單、繁體中文、準確的語言回答問題,避免冗長和重複內容。\n"
21
  "User: " + message + "\n"
22
  "Assistant: "
23
  )
24
 
25
- # Generate response with improved settings
26
  response = pipe(
27
  instruction,
28
- max_length=2048, # 限制最大輸出長度
29
  truncation=True,
30
  num_return_sequences=1,
31
  temperature=0.3,
@@ -43,24 +43,48 @@ def respond(message):
43
  inference_time = time.time() - start_time
44
  print(f"Inference time: {inference_time:.4f} seconds")
45
 
46
- # 返回對話記錄更新結果
47
- return [(message, reply)]
48
 
49
  except Exception as e:
50
  print(f"Error: {e}")
51
- return [(message, "Sorry, something went wrong. Please try again.")]
52
 
53
- # Set up Gradio chat interface
54
  with gr.Blocks() as demo:
55
  gr.Markdown("# DeepSeek-R1-Distill-Qwen-1.5B-openvino Chat")
56
  gr.Markdown("Chat with DeepSeek-R1-Distill-Qwen-1.5B-openvino model.")
57
 
58
- chatbot = gr.Chatbot()
59
- # 設置 clear_on_submit=True,使得訊息送出後立即清空輸入框
60
- msg = gr.Textbox(label="Your Message", clear_on_submit=True)
61
-
62
- # 提交後更新聊天記錄,輸入框會由 clear_on_submit 自動清空
 
 
63
  msg.submit(respond, inputs=msg, outputs=chatbot)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
 
65
  if __name__ == "__main__":
66
  demo.launch(share=True)
 
5
 
6
  # Load the model and tokenizer
7
  model_id = "hsuwill000/DeepSeek-R1-Distill-Qwen-1.5B-openvino"
8
+ model = OVModelForCausalLM.from_pretrained(model_id, device="CPU") # 明確指定設備
9
  tokenizer = AutoTokenizer.from_pretrained(model_id)
10
 
11
  # Create generation pipeline
 
15
  try:
16
  start_time = time.time()
17
 
18
+ # 強化 prompt 讓模型輸出更合理
19
  instruction = (
20
+ "請用簡單、準確的語言回答問題,避免冗長和重複內容。\n"
21
  "User: " + message + "\n"
22
  "Assistant: "
23
  )
24
 
25
+ # 生成回應
26
  response = pipe(
27
  instruction,
28
+ max_length=200, # 限制最大輸出長度
29
  truncation=True,
30
  num_return_sequences=1,
31
  temperature=0.3,
 
43
  inference_time = time.time() - start_time
44
  print(f"Inference time: {inference_time:.4f} seconds")
45
 
46
+ # 返回對話記錄更新結果(採用 openai-style 格式,避免未來版本警告)
47
+ return [{"role": "user", "content": message}, {"role": "assistant", "content": reply}]
48
 
49
  except Exception as e:
50
  print(f"Error: {e}")
51
+ return [{"role": "user", "content": message}, {"role": "assistant", "content": "Sorry, something went wrong. Please try again."}]
52
 
 
53
  with gr.Blocks() as demo:
54
  gr.Markdown("# DeepSeek-R1-Distill-Qwen-1.5B-openvino Chat")
55
  gr.Markdown("Chat with DeepSeek-R1-Distill-Qwen-1.5B-openvino model.")
56
 
57
+ # 設定 elem_id 以便後續在前端取得元素
58
+ chatbot = gr.Chatbot(type="messages")
59
+ msg = gr.Textbox(label="Your Message", elem_id="input_box")
60
+
61
+ # 點擊按鈕或按 Enter 都會送出訊息
62
+ send_button = gr.Button("Send")
63
+ # 兩種送出方式皆觸發 respond 函數
64
  msg.submit(respond, inputs=msg, outputs=chatbot)
65
+ send_button.click(respond, inputs=msg, outputs=chatbot)
66
+
67
+ # 注入 JavaScript,在按下 Enter 鍵時清空輸入框
68
+ gr.HTML(
69
+ """
70
+ <script>
71
+ // 當頁面載入完成後
72
+ window.addEventListener("load", function() {
73
+ const input_box = document.getElementById("input_box");
74
+ // 監聽鍵盤按下事件
75
+ input_box.addEventListener("keydown", function(e) {
76
+ // 如果按下 Enter 鍵
77
+ if (e.key === "Enter") {
78
+ // 延遲一點時間以確保送出事件被觸發
79
+ setTimeout(() => {
80
+ input_box.value = "";
81
+ }, 0);
82
+ }
83
+ });
84
+ });
85
+ </script>
86
+ """
87
+ )
88
 
89
  if __name__ == "__main__":
90
  demo.launch(share=True)