Update app.py
Browse files
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
|
@@ -25,12 +25,12 @@ def respond(message):
|
|
25 |
# Generate response with improved settings
|
26 |
response = pipe(
|
27 |
instruction,
|
28 |
-
max_length=200, #
|
29 |
truncation=True,
|
30 |
num_return_sequences=1,
|
31 |
-
temperature=0.3,
|
32 |
-
top_p=0.8,
|
33 |
-
repetition_penalty=1.5,
|
34 |
)
|
35 |
generated_text = response[0]['generated_text'].strip()
|
36 |
|
@@ -43,12 +43,17 @@ def respond(message):
|
|
43 |
inference_time = time.time() - start_time
|
44 |
print(f"Inference time: {inference_time:.4f} seconds")
|
45 |
|
|
|
46 |
return [(message, reply)]
|
47 |
|
48 |
except Exception as e:
|
49 |
print(f"Error: {e}")
|
50 |
return [(message, "Sorry, something went wrong. Please try again.")]
|
51 |
|
|
|
|
|
|
|
|
|
52 |
# Set up Gradio chat interface
|
53 |
with gr.Blocks() as demo:
|
54 |
gr.Markdown("# DeepSeek-R1-Distill-Qwen-1.5B-openvino Chat")
|
@@ -57,7 +62,8 @@ with gr.Blocks() as demo:
|
|
57 |
chatbot = gr.Chatbot()
|
58 |
msg = gr.Textbox(label="Your Message")
|
59 |
|
60 |
-
|
|
|
61 |
|
62 |
if __name__ == "__main__":
|
63 |
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
|
|
|
25 |
# Generate response with improved settings
|
26 |
response = pipe(
|
27 |
instruction,
|
28 |
+
max_length=200, # 限制最大輸出長度
|
29 |
truncation=True,
|
30 |
num_return_sequences=1,
|
31 |
+
temperature=0.3,
|
32 |
+
top_p=0.8,
|
33 |
+
repetition_penalty=1.5,
|
34 |
)
|
35 |
generated_text = response[0]['generated_text'].strip()
|
36 |
|
|
|
43 |
inference_time = time.time() - start_time
|
44 |
print(f"Inference time: {inference_time:.4f} seconds")
|
45 |
|
46 |
+
# 返回對話記錄更新結果 (以 tuple 的方式累加歷史訊息)
|
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")
|
|
|
62 |
chatbot = gr.Chatbot()
|
63 |
msg = gr.Textbox(label="Your Message")
|
64 |
|
65 |
+
# submit 完成後,使用 .then() 來清空輸入框
|
66 |
+
msg.submit(respond, inputs=msg, outputs=chatbot).then(clear_textbox, None, msg)
|
67 |
|
68 |
if __name__ == "__main__":
|
69 |
demo.launch(share=True)
|