Wajahat698 commited on
Commit
da48e2a
·
verified ·
1 Parent(s): c0af953

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -26
app.py CHANGED
@@ -924,10 +924,12 @@ def side():
924
  # Display the first matching trustbuilder
925
  if matching_trustbuilders:
926
  st.write("### Result:")
927
- st.write(f"- {matching_trustbuilders}")
 
928
  else:
929
  st.write("No TrustBuilders found matching the criteria.")
930
 
 
931
 
932
  # UI for saving TrustBuilders
933
  st.subheader("Save TrustBuilders®")
@@ -1168,13 +1170,14 @@ def google_search_tool(query: str):
1168
  return google_search(query)
1169
 
1170
  tavily_tool = TavilySearchResults(
1171
- max_results=7,
1172
  search_depth="advanced",
 
 
1173
  include_answer=True,
1174
- include_raw_content=False,
1175
- include_images=False,
1176
  # include_domains=[...],
1177
- # exclude_domains=[...],
1178
  # name="...", # overwrite default tool name
1179
  # description="...", # overwrite default tool description
1180
  # args_schema=..., # overwrite default args_schema: BaseModel
@@ -1983,26 +1986,34 @@ user_name = extract_name(st.session_state["email"])
1983
 
1984
 
1985
 
1986
- def clean_and_format_markdown(raw_text):
1987
- """
1988
- Dynamically cleans and formats Markdown text to ensure URLs are properly encoded
1989
- and handles issues with line breaks or improperly formatted Markdown.
1990
- """
1991
- # Regular expression to find Markdown links [text](url)
1992
- pattern = r'\[([^\]]+)\]\(([^)]+)\)'
1993
 
1994
- def encode_url(match):
1995
- text = match.group(1)
1996
- url = match.group(2).strip() # Remove leading/trailing spaces
1997
- encoded_url = quote(url, safe=':/') # Encode the URL while keeping : and /
1998
- return f"[{text}]({encoded_url})"
 
 
 
 
 
 
 
 
 
 
 
 
 
1999
 
2000
- # Fix Markdown links dynamically
2001
- formatted_text = re.sub(pattern, encode_url, raw_text)
 
2002
 
2003
- # Replace single newlines with spaces to avoid breaking Markdown rendering
 
2004
 
2005
- return formatted_text
2006
 
2007
 
2008
  prompt = st.chat_input("")
@@ -2101,6 +2112,8 @@ def handle_prompt(prompt):
2101
  "Strictly Avoid flowery language, typical AI phrases and jargons."
2102
  "When asked for top trustdrivers,buckets,builders,statements search in knowledgebase when asked for prospects or customers give exact result with percentage. Search google Give correct sources links always.For sales conversation donot give source in conversation give in list of trustbuilders . *Be specific with $ amounts awards, people and programs*."
2103
  "Strictly dont use trustbucket names in copy headings and content avoid it"
 
 
2104
  )
2105
 
2106
  # Check if user request includes blog, article, or newsletter
@@ -2136,22 +2149,25 @@ def handle_prompt(prompt):
2136
  "chat_history": st.session_state.chat_history
2137
  })
2138
  full_response = output["output"]
 
 
2139
 
2140
- cleaned_text = clean_text(full_response)
 
2141
  trust_tip, suggestion = get_trust_tip_and_suggestion()
2142
- combined_text = f"{cleaned_text}\n\n---\n\n**Trust Tip**: {trust_tip}\n\n**Suggestion**: {suggestion}"
2143
- formatted_text = clean_and_format_markdown(combined_text)
2144
 
2145
  with response_placeholder:
2146
  with st.chat_message("assistant"):
2147
- st.markdown(formatted_text, unsafe_allow_html=False)
2148
 
2149
 
2150
  except Exception as e:
2151
  logging.error(f"Error generating response: {e}")
2152
  st.error("An error occurred while generating the response. Please try again.")
2153
 
2154
- st.session_state.chat_history.append({"role": "assistant", "content": formatted_text})
2155
  copy_to_clipboard(formatted_text)
2156
  st.session_state["handled"] = True # Mark as handled
2157
 
 
924
  # Display the first matching trustbuilder
925
  if matching_trustbuilders:
926
  st.write("### Result:")
927
+ # Join the matching trustbuilders into a bullet list
928
+ st.markdown("\n".join([f"- {tb}" for tb in matching_trustbuilders]))
929
  else:
930
  st.write("No TrustBuilders found matching the criteria.")
931
 
932
+
933
 
934
  # UI for saving TrustBuilders
935
  st.subheader("Save TrustBuilders®")
 
1170
  return google_search(query)
1171
 
1172
  tavily_tool = TavilySearchResults(
1173
+ max_results=10,
1174
  search_depth="advanced",
1175
+ topic="news",
1176
+ days=1,
1177
  include_answer=True,
1178
+ include_raw_content=True,
 
1179
  # include_domains=[...],
1180
+ exclude_domains=['example.com'],
1181
  # name="...", # overwrite default tool name
1182
  # description="...", # overwrite default tool description
1183
  # args_schema=..., # overwrite default args_schema: BaseModel
 
1986
 
1987
 
1988
 
 
 
 
 
 
 
 
1989
 
1990
+ def clean_and_format_markdown(text: str) -> str:
1991
+ # Normalize Unicode
1992
+ text = unicodedata.normalize('NFKC', text)
1993
+
1994
+ # Remove zero-width and other invisible chars
1995
+ text = re.sub(r'[\u200B\uFEFF\u200C\u200D]', '', text)
1996
+
1997
+ # Remove control characters
1998
+ text = re.sub(r'[\x00-\x1F\x7F-\x9F]', '', text)
1999
+
2000
+ # Replace escaped and actual newlines with a single space
2001
+ text = text.replace('\\n', ' ').replace('\n', ' ')
2002
+
2003
+ # Remove HTML tags if any
2004
+ text = re.sub(r'<[^>]*>', '', text)
2005
+
2006
+ # Ensure space after punctuation if missing
2007
+ text = re.sub(r'([.,!?])(\S)', r'\1 \2', text)
2008
 
2009
+ # Ensure spacing between numbers and letters
2010
+ text = re.sub(r'(\d)([A-Za-z])', r'\1 \2', text)
2011
+ text = re.sub(r'([A-Za-z])(\d)', r'\1 \2', text)
2012
 
2013
+ # Normalize multiple spaces
2014
+ text = re.sub(r'\s+', ' ', text).strip()
2015
 
2016
+ return text
2017
 
2018
 
2019
  prompt = st.chat_input("")
 
2112
  "Strictly Avoid flowery language, typical AI phrases and jargons."
2113
  "When asked for top trustdrivers,buckets,builders,statements search in knowledgebase when asked for prospects or customers give exact result with percentage. Search google Give correct sources links always.For sales conversation donot give source in conversation give in list of trustbuilders . *Be specific with $ amounts awards, people and programs*."
2114
  "Strictly dont use trustbucket names in copy headings and content avoid it"
2115
+ "Response in same language in which asked."
2116
+ "Use google to provide correct sources links containing the trustbuilder text information."
2117
  )
2118
 
2119
  # Check if user request includes blog, article, or newsletter
 
2149
  "chat_history": st.session_state.chat_history
2150
  })
2151
  full_response = output["output"]
2152
+ import html
2153
+ escaped_text = full_response.replace("$", "\$")
2154
 
2155
+ #cleaned_text = clean_text(full_response)
2156
+
2157
  trust_tip, suggestion = get_trust_tip_and_suggestion()
2158
+ combined_text = f"{escaped_text}\n\n---\n\n**Trust Tip**: {trust_tip}\n\n**Suggestion**: {suggestion}"
2159
+ #formatted_text = clean_and_format_markdown(combined_text)
2160
 
2161
  with response_placeholder:
2162
  with st.chat_message("assistant"):
2163
+ st.markdown(combined_text)
2164
 
2165
 
2166
  except Exception as e:
2167
  logging.error(f"Error generating response: {e}")
2168
  st.error("An error occurred while generating the response. Please try again.")
2169
 
2170
+ st.session_state.chat_history.append({"role": "assistant", "content": escaped_text})
2171
  copy_to_clipboard(formatted_text)
2172
  st.session_state["handled"] = True # Mark as handled
2173