Wajahat698 commited on
Commit
d93473d
·
verified ·
1 Parent(s): 12ec6ea

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -35
app.py CHANGED
@@ -1721,13 +1721,20 @@ if st.session_state["used_messages"] < st.session_state["message_limit"]:
1721
 
1722
  # Handle missing Trust Bucket if needed
1723
  if st.session_state.get("missing_trustbucket_content") and not st.session_state["handled"]:
1724
- # [Your existing missing bucket handling code]
1725
- # ...
 
 
 
 
 
 
 
1726
  st.session_state["handled"] = True
1727
 
1728
  # Handle fetching saved TrustBuilders when user asks
1729
  if ("find my saved trustbuilders" in prompt.lower() or "show my saved trustbuilders" in prompt.lower()) and not st.session_state["handled"]:
1730
- trustbuilders = fetch_trustbuilders(st.session_state["wix_user_id"])
1731
  if trustbuilders:
1732
  saved_content = "\n".join([f"- {entry}" for entry in trustbuilders])
1733
  with st.chat_message("assistant"):
@@ -1737,10 +1744,37 @@ if st.session_state["used_messages"] < st.session_state["message_limit"]:
1737
  st.markdown("You haven't saved any TrustBuilders yet.")
1738
  st.session_state["handled"] = True
1739
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1740
  # Handle other memory queries if any
1741
  if not st.session_state["handled"]:
1742
  memory_response = handle_memory_queries(prompt)
1743
- if memory_response:
 
 
 
1744
  with st.chat_message("assistant"):
1745
  st.markdown(memory_response)
1746
  st.session_state["handled"] = True
@@ -1750,39 +1784,38 @@ if st.session_state["used_messages"] < st.session_state["message_limit"]:
1750
  # Your existing code block (kept unchanged)
1751
  # -----------------------------------------------------------
1752
 
1753
- # Generate a response with AI for other types of queries
1754
- with st.chat_message("user"):
1755
- st.markdown(prompt)
1756
- response_placeholder = st.empty()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1757
  with response_placeholder:
1758
  with st.chat_message("assistant"):
1759
- add_dot_typing_animation()
1760
- display_typing_indicator()
1761
- cleaned_text = ""
1762
- # Specialized responses if keywords detected
1763
- try:
1764
- #if "trustbuilder" in prompt.lower() or "trust" in prompt.lower():
1765
- output = agent_executor.invoke({
1766
- "input": f"{prompt} Be in natural tone,doesn’t use flowery language and typical ai words. Sources should be accurate latest and relevant.Avoid AI jargons.Give short headings in conversational language with each paragraph in content of article,blogs,writeup and newsletter keeping mentioned format only when asked .",
1767
- "chat_history": st.session_state.chat_history
1768
- })
1769
- full_response = output["output"]
1770
- #full_response= replace_terms(full_response)
1771
-
1772
- cleaned_text = clean_text(full_response)
1773
- trust_tip, suggestion = get_trust_tip_and_suggestion()
1774
- combined_text = f"{cleaned_text}\n\n---\n\n**Trust Tip**: {trust_tip}\n\n**Suggestion**: {suggestion}"
1775
- with response_placeholder:
1776
- with st.chat_message("assistant"):
1777
- st.markdown(combined_text, unsafe_allow_html=True)
1778
 
1779
- update_message_usage(st.session_state["wix_user_id"])
1780
 
1781
- except Exception as e:
1782
- logger.error(f"Error generating response: {e}")
1783
- st.error("An error occurred while generating the response. Please try again.")
1784
-
1785
- st.session_state.chat_history.append({"role": "assistant", "content": cleaned_text})
1786
- copy_to_clipboard(cleaned_text)
1787
  # -----------------------------------------------------------
1788
- st.session_state["handled"] = True # Mark as handled
 
1721
 
1722
  # Handle missing Trust Bucket if needed
1723
  if st.session_state.get("missing_trustbucket_content") and not st.session_state["handled"]:
1724
+ bucket = prompt.strip().capitalize()
1725
+ valid_buckets = ["Stability", "Development", "Relationship", "Benefit", "Vision", "Competence"]
1726
+
1727
+ if bucket in valid_buckets:
1728
+ content_to_save = st.session_state.pop("missing_trustbucket_content")
1729
+ handle_save_trustbuilder(content_to_save, bucket)
1730
+ else:
1731
+ with st.chat_message("assistant"):
1732
+ st.markdown("Invalid Trust Bucket. Please choose from Stability, Development, Relationship, Benefit, Vision, or Competence.")
1733
  st.session_state["handled"] = True
1734
 
1735
  # Handle fetching saved TrustBuilders when user asks
1736
  if ("find my saved trustbuilders" in prompt.lower() or "show my saved trustbuilders" in prompt.lower()) and not st.session_state["handled"]:
1737
+ trustbuilders = fetch_trustbuilders(st.session_state.get("wix_user_id", "default_user"))
1738
  if trustbuilders:
1739
  saved_content = "\n".join([f"- {entry}" for entry in trustbuilders])
1740
  with st.chat_message("assistant"):
 
1744
  st.markdown("You haven't saved any TrustBuilders yet.")
1745
  st.session_state["handled"] = True
1746
 
1747
+ # Handle save TrustBuilder command
1748
+ if not st.session_state["handled"]:
1749
+ save_match = re.search(r"\b(save|add|keep|store)\s+(this)?\s*(as)?\s*(\w+\s*trustbuilder|trustbuilder)\s*:?(.+)?", prompt, re.IGNORECASE)
1750
+ if save_match:
1751
+ content_to_save = save_match.group(5).strip() if save_match.group(5) else None
1752
+ specified_bucket = None
1753
+
1754
+ # Check for explicit bucket mention in the same prompt
1755
+ bucket_match = re.search(r"\b(stability|development|relationship|benefit|vision|competence)\b", prompt, re.IGNORECASE)
1756
+ if bucket_match:
1757
+ specified_bucket = bucket_match.group(1).capitalize()
1758
+
1759
+ if content_to_save:
1760
+ handle_save_trustbuilder(content_to_save, specified_bucket)
1761
+ else:
1762
+ # If content is not provided after the command, extract from prompt
1763
+ content_to_save = re.sub(r"\b(save|add|keep|store)\s+(this)?\s*(as)?\s*(\w+\s*trustbuilder|trustbuilder)\b", "", prompt, flags=re.IGNORECASE).strip()
1764
+ if content_to_save:
1765
+ handle_save_trustbuilder(content_to_save, specified_bucket)
1766
+ else:
1767
+ with st.chat_message("assistant"):
1768
+ st.markdown("Please provide the content to save as a TrustBuilder.")
1769
+ st.session_state["handled"] = True
1770
+
1771
  # Handle other memory queries if any
1772
  if not st.session_state["handled"]:
1773
  memory_response = handle_memory_queries(prompt)
1774
+ if memory_response == "find_my_saved_trustbuilders":
1775
+ # This case is already handled above, so we can set handled to True
1776
+ st.session_state["handled"] = True
1777
+ elif memory_response:
1778
  with st.chat_message("assistant"):
1779
  st.markdown(memory_response)
1780
  st.session_state["handled"] = True
 
1784
  # Your existing code block (kept unchanged)
1785
  # -----------------------------------------------------------
1786
 
1787
+ # Generate a response with AI for other types of queries
1788
+ with st.chat_message("user"):
1789
+ st.markdown(prompt)
1790
+ response_placeholder = st.empty()
1791
+ with response_placeholder:
1792
+ with st.chat_message("assistant"):
1793
+ add_dot_typing_animation()
1794
+ display_typing_indicator()
1795
+ cleaned_text = ""
1796
+ # Specialized responses if keywords detected
1797
+ try:
1798
+ output = agent_executor.invoke({
1799
+ "input": f"{prompt} Be in natural tone, doesn’t use flowery language and typical AI words. Sources should be accurate, latest, and relevant. Avoid AI jargon. Give short headings in conversational language with each paragraph in content of articles, blogs, write-ups, and newsletters, keeping the mentioned format only when asked.",
1800
+ "chat_history": st.session_state.chat_history
1801
+ })
1802
+ full_response = output["output"]
1803
+ # full_response = replace_terms(full_response)
1804
+
1805
+ cleaned_text = clean_text(full_response)
1806
+ trust_tip, suggestion = get_trust_tip_and_suggestion()
1807
+ combined_text = f"{cleaned_text}\n\n---\n\n**Trust Tip**: {trust_tip}\n\n**Suggestion**: {suggestion}"
1808
  with response_placeholder:
1809
  with st.chat_message("assistant"):
1810
+ st.markdown(combined_text, unsafe_allow_html=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1811
 
1812
+ update_message_usage(st.session_state.get("wix_user_id", "default_user"))
1813
 
1814
+ except Exception as e:
1815
+ logging.error(f"Error generating response: {e}")
1816
+ st.error("An error occurred while generating the response. Please try again.")
1817
+
1818
+ st.session_state.chat_history.append({"role": "assistant", "content": cleaned_text})
1819
+ copy_to_clipboard(cleaned_text)
1820
  # -----------------------------------------------------------
1821
+ st.session_state["handled"] = True # Mark as handled