YC-Chen commited on
Commit
c417521
·
verified ·
1 Parent(s): 1ccecb7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -37
app.py CHANGED
@@ -121,47 +121,46 @@ with gr.Blocks() as demo:
121
  if assistant_msg is not None:
122
  chat_data.append({"role": "assistant", "content": assistant_msg})
123
 
124
- if refusal_condition(history[-1][0]):
125
- history[-1][1] = '抱歉,我無法回答您這個問題'
126
- return history
127
-
128
  message = tokenizer.apply_chat_template(chat_data, tokenize=False)
129
  message = message[3:] # remove SOT token
130
 
131
-
132
- data = {
133
- "model": MODEL_NAME,
134
- "prompt": str(message),
135
- "temperature": float(temperature) + 0.01,
136
- "n": 1,
137
- "max_tokens": int(max_new_tokens),
138
- "stop": "",
139
- "top_p": float(top_p),
140
- "logprobs": 0,
141
- "echo": False,
142
- "presence_penalty": PRESENCE_PENALTY,
143
- "frequency_penalty": FREQUENCY_PENALTY,
144
- "stream": True,
145
- }
146
-
147
- with requests.post(API_URL, headers=HEADERS, data=json.dumps(data), stream=True) as r:
148
- for response in r.iter_lines():
149
- if len(response) > 0:
150
- text = response.decode()
151
- if text != "data: [DONE]":
152
- if text.startswith("data: "):
153
- text = text[5:]
154
- delta = json.loads(text)["choices"][0]["text"]
155
-
156
- if history[-1][1] is None:
157
- history[-1][1] = delta
158
- else:
159
- history[-1][1] += delta
160
- yield history
161
- if history[-1][1].endswith('</s>'):
162
- history[-1][1] = history[-1][1][:-4]
163
  yield history
164
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
165
  print('== Record ==\nQuery: {query}\nResponse: {response}'.format(query=repr(message), response=repr(history[-1][1])))
166
 
167
  msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
 
121
  if assistant_msg is not None:
122
  chat_data.append({"role": "assistant", "content": assistant_msg})
123
 
 
 
 
 
124
  message = tokenizer.apply_chat_template(chat_data, tokenize=False)
125
  message = message[3:] # remove SOT token
126
 
127
+ if refusal_condition(history[-1][0]):
128
+ history[-1][1] = '抱歉,我無法回答您這個問題'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
129
  yield history
130
+ else:
131
+ data = {
132
+ "model": MODEL_NAME,
133
+ "prompt": str(message),
134
+ "temperature": float(temperature) + 0.01,
135
+ "n": 1,
136
+ "max_tokens": int(max_new_tokens),
137
+ "stop": "",
138
+ "top_p": float(top_p),
139
+ "logprobs": 0,
140
+ "echo": False,
141
+ "presence_penalty": PRESENCE_PENALTY,
142
+ "frequency_penalty": FREQUENCY_PENALTY,
143
+ "stream": True,
144
+ }
145
+
146
+ with requests.post(API_URL, headers=HEADERS, data=json.dumps(data), stream=True) as r:
147
+ for response in r.iter_lines():
148
+ if len(response) > 0:
149
+ text = response.decode()
150
+ if text != "data: [DONE]":
151
+ if text.startswith("data: "):
152
+ text = text[5:]
153
+ delta = json.loads(text)["choices"][0]["text"]
154
+
155
+ if history[-1][1] is None:
156
+ history[-1][1] = delta
157
+ else:
158
+ history[-1][1] += delta
159
+ yield history
160
+ if history[-1][1].endswith('</s>'):
161
+ history[-1][1] = history[-1][1][:-4]
162
+ yield history
163
+
164
  print('== Record ==\nQuery: {query}\nResponse: {response}'.format(query=repr(message), response=repr(history[-1][1])))
165
 
166
  msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(