Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1722,115 +1722,117 @@ user_name = extract_name(st.session_state["email"])
|
|
| 1722 |
|
| 1723 |
prompt = st.chat_input("")
|
| 1724 |
global combined_text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1725 |
|
| 1726 |
-
|
| 1727 |
-
|
| 1728 |
-
|
| 1729 |
-
|
| 1730 |
-
|
| 1731 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1732 |
|
| 1733 |
-
|
| 1734 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1735 |
|
| 1736 |
-
|
| 1737 |
-
|
| 1738 |
-
|
| 1739 |
-
|
| 1740 |
|
| 1741 |
-
if bucket in valid_buckets:
|
| 1742 |
-
content_to_save = st.session_state.pop("missing_trustbucket_content")
|
| 1743 |
-
handle_save_trustbuilder(content_to_save, bucket)
|
| 1744 |
-
else:
|
| 1745 |
-
with st.chat_message("assistant"):
|
| 1746 |
-
st.markdown("Invalid Trust Bucket. Please choose from Stability, Development, Relationship, Benefit, Vision, or Competence.")
|
| 1747 |
-
st.session_state["handled"] = True
|
| 1748 |
-
|
| 1749 |
-
# Handle fetching saved TrustBuilders when user asks
|
| 1750 |
-
if ("find my saved trustbuilders" in prompt.lower() or "show my saved trustbuilders" in prompt.lower()) and not st.session_state["handled"]:
|
| 1751 |
-
trustbuilders = fetch_trustbuilders(st.session_state.get("wix_user_id", "default_user"))
|
| 1752 |
-
if trustbuilders:
|
| 1753 |
-
saved_content = "\n".join([f"- {entry}" for entry in trustbuilders])
|
| 1754 |
-
with st.chat_message("assistant"):
|
| 1755 |
-
st.markdown(f"Here are your saved TrustBuilders:\n{saved_content}")
|
| 1756 |
-
else:
|
| 1757 |
-
with st.chat_message("assistant"):
|
| 1758 |
-
st.markdown("You haven't saved any TrustBuilders yet.")
|
| 1759 |
-
st.session_state["handled"] = True
|
| 1760 |
-
|
| 1761 |
-
# Handle save TrustBuilder command
|
| 1762 |
-
if st.session_state["handled"]:
|
| 1763 |
-
save_match = re.search(r"\b(save|add|keep|store)\s+(this)?\s*(as)?\s*(\w+\s*trustbuilder|trustbuilder)\s*:?(.+)?", prompt, re.IGNORECASE)
|
| 1764 |
-
if save_match:
|
| 1765 |
-
content_to_save = save_match.group(5).strip() if save_match.group(5) else None
|
| 1766 |
-
specified_bucket = None
|
| 1767 |
-
|
| 1768 |
-
# Check for explicit bucket mention in the same prompt
|
| 1769 |
-
bucket_match = re.search(r"\b(stability|development|relationship|benefit|vision|competence)\b", prompt, re.IGNORECASE)
|
| 1770 |
-
if bucket_match:
|
| 1771 |
-
specified_bucket = bucket_match.group(1).capitalize()
|
| 1772 |
-
|
| 1773 |
-
if content_to_save:
|
| 1774 |
-
handle_save_trustbuilder(content_to_save, specified_bucket)
|
| 1775 |
-
else:
|
| 1776 |
-
# If content is not provided after the command, extract from prompt
|
| 1777 |
-
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()
|
| 1778 |
if content_to_save:
|
| 1779 |
handle_save_trustbuilder(content_to_save, specified_bucket)
|
| 1780 |
else:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1781 |
with st.chat_message("assistant"):
|
| 1782 |
-
st.markdown(
|
| 1783 |
-
st.session_state["handled"] = True
|
| 1784 |
-
return
|
| 1785 |
|
| 1786 |
-
|
| 1787 |
-
# Handle other memory queries if any
|
| 1788 |
-
if not st.session_state["handled"]:
|
| 1789 |
-
memory_response = handle_memory_queries(prompt)
|
| 1790 |
-
if memory_response == "find_my_saved_trustbuilders":
|
| 1791 |
-
# This case is already handled above, so we can set handled to True
|
| 1792 |
-
st.session_state["handled"] = True
|
| 1793 |
-
elif memory_response:
|
| 1794 |
-
with st.chat_message("assistant"):
|
| 1795 |
-
st.markdown(memory_response)
|
| 1796 |
-
st.session_state["handled"] = True
|
| 1797 |
|
| 1798 |
-
|
| 1799 |
-
|
| 1800 |
-
|
| 1801 |
-
# -----------------------------------------------------------
|
| 1802 |
-
|
| 1803 |
-
# Generate a response with AI for other types of queries
|
| 1804 |
-
with st.chat_message("user"):
|
| 1805 |
-
st.markdown(prompt)
|
| 1806 |
-
response_placeholder = st.empty()
|
| 1807 |
-
with response_placeholder:
|
| 1808 |
-
with st.chat_message("assistant"):
|
| 1809 |
-
add_dot_typing_animation()
|
| 1810 |
-
display_typing_indicator()
|
| 1811 |
-
cleaned_text = ""
|
| 1812 |
-
# Specialized responses if keywords detected
|
| 1813 |
-
try:
|
| 1814 |
-
output = agent_executor.invoke({
|
| 1815 |
-
"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.",
|
| 1816 |
-
"chat_history": st.session_state.chat_history
|
| 1817 |
-
})
|
| 1818 |
-
full_response = output["output"]
|
| 1819 |
-
# full_response = replace_terms(full_response)
|
| 1820 |
-
|
| 1821 |
-
cleaned_text = clean_text(full_response)
|
| 1822 |
-
trust_tip, suggestion = get_trust_tip_and_suggestion()
|
| 1823 |
-
combined_text = f"{cleaned_text}\n\n---\n\n**Trust Tip**: {trust_tip}\n\n**Suggestion**: {suggestion}"
|
| 1824 |
-
with response_placeholder:
|
| 1825 |
-
with st.chat_message("assistant"):
|
| 1826 |
-
st.markdown(combined_text, unsafe_allow_html=True)
|
| 1827 |
|
| 1828 |
-
|
|
|
|
|
|
|
| 1829 |
|
| 1830 |
-
|
| 1831 |
-
|
| 1832 |
-
st.error("An error occurred while generating the response. Please try again.")
|
| 1833 |
-
|
| 1834 |
-
st.session_state.chat_history.append({"role": "assistant", "content": cleaned_text})
|
| 1835 |
-
copy_to_clipboard(cleaned_text)
|
| 1836 |
-
st.session_state["handled"] = True # Mark as handled
|
|
|
|
| 1722 |
|
| 1723 |
prompt = st.chat_input("")
|
| 1724 |
global combined_text
|
| 1725 |
+
def handle_prompt(prompt):
|
| 1726 |
+
if st.session_state["used_messages"] < st.session_state["message_limit"]:
|
| 1727 |
+
if prompt:
|
| 1728 |
+
st.session_state.chat_started = True
|
| 1729 |
+
|
| 1730 |
+
# Prevent duplicate messages in chat history
|
| 1731 |
+
if not any(msg["content"] == prompt for msg in st.session_state["chat_history"]):
|
| 1732 |
+
st.session_state.chat_history.append({"role": "user", "content": prompt})
|
| 1733 |
+
|
| 1734 |
+
# Introduce a flag to track if a specific flow is handled
|
| 1735 |
+
st.session_state["handled"] = False
|
| 1736 |
+
|
| 1737 |
+
# Handle missing Trust Bucket if needed
|
| 1738 |
+
if st.session_state.get("missing_trustbucket_content") and not st.session_state["handled"]:
|
| 1739 |
+
bucket = prompt.strip().capitalize()
|
| 1740 |
+
valid_buckets = ["Stability", "Development", "Relationship", "Benefit", "Vision", "Competence"]
|
| 1741 |
+
|
| 1742 |
+
if bucket in valid_buckets:
|
| 1743 |
+
content_to_save = st.session_state.pop("missing_trustbucket_content")
|
| 1744 |
+
handle_save_trustbuilder(content_to_save, bucket)
|
| 1745 |
+
else:
|
| 1746 |
+
with st.chat_message("assistant"):
|
| 1747 |
+
st.markdown("Invalid Trust Bucket. Please choose from Stability, Development, Relationship, Benefit, Vision, or Competence.")
|
| 1748 |
+
st.session_state["handled"] = True
|
| 1749 |
|
| 1750 |
+
# Handle fetching saved TrustBuilders when user asks
|
| 1751 |
+
if ("find my saved trustbuilders" in prompt.lower() or "show my saved trustbuilders" in prompt.lower()) and not st.session_state["handled"]:
|
| 1752 |
+
trustbuilders = fetch_trustbuilders(st.session_state.get("wix_user_id", "default_user"))
|
| 1753 |
+
if trustbuilders:
|
| 1754 |
+
saved_content = "\n".join([f"- {entry}" for entry in trustbuilders])
|
| 1755 |
+
with st.chat_message("assistant"):
|
| 1756 |
+
st.markdown(f"Here are your saved TrustBuilders:\n{saved_content}")
|
| 1757 |
+
else:
|
| 1758 |
+
with st.chat_message("assistant"):
|
| 1759 |
+
st.markdown("You haven't saved any TrustBuilders yet.")
|
| 1760 |
+
st.session_state["handled"] = True
|
| 1761 |
|
| 1762 |
+
# Handle save TrustBuilder command
|
| 1763 |
+
if not st.session_state["handled"]:
|
| 1764 |
+
save_match = re.search(r"\b(save|add|keep|store)\s+(this)?\s*(as)?\s*(\w+\s*trustbuilder|trustbuilder)\s*:?(.+)?", prompt, re.IGNORECASE)
|
| 1765 |
+
if save_match:
|
| 1766 |
+
content_to_save = save_match.group(5).strip() if save_match.group(5) else None
|
| 1767 |
+
specified_bucket = None
|
| 1768 |
|
| 1769 |
+
# Check for explicit bucket mention in the same prompt
|
| 1770 |
+
bucket_match = re.search(r"\b(stability|development|relationship|benefit|vision|competence)\b", prompt, re.IGNORECASE)
|
| 1771 |
+
if bucket_match:
|
| 1772 |
+
specified_bucket = bucket_match.group(1).capitalize()
|
| 1773 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1774 |
if content_to_save:
|
| 1775 |
handle_save_trustbuilder(content_to_save, specified_bucket)
|
| 1776 |
else:
|
| 1777 |
+
# If content is not provided after the command, extract from prompt
|
| 1778 |
+
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()
|
| 1779 |
+
if content_to_save:
|
| 1780 |
+
handle_save_trustbuilder(content_to_save, specified_bucket)
|
| 1781 |
+
else:
|
| 1782 |
+
with st.chat_message("assistant"):
|
| 1783 |
+
st.markdown("Please provide the content to save as a TrustBuilder.")
|
| 1784 |
+
|
| 1785 |
+
# Mark as handled and exit to prevent further processing
|
| 1786 |
+
st.session_state["handled"] = True
|
| 1787 |
+
return # Exit here to avoid triggering normal AI response
|
| 1788 |
+
|
| 1789 |
+
# Handle other memory queries if any
|
| 1790 |
+
if not st.session_state["handled"]:
|
| 1791 |
+
memory_response = handle_memory_queries(prompt)
|
| 1792 |
+
if memory_response == "find_my_saved_trustbuilders":
|
| 1793 |
+
# This case is already handled above, so we can set handled to True
|
| 1794 |
+
st.session_state["handled"] = True
|
| 1795 |
+
elif memory_response:
|
| 1796 |
+
with st.chat_message("assistant"):
|
| 1797 |
+
st.markdown(memory_response)
|
| 1798 |
+
st.session_state["handled"] = True
|
| 1799 |
+
|
| 1800 |
+
# If not handled yet, proceed to the existing AI response generation
|
| 1801 |
+
if not st.session_state["handled"]:
|
| 1802 |
+
# Generate a response with AI for other types of queries
|
| 1803 |
+
with st.chat_message("user"):
|
| 1804 |
+
st.markdown(prompt)
|
| 1805 |
+
response_placeholder = st.empty()
|
| 1806 |
+
with response_placeholder:
|
| 1807 |
+
with st.chat_message("assistant"):
|
| 1808 |
+
add_dot_typing_animation()
|
| 1809 |
+
display_typing_indicator()
|
| 1810 |
+
|
| 1811 |
+
cleaned_text = ""
|
| 1812 |
+
# Specialized responses if keywords detected
|
| 1813 |
+
try:
|
| 1814 |
+
output = agent_executor.invoke({
|
| 1815 |
+
"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.",
|
| 1816 |
+
"chat_history": st.session_state.chat_history
|
| 1817 |
+
})
|
| 1818 |
+
full_response = output["output"]
|
| 1819 |
+
|
| 1820 |
+
cleaned_text = clean_text(full_response)
|
| 1821 |
+
trust_tip, suggestion = get_trust_tip_and_suggestion()
|
| 1822 |
+
combined_text = f"{cleaned_text}\n\n---\n\n**Trust Tip**: {trust_tip}\n\n**Suggestion**: {suggestion}"
|
| 1823 |
+
with response_placeholder:
|
| 1824 |
with st.chat_message("assistant"):
|
| 1825 |
+
st.markdown(combined_text, unsafe_allow_html=True)
|
|
|
|
|
|
|
| 1826 |
|
| 1827 |
+
update_message_usage(st.session_state.get("wix_user_id", "default_user"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1828 |
|
| 1829 |
+
except Exception as e:
|
| 1830 |
+
logging.error(f"Error generating response: {e}")
|
| 1831 |
+
st.error("An error occurred while generating the response. Please try again.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1832 |
|
| 1833 |
+
st.session_state.chat_history.append({"role": "assistant", "content": cleaned_text})
|
| 1834 |
+
copy_to_clipboard(cleaned_text)
|
| 1835 |
+
st.session_state["handled"] = True # Mark as handled
|
| 1836 |
|
| 1837 |
+
# Call the function to handle the prompt
|
| 1838 |
+
handle_prompt(prompt)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|