ntphuc149 commited on
Commit
eb90bce
·
verified ·
1 Parent(s): e7ae3a9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +108 -105
app.py CHANGED
@@ -171,117 +171,120 @@ if prompt := st.chat_input(placeholder='Tôi có thể giúp được gì cho b
171
  message_placeholder = st.empty()
172
 
173
  full_response = ""
174
- classify_result = classify_question(question=prompt).json()
 
175
 
176
- print(f"The type of user query: {classify_result}")
177
-
178
- if classify_result == "BIDDING_RELATED":
179
- abs_answer = get_abstractive_answer(question=prompt)
180
-
181
- if isinstance(abs_answer, str):
182
- full_response = abs_answer
183
- message_placeholder.markdown(f"""
184
- <div class="assistant-message">
185
- <img src="./app/static/ai.jpg" class="assistant-avatar" />
186
- <div class="stMarkdown">{full_response}</div>
187
- </div>
188
- """, unsafe_allow_html=True)
189
- else:
190
- full_response = ""
191
- for line in abs_answer.iter_lines():
192
- if line:
193
- line = line.decode('utf-8')
194
- if line.startswith('data: '):
195
- data_str = line[6:]
196
- if data_str == '[DONE]':
197
- break
198
-
199
- try:
200
- data = json.loads(data_str)
201
- token = data.get('token', '')
202
- full_response += token
203
 
204
- message_placeholder.markdown(f"""
205
- <div class="assistant-message">
206
- <img src="./app/static/ai.jpg" class="assistant-avatar" />
207
- <div class="stMarkdown">{full_response}●</div>
208
- </div>
209
- """, unsafe_allow_html=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
210
 
211
- except json.JSONDecodeError:
212
- pass
213
-
214
- elif classify_result == "ABOUT_CHATBOT":
215
- answer = introduce_system(question=prompt)
216
-
217
- if isinstance(answer, str):
218
- full_response = answer
219
- message_placeholder.markdown(f"""
220
- <div class="assistant-message">
221
- <img src="./app/static/ai.jpg" class="assistant-avatar" />
222
- <div class="stMarkdown">{full_response}</div>
223
- </div>
224
- """, unsafe_allow_html=True)
 
225
  else:
226
- full_response = ""
227
- for line in answer.iter_lines():
228
- if line:
229
- line = line.decode('utf-8')
230
- if line.startswith('data: '):
231
- data_str = line[6:]
232
- if data_str == '[DONE]':
233
- break
234
-
235
- try:
236
- data = json.loads(data_str)
237
- token = data.get('token', '')
238
- full_response += token
239
-
240
- message_placeholder.markdown(f"""
241
- <div class="assistant-message">
242
- <img src="./app/static/ai.jpg" class="assistant-avatar" />
243
- <div class="stMarkdown">{full_response}●</div>
244
- </div>
245
- """, unsafe_allow_html=True)
246
-
247
- except json.JSONDecodeError:
248
- pass
249
 
250
- else:
251
- answer = response_unrelated_question(question=prompt)
252
-
253
- if isinstance(answer, str):
254
- full_response = answer
255
- message_placeholder.markdown(f"""
256
- <div class="assistant-message">
257
- <img src="./app/static/ai.jpg" class="assistant-avatar" />
258
- <div class="stMarkdown">{full_response}</div>
259
- </div>
260
- """, unsafe_allow_html=True)
261
- else:
262
- full_response = ""
263
- for line in answer.iter_lines():
264
- if line:
265
- line = line.decode('utf-8')
266
- if line.startswith('data: '):
267
- data_str = line[6:]
268
- if data_str == '[DONE]':
269
- break
270
-
271
- try:
272
- data = json.loads(data_str)
273
- token = data.get('token', '')
274
- full_response += token
275
-
276
- message_placeholder.markdown(f"""
277
- <div class="assistant-message">
278
- <img src="./app/static/ai.jpg" class="assistant-avatar" />
279
- <div class="stMarkdown">{full_response}●</div>
280
- </div>
281
- """, unsafe_allow_html=True)
282
 
283
- except json.JSONDecodeError:
284
- pass
 
 
 
 
 
 
 
 
 
 
 
 
 
 
285
 
286
  message_placeholder.markdown(f"""
287
  <div class="assistant-message">
 
171
  message_placeholder = st.empty()
172
 
173
  full_response = ""
174
+ try:
175
+ classify_result = classify_question(question=prompt).json()
176
 
177
+ print(f"The type of user query: {classify_result}")
178
+
179
+ if classify_result == "BIDDING_RELATED":
180
+ abs_answer = get_abstractive_answer(question=prompt)
181
+
182
+ if isinstance(abs_answer, str):
183
+ full_response = abs_answer
184
+ message_placeholder.markdown(f"""
185
+ <div class="assistant-message">
186
+ <img src="./app/static/ai.jpg" class="assistant-avatar" />
187
+ <div class="stMarkdown">{full_response}</div>
188
+ </div>
189
+ """, unsafe_allow_html=True)
190
+ else:
191
+ full_response = ""
192
+ for line in abs_answer.iter_lines():
193
+ if line:
194
+ line = line.decode('utf-8')
195
+ if line.startswith('data: '):
196
+ data_str = line[6:]
197
+ if data_str == '[DONE]':
198
+ break
 
 
 
 
 
199
 
200
+ try:
201
+ data = json.loads(data_str)
202
+ token = data.get('token', '')
203
+ full_response += token
204
+
205
+ message_placeholder.markdown(f"""
206
+ <div class="assistant-message">
207
+ <img src="./app/static/ai.jpg" class="assistant-avatar" />
208
+ <div class="stMarkdown">{full_response}●</div>
209
+ </div>
210
+ """, unsafe_allow_html=True)
211
+
212
+ except json.JSONDecodeError:
213
+ pass
214
+
215
+ elif classify_result == "ABOUT_CHATBOT":
216
+ answer = introduce_system(question=prompt)
217
+
218
+ if isinstance(answer, str):
219
+ full_response = answer
220
+ message_placeholder.markdown(f"""
221
+ <div class="assistant-message">
222
+ <img src="./app/static/ai.jpg" class="assistant-avatar" />
223
+ <div class="stMarkdown">{full_response}</div>
224
+ </div>
225
+ """, unsafe_allow_html=True)
226
+ else:
227
+ full_response = ""
228
+ for line in answer.iter_lines():
229
+ if line:
230
+ line = line.decode('utf-8')
231
+ if line.startswith('data: '):
232
+ data_str = line[6:]
233
+ if data_str == '[DONE]':
234
+ break
235
 
236
+ try:
237
+ data = json.loads(data_str)
238
+ token = data.get('token', '')
239
+ full_response += token
240
+
241
+ message_placeholder.markdown(f"""
242
+ <div class="assistant-message">
243
+ <img src="./app/static/ai.jpg" class="assistant-avatar" />
244
+ <div class="stMarkdown">{full_response}●</div>
245
+ </div>
246
+ """, unsafe_allow_html=True)
247
+
248
+ except json.JSONDecodeError:
249
+ pass
250
+
251
  else:
252
+ answer = response_unrelated_question(question=prompt)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
253
 
254
+ if isinstance(answer, str):
255
+ full_response = answer
256
+ message_placeholder.markdown(f"""
257
+ <div class="assistant-message">
258
+ <img src="./app/static/ai.jpg" class="assistant-avatar" />
259
+ <div class="stMarkdown">{full_response}</div>
260
+ </div>
261
+ """, unsafe_allow_html=True)
262
+ else:
263
+ full_response = ""
264
+ for line in answer.iter_lines():
265
+ if line:
266
+ line = line.decode('utf-8')
267
+ if line.startswith('data: '):
268
+ data_str = line[6:]
269
+ if data_str == '[DONE]':
270
+ break
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
271
 
272
+ try:
273
+ data = json.loads(data_str)
274
+ token = data.get('token', '')
275
+ full_response += token
276
+
277
+ message_placeholder.markdown(f"""
278
+ <div class="assistant-message">
279
+ <img src="./app/static/ai.jpg" class="assistant-avatar" />
280
+ <div class="stMarkdown">{full_response}●</div>
281
+ </div>
282
+ """, unsafe_allow_html=True)
283
+
284
+ except json.JSONDecodeError:
285
+ pass
286
+ except error:
287
+ full_response = "Hiện tại bot đang nghỉ xíu để sạc pin 🔌. Bạn hãy quay lại sau nhé!"
288
 
289
  message_placeholder.markdown(f"""
290
  <div class="assistant-message">