Update run.py
#8
by
brian033
- opened
run.py
CHANGED
@@ -1,11 +1,35 @@
|
|
1 |
import time
|
2 |
import gradio as gr
|
|
|
3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
def slow_echo(message, history):
|
|
|
5 |
for i in range(len(message)):
|
6 |
-
time.sleep(0.05)
|
7 |
-
yield "
|
|
|
|
|
|
|
|
|
8 |
|
|
|
9 |
demo = gr.ChatInterface(
|
10 |
slow_echo,
|
11 |
type="messages",
|
|
|
1 |
import time
|
2 |
import gradio as gr
|
3 |
+
import requests
|
4 |
|
5 |
+
# 使用 requests 库呼叫本地服務生成回應
|
6 |
+
def generate_response(message, history):
|
7 |
+
url = "http://192.168.208.223:48763/v1/chat/completions"
|
8 |
+
headers = {"Content-Type": "application/json"}
|
9 |
+
data = {
|
10 |
+
"model": "deepseek-ai/deepseek-coder-6.7b-instruct",
|
11 |
+
"messages": [{"role": "user", "content": message}]
|
12 |
+
}
|
13 |
+
|
14 |
+
response = requests.post(url, headers=headers, json=data)
|
15 |
+
|
16 |
+
if response.status_code == 200:
|
17 |
+
return response.json()["choices"][0]["message"]["content"]
|
18 |
+
else:
|
19 |
+
return "抱歉,無法從模型獲得回應。"
|
20 |
+
|
21 |
+
# 更新 slow_echo 函數,將其替換為使用 LLM 的邏輯
|
22 |
def slow_echo(message, history):
|
23 |
+
# 模擬進度條效果,實際使用模型回應
|
24 |
for i in range(len(message)):
|
25 |
+
time.sleep(0.05) # 模擬延遲
|
26 |
+
yield "你輸入了: " + message[: i + 1]
|
27 |
+
|
28 |
+
# 呼叫 LLM 生成回應
|
29 |
+
llm_response = generate_response(message, history)
|
30 |
+
yield llm_response
|
31 |
|
32 |
+
# 設置 Gradio 的聊天界面
|
33 |
demo = gr.ChatInterface(
|
34 |
slow_echo,
|
35 |
type="messages",
|