Wajahat698 commited on
Commit
83bfe1d
·
verified ·
1 Parent(s): 49c5bbe

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -65
app.py CHANGED
@@ -1463,77 +1463,72 @@ def update_message_usage(wix_user_id):
1463
  st.error("Error updating message usage.")
1464
 
1465
  # Main interface
1466
- if st.session_state.get("wix_user_id") and st.session_state.get("email"):
1467
- initialize_user_session(st.session_state["wix_user_id"], st.session_state["email"])
1468
 
1469
- retrieve_user_data(st.session_state["wix_user_id"]) # Fetch and display saved data for the user
1470
- display_saved_trustbuilders()
1471
 
1472
- user_name = extract_name(st.session_state["email"])
1473
- welcome_placeholder = st.empty()
1474
- welcome_placeholder.info(f"**Hello, {user_name}!**")
1475
- time.sleep(3)
1476
- welcome_placeholder.empty()
1477
 
1478
 
1479
 
1480
- # Input field for chatbot interaction
1481
- prompt = st.chat_input("")
1482
- global combined_text
1483
- if st.session_state["used_messages"] < st.session_state["message_limit"]:
1484
- if prompt:
1485
- st.session_state.chat_started = True
1486
- st.session_state.chat_history.append({"role": "user", "content": prompt})
1487
 
1488
- # Handle save commands based on the user prompt
1489
- memory_response = handle_memory_queries(prompt)
1490
- if memory_response:
1491
- with st.chat_message("assistant"):
1492
- st.markdown(memory_response)
 
 
 
 
 
 
 
 
 
 
 
 
1493
  else:
1494
- save_as_trustbuilder = re.search(r"\b(save|add|store)\s*(this)?\s*(as)?\s*(trust\s*builder|trustbuilder)\b", prompt, re.IGNORECASE)
1495
- save_as_tonality = re.search(r"\b(save|add|store)\s*(this)?\s*(as)?\s*(brand\s*tonality|tonality)\b", prompt, re.IGNORECASE)
1496
-
1497
- if save_as_trustbuilder or save_as_tonality:
1498
- user_id = st.session_state["wix_user_id"]
1499
- if save_as_trustbuilder:
1500
- handle_save_trustbuilder_or_tonality(user_id, prompt, "trustbuilder")
1501
- elif save_as_tonality:
1502
- handle_save_trustbuilder_or_tonality(user_id, prompt, "brandtonality")
1503
-
1504
-
1505
- else:
1506
- # Generate a response with AI for general queries
1507
- with st.chat_message("user"):
1508
- st.markdown(prompt)
1509
- response_placeholder = st.empty()
 
 
1510
  with response_placeholder:
1511
  with st.chat_message("assistant"):
1512
- add_dot_typing_animation()
1513
- display_typing_indicator()
1514
- cleaned_text = ""
1515
- try:
1516
- output = agent_executor.invoke({
1517
- "input": f"{prompt} Be in natural tone,doesn’t use flowery language and typical ai words .Sources should be accurate search on google for that.Avoid AI jargons.Give headings with each paragraph in content of article,blogs,writeup and newsletter keeping mentioned format.",
1518
- "chat_history": st.session_state.chat_history
1519
- })
1520
- full_response = output["output"]
1521
- cleaned_text = clean_text(full_response)
1522
- trust_tip, suggestion = get_trust_tip_and_suggestion()
1523
- combined_text = f"{cleaned_text}\n\n---\n\n**Trust Tip**: {trust_tip}\n\n**Suggestion**: {suggestion}"
1524
- with response_placeholder:
1525
- with st.chat_message("assistant"):
1526
- st.markdown(combined_text, unsafe_allow_html=True)
1527
-
1528
- update_message_usage(st.session_state["wix_user_id"])
1529
-
1530
- except Exception as e:
1531
- logger.error(f"Error generating response: {e}")
1532
- st.error("An error occurred while generating the response. Please try again.")
1533
-
1534
- st.session_state.chat_history.append({"role": "assistant", "content": cleaned_text})
1535
- copy_to_clipboard(cleaned_text)
1536
- else:
1537
- st.warning("You have reached your message limit. Please upgrade your plan to continue.")
1538
- else:
1539
- st.warning("Please log in to access the chatbot features.")
 
1463
  st.error("Error updating message usage.")
1464
 
1465
  # Main interface
1466
+ initialize_user_session(st.session_state["wix_user_id"], st.session_state["email"])
 
1467
 
1468
+ retrieve_user_data(st.session_state["wix_user_id"]) # Fetch and display saved data for the user
1469
+ display_saved_trustbuilders()
1470
 
1471
+ user_name = extract_name(st.session_state["email"])
1472
+ welcome_placeholder = st.empty()
1473
+ welcome_placeholder.info(f"**Hello, {user_name}!**")
1474
+ time.sleep(3)
1475
+ welcome_placeholder.empty()
1476
 
1477
 
1478
 
1479
+ # Input field for chatbot interaction
1480
+ prompt = st.chat_input("")
1481
+ global combined_text
1482
+ if st.session_state["used_messages"] < st.session_state["message_limit"]:
1483
+ if prompt:
1484
+ st.session_state.chat_started = True
1485
+ st.session_state.chat_history.append({"role": "user", "content": prompt})
1486
 
1487
+ # Handle save commands based on the user prompt
1488
+ memory_response = handle_memory_queries(prompt)
1489
+ if memory_response:
1490
+ with st.chat_message("assistant"):
1491
+ st.markdown(memory_response)
1492
+ else:
1493
+ save_as_trustbuilder = re.search(r"\b(save|add|store)\s*(this)?\s*(as)?\s*(trust\s*builder|trustbuilder)\b", prompt, re.IGNORECASE)
1494
+ save_as_tonality = re.search(r"\b(save|add|store)\s*(this)?\s*(as)?\s*(brand\s*tonality|tonality)\b", prompt, re.IGNORECASE)
1495
+
1496
+ if save_as_trustbuilder or save_as_tonality:
1497
+ user_id = st.session_state["wix_user_id"]
1498
+ if save_as_trustbuilder:
1499
+ handle_save_trustbuilder_or_tonality(user_id, prompt, "trustbuilder")
1500
+ elif save_as_tonality:
1501
+ handle_save_trustbuilder_or_tonality(user_id, prompt, "brandtonality")
1502
+
1503
+
1504
  else:
1505
+ # Generate a response with AI for general queries
1506
+ with st.chat_message("user"):
1507
+ st.markdown(prompt)
1508
+ response_placeholder = st.empty()
1509
+ with response_placeholder:
1510
+ with st.chat_message("assistant"):
1511
+ add_dot_typing_animation()
1512
+ display_typing_indicator()
1513
+ cleaned_text = ""
1514
+ try:
1515
+ output = agent_executor.invoke({
1516
+ "input": f"{prompt} Be in natural tone,doesn’t use flowery language and typical ai words .Sources should be accurate search on google for that.Avoid AI jargons.Give headings with each paragraph in content of article,blogs,writeup and newsletter keeping mentioned format.",
1517
+ "chat_history": st.session_state.chat_history
1518
+ })
1519
+ full_response = output["output"]
1520
+ cleaned_text = clean_text(full_response)
1521
+ trust_tip, suggestion = get_trust_tip_and_suggestion()
1522
+ combined_text = f"{cleaned_text}\n\n---\n\n**Trust Tip**: {trust_tip}\n\n**Suggestion**: {suggestion}"
1523
  with response_placeholder:
1524
  with st.chat_message("assistant"):
1525
+ st.markdown(combined_text, unsafe_allow_html=True)
1526
+
1527
+ update_message_usage(st.session_state["wix_user_id"])
1528
+
1529
+ except Exception as e:
1530
+ logger.error(f"Error generating response: {e}")
1531
+ st.error("An error occurred while generating the response. Please try again.")
1532
+
1533
+ st.session_state.chat_history.append({"role": "assistant", "content": cleaned_text})
1534
+ copy_to_clipboard(cleaned_text)