Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1701,10 +1701,7 @@ initialize_user_session()
|
|
| 1701 |
retrieve_user_data(st.session_state["wix_user_id"]) # Fetch and display saved data for the user
|
| 1702 |
|
| 1703 |
user_name = extract_name(st.session_state["email"])
|
| 1704 |
-
|
| 1705 |
-
welcome_placeholder.info(f"**Hello User!**")
|
| 1706 |
-
time.sleep(3)
|
| 1707 |
-
welcome_placeholder.empty()
|
| 1708 |
|
| 1709 |
|
| 1710 |
|
|
@@ -1715,49 +1712,32 @@ global combined_text
|
|
| 1715 |
if st.session_state["used_messages"] < st.session_state["message_limit"]:
|
| 1716 |
if prompt:
|
| 1717 |
st.session_state.chat_started = True
|
| 1718 |
-
|
| 1719 |
# Prevent duplicate messages in chat history
|
| 1720 |
if not any(msg["content"] == prompt for msg in st.session_state["chat_history"]):
|
| 1721 |
st.session_state.chat_history.append({"role": "user", "content": prompt})
|
| 1722 |
|
| 1723 |
-
# Introduce a flag to
|
| 1724 |
st.session_state["handled"] = False
|
| 1725 |
|
| 1726 |
-
#
|
| 1727 |
if st.session_state.get("missing_trustbucket_content") and not st.session_state["handled"]:
|
| 1728 |
-
bucket
|
| 1729 |
-
|
| 1730 |
-
|
| 1731 |
-
if bucket in valid_buckets:
|
| 1732 |
-
content_to_save = st.session_state.pop("missing_trustbucket_content")
|
| 1733 |
-
handle_save_trustbuilder(content_to_save, bucket)
|
| 1734 |
-
with st.chat_message("assistant"):
|
| 1735 |
-
st.markdown(f"Got it! I've saved this TrustBuilder under '{bucket}' Trust. Let me know if there's anything else you'd like to do!")
|
| 1736 |
-
else:
|
| 1737 |
-
with st.chat_message("assistant"):
|
| 1738 |
-
st.markdown("That doesn't seem to be a valid Trust Bucket. Please choose from Stability, Development, Relationship, Benefit, Vision, or Competence.")
|
| 1739 |
st.session_state["handled"] = True
|
| 1740 |
|
| 1741 |
-
# Handle saved
|
| 1742 |
-
if "
|
| 1743 |
-
trustbuilders = st.session_state
|
| 1744 |
if trustbuilders:
|
| 1745 |
-
|
| 1746 |
-
saved_content = "\n".join([entry["message"] for entry in trustbuilders.values()])
|
| 1747 |
-
|
| 1748 |
-
# Append saved content to the chat history for contextual AI responses
|
| 1749 |
-
st.session_state.chat_history.append({"role": "assistant", "content": saved_content})
|
| 1750 |
-
|
| 1751 |
-
# Inform the user that saved content is being used
|
| 1752 |
with st.chat_message("assistant"):
|
| 1753 |
-
st.markdown("
|
| 1754 |
-
st.session_state["handled"] = True
|
| 1755 |
else:
|
| 1756 |
with st.chat_message("assistant"):
|
| 1757 |
-
st.markdown("You haven't saved any TrustBuilders yet.
|
| 1758 |
-
|
| 1759 |
|
| 1760 |
-
# Handle
|
| 1761 |
if not st.session_state["handled"]:
|
| 1762 |
memory_response = handle_memory_queries(prompt)
|
| 1763 |
if memory_response:
|
|
@@ -1765,46 +1745,44 @@ if st.session_state["used_messages"] < st.session_state["message_limit"]:
|
|
| 1765 |
st.markdown(memory_response)
|
| 1766 |
st.session_state["handled"] = True
|
| 1767 |
|
| 1768 |
-
#
|
| 1769 |
if not st.session_state["handled"]:
|
| 1770 |
-
|
| 1771 |
-
|
| 1772 |
-
|
| 1773 |
-
|
| 1774 |
-
with st.chat_message("
|
| 1775 |
-
|
| 1776 |
-
|
| 1777 |
-
|
| 1778 |
-
try:
|
| 1779 |
-
# Include saved TrustBuilders in the prompt context
|
| 1780 |
-
trustbuilders = st.session_state.get("TrustBuilder", {})
|
| 1781 |
-
saved_content = "\n".join([entry["message"] for entry in trustbuilders.values()]) if trustbuilders else ""
|
| 1782 |
-
|
| 1783 |
-
# Generate AI response with saved content as context
|
| 1784 |
-
ai_prompt = f"{saved_content}\n\nUser Input: {prompt}\nBe in natural tone, avoid flowery language or typical AI words. Use knowledge directly from our base; if unavailable, use Google with sources. Avoid AI jargon and provide concise, conversational headings for each section."
|
| 1785 |
-
|
| 1786 |
-
output = agent_executor.invoke({
|
| 1787 |
-
"input": ai_prompt,
|
| 1788 |
-
"chat_history": st.session_state.chat_history
|
| 1789 |
-
})
|
| 1790 |
-
full_response = output["output"]
|
| 1791 |
-
cleaned_text = clean_text(full_response)
|
| 1792 |
-
trust_tip, suggestion = get_trust_tip_and_suggestion()
|
| 1793 |
-
combined_text = f"{cleaned_text}\n\n---\n\n**Trust Tip**: {trust_tip}\n\n**Suggestion**: {suggestion}"
|
| 1794 |
-
|
| 1795 |
with response_placeholder:
|
| 1796 |
with st.chat_message("assistant"):
|
| 1797 |
-
|
| 1798 |
-
|
| 1799 |
-
|
| 1800 |
-
|
| 1801 |
-
|
| 1802 |
-
|
| 1803 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1804 |
|
| 1805 |
-
|
| 1806 |
-
st.session_state.chat_history.append({"role": "assistant", "content": cleaned_text})
|
| 1807 |
-
copy_to_clipboard(cleaned_text)
|
| 1808 |
|
| 1809 |
-
|
| 1810 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1701 |
retrieve_user_data(st.session_state["wix_user_id"]) # Fetch and display saved data for the user
|
| 1702 |
|
| 1703 |
user_name = extract_name(st.session_state["email"])
|
| 1704 |
+
|
|
|
|
|
|
|
|
|
|
| 1705 |
|
| 1706 |
|
| 1707 |
|
|
|
|
| 1712 |
if st.session_state["used_messages"] < st.session_state["message_limit"]:
|
| 1713 |
if prompt:
|
| 1714 |
st.session_state.chat_started = True
|
|
|
|
| 1715 |
# Prevent duplicate messages in chat history
|
| 1716 |
if not any(msg["content"] == prompt for msg in st.session_state["chat_history"]):
|
| 1717 |
st.session_state.chat_history.append({"role": "user", "content": prompt})
|
| 1718 |
|
| 1719 |
+
# Introduce a flag to track if a specific flow is handled
|
| 1720 |
st.session_state["handled"] = False
|
| 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"):
|
| 1734 |
+
st.markdown(f"Here are your saved TrustBuilders:\n{saved_content}")
|
|
|
|
| 1735 |
else:
|
| 1736 |
with st.chat_message("assistant"):
|
| 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:
|
|
|
|
| 1745 |
st.markdown(memory_response)
|
| 1746 |
st.session_state["handled"] = True
|
| 1747 |
|
| 1748 |
+
# If not handled yet, proceed to your existing AI response generation
|
| 1749 |
if not st.session_state["handled"]:
|
| 1750 |
+
# Your existing code block (kept unchanged)
|
| 1751 |
+
# -----------------------------------------------------------
|
| 1752 |
+
else:
|
| 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
|