Wajahat698 commited on
Commit
ec7d7e5
·
verified ·
1 Parent(s): a4c8ac3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +4 -47
app.py CHANGED
@@ -26,6 +26,7 @@ from bs4 import BeautifulSoup
26
  import smtplib
27
  from email.mime.multipart import MIMEMultipart
28
  from email.mime.text import MIMEText
 
29
 
30
  # Initialize logging and load environment variables
31
  logging.basicConfig(level=logging.INFO)
@@ -146,52 +147,8 @@ def send_feedback_via_email(name, email, feedback):
146
 
147
 
148
  def clean_text(text):
149
- # Use BeautifulSoup to parse and clean HTML
150
- soup = BeautifulSoup(text, 'html.parser')
151
-
152
- # Convert <a> tags to Markdown links
153
- for a in soup.find_all('a'):
154
- a.replace_with(f"[{a.get_text()}]({a['href']})")
155
-
156
- # Convert <b> and <strong> tags to Markdown bold
157
- for tag in ['b', 'strong']:
158
- for element in soup.find_all(tag):
159
- element.insert_before('**')
160
- element.insert_after('**')
161
- element.unwrap()
162
-
163
- # Convert <i> and <em> tags to Markdown italic
164
- for tag in ['i', 'em']:
165
- for element in soup.find_all(tag):
166
- element.insert_before('*')
167
- element.insert_after('*')
168
- element.unwrap()
169
-
170
- # Preserve <h1> to <h6> tags as Markdown headings
171
- for level in range(1, 7):
172
- for heading in soup.find_all(f'h{level}'):
173
- heading.insert_before('#' * level + ' ')
174
- heading.insert_after('\n')
175
- heading.unwrap()
176
-
177
- # Convert <u> tags to Markdown underline (if needed)
178
- # Markdown does not support underline, so we'll remove the tags
179
- for tag in ['u']:
180
- for element in soup.find_all(tag):
181
- element.unwrap()
182
-
183
- # Get the cleaned text
184
- cleaned_text = soup.get_text()
185
-
186
- # Handle extra whitespace and preserve spacing
187
- # Maintain paragraph breaks
188
- cleaned_text = re.sub(r'\n\s*\n', '\n\n', cleaned_text)
189
- # Replace multiple spaces with a single space
190
- cleaned_text = re.sub(r'\s+', ' ', cleaned_text)
191
- # Strip leading/trailing spaces
192
- cleaned_text = cleaned_text.strip()
193
-
194
- return cleaned_text
195
 
196
  def side():
197
  with st.sidebar.form(key='feedback_form'):
@@ -616,7 +573,7 @@ if prompt :
616
  suggestion = random.choice(suggestions)
617
  cleaned_text += f"\n\n**Trust Tip**: {trust_tip} \n\n **Suggestion**: {suggestion}"
618
 
619
- st.write(cleaned_text)
620
 
621
 
622
 
 
26
  import smtplib
27
  from email.mime.multipart import MIMEMultipart
28
  from email.mime.text import MIMEText
29
+ from markdownify import markdownify
30
 
31
  # Initialize logging and load environment variables
32
  logging.basicConfig(level=logging.INFO)
 
147
 
148
 
149
  def clean_text(text):
150
+ """Converts HTML content to Markdown format using markdownify."""
151
+ return markdownify(html_content, strip=['img', 'video'])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
152
 
153
  def side():
154
  with st.sidebar.form(key='feedback_form'):
 
573
  suggestion = random.choice(suggestions)
574
  cleaned_text += f"\n\n**Trust Tip**: {trust_tip} \n\n **Suggestion**: {suggestion}"
575
 
576
+ st.markdown(cleaned_text)
577
 
578
 
579