YC-Chen commited on
Commit
8ca00aa
1 Parent(s): f7bf665

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -35
app.py CHANGED
@@ -148,6 +148,40 @@ with gr.Blocks() as demo:
148
  return "", history + [[user_message, None]]
149
 
150
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
151
  def bot(history, max_new_tokens, temperature, top_p, system_prompt):
152
  chat_data = []
153
  system_prompt = system_prompt.strip()
@@ -163,7 +197,7 @@ with gr.Blocks() as demo:
163
  if len(message) > MAX_INPUT_LENGTH:
164
  raise Exception()
165
 
166
- response = ''
167
  if refusal_condition(history[-1][0]):
168
  history = [['[安全拒答啟動]', '[安全拒答啟動] 請清除再開啟對話']]
169
  response = '[REFUSAL]'
@@ -180,41 +214,23 @@ with gr.Blocks() as demo:
180
  }
181
  }
182
 
183
- start_time = time.time()
184
- keep_streaming = True
185
- s = requests.Session()
186
- with s.post(API_URL, headers=HEADERS, json=data, stream=True, timeout=30) as r:
187
- for line in r.iter_lines():
188
- time.sleep(0.005)
189
- if time.time() - start_time > MAX_SEC:
190
- keep_streaming = False
191
- break
192
-
193
- if line and keep_streaming:
194
- if r.status_code != 200:
195
- continue
196
- json_response = json.loads(line)
197
-
198
- if "fragment" not in json_response["result"]:
199
- keep_streaming = False
200
- break
201
 
202
- delta = json_response["result"]["fragment"]["data"]["text"]
203
-
204
- if history[-1][1] is None:
205
- history[-1][1] = ''
206
- history[-1][1] += delta
207
- yield history
208
-
209
- if history[-1][1].endswith('</s>'):
210
- history[-1][1] = history[-1][1][:-4]
211
- yield history
212
-
213
- response = history[-1][1]
214
-
215
- if refusal_condition(history[-1][1]):
216
- history[-1][1] = history[-1][1] + '\n\n**[免責聲明: Breeze-7B-Instruct 和 Breeze-7B-Instruct-64k 並未針對問答進行安全保護,因此語言模型的任何回應不代表 MediaTek Research 立場。]**'
217
- yield history
218
 
219
  print('== Record ==\nQuery: {query}\nResponse: {response}'.format(query=repr(message), response=repr(history[-1][1])))
220
  insert_to_db(message, response, float(temperature), float(top_p))
 
148
  return "", history + [[user_message, None]]
149
 
150
 
151
+ def connect_server(data):
152
+ for _ in range(3):
153
+ s = requests.Session()
154
+ r = s.post(API_URL, headers=HEADERS, json=data, stream=True, timeout=30)
155
+ time.sleep(1)
156
+ if r.status_code == 200:
157
+ return r
158
+ return None
159
+
160
+
161
+ def stream_response_from_server(r):
162
+ # start_time = time.time()
163
+ keep_streaming = True
164
+ for line in r.iter_lines():
165
+ time.sleep(0.001)
166
+ # if time.time() - start_time > MAX_SEC:
167
+ # keep_streaming = False
168
+ # break
169
+
170
+ if line and keep_streaming:
171
+ if r.status_code != 200:
172
+ continue
173
+ json_response = json.loads(line)
174
+
175
+ if "fragment" not in json_response["result"]:
176
+ keep_streaming = False
177
+ break
178
+
179
+ delta = json_response["result"]["fragment"]["data"]["text"]
180
+ yield delta
181
+
182
+ # start_time = time.time()
183
+
184
+
185
  def bot(history, max_new_tokens, temperature, top_p, system_prompt):
186
  chat_data = []
187
  system_prompt = system_prompt.strip()
 
197
  if len(message) > MAX_INPUT_LENGTH:
198
  raise Exception()
199
 
200
+ response = '[ERROR]'
201
  if refusal_condition(history[-1][0]):
202
  history = [['[安全拒答啟動]', '[安全拒答啟動] 請清除再開啟對話']]
203
  response = '[REFUSAL]'
 
214
  }
215
  }
216
 
217
+ r = connect_server(data)
218
+ if r is not None:
219
+ for delta in stream_response_from_server(r):
220
+ if history[-1][1] is None:
221
+ history[-1][1] = ''
222
+ history[-1][1] += delta
223
+ yield history
224
+
225
+ if history[-1][1].endswith('</s>'):
226
+ history[-1][1] = history[-1][1][:-4]
227
+ yield history
228
+
229
+ response = history[-1][1]
 
 
 
 
 
230
 
231
+ if refusal_condition(history[-1][1]):
232
+ history[-1][1] = history[-1][1] + '\n\n**[免責聲明: Breeze-7B-Instruct 和 Breeze-7B-Instruct-64k 並未針對問答進行安全保護,因此語言模型的任何回應不代表 MediaTek Research 立場。]**'
233
+ yield history
 
 
 
 
 
 
 
 
 
 
 
 
 
234
 
235
  print('== Record ==\nQuery: {query}\nResponse: {response}'.format(query=repr(message), response=repr(history[-1][1])))
236
  insert_to_db(message, response, float(temperature), float(top_p))