feat: init
Browse files- README.md +1 -1
- app.py +63 -24
- requirements.txt +2 -1
README.md
CHANGED
@@ -4,7 +4,7 @@ emoji: 💬
|
|
4 |
colorFrom: yellow
|
5 |
colorTo: purple
|
6 |
sdk: gradio
|
7 |
-
sdk_version: 5.0
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
license: mit
|
|
|
4 |
colorFrom: yellow
|
5 |
colorTo: purple
|
6 |
sdk: gradio
|
7 |
+
sdk_version: 5.30.0
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
license: mit
|
app.py
CHANGED
@@ -1,15 +1,31 @@
|
|
1 |
import gradio as gr
|
2 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
"""
|
5 |
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
6 |
"""
|
7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
8 |
-
|
9 |
|
10 |
def respond(
|
11 |
message,
|
12 |
-
history: list[
|
13 |
system_message,
|
14 |
max_tokens,
|
15 |
temperature,
|
@@ -17,27 +33,49 @@ def respond(
|
|
17 |
):
|
18 |
messages = [{"role": "system", "content": system_message}]
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
25 |
-
|
26 |
messages.append({"role": "user", "content": message})
|
27 |
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
top_p=top_p,
|
36 |
-
):
|
37 |
-
token = message.choices[0].delta.content
|
38 |
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
|
43 |
"""
|
@@ -46,9 +84,9 @@ For information on how to customize the ChatInterface, peruse the gradio docs: h
|
|
46 |
demo = gr.ChatInterface(
|
47 |
respond,
|
48 |
additional_inputs=[
|
49 |
-
gr.Textbox(value="You are
|
50 |
-
gr.Slider(minimum=1, maximum=
|
51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.
|
52 |
gr.Slider(
|
53 |
minimum=0.1,
|
54 |
maximum=1.0,
|
@@ -57,6 +95,7 @@ demo = gr.ChatInterface(
|
|
57 |
label="Top-p (nucleus sampling)",
|
58 |
),
|
59 |
],
|
|
|
60 |
)
|
61 |
|
62 |
|
|
|
1 |
import gradio as gr
|
2 |
+
import requests
|
3 |
+
import json
|
4 |
+
import os
|
5 |
+
from dotenv import load_dotenv
|
6 |
+
|
7 |
+
# 加载.env文件中的环境变量
|
8 |
+
load_dotenv()
|
9 |
+
|
10 |
+
# 从环境变量中读取配置
|
11 |
+
API_URL = os.getenv("API_URL")
|
12 |
+
API_TOKEN = os.getenv("API_TOKEN")
|
13 |
+
|
14 |
+
# 验证必要的环境变量
|
15 |
+
if not API_URL or not API_TOKEN:
|
16 |
+
raise ValueError("make sure API_URL & API_TOKEN")
|
17 |
+
|
18 |
+
print(f"[INFO] starting:")
|
19 |
+
print(f"[INFO] API_URL: {API_URL[:6]}...{API_URL[-12:]}")
|
20 |
+
print(f"[INFO] API_TOKEN: {API_TOKEN[:10]}...{API_TOKEN[-10:]}") # 只显示token的前10位和后10位
|
21 |
|
22 |
"""
|
23 |
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
24 |
"""
|
|
|
|
|
25 |
|
26 |
def respond(
|
27 |
message,
|
28 |
+
history: list[dict], # 修改为新的消息格式
|
29 |
system_message,
|
30 |
max_tokens,
|
31 |
temperature,
|
|
|
33 |
):
|
34 |
messages = [{"role": "system", "content": system_message}]
|
35 |
|
36 |
+
# 添加历史消息
|
37 |
+
messages.extend(history)
|
38 |
+
|
39 |
+
# 添加当前用户消息
|
|
|
|
|
40 |
messages.append({"role": "user", "content": message})
|
41 |
|
42 |
+
headers = {
|
43 |
+
"Content-Type": "application/json",
|
44 |
+
"Authorization": f"Bearer {API_TOKEN}"
|
45 |
+
}
|
46 |
+
|
47 |
+
data = {
|
48 |
+
"model": "/data/DMind-1",
|
49 |
+
"stream": False,
|
50 |
+
"messages": messages,
|
51 |
+
"temperature": temperature,
|
52 |
+
"top_p": top_p,
|
53 |
+
"top_k": 20,
|
54 |
+
"min_p": 0.1,
|
55 |
+
"max_tokens": 32768
|
56 |
+
}
|
57 |
|
58 |
+
print(f"[INFO] process user msg...")
|
59 |
+
print(f"[INFO] sysMsg: {system_message}")
|
60 |
+
print(f"[INFO] userMsg: {message}")
|
61 |
+
print(f"[INFO] modelParam: temperature={temperature}, top_p={top_p}")
|
62 |
+
print(f"[INFO] reqData: {data}")
|
|
|
|
|
|
|
63 |
|
64 |
+
try:
|
65 |
+
with requests.post(API_URL, headers=headers, json=data) as r:
|
66 |
+
if r.status_code == 200:
|
67 |
+
json_response = r.json()
|
68 |
+
if 'choices' in json_response and len(json_response['choices']) > 0:
|
69 |
+
content = json_response['choices'][0].get('message', {}).get('content', '')
|
70 |
+
if content:
|
71 |
+
# if '<think>' in content and '</think>' in content:
|
72 |
+
# content = content.split('</think>')[-1].strip()
|
73 |
+
print(f"[INFO] response: {content}")
|
74 |
+
return content
|
75 |
+
return "Service temporarily unavailable"
|
76 |
+
except Exception as e:
|
77 |
+
print(f"[ERROR] Request error: {e}")
|
78 |
+
return "Service error occurred"
|
79 |
|
80 |
|
81 |
"""
|
|
|
84 |
demo = gr.ChatInterface(
|
85 |
respond,
|
86 |
additional_inputs=[
|
87 |
+
gr.Textbox(value="You are DMind AI Assistant, built by DMind.AI. Never present as Qwen, ChatGPT, Tongyi, OpenAI, etc.", label="System message"),
|
88 |
+
gr.Slider(minimum=1, maximum=32768, value=16384, step=1, label="Max new tokens"),
|
89 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.6, step=0.1, label="Temperature"),
|
90 |
gr.Slider(
|
91 |
minimum=0.1,
|
92 |
maximum=1.0,
|
|
|
95 |
label="Top-p (nucleus sampling)",
|
96 |
),
|
97 |
],
|
98 |
+
type="messages" # 指定使用新的消息格式
|
99 |
)
|
100 |
|
101 |
|
requirements.txt
CHANGED
@@ -1 +1,2 @@
|
|
1 |
-
huggingface_hub==0.25.2
|
|
|
|
1 |
+
huggingface_hub==0.25.2
|
2 |
+
python-dotenv==1.0.1
|