Wajahat698 commited on
Commit
1492f8f
·
verified ·
1 Parent(s): da341ac

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -39
app.py CHANGED
@@ -1720,8 +1720,11 @@ if st.session_state["used_messages"] < st.session_state["message_limit"]:
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
- # Handle missing Trust Bucket
1724
- if st.session_state.get("missing_trustbucket_content"):
 
 
 
1725
  bucket = prompt.strip().capitalize()
1726
  valid_buckets = ["Stability", "Development", "Relationship", "Benefit", "Vision", "Competence"]
1727
 
@@ -1733,47 +1736,75 @@ if st.session_state["used_messages"] < st.session_state["message_limit"]:
1733
  else:
1734
  with st.chat_message("assistant"):
1735
  st.markdown("That doesn't seem to be a valid Trust Bucket. Please choose from Stability, Development, Relationship, Benefit, Vision, or Competence.")
1736
- # Exit the flow here to prevent triggering additional AI responses
1737
-
1738
- # Handle TrustBuilder or Brand Tonality save commands
1739
- memory_response = handle_memory_queries(prompt)
1740
- if memory_response:
1741
- with st.chat_message("assistant"):
1742
- st.markdown(memory_response)
1743
- # Exit after handling the specific save command
1744
-
 
 
1745
 
1746
- # General AI query handling
1747
- with st.chat_message("user"):
1748
- st.markdown(prompt)
1749
- response_placeholder = st.empty()
1750
- with response_placeholder:
1751
- with st.chat_message("assistant"):
1752
- add_dot_typing_animation()
1753
- display_typing_indicator()
1754
- cleaned_text = ""
1755
-
1756
- try:
1757
- # Generate AI response for general queries
1758
- output = agent_executor.invoke({
1759
- "input": f"{prompt} Be 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.",
1760
- "chat_history": st.session_state.chat_history
1761
- })
1762
- full_response = output["output"]
1763
- cleaned_text = clean_text(full_response)
1764
- trust_tip, suggestion = get_trust_tip_and_suggestion()
1765
- combined_text = f"{cleaned_text}\n\n---\n\n**Trust Tip**: {trust_tip}\n\n**Suggestion**: {suggestion}"
1766
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1767
  with response_placeholder:
1768
  with st.chat_message("assistant"):
1769
- st.markdown(combined_text, unsafe_allow_html=True)
 
 
 
 
 
 
 
 
 
1770
 
1771
- update_message_usage(st.session_state["wix_user_id"])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1772
 
1773
- except Exception as e:
1774
- logger.error(f"Error generating response: {e}")
1775
- st.error("An error occurred while generating the response. Please try again.")
1776
 
1777
- # Update chat history with AI response
1778
- st.session_state.chat_history.append({"role": "assistant", "content": cleaned_text})
1779
- copy_to_clipboard(cleaned_text)
 
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 prevent further processing if a specific flow is handled
1724
+ st.session_state["handled"] = False
1725
+
1726
+ # Check if a missing Trust Bucket needs to be handled
1727
+ if st.session_state.get("missing_trustbucket_content") and not st.session_state["handled"]:
1728
  bucket = prompt.strip().capitalize()
1729
  valid_buckets = ["Stability", "Development", "Relationship", "Benefit", "Vision", "Competence"]
1730
 
 
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 data usage
1742
+ if "use my saved" in prompt.lower() and not st.session_state["handled"]:
1743
+ trustbuilders = st.session_state.get("TrustBuilder", {})
1744
+ if trustbuilders:
1745
+ # Retrieve all saved TrustBuilders
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("Using your saved TrustBuilders to generate a response...")
1754
+ st.session_state["handled"] = True
1755
+ else:
1756
+ with st.chat_message("assistant"):
1757
+ st.markdown("You haven't saved any TrustBuilders yet. Please save some to use them in responses.")
1758
+ st.session_state["handled"] = True
 
 
 
 
 
 
 
 
 
 
 
 
1759
 
1760
+ # Handle save commands or memory queries
1761
+ if not st.session_state["handled"]:
1762
+ memory_response = handle_memory_queries(prompt)
1763
+ if memory_response:
1764
+ with st.chat_message("assistant"):
1765
+ st.markdown(memory_response)
1766
+ st.session_state["handled"] = True
1767
+
1768
+ # Handle general AI queries
1769
+ if not st.session_state["handled"]:
1770
+ with st.chat_message("user"):
1771
+ st.markdown(prompt)
1772
+ response_placeholder = st.empty()
1773
  with response_placeholder:
1774
  with st.chat_message("assistant"):
1775
+ add_dot_typing_animation()
1776
+ display_typing_indicator()
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
+ st.markdown(combined_text, unsafe_allow_html=True)
1798
+
1799
+ update_message_usage(st.session_state["wix_user_id"])
1800
+
1801
+ except Exception as e:
1802
+ logger.error(f"Error generating response: {e}")
1803
+ st.error("An error occurred while generating the response. Please try again.")
1804
 
1805
+ # Update chat history with AI response
1806
+ st.session_state.chat_history.append({"role": "assistant", "content": cleaned_text})
1807
+ copy_to_clipboard(cleaned_text)
1808
 
1809
+ # Reset the handled flag for the next prompt
1810
+ st.session_state["handled"] = False