Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2054,27 +2054,29 @@ user_name = extract_name(st.session_state["email"])
|
|
| 2054 |
|
| 2055 |
|
| 2056 |
|
| 2057 |
-
|
| 2058 |
-
|
| 2059 |
-
|
| 2060 |
-
|
| 2061 |
-
|
| 2062 |
-
#
|
| 2063 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2064 |
|
| 2065 |
-
|
| 2066 |
-
# text = match.group(1)
|
| 2067 |
-
# url = match.group(2).strip() # Remove leading/trailing spaces
|
| 2068 |
-
# encoded_url = quote(url, safe=':/') # Encode the URL while keeping : and /
|
| 2069 |
-
# return f"[{text}]({encoded_url})"
|
| 2070 |
|
| 2071 |
-
#
|
| 2072 |
-
|
| 2073 |
|
| 2074 |
-
#
|
| 2075 |
-
|
| 2076 |
|
| 2077 |
-
|
| 2078 |
|
| 2079 |
prompt = st.chat_input("")
|
| 2080 |
global combined_text
|
|
@@ -2222,8 +2224,8 @@ def handle_prompt(prompt):
|
|
| 2222 |
})
|
| 2223 |
full_response = output["output"]
|
| 2224 |
|
| 2225 |
-
|
| 2226 |
-
|
| 2227 |
|
| 2228 |
trust_tip, suggestion = get_trust_tip_and_suggestion()
|
| 2229 |
combined_text = f"{formatted_text}\n\n---\n\n**Trust Tip**: {trust_tip}\n\n**Suggestion**: {suggestion}"
|
|
|
|
| 2054 |
|
| 2055 |
|
| 2056 |
|
| 2057 |
+
def clean_and_format_markdown(raw_text):
|
| 2058 |
+
"""
|
| 2059 |
+
Cleans and formats Markdown text to ensure URLs are properly encoded
|
| 2060 |
+
and handles issues with line breaks or improperly formatted Markdown.
|
| 2061 |
+
"""
|
| 2062 |
+
# Regular expression to find Markdown links [text](url)
|
| 2063 |
+
pattern = r'\[([^\]]+)\]\(([^)]+)\)'
|
| 2064 |
+
|
| 2065 |
+
def encode_url(match):
|
| 2066 |
+
text = match.group(1)
|
| 2067 |
+
url = match.group(2).strip() # Remove leading/trailing spaces
|
| 2068 |
+
encoded_url = quote(url, safe=':/') # Encode the URL while keeping : and /
|
| 2069 |
+
corrected_url = encoded_url.replace("+", "%20")
|
| 2070 |
|
| 2071 |
+
return f"[{text}]({corrected_url})"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2072 |
|
| 2073 |
+
# Fix Markdown links dynamically
|
| 2074 |
+
formatted_text = re.sub(pattern, corrected_url, raw_text)
|
| 2075 |
|
| 2076 |
+
# Replace single newlines with spaces to avoid breaking Markdown rendering
|
| 2077 |
+
formatted_text = re.sub(r'(?<!\n)\n(?!\n)', ' ', formatted_text)
|
| 2078 |
|
| 2079 |
+
return formatted_text
|
| 2080 |
|
| 2081 |
prompt = st.chat_input("")
|
| 2082 |
global combined_text
|
|
|
|
| 2224 |
})
|
| 2225 |
full_response = output["output"]
|
| 2226 |
|
| 2227 |
+
cleaned_text = clean_text(full_response)
|
| 2228 |
+
formatted_text = clean_and_format_markdown(cleaned_text)
|
| 2229 |
|
| 2230 |
trust_tip, suggestion = get_trust_tip_and_suggestion()
|
| 2231 |
combined_text = f"{formatted_text}\n\n---\n\n**Trust Tip**: {trust_tip}\n\n**Suggestion**: {suggestion}"
|