Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2077,6 +2077,35 @@ def clean_and_format_markdown(raw_text):
|
|
| 2077 |
return formatted_text
|
| 2078 |
|
| 2079 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2080 |
|
| 2081 |
prompt = st.chat_input("")
|
| 2082 |
global combined_text
|
|
@@ -2229,7 +2258,10 @@ def handle_prompt(prompt):
|
|
| 2229 |
})
|
| 2230 |
full_response = output["output"]
|
| 2231 |
|
| 2232 |
-
|
|
|
|
|
|
|
|
|
|
| 2233 |
#formatted_text = clean_and_format_markdown(cleaned_text)
|
| 2234 |
|
| 2235 |
if "Trust Tip" not in full_response and "Suggestion" not in full_response:
|
|
@@ -2238,12 +2270,18 @@ def handle_prompt(prompt):
|
|
| 2238 |
|
| 2239 |
# Combine the response text with Trust Tip and Suggestion
|
| 2240 |
combined_text = f"{full_response}\n\n---\n\n**Trust Tip**: {trust_tip}\n\n**Suggestion**: {suggestion}"
|
|
|
|
| 2241 |
else:
|
| 2242 |
# If already present, just use the full response
|
| 2243 |
combined_text = full_response
|
| 2244 |
with response_placeholder:
|
| 2245 |
with st.chat_message("assistant"):
|
| 2246 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2247 |
st.session_state.chat_history.append({"role": "assistant", "content": combined_text})
|
| 2248 |
copy_to_clipboard(combined_text)
|
| 2249 |
|
|
|
|
| 2077 |
return formatted_text
|
| 2078 |
|
| 2079 |
|
| 2080 |
+
def deep_clean_text(text):
|
| 2081 |
+
# Normalize Unicode to a standard form
|
| 2082 |
+
text = unicodedata.normalize('NFKC', text)
|
| 2083 |
+
|
| 2084 |
+
# Remove zero-width characters and other invisible Unicode chars (ZWSP: \u200B, ZWNBSP: \uFEFF, etc.)
|
| 2085 |
+
text = re.sub(r'[\u200B\uFEFF\u200C\u200D]', '', text)
|
| 2086 |
+
|
| 2087 |
+
# Replace escaped and actual newlines with a space
|
| 2088 |
+
text = text.replace('\\n', ' ').replace('\n', ' ')
|
| 2089 |
+
|
| 2090 |
+
# Remove HTML tags if any
|
| 2091 |
+
text = re.sub(r'<[^>]*>', '', text)
|
| 2092 |
+
|
| 2093 |
+
# Insert space after punctuation if missing
|
| 2094 |
+
text = re.sub(r'([.,!?])(\S)', r'\1 \2', text)
|
| 2095 |
+
|
| 2096 |
+
# Fix spacing between numbers and letters
|
| 2097 |
+
text = re.sub(r'(\d)([A-Za-z])', r'\1 \2', text)
|
| 2098 |
+
text = re.sub(r'([A-Za-z])(\d)', r'\1 \2', text)
|
| 2099 |
+
|
| 2100 |
+
# Replace multiple spaces with a single space
|
| 2101 |
+
text = re.sub(r'\s+', ' ', text).strip()
|
| 2102 |
+
|
| 2103 |
+
# You can also replace unusual hyphens or dashes with a standard dash
|
| 2104 |
+
text = text.replace('−', '-')
|
| 2105 |
+
|
| 2106 |
+
return text
|
| 2107 |
+
|
| 2108 |
+
|
| 2109 |
|
| 2110 |
prompt = st.chat_input("")
|
| 2111 |
global combined_text
|
|
|
|
| 2258 |
})
|
| 2259 |
full_response = output["output"]
|
| 2260 |
|
| 2261 |
+
cleaned_text = deep_clean_text(full_response)
|
| 2262 |
+
|
| 2263 |
+
# Use Markdown with a styled div to ensure proper word wrapping
|
| 2264 |
+
|
| 2265 |
#formatted_text = clean_and_format_markdown(cleaned_text)
|
| 2266 |
|
| 2267 |
if "Trust Tip" not in full_response and "Suggestion" not in full_response:
|
|
|
|
| 2270 |
|
| 2271 |
# Combine the response text with Trust Tip and Suggestion
|
| 2272 |
combined_text = f"{full_response}\n\n---\n\n**Trust Tip**: {trust_tip}\n\n**Suggestion**: {suggestion}"
|
| 2273 |
+
|
| 2274 |
else:
|
| 2275 |
# If already present, just use the full response
|
| 2276 |
combined_text = full_response
|
| 2277 |
with response_placeholder:
|
| 2278 |
with st.chat_message("assistant"):
|
| 2279 |
+
html_content = f"""
|
| 2280 |
+
<div style="white-space: pre-wrap; word-wrap: break-word;">
|
| 2281 |
+
{combined_text}
|
| 2282 |
+
</div>
|
| 2283 |
+
"""
|
| 2284 |
+
st.write(html_content,unsafe_allow_html=True)
|
| 2285 |
st.session_state.chat_history.append({"role": "assistant", "content": combined_text})
|
| 2286 |
copy_to_clipboard(combined_text)
|
| 2287 |
|