Wajahat698 commited on
Commit
8fccf21
·
verified ·
1 Parent(s): ed77192

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -7
app.py CHANGED
@@ -157,7 +157,51 @@ def save_feedback_to_excel(name, email, feedback):
157
  st.error(f"Error saving feedback to Excel: {e}")
158
 
159
 
160
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
161
 
162
  def side():
163
  with st.sidebar.form(key='feedback_form'):
@@ -536,16 +580,15 @@ if prompt :
536
  })
537
  full_response = output["output"]
538
 
539
- text = re.sub(r'\s+', ' ', full_response) # Replace multiple spaces or newlines with a single space
540
- text = re.sub(r'([a-z])(\d)', r'\1 \2', full_response)
541
- text = re.sub(r'</span>', '', full_response) # Remove closing </span> tags
542
  # Display the response
543
- if "**Trust Tip**" not in full_response and "**Suggestion**" not in full_response:
544
  trust_tip = random.choice(trust_tips)
545
  suggestion = random.choice(suggestions)
546
- text += f"\n\n**Trust Tip**: {trust_tip} \n\n **Suggestion**: {suggestion}"
547
 
548
- st.markdown(text, unsafe_allow_html=True)
549
 
550
 
551
 
 
157
  st.error(f"Error saving feedback to Excel: {e}")
158
 
159
 
160
+ def clean_text(text):
161
+ # Replace escaped newlines with actual newlines
162
+ text = text.replace('\\n', '\n')
163
+
164
+ # Remove any span and italic tags
165
+ text = re.sub(r'<span[^>]*>', '', text)
166
+ text = re.sub(r'</span>', '', text)
167
+ text = re.sub(r'<i[^>]*>', '', text)
168
+ text = re.sub(r'</i>', '', text)
169
+
170
+ # Preserve and correctly format markdown links
171
+ text = re.sub(r'\[([^\]]+)\]\((https?://[^\)]+)\)', r'\1: \2', text)
172
+
173
+ # Split the text into paragraphs
174
+ paragraphs = text.split('\n\n')
175
+
176
+ cleaned_paragraphs = []
177
+ for paragraph in paragraphs:
178
+ lines = paragraph.split('\n')
179
+ cleaned_lines = []
180
+ for line in lines:
181
+ # Preserve bold formatting for headings
182
+ if line.strip().startswith('**') and line.strip().endswith('**'):
183
+ cleaned_line = line.strip()
184
+ else:
185
+ # Remove asterisks, special characters, and fix merged text
186
+ cleaned_line = re.sub(r'\*|\−|\∗', '', line)
187
+ cleaned_line = re.sub(r'([a-z])([A-Z])', r'\1 \2', cleaned_line)
188
+
189
+ # Handle bullet points
190
+ if cleaned_line.strip().startswith('-'):
191
+ cleaned_line = '\n' + cleaned_line.strip()
192
+
193
+ # Remove extra spaces
194
+ cleaned_line = re.sub(r'\s+', ' ', cleaned_line).strip()
195
+ cleaned_lines.append(cleaned_line)
196
+
197
+ # Join the lines within each paragraph
198
+ cleaned_paragraph = '\n'.join(cleaned_lines)
199
+ cleaned_paragraphs.append(cleaned_paragraph)
200
+
201
+ # Join the paragraphs back together
202
+ cleaned_text = '\n\n'.join(para for para in cleaned_paragraphs if para)
203
+
204
+ return cleaned_text
205
 
206
  def side():
207
  with st.sidebar.form(key='feedback_form'):
 
580
  })
581
  full_response = output["output"]
582
 
583
+ cleaned_text = clean_text(full_response)
584
+
 
585
  # Display the response
586
+ if "**Trust Tip**" not in full_response and "**Suggestion**" not in cleaned_text:
587
  trust_tip = random.choice(trust_tips)
588
  suggestion = random.choice(suggestions)
589
+ cleaned_text += f"\n\n**Trust Tip**: {trust_tip} \n\n **Suggestion**: {suggestion}"
590
 
591
+ st.write(cleaned_text, unsafe_allow_html=True)
592
 
593
 
594