ntphuc149 commited on
Commit
dc9c974
·
verified ·
1 Parent(s): beec66a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +110 -111
app.py CHANGED
@@ -26,7 +26,6 @@ url_api_extract_reference_model = f"{routing_response_module}/extract_references
26
  with open("./static/styles.css") as f:
27
  st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html=True)
28
 
29
-
30
  if 'messages' not in st.session_state:
31
  st.session_state.messages = [{'role': 'assistant', 'content': "Xin chào. Tôi là trợ lý AI giáo dục Việt Nam được phát triển bởi Đào Thị Ngọc Ánh. Rất vui khi được hỗ trợ bạn trong học tập!"}]
32
 
@@ -162,130 +161,130 @@ if prompt := st.chat_input(placeholder='Tôi có thể giúp được gì cho b
162
 
163
  message_placeholder = st.empty()
164
 
165
- full_response = ""
166
- classify_result = classify_question(question=prompt).json()
167
-
168
- print(f"The type of user query: {classify_result}")
169
-
170
- if classify_result == "EDUCATION_RELATED":
171
- retrieved_context = retrieve_context(question=prompt, top_k=10)
172
- retrieved_context = [item['text'] for item in retrieved_context]
173
- reranked_context = rerank_context(url_rerank_module=url_api_reranker_model,
174
- question=prompt,
175
- relevant_docs=retrieved_context,
176
- top_k=5)[0]
177
-
178
- abs_answer = get_abstractive_answer(context=reranked_context, question=prompt)
179
-
180
- if isinstance(abs_answer, str):
181
- full_response = abs_answer
182
- message_placeholder.markdown(f"""
183
- <div class="assistant-message">
184
- <img src="./app/static/ai.jpg" class="assistant-avatar" />
185
- <div class="stMarkdown">{full_response}</div>
186
- </div>
187
- """, unsafe_allow_html=True)
188
- else:
189
- full_response = ""
190
- for line in abs_answer.iter_lines():
191
- if line:
192
- line = line.decode('utf-8')
193
- if line.startswith('data: '):
194
- data_str = line[6:]
195
- if data_str == '[DONE]':
196
- break
 
 
 
 
 
197
 
198
- try:
199
- data = json.loads(data_str)
200
- token = data.get('token', '')
201
- full_response += token
202
-
203
- message_placeholder.markdown(f"""
204
- <div class="assistant-message">
205
- <img src="./app/static/ai.jpg" class="assistant-avatar" />
206
- <div class="stMarkdown">{full_response}●</div>
207
- </div>
208
- """, unsafe_allow_html=True)
209
-
210
- except json.JSONDecodeError:
211
- pass
212
 
213
  # refs = st.expander("Tài liệu tham khảo", expanded=False)
214
  # refs_list = get_references(context=reranked_context, question=prompt, answer=full_response)
215
  # print(refs_list)
216
  # refs.write(f"{refs_list}")
217
 
218
- elif classify_result == "ABOUT_CHATBOT":
219
- answer = introduce_system(question=prompt)
220
-
221
- if isinstance(answer, str):
222
- full_response = answer
223
- message_placeholder.markdown(f"""
224
- <div class="assistant-message">
225
- <img src="./app/static/ai.jpg" class="assistant-avatar" />
226
- <div class="stMarkdown">{full_response}</div>
227
- </div>
228
- """, unsafe_allow_html=True)
229
- else:
230
- full_response = ""
231
- for line in answer.iter_lines():
232
- if line:
233
- line = line.decode('utf-8')
234
- if line.startswith('data: '):
235
- data_str = line[6:]
236
- if data_str == '[DONE]':
237
- break
238
 
239
- try:
240
- data = json.loads(data_str)
241
- token = data.get('token', '')
242
- full_response += token
243
 
244
- message_placeholder.markdown(f"""
245
- <div class="assistant-message">
246
- <img src="./app/static/ai.jpg" class="assistant-avatar" />
247
- <div class="stMarkdown">{full_response}●</div>
248
- </div>
249
- """, unsafe_allow_html=True)
250
 
251
- except json.JSONDecodeError:
252
- pass
253
 
254
- else:
255
- answer = response_unrelated_question(question=prompt)
256
-
257
- if isinstance(answer, str):
258
- full_response = answer
259
- message_placeholder.markdown(f"""
260
- <div class="assistant-message">
261
- <img src="./app/static/ai.jpg" class="assistant-avatar" />
262
- <div class="stMarkdown">{full_response}</div>
263
- </div>
264
- """, unsafe_allow_html=True)
265
- else:
266
- full_response = ""
267
- for line in answer.iter_lines():
268
- if line:
269
- line = line.decode('utf-8')
270
- if line.startswith('data: '):
271
- data_str = line[6:]
272
- if data_str == '[DONE]':
273
- break
274
 
275
- try:
276
- data = json.loads(data_str)
277
- token = data.get('token', '')
278
- full_response += token
279
 
280
- message_placeholder.markdown(f"""
281
- <div class="assistant-message">
282
- <img src="./app/static/ai.jpg" class="assistant-avatar" />
283
- <div class="stMarkdown">{full_response}●</div>
284
- </div>
285
- """, unsafe_allow_html=True)
286
 
287
- except json.JSONDecodeError:
288
- pass
289
 
290
  message_placeholder.markdown(f"""
291
  <div class="assistant-message">
 
26
  with open("./static/styles.css") as f:
27
  st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html=True)
28
 
 
29
  if 'messages' not in st.session_state:
30
  st.session_state.messages = [{'role': 'assistant', 'content': "Xin chào. Tôi là trợ lý AI giáo dục Việt Nam được phát triển bởi Đào Thị Ngọc Ánh. Rất vui khi được hỗ trợ bạn trong học tập!"}]
31
 
 
161
 
162
  message_placeholder = st.empty()
163
 
164
+ # full_response = ""
165
+ # classify_result = classify_question(question=prompt).json()
166
+
167
+ # print(f"The type of user query: {classify_result}")
168
+
169
+ # if classify_result == "EDUCATION_RELATED":
170
+ retrieved_context = retrieve_context(question=prompt, top_k=10)
171
+ retrieved_context = [item['text'] for item in retrieved_context]
172
+ reranked_context = rerank_context(url_rerank_module=url_api_reranker_model,
173
+ question=prompt,
174
+ relevant_docs=retrieved_context,
175
+ top_k=5)[0]
176
+
177
+ abs_answer = get_abstractive_answer(context=reranked_context, question=prompt)
178
+
179
+ if isinstance(abs_answer, str):
180
+ full_response = abs_answer
181
+ message_placeholder.markdown(f"""
182
+ <div class="assistant-message">
183
+ <img src="./app/static/ai.jpg" class="assistant-avatar" />
184
+ <div class="stMarkdown">{full_response}</div>
185
+ </div>
186
+ """, unsafe_allow_html=True)
187
+ else:
188
+ full_response = ""
189
+ for line in abs_answer.iter_lines():
190
+ if line:
191
+ line = line.decode('utf-8')
192
+ if line.startswith('data: '):
193
+ data_str = line[6:]
194
+ if data_str == '[DONE]':
195
+ break
196
+
197
+ try:
198
+ data = json.loads(data_str)
199
+ token = data.get('token', '')
200
+ full_response += token
201
 
202
+ message_placeholder.markdown(f"""
203
+ <div class="assistant-message">
204
+ <img src="./app/static/ai.jpg" class="assistant-avatar" />
205
+ <div class="stMarkdown">{full_response}●</div>
206
+ </div>
207
+ """, unsafe_allow_html=True)
208
+
209
+ except json.JSONDecodeError:
210
+ pass
 
 
 
 
 
211
 
212
  # refs = st.expander("Tài liệu tham khảo", expanded=False)
213
  # refs_list = get_references(context=reranked_context, question=prompt, answer=full_response)
214
  # print(refs_list)
215
  # refs.write(f"{refs_list}")
216
 
217
+ # elif classify_result == "ABOUT_CHATBOT":
218
+ # answer = introduce_system(question=prompt)
219
+
220
+ # if isinstance(answer, str):
221
+ # full_response = answer
222
+ # message_placeholder.markdown(f"""
223
+ # <div class="assistant-message">
224
+ # <img src="./app/static/ai.jpg" class="assistant-avatar" />
225
+ # <div class="stMarkdown">{full_response}</div>
226
+ # </div>
227
+ # """, unsafe_allow_html=True)
228
+ # else:
229
+ # full_response = ""
230
+ # for line in answer.iter_lines():
231
+ # if line:
232
+ # line = line.decode('utf-8')
233
+ # if line.startswith('data: '):
234
+ # data_str = line[6:]
235
+ # if data_str == '[DONE]':
236
+ # break
237
 
238
+ # try:
239
+ # data = json.loads(data_str)
240
+ # token = data.get('token', '')
241
+ # full_response += token
242
 
243
+ # message_placeholder.markdown(f"""
244
+ # <div class="assistant-message">
245
+ # <img src="./app/static/ai.jpg" class="assistant-avatar" />
246
+ # <div class="stMarkdown">{full_response}●</div>
247
+ # </div>
248
+ # """, unsafe_allow_html=True)
249
 
250
+ # except json.JSONDecodeError:
251
+ # pass
252
 
253
+ # else:
254
+ # answer = response_unrelated_question(question=prompt)
255
+
256
+ # if isinstance(answer, str):
257
+ # full_response = answer
258
+ # message_placeholder.markdown(f"""
259
+ # <div class="assistant-message">
260
+ # <img src="./app/static/ai.jpg" class="assistant-avatar" />
261
+ # <div class="stMarkdown">{full_response}</div>
262
+ # </div>
263
+ # """, unsafe_allow_html=True)
264
+ # else:
265
+ # full_response = ""
266
+ # for line in answer.iter_lines():
267
+ # if line:
268
+ # line = line.decode('utf-8')
269
+ # if line.startswith('data: '):
270
+ # data_str = line[6:]
271
+ # if data_str == '[DONE]':
272
+ # break
273
 
274
+ # try:
275
+ # data = json.loads(data_str)
276
+ # token = data.get('token', '')
277
+ # full_response += token
278
 
279
+ # message_placeholder.markdown(f"""
280
+ # <div class="assistant-message">
281
+ # <img src="./app/static/ai.jpg" class="assistant-avatar" />
282
+ # <div class="stMarkdown">{full_response}●</div>
283
+ # </div>
284
+ # """, unsafe_allow_html=True)
285
 
286
+ # except json.JSONDecodeError:
287
+ # pass
288
 
289
  message_placeholder.markdown(f"""
290
  <div class="assistant-message">