Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,4 @@
|
|
|
|
1 |
import time
|
2 |
import gradio as gr
|
3 |
from huggingface_hub import InferenceClient
|
@@ -12,53 +13,41 @@ tokenizer = AutoTokenizer.from_pretrained(model_id)
|
|
12 |
# 建立生成管道
|
13 |
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
14 |
|
15 |
-
|
16 |
-
def respond_stream(message):
|
17 |
start_time = time.time() # 記錄開始時間
|
18 |
|
|
|
|
|
|
|
19 |
# 設定生成參數
|
20 |
-
max_length = 1024
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
if stop_token in new_text:
|
32 |
break
|
|
|
|
|
|
|
33 |
|
34 |
-
end_time = time.time()
|
35 |
-
|
36 |
-
|
37 |
-
# 聊天接口函數
|
38 |
-
def chatbot_interface(message, history):
|
39 |
-
history = history or []
|
40 |
-
history.append({"role": "user", "content": message})
|
41 |
-
response = respond_stream(message)
|
42 |
-
for partial_response in response:
|
43 |
-
history.append({"role": "assistant", "content": partial_response})
|
44 |
-
yield history # 實時更新歷史紀錄
|
45 |
|
46 |
-
#
|
47 |
-
|
48 |
-
|
|
|
49 |
|
50 |
-
|
51 |
-
with gr.Blocks() as demo:
|
52 |
-
chatbot = gr.Chatbot(label="Chat with Model")
|
53 |
-
msg = gr.Textbox(label="Enter your message", placeholder="Type your message here...", lines=2)
|
54 |
-
send = gr.Button("Send") # 顯示送出按鈕
|
55 |
-
clear = gr.Button("Clear") # 清除按鈕
|
56 |
|
57 |
-
|
58 |
-
|
59 |
-
msg.submit(chatbot_interface, inputs=[msg, chatbot], outputs=chatbot) # 支援按下Enter
|
60 |
-
clear.click(clear_chat, inputs=[], outputs=[chatbot, msg]) # 清除按鈕功能
|
61 |
|
62 |
-
# 啟動
|
63 |
if __name__ == "__main__":
|
64 |
demo.launch()
|
|
|
1 |
+
|
2 |
import time
|
3 |
import gradio as gr
|
4 |
from huggingface_hub import InferenceClient
|
|
|
13 |
# 建立生成管道
|
14 |
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
15 |
|
16 |
+
def respond(message, history):
|
|
|
17 |
start_time = time.time() # 記錄開始時間
|
18 |
|
19 |
+
# 將當前訊息與歷史訊息合併
|
20 |
+
input_text = message if not history else history[-1]["content"] + " " + message
|
21 |
+
input_text = message
|
22 |
# 設定生成參數
|
23 |
+
max_length = 1024 # 增加最大生成長度
|
24 |
+
output_text = ""
|
25 |
+
stop_token = "<|endoftext|>" # 可選:結束標記
|
26 |
+
while True:
|
27 |
+
response = pipe(input_text, max_length=max_length, truncation=False, num_return_sequences=1)
|
28 |
+
reply = response[0]['generated_text']
|
29 |
+
output_text += reply
|
30 |
+
|
31 |
+
# 檢測是否包含結束標記,或者生成結束
|
32 |
+
if stop_token in reply or len(output_text) >= max_length:
|
33 |
+
output_text = output_text.split(stop_token)[0] # 去掉結束標記以後的部分
|
|
|
34 |
break
|
35 |
+
|
36 |
+
# 更新輸入文字繼續生成
|
37 |
+
input_text = reply
|
38 |
|
39 |
+
end_time = time.time() # 記錄結束時間
|
40 |
+
duration = end_time - start_time # 計算耗時
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
+
# 輸出耗時到控制台
|
43 |
+
print(f"Message: {message}")
|
44 |
+
print(f"Reply: {output_text}")
|
45 |
+
print(f"Time taken for response: {duration:.2f} seconds")
|
46 |
|
47 |
+
return output_text
|
|
|
|
|
|
|
|
|
|
|
48 |
|
49 |
+
# 設定 Gradio 的聊天界面
|
50 |
+
demo = gr.ChatInterface(fn=respond, title="Phi-3.5-mini-instruct-openvino", description="Phi-3.5-mini-instruct-openvino", type='messages')
|
|
|
|
|
51 |
|
|
|
52 |
if __name__ == "__main__":
|
53 |
demo.launch()
|