Update app.py
Browse files
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 |
-
|
|
|
175 |
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
try:
|
200 |
-
data = json.loads(data_str)
|
201 |
-
token = data.get('token', '')
|
202 |
-
full_response += token
|
203 |
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
210 |
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
|
|
225 |
else:
|
226 |
-
|
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 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
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 |
-
|
284 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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">
|