Spaces:
Running
Running
update
Browse files
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
@@ -4,20 +4,17 @@ 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("
|
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:]}")
|
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
|
@@ -25,7 +22,7 @@ For more information on `huggingface_hub` Inference API support, please check th
|
|
25 |
|
26 |
def respond(
|
27 |
message,
|
28 |
-
history: list[dict],
|
29 |
system_message,
|
30 |
max_tokens,
|
31 |
temperature,
|
@@ -56,22 +53,28 @@ def respond(
|
|
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 |
-
|
|
|
|
|
74 |
return content
|
|
|
|
|
|
|
|
|
75 |
return "Service temporarily unavailable"
|
76 |
except Exception as e:
|
77 |
print(f"[ERROR] Request error: {e}")
|
@@ -84,7 +87,7 @@ For information on how to customize the ChatInterface, peruse the gradio docs: h
|
|
84 |
demo = gr.ChatInterface(
|
85 |
respond,
|
86 |
additional_inputs=[
|
87 |
-
gr.Textbox(value="You are
|
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(
|
@@ -95,7 +98,7 @@ demo = gr.ChatInterface(
|
|
95 |
label="Top-p (nucleus sampling)",
|
96 |
),
|
97 |
],
|
98 |
-
type="messages"
|
99 |
)
|
100 |
|
101 |
|
|
|
4 |
import os
|
5 |
from dotenv import load_dotenv
|
6 |
|
|
|
7 |
load_dotenv()
|
8 |
|
|
|
9 |
API_URL = os.getenv("API_URL")
|
10 |
API_TOKEN = os.getenv("API_TOKEN")
|
11 |
|
|
|
12 |
if not API_URL or not API_TOKEN:
|
13 |
+
raise ValueError("invalid API_URL | API_TOKEN")
|
14 |
|
15 |
print(f"[INFO] starting:")
|
16 |
print(f"[INFO] API_URL: {API_URL[:6]}...{API_URL[-12:]}")
|
17 |
+
print(f"[INFO] API_TOKEN: {API_TOKEN[:10]}...{API_TOKEN[-10:]}")
|
18 |
|
19 |
"""
|
20 |
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
|
|
|
22 |
|
23 |
def respond(
|
24 |
message,
|
25 |
+
history: list[dict],
|
26 |
system_message,
|
27 |
max_tokens,
|
28 |
temperature,
|
|
|
53 |
}
|
54 |
|
55 |
print(f"[INFO] process user msg...")
|
|
|
56 |
print(f"[INFO] userMsg: {message}")
|
|
|
|
|
57 |
|
58 |
try:
|
59 |
with requests.post(API_URL, headers=headers, json=data) as r:
|
60 |
+
# print(f"[INFO] response status: {r.status_code}")
|
61 |
if r.status_code == 200:
|
62 |
json_response = r.json()
|
63 |
+
# print(f"[INFO] response json: {json_response}")
|
64 |
if 'choices' in json_response and len(json_response['choices']) > 0:
|
65 |
content = json_response['choices'][0].get('message', {}).get('content', '')
|
66 |
+
# print(f"[INFO] response content: {content}")
|
67 |
if content:
|
68 |
if '<think>' in content and '</think>' in content:
|
69 |
content = content.split('</think>')[-1].strip()
|
70 |
+
if '**Final Answer**' in content:
|
71 |
+
content = content.split('**Final Answer**')[-1].strip()
|
72 |
+
print(f"[INFO] final response: {content}")
|
73 |
return content
|
74 |
+
else:
|
75 |
+
print(f"[ERROR] No choices in response: {json_response}")
|
76 |
+
else:
|
77 |
+
print(f"[ERROR] Bad status code: {r.status_code}, response: {r.text}")
|
78 |
return "Service temporarily unavailable"
|
79 |
except Exception as e:
|
80 |
print(f"[ERROR] Request error: {e}")
|
|
|
87 |
demo = gr.ChatInterface(
|
88 |
respond,
|
89 |
additional_inputs=[
|
90 |
+
gr.Textbox(value="You are DMind AI Assistant, built by DMind.AI. Never present as Qwen, ChatGPT, Tongyi, OpenAI, etc.", label="System message"),
|
91 |
gr.Slider(minimum=1, maximum=32768, value=16384, step=1, label="Max new tokens"),
|
92 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.6, step=0.1, label="Temperature"),
|
93 |
gr.Slider(
|
|
|
98 |
label="Top-p (nucleus sampling)",
|
99 |
),
|
100 |
],
|
101 |
+
type="messages"
|
102 |
)
|
103 |
|
104 |
|