Spaces:
Sleeping
Sleeping
update
Browse files
app.py
CHANGED
|
@@ -27,11 +27,17 @@ def respond(
|
|
| 27 |
max_tokens,
|
| 28 |
temperature,
|
| 29 |
top_p,
|
|
|
|
| 30 |
):
|
| 31 |
messages = [{"role": "system", "content": system_message}]
|
| 32 |
|
| 33 |
messages.extend(history)
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
messages.append({"role": "user", "content": message})
|
| 36 |
|
| 37 |
headers = {
|
|
@@ -39,6 +45,9 @@ def respond(
|
|
| 39 |
"Authorization": f"Bearer {API_TOKEN}"
|
| 40 |
}
|
| 41 |
|
|
|
|
|
|
|
|
|
|
| 42 |
data = {
|
| 43 |
"model": "/data/DMind-1-mini",
|
| 44 |
"stream": True,
|
|
@@ -50,9 +59,6 @@ def respond(
|
|
| 50 |
"max_tokens": 16384
|
| 51 |
}
|
| 52 |
|
| 53 |
-
# print(f"[INFO] process user msg...")
|
| 54 |
-
# print(f"[INFO] userMsg: {message}")
|
| 55 |
-
|
| 56 |
try:
|
| 57 |
with requests.post(API_URL, headers=headers, json=data, stream=True) as r:
|
| 58 |
if r.status_code == 200:
|
|
@@ -70,23 +76,30 @@ def respond(
|
|
| 70 |
if content:
|
| 71 |
current_response += content
|
| 72 |
|
| 73 |
-
if len(current_response) >
|
| 74 |
-
if
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
except json.JSONDecodeError:
|
| 91 |
continue
|
| 92 |
if current_response:
|
|
@@ -127,6 +140,7 @@ demo = gr.ChatInterface(
|
|
| 127 |
step=0.05,
|
| 128 |
label="Top-p (nucleus sampling)",
|
| 129 |
),
|
|
|
|
| 130 |
],
|
| 131 |
type="messages",
|
| 132 |
css="""
|
|
|
|
| 27 |
max_tokens,
|
| 28 |
temperature,
|
| 29 |
top_p,
|
| 30 |
+
with_think,
|
| 31 |
):
|
| 32 |
messages = [{"role": "system", "content": system_message}]
|
| 33 |
|
| 34 |
messages.extend(history)
|
| 35 |
|
| 36 |
+
if with_think:
|
| 37 |
+
message = message + " /think"
|
| 38 |
+
else:
|
| 39 |
+
message = message + " /no_think"
|
| 40 |
+
|
| 41 |
messages.append({"role": "user", "content": message})
|
| 42 |
|
| 43 |
headers = {
|
|
|
|
| 45 |
"Authorization": f"Bearer {API_TOKEN}"
|
| 46 |
}
|
| 47 |
|
| 48 |
+
# print(f"[INFO] process user msg...")
|
| 49 |
+
# print(f"[INFO] userMsg: {message}")
|
| 50 |
+
|
| 51 |
data = {
|
| 52 |
"model": "/data/DMind-1-mini",
|
| 53 |
"stream": True,
|
|
|
|
| 59 |
"max_tokens": 16384
|
| 60 |
}
|
| 61 |
|
|
|
|
|
|
|
|
|
|
| 62 |
try:
|
| 63 |
with requests.post(API_URL, headers=headers, json=data, stream=True) as r:
|
| 64 |
if r.status_code == 200:
|
|
|
|
| 76 |
if content:
|
| 77 |
current_response += content
|
| 78 |
|
| 79 |
+
if len(current_response) > 21:
|
| 80 |
+
if with_think:
|
| 81 |
+
if '<think>' in current_response:
|
| 82 |
+
current_response = current_response.replace('<think>', '<details open><summary>Thinking</summary>\n\n```')
|
| 83 |
+
if '</think>' in current_response:
|
| 84 |
+
current_response = current_response.replace('</think>', '```\n\n</details>')
|
| 85 |
+
if '**Final Answer**' in current_response:
|
| 86 |
+
current_response = current_response.replace('**Final Answer**', '')
|
| 87 |
+
|
| 88 |
+
formatted_response = current_response[:-16]
|
| 89 |
+
|
| 90 |
+
formatted_response = formatted_response.replace('<', '<').replace('>', '>')
|
| 91 |
+
formatted_response = formatted_response.replace('<details open>', '<details open>')
|
| 92 |
+
formatted_response = formatted_response.replace('</details>', '</details>')
|
| 93 |
+
formatted_response = formatted_response.replace('<summary>', '<summary>')
|
| 94 |
+
formatted_response = formatted_response.replace('</summary>', '</summary>')
|
| 95 |
+
formatted_response = formatted_response.replace('*', '\\*')
|
| 96 |
+
yield formatted_response
|
| 97 |
+
else:
|
| 98 |
+
if '<think>' in current_response and '</think>\n' in current_response:
|
| 99 |
+
start = current_response.find('<think>')
|
| 100 |
+
end = current_response.find('</think>\n') + len('</think>\n')
|
| 101 |
+
current_response = current_response[:start] + current_response[end:]
|
| 102 |
+
yield current_response
|
| 103 |
except json.JSONDecodeError:
|
| 104 |
continue
|
| 105 |
if current_response:
|
|
|
|
| 140 |
step=0.05,
|
| 141 |
label="Top-p (nucleus sampling)",
|
| 142 |
),
|
| 143 |
+
gr.Checkbox(value=False, label="With Think"),
|
| 144 |
],
|
| 145 |
type="messages",
|
| 146 |
css="""
|