Update app.py
Browse files
app.py
CHANGED
@@ -17,7 +17,7 @@ def respond(message):
|
|
17 |
|
18 |
# 強化 Prompt 讓模型輸出更合理
|
19 |
instruction = (
|
20 |
-
"
|
21 |
"User: " + message + "\n"
|
22 |
"Assistant: "
|
23 |
)
|
@@ -25,7 +25,7 @@ def respond(message):
|
|
25 |
# Generate response with improved settings
|
26 |
response = pipe(
|
27 |
instruction,
|
28 |
-
max_length=
|
29 |
truncation=True,
|
30 |
num_return_sequences=1,
|
31 |
temperature=0.3,
|
@@ -43,27 +43,24 @@ 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 |
-
# 定義一個清空文字框的函數
|
54 |
-
def clear_textbox():
|
55 |
-
return gr.update(value="")
|
56 |
-
|
57 |
# Set up Gradio chat interface
|
58 |
with gr.Blocks() as demo:
|
59 |
gr.Markdown("# DeepSeek-R1-Distill-Qwen-1.5B-openvino Chat")
|
60 |
gr.Markdown("Chat with DeepSeek-R1-Distill-Qwen-1.5B-openvino model.")
|
61 |
|
62 |
chatbot = gr.Chatbot()
|
63 |
-
|
|
|
64 |
|
65 |
-
#
|
66 |
-
msg.submit(respond, inputs=msg, outputs=chatbot)
|
67 |
|
68 |
if __name__ == "__main__":
|
69 |
demo.launch(share=True)
|
|
|
17 |
|
18 |
# 強化 Prompt 讓模型輸出更合理
|
19 |
instruction = (
|
20 |
+
"請用簡單、繁體中文、準確的語言回答問題,避免冗長和重複內容。\n"
|
21 |
"User: " + message + "\n"
|
22 |
"Assistant: "
|
23 |
)
|
|
|
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 |
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)
|