shivanis14
commited on
Commit
•
e7a00b1
1
Parent(s):
59f7396
Update app.py
Browse files
app.py
CHANGED
@@ -514,7 +514,9 @@ The output must be in JSON format as follows:
|
|
514 |
# claims_not_found_in_doc.append(key)
|
515 |
# print(f"Claims not found in the doc are {','.join(claims_not_found_in_doc)}")
|
516 |
#claims_analysis = json.loads(message_content.value.replace("```", "").replace("json", "").replace("(NOT FOUND IN DOCUMENT) ", ""))
|
517 |
-
claims_analysis =
|
|
|
|
|
518 |
|
519 |
claims_analysis_str = ""
|
520 |
for key, value in claims_analysis.items():
|
@@ -604,7 +606,6 @@ def analyze_product(product_info_raw, system_prompt):
|
|
604 |
|
605 |
if nutritional_information:
|
606 |
product_type, calories, sugar, salt, serving_size = find_product_nutrients(product_info_from_db)
|
607 |
-
nutrient_analysis = analyze_nutrients(product_type, calories, sugar, salt, serving_size)
|
608 |
if product_type is not None and serving_size is not None:
|
609 |
nutrient_analysis = analyze_nutrients(product_type, calories, sugar, salt, serving_size)
|
610 |
else:
|
@@ -632,8 +633,8 @@ def analyze_product(product_info_raw, system_prompt):
|
|
632 |
final_analysis = generate_final_analysis(brand_name, product_name, nutritional_level, processing_level, harmful_ingredient_analysis, claims_analysis, system_prompt)
|
633 |
|
634 |
return final_analysis
|
635 |
-
else:
|
636 |
-
|
637 |
|
638 |
# Streamlit app
|
639 |
# Initialize session state
|
@@ -758,29 +759,37 @@ class ProductSelector:
|
|
758 |
|
759 |
# Confirm button
|
760 |
confirm_clicked = st.button("Confirm Selection")
|
761 |
-
|
762 |
# Only process the selection when confirm is clicked
|
763 |
if confirm_clicked:
|
|
|
764 |
if choice != "None of the above":
|
765 |
-
st.session_state.
|
766 |
-
st.session_state.
|
767 |
-
|
768 |
-
|
769 |
-
|
770 |
-
|
771 |
-
|
772 |
-
|
773 |
-
|
774 |
-
|
775 |
-
|
776 |
-
|
777 |
-
|
778 |
-
|
779 |
-
|
780 |
-
|
|
|
|
|
|
|
|
|
781 |
st.session_state.messages.append(
|
782 |
-
{"role": "assistant", "content": "Please provide the image URL of the product."}
|
783 |
)
|
|
|
|
|
|
|
784 |
|
785 |
st.rerun()
|
786 |
|
@@ -795,10 +804,11 @@ class ChatManager:
|
|
795 |
def process_response(user_input):
|
796 |
if not st.session_state.product_selected:
|
797 |
if "http:/" not in user_input and "https:/" not in user_input:
|
798 |
-
|
799 |
else:
|
800 |
-
|
801 |
-
|
|
|
802 |
|
803 |
@staticmethod
|
804 |
def _handle_product_name(user_input):
|
@@ -809,11 +819,12 @@ class ChatManager:
|
|
809 |
st.session_state.system_prompt, extract_info=False
|
810 |
)
|
811 |
|
812 |
-
if similar_products:
|
813 |
st.session_state.similar_products = similar_products
|
814 |
st.session_state.awaiting_selection = True
|
815 |
-
return "Here are some similar products from our database. Please select:"
|
816 |
-
|
|
|
817 |
|
818 |
@staticmethod
|
819 |
def _handle_product_url(user_input):
|
@@ -829,9 +840,19 @@ class ChatManager:
|
|
829 |
st.session_state.system_prompt, extract_info=True
|
830 |
)
|
831 |
st.session_state.product_selected = True
|
832 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
833 |
|
834 |
-
return "Please provide valid image URL of the product."
|
835 |
|
836 |
def main():
|
837 |
# Initialize session state
|
@@ -876,9 +897,13 @@ def main():
|
|
876 |
st.markdown(user_input)
|
877 |
|
878 |
# Process response
|
879 |
-
response = ChatManager.process_response(user_input)
|
|
|
|
|
|
|
|
|
880 |
|
881 |
-
if
|
882 |
SessionState.initialize() # Reset states for next product
|
883 |
#st.session_state.welcome_msg = "What is the next product you would like me to analyze today?"
|
884 |
keys_to_keep = ["system_prompt", "messages", "welcome_msg"]
|
@@ -887,15 +912,11 @@ def main():
|
|
887 |
for key in keys_to_delete:
|
888 |
del st.session_state[key]
|
889 |
st.session_state.welcome_msg = "What product would you like me to analyze next?"
|
890 |
-
st.rerun()
|
891 |
|
892 |
-
elif response: # Only add response if it's not None
|
893 |
-
|
894 |
-
|
895 |
-
|
896 |
-
print(f"DEBUG : st.session_state.awaiting_selection : {st.session_state.awaiting_selection}")
|
897 |
-
print(f"response : {response}")
|
898 |
-
st.rerun()
|
899 |
else:
|
900 |
# Disable chat input while selection is in progress
|
901 |
st.chat_input("Please confirm your selection above first...", disabled=True)
|
|
|
514 |
# claims_not_found_in_doc.append(key)
|
515 |
# print(f"Claims not found in the doc are {','.join(claims_not_found_in_doc)}")
|
516 |
#claims_analysis = json.loads(message_content.value.replace("```", "").replace("json", "").replace("(NOT FOUND IN DOCUMENT) ", ""))
|
517 |
+
claims_analysis = {}
|
518 |
+
if message_content.value != "":
|
519 |
+
claims_analysis = json.loads(message_content.value.replace("```", "").replace("json", ""))
|
520 |
|
521 |
claims_analysis_str = ""
|
522 |
for key, value in claims_analysis.items():
|
|
|
606 |
|
607 |
if nutritional_information:
|
608 |
product_type, calories, sugar, salt, serving_size = find_product_nutrients(product_info_from_db)
|
|
|
609 |
if product_type is not None and serving_size is not None:
|
610 |
nutrient_analysis = analyze_nutrients(product_type, calories, sugar, salt, serving_size)
|
611 |
else:
|
|
|
633 |
final_analysis = generate_final_analysis(brand_name, product_name, nutritional_level, processing_level, harmful_ingredient_analysis, claims_analysis, system_prompt)
|
634 |
|
635 |
return final_analysis
|
636 |
+
#else:
|
637 |
+
# return "I'm sorry, product information could not be extracted from the url."
|
638 |
|
639 |
# Streamlit app
|
640 |
# Initialize session state
|
|
|
759 |
|
760 |
# Confirm button
|
761 |
confirm_clicked = st.button("Confirm Selection")
|
762 |
+
msg = ""
|
763 |
# Only process the selection when confirm is clicked
|
764 |
if confirm_clicked:
|
765 |
+
st.session_state.awaiting_selection = False
|
766 |
if choice != "None of the above":
|
767 |
+
#st.session_state.selected_product = choice
|
768 |
+
st.session_state.messages.append({"role": "assistant", "content": f"You selected {choice}"})
|
769 |
+
_, msg = chatbot_response("", choice, data_extractor_url="", extract_info=True)
|
770 |
+
#Check if analysis couldn't be done because db had incomplete information
|
771 |
+
if msg != "product not found because product information in the db is corrupt":
|
772 |
+
#Only when msg is acceptable
|
773 |
+
st.session_state.messages.append({"role": "assistant", "content": msg})
|
774 |
+
with st.chat_message("assistant"):
|
775 |
+
st.markdown(msg)
|
776 |
+
|
777 |
+
st.session_state.product_selected = True
|
778 |
+
|
779 |
+
keys_to_keep = ["system_prompt", "messages", "welcome_msg"]
|
780 |
+
keys_to_delete = [key for key in st.session_state.keys() if key not in keys_to_keep]
|
781 |
+
|
782 |
+
for key in keys_to_delete:
|
783 |
+
del st.session_state[key]
|
784 |
+
st.session_state.welcome_msg = "What product would you like me to analyze next?"
|
785 |
+
|
786 |
+
if choice == "None of the above" or msg == "product not found because product information in the db is corrupt":
|
787 |
st.session_state.messages.append(
|
788 |
+
{"role": "assistant", "content": "Please provide the image URL of the product to analyze based on the latest information."}
|
789 |
)
|
790 |
+
with st.chat_message("assistant"):
|
791 |
+
st.markdown("Please provide the image URL of the product to analyze based on the latest information.")
|
792 |
+
#st.session_state.selected_product = None
|
793 |
|
794 |
st.rerun()
|
795 |
|
|
|
804 |
def process_response(user_input):
|
805 |
if not st.session_state.product_selected:
|
806 |
if "http:/" not in user_input and "https:/" not in user_input:
|
807 |
+
response, status = ChatManager._handle_product_name(user_input)
|
808 |
else:
|
809 |
+
response, status = ChatManager._handle_product_url(user_input)
|
810 |
+
|
811 |
+
return response, status
|
812 |
|
813 |
@staticmethod
|
814 |
def _handle_product_name(user_input):
|
|
|
819 |
st.session_state.system_prompt, extract_info=False
|
820 |
)
|
821 |
|
822 |
+
if len(similar_products) > 0:
|
823 |
st.session_state.similar_products = similar_products
|
824 |
st.session_state.awaiting_selection = True
|
825 |
+
return "Here are some similar products from our database. Please select:", "no success"
|
826 |
+
|
827 |
+
return "Product not found in our database. Please provide the image URL of the product.", "no success"
|
828 |
|
829 |
@staticmethod
|
830 |
def _handle_product_url(user_input):
|
|
|
840 |
st.session_state.system_prompt, extract_info=True
|
841 |
)
|
842 |
st.session_state.product_selected = True
|
843 |
+
if msg != "product not found because image is not clear" and "Product information could not be extracted from the image" not in msg:
|
844 |
+
response = msg
|
845 |
+
status = "success"
|
846 |
+
elif msg == "product not found because image is not clear":
|
847 |
+
response = msg + ". Please share clear image URLs!"
|
848 |
+
status = "no success"
|
849 |
+
else:
|
850 |
+
response = msg + ".Please re-try!!"
|
851 |
+
status = "no success"
|
852 |
+
|
853 |
+
return response, status
|
854 |
|
855 |
+
return "Please provide valid image URL of the product.", "no success"
|
856 |
|
857 |
def main():
|
858 |
# Initialize session state
|
|
|
897 |
st.markdown(user_input)
|
898 |
|
899 |
# Process response
|
900 |
+
response, status = ChatManager.process_response(user_input)
|
901 |
+
|
902 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
903 |
+
with st.chat_message("assistant"):
|
904 |
+
st.markdown(response)
|
905 |
|
906 |
+
if status == "success":
|
907 |
SessionState.initialize() # Reset states for next product
|
908 |
#st.session_state.welcome_msg = "What is the next product you would like me to analyze today?"
|
909 |
keys_to_keep = ["system_prompt", "messages", "welcome_msg"]
|
|
|
912 |
for key in keys_to_delete:
|
913 |
del st.session_state[key]
|
914 |
st.session_state.welcome_msg = "What product would you like me to analyze next?"
|
|
|
915 |
|
916 |
+
#elif response: # Only add response if it's not None
|
917 |
+
# print(f"DEBUG : st.session_state.awaiting_selection : {st.session_state.awaiting_selection}")
|
918 |
+
# print(f"response : {response}")
|
919 |
+
st.rerun()
|
|
|
|
|
|
|
920 |
else:
|
921 |
# Disable chat input while selection is in progress
|
922 |
st.chat_input("Please confirm your selection above first...", disabled=True)
|