nanova commited on
Commit
0046a95
·
1 Parent(s): 729c27a
Files changed (1) hide show
  1. app.py +34 -20
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) > 16:
74
- if '<think>' in current_response:
75
- current_response = current_response.replace('<think>', '<details open><summary>Thinking</summary>\n\n```')
76
- if '</think>' in current_response:
77
- current_response = current_response.replace('</think>', '```\n\n</details>')
78
- if '**Final Answer**' in current_response:
79
- current_response = current_response.replace('**Final Answer**', '')
80
-
81
- formatted_response = current_response[:-16]
82
-
83
- formatted_response = formatted_response.replace('<', '&lt;').replace('>', '&gt;')
84
- formatted_response = formatted_response.replace('&lt;details open&gt;', '<details open>')
85
- formatted_response = formatted_response.replace('&lt;/details&gt;', '</details>')
86
- formatted_response = formatted_response.replace('&lt;summary&gt;', '<summary>')
87
- formatted_response = formatted_response.replace('&lt;/summary&gt;', '</summary>')
88
- formatted_response = formatted_response.replace('*', '\\*')
89
- yield formatted_response
 
 
 
 
 
 
 
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('<', '&lt;').replace('>', '&gt;')
91
+ formatted_response = formatted_response.replace('&lt;details open&gt;', '<details open>')
92
+ formatted_response = formatted_response.replace('&lt;/details&gt;', '</details>')
93
+ formatted_response = formatted_response.replace('&lt;summary&gt;', '<summary>')
94
+ formatted_response = formatted_response.replace('&lt;/summary&gt;', '</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="""