Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -467,7 +467,21 @@ if not st.session_state.get("chat_started", False):
|
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 471 |
prompt = st.chat_input("")
|
| 472 |
if prompt :
|
| 473 |
|
|
@@ -492,19 +506,18 @@ if prompt :
|
|
| 492 |
})
|
| 493 |
full_response = output["output"]
|
| 494 |
|
| 495 |
-
|
| 496 |
-
|
| 497 |
|
| 498 |
# Display the response
|
|
|
|
| 499 |
|
| 500 |
-
st.write(full_response)
|
| 501 |
follow_up_output = agent_executor.invoke({
|
| 502 |
"input": f"Extract 1-2 follow-up questions from the following text: {full_response}",
|
| 503 |
"chat_history": st.session_state.chat_history
|
| 504 |
})
|
| 505 |
follow_up_questions = follow_up_output["output"]
|
| 506 |
-
|
| 507 |
-
|
| 508 |
|
| 509 |
|
| 510 |
except Exception as e:
|
|
@@ -514,4 +527,12 @@ if prompt :
|
|
| 514 |
|
| 515 |
# Add AI response to chat history
|
| 516 |
st.session_state.chat_history.append({"role": "assistant", "content": full_response})
|
| 517 |
-
copy_to_clipboard(full_response)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
+
def preprocess_text(text):
|
| 471 |
+
# Remove extra spaces
|
| 472 |
+
text = re.sub(r'\s+', ' ', text).strip()
|
| 473 |
+
|
| 474 |
+
# Ensure proper spacing after punctuation
|
| 475 |
+
text = re.sub(r'([.,!?])(\S)', r'\1 \2', text)
|
| 476 |
+
|
| 477 |
+
# Replace markdown bold and italic with HTML tags
|
| 478 |
+
text = re.sub(r'\*\*(.*?)\*\*', r'<strong>\1</strong>', text)
|
| 479 |
+
text = re.sub(r'\*(.*?)\*', r'<em>\1</em>', text)
|
| 480 |
+
|
| 481 |
+
# Ensure proper line breaks
|
| 482 |
+
text = text.replace('\n', '<br>')
|
| 483 |
+
|
| 484 |
+
return text
|
| 485 |
prompt = st.chat_input("")
|
| 486 |
if prompt :
|
| 487 |
|
|
|
|
| 506 |
})
|
| 507 |
full_response = output["output"]
|
| 508 |
|
| 509 |
+
processed_response = preprocess_text(full_response)
|
|
|
|
| 510 |
|
| 511 |
# Display the response
|
| 512 |
+
st.markdown(processed_response, unsafe_allow_html=True)
|
| 513 |
|
|
|
|
| 514 |
follow_up_output = agent_executor.invoke({
|
| 515 |
"input": f"Extract 1-2 follow-up questions from the following text: {full_response}",
|
| 516 |
"chat_history": st.session_state.chat_history
|
| 517 |
})
|
| 518 |
follow_up_questions = follow_up_output["output"]
|
| 519 |
+
st.session_state.follow_up_questions = preprocess_text(follow_up_questions)
|
| 520 |
+
|
| 521 |
|
| 522 |
|
| 523 |
except Exception as e:
|
|
|
|
| 527 |
|
| 528 |
# Add AI response to chat history
|
| 529 |
st.session_state.chat_history.append({"role": "assistant", "content": full_response})
|
| 530 |
+
copy_to_clipboard(full_response)
|
| 531 |
+
|
| 532 |
+
|
| 533 |
+
|
| 534 |
+
if st.session_state.get("follow_up_questions"):
|
| 535 |
+
st.markdown("Follow-up questions:")
|
| 536 |
+
st.markdown(st.session_state.follow_up_questions, unsafe_allow_html=True)
|
| 537 |
+
# Clear follow-up questions for the next iteration
|
| 538 |
+
st.session_state.follow_up_questions = None
|