Wajahat698 commited on
Commit
e277fb3
·
verified ·
1 Parent(s): cafe269

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -49
app.py CHANGED
@@ -145,55 +145,23 @@ def send_feedback_via_email(name, email, feedback):
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'):
@@ -631,7 +599,7 @@ if prompt :
631
  st.write(full_response)
632
 
633
  # Add AI response to chat history
634
- st.session_state.chat_history.append({"role": "assistant", "content": full_response})
635
  copy_to_clipboard(full_response)
636
 
637
 
 
145
  st.error(f"Error sending email: {e}")
146
 
147
  def clean_text(text):
148
+ soup = BeautifulSoup(text, 'html.parser')
149
+
 
150
  # Convert <a> tags to Markdown links
151
+ for a in soup.find_all('a'):
152
+ a.replace_with(f"[{a.get_text()}]({a['href']})")
153
+
154
+ # Remove unwanted tags but preserve text
155
+ for tag in ['span', 'i', 'b']:
156
+ for element in soup.find_all(tag):
157
+ element.unwrap() # Remove the tag but keep the content
158
+
159
+ # Get the cleaned text
160
+ cleaned_text = soup.get_text()
161
+
162
+ # Remove excessive whitespace
163
+ cleaned_text = re.sub(r'\s+', ' ', cleaned_text).strip()
164
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
165
  return cleaned_text
166
  def side():
167
  with st.sidebar.form(key='feedback_form'):
 
599
  st.write(full_response)
600
 
601
  # Add AI response to chat history
602
+ st.session_state.chat_history.append({"role": "assistant", "content": cleaned_text})
603
  copy_to_clipboard(full_response)
604
 
605