Wajahat698 commited on
Commit
39d0e4b
·
verified ·
1 Parent(s): 72b7576

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -20
app.py CHANGED
@@ -145,24 +145,55 @@ def send_feedback_via_email(name, email, feedback):
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'):
@@ -550,9 +581,9 @@ suggestions = [
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
 
145
  st.error(f"Error sending email: {e}")
146
 
147
  def clean_text(text):
148
+ # Replace escaped newlines with actual newlines
149
+ text = text.replace('\\n', '\n')
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
+ # Remove <span>, <i>, <b>, and other unwanted HTML tags
161
+ text = re.sub(r'<span[^>]*>|</span>|<i[^>]*>|</i>|<b[^>]*>|</b>', '', text)
162
+
163
+ # Remove any remaining HTML tags
164
+ text = re.sub(r'<[^>]+>', '', text)
165
+
166
+ # Split the text into paragraphs
167
+ paragraphs = text.split('\n\n')
168
+
169
+ cleaned_paragraphs = []
170
+ for paragraph in paragraphs:
171
+ lines = paragraph.split('\n')
172
+ cleaned_lines = []
173
+ for line in lines:
174
+ # Preserve and correctly format headings or bold text
175
+ if line.strip().startswith('**') and line.strip().endswith('**'):
176
+ cleaned_line = line.strip()
177
+ else:
178
+ # Remove asterisks, special characters, and fix merged text
179
+ cleaned_line = re.sub(r'\*|\−|\∗', '', line)
180
+ cleaned_line = re.sub(r'([a-z])([A-Z])', r'\1 \2', cleaned_line)
181
+
182
+ # Handle bullet points correctly
183
+ if cleaned_line.strip().startswith('-'):
184
+ cleaned_line = '\n' + cleaned_line.strip()
185
+
186
+ # Remove extra spaces
187
+ cleaned_line = re.sub(r'\s+', ' ', cleaned_line).strip()
188
+ cleaned_lines.append(cleaned_line)
189
+
190
+ # Join the lines within each paragraph
191
+ cleaned_paragraph = '\n'.join(cleaned_lines)
192
+ cleaned_paragraphs.append(cleaned_paragraph)
193
+
194
+ # Join the paragraphs back together
195
+ cleaned_text = '\n\n'.join(para for para in cleaned_paragraphs if para)
196
+
197
  return cleaned_text
198
  def side():
199
  with st.sidebar.form(key='feedback_form'):
 
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