Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
| 172 |
-
|
| 173 |
-
|
| 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 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 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 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
|
| 251 |
-
|
| 252 |
-
|
| 253 |
|
| 254 |
-
else:
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
|
| 287 |
-
|
| 288 |
-
|
| 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">
|