Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -145,56 +145,25 @@ def send_feedback_via_email(name, email, feedback):
|
|
| 145 |
st.error(f"Error sending email: {e}")
|
| 146 |
|
| 147 |
def clean_text(text):
|
| 148 |
-
#
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
# Convert <a> tags to Markdown links
|
| 152 |
-
def convert_links(match):
|
| 153 |
-
url = match.group(1)
|
| 154 |
-
link_text = match.group(2)
|
| 155 |
-
return f"[{link_text}]({url})"
|
| 156 |
-
|
| 157 |
-
# Handle <a> tags to preserve clickable URLs in Markdown format
|
| 158 |
-
text = re.sub(r'<a [^>]*href="([^"]+)"[^>]*>(.*?)</a>', convert_links, text)
|
| 159 |
|
| 160 |
-
#
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
return f"**{content.strip()}**"
|
| 164 |
-
|
| 165 |
-
text = re.sub(r'(<h[1-6][^>]*>)(.*?)(</h[1-6]>)', preserve_headings, text)
|
| 166 |
-
|
| 167 |
-
# Remove all other HTML tags including <span>, <p>, <b>, <i>, etc.
|
| 168 |
-
text = re.sub(r'<[^>]+>', '', text)
|
| 169 |
|
| 170 |
-
#
|
| 171 |
-
|
|
|
|
|
|
|
| 172 |
|
| 173 |
-
#
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
# Split the text into paragraphs
|
| 177 |
-
paragraphs = text.split('\n\n')
|
| 178 |
-
|
| 179 |
-
cleaned_paragraphs = []
|
| 180 |
-
for paragraph in paragraphs:
|
| 181 |
-
lines = paragraph.split('\n')
|
| 182 |
-
cleaned_lines = []
|
| 183 |
-
for line in lines:
|
| 184 |
-
# Remove extra spaces
|
| 185 |
-
cleaned_line = re.sub(r'\s+', ' ', line).strip()
|
| 186 |
-
cleaned_lines.append(cleaned_line)
|
| 187 |
-
|
| 188 |
-
# Join the lines within each paragraph
|
| 189 |
-
cleaned_paragraph = '\n'.join(cleaned_lines)
|
| 190 |
-
cleaned_paragraphs.append(cleaned_paragraph)
|
| 191 |
|
| 192 |
-
#
|
| 193 |
-
cleaned_text = '\n\n'
|
| 194 |
|
| 195 |
return cleaned_text
|
| 196 |
-
|
| 197 |
-
|
| 198 |
def side():
|
| 199 |
with st.sidebar.form(key='feedback_form'):
|
| 200 |
|
|
@@ -581,57 +550,57 @@ suggestions = [
|
|
| 581 |
]
|
| 582 |
|
| 583 |
|
| 584 |
-
|
| 585 |
-
prompt = st.chat_input("")
|
| 586 |
-
if prompt :
|
| 587 |
-
|
| 588 |
-
st.session_state["chat_started"] = True
|
| 589 |
-
# Add user message to chat history
|
| 590 |
-
st.session_state.chat_history.append({"role": "user", "content": prompt})
|
| 591 |
|
| 592 |
-
|
| 593 |
-
|
| 594 |
-
st.
|
| 595 |
-
|
| 596 |
-
# Generate AI response
|
| 597 |
-
with st.chat_message("assistant"):
|
| 598 |
-
full_response = ""
|
| 599 |
|
| 600 |
-
|
| 601 |
-
|
| 602 |
-
|
| 603 |
-
|
| 604 |
-
|
| 605 |
-
|
| 606 |
-
|
| 607 |
-
full_response = output["output"]
|
| 608 |
-
full_response= replace_terms(full_response)
|
| 609 |
-
|
| 610 |
-
cleaned_text = clean_text(full_response)
|
| 611 |
-
|
| 612 |
|
| 613 |
-
|
| 614 |
-
|
| 615 |
-
|
| 616 |
-
|
| 617 |
-
|
| 618 |
-
|
| 619 |
-
|
|
|
|
|
|
|
| 620 |
|
| 621 |
-
|
| 622 |
-
|
| 623 |
-
|
| 624 |
-
|
| 625 |
-
|
| 626 |
-
|
| 627 |
-
|
| 628 |
-
|
| 629 |
-
|
| 630 |
-
|
| 631 |
-
|
| 632 |
-
|
| 633 |
-
|
| 634 |
-
|
| 635 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 636 |
|
| 637 |
|
|
|
|
| 145 |
st.error(f"Error sending email: {e}")
|
| 146 |
|
| 147 |
def clean_text(text):
|
| 148 |
+
# Parse HTML with BeautifulSoup
|
| 149 |
+
soup = BeautifulSoup(text, 'html.parser')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
|
| 151 |
+
# Convert <a> tags to Markdown links
|
| 152 |
+
for a in soup.find_all('a'):
|
| 153 |
+
a.replace_with(f"[{a.get_text()}]({a['href']})")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
|
| 155 |
+
# Remove unwanted tags but preserve text
|
| 156 |
+
for tag in ['span', 'i', 'b']:
|
| 157 |
+
for element in soup.find_all(tag):
|
| 158 |
+
element.unwrap() # Remove the tag but keep the content
|
| 159 |
|
| 160 |
+
# Get the cleaned text
|
| 161 |
+
cleaned_text = soup.get_text()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
|
| 163 |
+
# Fix text formatting (e.g., add newlines for paragraphs)
|
| 164 |
+
cleaned_text = re.sub(r'\n\s*\n', '\n\n', cleaned_text)
|
| 165 |
|
| 166 |
return cleaned_text
|
|
|
|
|
|
|
| 167 |
def side():
|
| 168 |
with st.sidebar.form(key='feedback_form'):
|
| 169 |
|
|
|
|
| 550 |
]
|
| 551 |
|
| 552 |
|
| 553 |
+
with st.container():
|
| 554 |
+
prompt = st.chat_input("")
|
| 555 |
+
if prompt :
|
|
|
|
|
|
|
|
|
|
|
|
|
| 556 |
|
| 557 |
+
st.session_state["chat_started"] = True
|
| 558 |
+
# Add user message to chat history
|
| 559 |
+
st.session_state.chat_history.append({"role": "user", "content": prompt})
|
|
|
|
|
|
|
|
|
|
|
|
|
| 560 |
|
| 561 |
+
# Display user message
|
| 562 |
+
with st.chat_message("user"):
|
| 563 |
+
st.markdown(prompt)
|
| 564 |
+
|
| 565 |
+
# Generate AI response
|
| 566 |
+
with st.chat_message("assistant"):
|
| 567 |
+
full_response = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 568 |
|
| 569 |
+
try:
|
| 570 |
+
# Generate response using the agent executor
|
| 571 |
+
output = agent_executor.invoke({
|
| 572 |
+
"input": f"{prompt} Always Be specific with numbers, dates, people, and $dollar amounts( Do Mention them) provide Latest information . Always Be specific and provide bold point examples and also Embed source links directly next to each point. All outputs should be in structured format.Remove itallic and span tags ",
|
| 573 |
+
|
| 574 |
+
"chat_history": st.session_state.chat_history
|
| 575 |
+
})
|
| 576 |
+
full_response = output["output"]
|
| 577 |
+
full_response= replace_terms(full_response)
|
| 578 |
|
| 579 |
+
cleaned_text = clean_text(full_response)
|
| 580 |
+
|
| 581 |
+
|
| 582 |
+
#cleaned_text = re.sub(r'</span>', '', cleaned_text)
|
| 583 |
+
#cleaned_text = re.sub(r'<span[^>]*>', '', cleaned_text)
|
| 584 |
+
# Display the response
|
| 585 |
+
if "**Trust Tip**" not in cleaned_text and "**Suggestion**" not in cleaned_text:
|
| 586 |
+
trust_tip = random.choice(trust_tips)
|
| 587 |
+
suggestion = random.choice(suggestions)
|
| 588 |
+
cleaned_text += f"\n\n**Trust Tip**: {trust_tip} \n\n **Suggestion**: {suggestion}"
|
| 589 |
+
|
| 590 |
+
st.markdown(cleaned_text,unsafe_allow_html=True)
|
| 591 |
+
|
| 592 |
+
|
| 593 |
+
|
| 594 |
+
|
| 595 |
+
|
| 596 |
+
|
| 597 |
+
except Exception as e:
|
| 598 |
+
logger.error(f"Error generating response: {e}")
|
| 599 |
+
full_response = "I apologize, but an error occurred while generating the response. Please try again."
|
| 600 |
+
st.write(full_response)
|
| 601 |
+
|
| 602 |
+
# Add AI response to chat history
|
| 603 |
+
st.session_state.chat_history.append({"role": "assistant", "content": full_response})
|
| 604 |
+
copy_to_clipboard(full_response)
|
| 605 |
|
| 606 |
|