Wajahat698 commited on
Commit
eecfe41
·
verified ·
1 Parent(s): 1bc8f52

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -14
app.py CHANGED
@@ -2078,31 +2078,37 @@ def clean_and_format_markdown(raw_text):
2078
  return formatted_text
2079
 
2080
 
2081
- def deep_clean_text(text):
2082
  # Normalize Unicode
2083
  text = unicodedata.normalize('NFKC', text)
2084
- # Remove zero-width and invisible chars
 
2085
  text = re.sub(r'[\u200B\uFEFF\u200C\u200D]', '', text)
2086
- # Remove control chars
 
2087
  text = re.sub(r'[\x00-\x1F\x7F-\x9F]', '', text)
2088
- # Replace newlines
 
2089
  text = text.replace('\\n', ' ').replace('\n', ' ')
2090
- # Remove HTML tags
 
2091
  text = re.sub(r'<[^>]*>', '', text)
2092
- # Space after punctuation
 
2093
  text = re.sub(r'([.,!?])(\S)', r'\1 \2', text)
2094
- # Space between numbers and letters
 
2095
  text = re.sub(r'(\d)([A-Za-z])', r'\1 \2', text)
2096
  text = re.sub(r'([A-Za-z])(\d)', r'\1 \2', text)
2097
- # Normalize spaces
 
2098
  text = re.sub(r'\s+', ' ', text).strip()
2099
- # Normalize dashes
2100
- text = text.replace('−', '-')
2101
 
2102
  return text
2103
 
2104
 
2105
 
 
2106
  prompt = st.chat_input("")
2107
  global combined_text
2108
  def handle_prompt(prompt):
@@ -2254,7 +2260,7 @@ def handle_prompt(prompt):
2254
  })
2255
  full_response = output["output"]
2256
 
2257
- cleaned_text = deep_clean_text(full_response)
2258
 
2259
  # Use Markdown with a styled div to ensure proper word wrapping
2260
 
@@ -2265,14 +2271,14 @@ def handle_prompt(prompt):
2265
  trust_tip, suggestion = get_trust_tip_and_suggestion()
2266
 
2267
  # Combine the response text with Trust Tip and Suggestion
2268
- combined_text = f"{full_response}\n\n---\n\n**Trust Tip**: {trust_tip}\n\n**Suggestion**: {suggestion}"
2269
 
2270
  else:
2271
  # If already present, just use the full response
2272
- combined_text = full_response
2273
  with response_placeholder:
2274
  with st.chat_message("assistant"):
2275
- st.markdown(f"<pre style='white-space:pre-wrap;word-wrap:break-word;'>{combined_text}</pre>", unsafe_allow_html=True)
2276
 
2277
  #st.write(combined_text,unsafe_allow_html=True)
2278
  st.session_state.chat_history.append({"role": "assistant", "content": combined_text})
 
2078
  return formatted_text
2079
 
2080
 
2081
+ def clean_and_format_markdown(text: str) -> str:
2082
  # Normalize Unicode
2083
  text = unicodedata.normalize('NFKC', text)
2084
+
2085
+ # Remove zero-width and other invisible chars
2086
  text = re.sub(r'[\u200B\uFEFF\u200C\u200D]', '', text)
2087
+
2088
+ # Remove control characters
2089
  text = re.sub(r'[\x00-\x1F\x7F-\x9F]', '', text)
2090
+
2091
+ # Replace escaped and actual newlines with a single space
2092
  text = text.replace('\\n', ' ').replace('\n', ' ')
2093
+
2094
+ # Remove HTML tags if any
2095
  text = re.sub(r'<[^>]*>', '', text)
2096
+
2097
+ # Ensure space after punctuation if missing
2098
  text = re.sub(r'([.,!?])(\S)', r'\1 \2', text)
2099
+
2100
+ # Ensure spacing between numbers and letters
2101
  text = re.sub(r'(\d)([A-Za-z])', r'\1 \2', text)
2102
  text = re.sub(r'([A-Za-z])(\d)', r'\1 \2', text)
2103
+
2104
+ # Normalize multiple spaces
2105
  text = re.sub(r'\s+', ' ', text).strip()
 
 
2106
 
2107
  return text
2108
 
2109
 
2110
 
2111
+
2112
  prompt = st.chat_input("")
2113
  global combined_text
2114
  def handle_prompt(prompt):
 
2260
  })
2261
  full_response = output["output"]
2262
 
2263
+ cleaned_text = clean_and_format_markdown(full_response)
2264
 
2265
  # Use Markdown with a styled div to ensure proper word wrapping
2266
 
 
2271
  trust_tip, suggestion = get_trust_tip_and_suggestion()
2272
 
2273
  # Combine the response text with Trust Tip and Suggestion
2274
+ combined_text = f"{cleaned_text}\n\n---\n\n**Trust Tip**: {trust_tip}\n\n**Suggestion**: {suggestion}"
2275
 
2276
  else:
2277
  # If already present, just use the full response
2278
+ combined_text = cleaned_text
2279
  with response_placeholder:
2280
  with st.chat_message("assistant"):
2281
+ st.markdown(combined_text, unsafe_allow_html=True)
2282
 
2283
  #st.write(combined_text,unsafe_allow_html=True)
2284
  st.session_state.chat_history.append({"role": "assistant", "content": combined_text})