shivanis14
commited on
Commit
•
346df21
1
Parent(s):
bef1a9e
Update app.py
Browse files
app.py
CHANGED
@@ -406,6 +406,7 @@ def chatbot_response(image_urls_str, product_name_by_user, data_extractor_url, s
|
|
406 |
image_urls = []
|
407 |
if product_name_by_user != "":
|
408 |
similar_product_list_json = get_product_list(product_name_by_user, data_extractor_url)
|
|
|
409 |
if similar_product_list_json and extract_info == False:
|
410 |
with st.spinner("Fetching product information from our database... This may take a moment."):
|
411 |
print(f"similar_product_list_json : {similar_product_list_json}")
|
@@ -460,6 +461,12 @@ if "welcome_msg" not in st.session_state:
|
|
460 |
st.session_state.welcome_msg = "Welcome to ConsumeWise! What product would you like me to analyze today?"
|
461 |
if "system_prompt" not in st.session_state:
|
462 |
st.session_state.system_prompt = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
463 |
|
464 |
# Sidebar input for system prompt with an 'Apply' button
|
465 |
st.sidebar.header("System Prompt")
|
@@ -474,7 +481,6 @@ system_prompt = st.sidebar.text_area(
|
|
474 |
if st.sidebar.button("Submit Prompt"):
|
475 |
if system_prompt.strip():
|
476 |
st.session_state.system_prompt = system_prompt
|
477 |
-
|
478 |
# Reset session state when system prompt is changed
|
479 |
st.session_state.product_selected = False
|
480 |
st.session_state.analyze_more = True
|
@@ -482,8 +488,11 @@ if st.sidebar.button("Submit Prompt"):
|
|
482 |
st.session_state.yes_no_choice = None
|
483 |
st.session_state.product_shared = False
|
484 |
st.session_state.welcome_msg = "Welcome to ConsumeWise! What product would you like me to analyze today?"
|
485 |
-
st.session_state.messages = []
|
486 |
-
st.
|
|
|
|
|
|
|
487 |
else:
|
488 |
st.sidebar.error("Please enter a valid system prompt.")
|
489 |
|
@@ -492,7 +501,7 @@ st.title("ConsumeWise - Your Food Product Analysis Assistant")
|
|
492 |
|
493 |
# Show warning if system prompt is empty and disable chat input
|
494 |
if not st.session_state.system_prompt.strip():
|
495 |
-
st.sidebar.error("Please enter a system prompt before starting the conversation.")
|
496 |
st.warning("⚠️ Please enter a system prompt in the sidebar before proceeding.")
|
497 |
st.chat_input("Enter your message:", disabled=True)
|
498 |
else:
|
@@ -504,81 +513,77 @@ else:
|
|
504 |
st.markdown(message["content"])
|
505 |
st.markdown("</div>", unsafe_allow_html=True)
|
506 |
|
507 |
-
# Function to display the welcome message
|
508 |
def show_welcome_message():
|
509 |
-
"""Display the welcome message at the start of the conversation."""
|
510 |
if not st.session_state.welcome_shown:
|
511 |
st.session_state.messages.append({"role": "assistant", "content": st.session_state.welcome_msg})
|
512 |
with st.chat_message("assistant"):
|
513 |
st.markdown(st.session_state.welcome_msg)
|
514 |
st.session_state.welcome_shown = True
|
515 |
|
516 |
-
|
517 |
-
|
518 |
-
|
519 |
-
|
520 |
-
|
521 |
-
|
522 |
-
|
523 |
-
|
524 |
-
|
525 |
-
|
526 |
-
|
527 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
528 |
def chat_response(user_input, system_prompt):
|
529 |
-
"""Simulate product selection and analysis logic."""
|
530 |
if not st.session_state.product_selected:
|
531 |
-
# Fetch similar products
|
532 |
if "http:/" not in user_input and "https:/" not in user_input:
|
533 |
st.session_state.product_shared = True
|
|
|
534 |
similar_products, msg = chatbot_response("", user_input, data_extractor_url, system_prompt, extract_info=False)
|
|
|
535 |
if len(similar_products) > 0:
|
536 |
-
st.session_state.
|
537 |
-
|
538 |
-
|
539 |
-
with st.chat_message("assistant"):
|
540 |
-
st.markdown("Here are some similar products. Please select one or enter 'None':")
|
541 |
-
|
542 |
-
# Show product selection radio only once
|
543 |
-
choice = st.radio("Select a product:", similar_products + ["None of the above"], key="product_choice")
|
544 |
-
if choice and choice != "None of the above":
|
545 |
-
st.session_state.product_selected = True
|
546 |
-
_, msg = chatbot_response("", user_input, data_extractor_url, system_prompt, extract_info=True)
|
547 |
-
return msg
|
548 |
-
|
549 |
-
elif choice == "None of the above":
|
550 |
-
return "Please provide the image URL of the product."
|
551 |
else:
|
552 |
-
return "Product not found in our database. Please provide the image URL of the product
|
553 |
|
554 |
else:
|
555 |
if ("http:/" in user_input or "https:/" in user_input) and (".jpeg" in user_input or ".jpg" in user_input) and st.session_state.product_shared:
|
556 |
_, msg = chatbot_response(user_input, "", data_extractor_url, system_prompt, extract_info=True)
|
557 |
-
|
558 |
st.session_state.product_selected = True
|
559 |
-
|
560 |
-
st.session_state.messages.append({"role": "assistant", "content": msg})
|
561 |
-
with st.chat_message("assistant"):
|
562 |
-
st.markdown(msg)
|
563 |
-
|
564 |
-
return "Next Product"
|
565 |
-
|
566 |
else:
|
567 |
if not st.session_state.product_shared:
|
568 |
-
return
|
569 |
else:
|
570 |
-
return "Please provide valid image URL of the product
|
571 |
else:
|
572 |
return "Next Product"
|
573 |
|
574 |
# Show the welcome message at the start
|
575 |
show_welcome_message()
|
576 |
|
577 |
-
#
|
|
|
|
|
|
|
578 |
user_input = st.chat_input("Enter your message:", key="user_input")
|
579 |
|
580 |
if user_input:
|
581 |
-
|
|
|
|
|
|
|
|
|
582 |
|
583 |
if response != "Next Product":
|
584 |
st.session_state.messages.append({"role": "assistant", "content": response})
|
@@ -591,6 +596,9 @@ else:
|
|
591 |
st.session_state.yes_no_choice = None
|
592 |
st.session_state.product_shared = False
|
593 |
st.session_state.welcome_msg = "What is the next product you would like me to analyze today?"
|
|
|
|
|
|
|
594 |
st.rerun()
|
595 |
|
596 |
# Option to Clear Chat History
|
|
|
406 |
image_urls = []
|
407 |
if product_name_by_user != "":
|
408 |
similar_product_list_json = get_product_list(product_name_by_user, data_extractor_url)
|
409 |
+
|
410 |
if similar_product_list_json and extract_info == False:
|
411 |
with st.spinner("Fetching product information from our database... This may take a moment."):
|
412 |
print(f"similar_product_list_json : {similar_product_list_json}")
|
|
|
461 |
st.session_state.welcome_msg = "Welcome to ConsumeWise! What product would you like me to analyze today?"
|
462 |
if "system_prompt" not in st.session_state:
|
463 |
st.session_state.system_prompt = ""
|
464 |
+
if "similar_products" not in st.session_state:
|
465 |
+
st.session_state.similar_products = []
|
466 |
+
if "awaiting_selection" not in st.session_state:
|
467 |
+
st.session_state.awaiting_selection = False
|
468 |
+
if "current_user_input" not in st.session_state:
|
469 |
+
st.session_state.current_user_input = ""
|
470 |
|
471 |
# Sidebar input for system prompt with an 'Apply' button
|
472 |
st.sidebar.header("System Prompt")
|
|
|
481 |
if st.sidebar.button("Submit Prompt"):
|
482 |
if system_prompt.strip():
|
483 |
st.session_state.system_prompt = system_prompt
|
|
|
484 |
# Reset session state when system prompt is changed
|
485 |
st.session_state.product_selected = False
|
486 |
st.session_state.analyze_more = True
|
|
|
488 |
st.session_state.yes_no_choice = None
|
489 |
st.session_state.product_shared = False
|
490 |
st.session_state.welcome_msg = "Welcome to ConsumeWise! What product would you like me to analyze today?"
|
491 |
+
st.session_state.messages = []
|
492 |
+
st.session_state.similar_products = []
|
493 |
+
st.session_state.awaiting_selection = False
|
494 |
+
st.session_state.current_user_input = ""
|
495 |
+
st.rerun()
|
496 |
else:
|
497 |
st.sidebar.error("Please enter a valid system prompt.")
|
498 |
|
|
|
501 |
|
502 |
# Show warning if system prompt is empty and disable chat input
|
503 |
if not st.session_state.system_prompt.strip():
|
504 |
+
st.sidebar.error("Please enter a system prompt in the sidebar before starting the conversation.")
|
505 |
st.warning("⚠️ Please enter a system prompt in the sidebar before proceeding.")
|
506 |
st.chat_input("Enter your message:", disabled=True)
|
507 |
else:
|
|
|
513 |
st.markdown(message["content"])
|
514 |
st.markdown("</div>", unsafe_allow_html=True)
|
515 |
|
|
|
516 |
def show_welcome_message():
|
|
|
517 |
if not st.session_state.welcome_shown:
|
518 |
st.session_state.messages.append({"role": "assistant", "content": st.session_state.welcome_msg})
|
519 |
with st.chat_message("assistant"):
|
520 |
st.markdown(st.session_state.welcome_msg)
|
521 |
st.session_state.welcome_shown = True
|
522 |
|
523 |
+
def handle_radio_selection():
|
524 |
+
if st.session_state.awaiting_selection and st.session_state.similar_products:
|
525 |
+
choice = st.radio(
|
526 |
+
"Select a product:",
|
527 |
+
st.session_state.similar_products + ["None of the above"],
|
528 |
+
key="product_choice"
|
529 |
+
)
|
530 |
+
|
531 |
+
if choice:
|
532 |
+
if choice != "None of the above":
|
533 |
+
st.session_state.product_selected = True
|
534 |
+
st.session_state.awaiting_selection = False
|
535 |
+
_, msg = chatbot_response("", st.session_state.current_user_input, data_extractor_url, system_prompt, extract_info=True)
|
536 |
+
st.session_state.messages.append({"role": "assistant", "content": msg})
|
537 |
+
st.rerun()
|
538 |
+
else:
|
539 |
+
st.session_state.awaiting_selection = False
|
540 |
+
st.session_state.messages.append(
|
541 |
+
{"role": "assistant", "content": "Please provide the image URL of the product."}
|
542 |
+
)
|
543 |
+
st.rerun()
|
544 |
+
|
545 |
def chat_response(user_input, system_prompt):
|
|
|
546 |
if not st.session_state.product_selected:
|
|
|
547 |
if "http:/" not in user_input and "https:/" not in user_input:
|
548 |
st.session_state.product_shared = True
|
549 |
+
st.session_state.current_user_input = user_input
|
550 |
similar_products, msg = chatbot_response("", user_input, data_extractor_url, system_prompt, extract_info=False)
|
551 |
+
|
552 |
if len(similar_products) > 0:
|
553 |
+
st.session_state.similar_products = similar_products
|
554 |
+
st.session_state.awaiting_selection = True
|
555 |
+
return "Here are some similar products. Please select:"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
556 |
else:
|
557 |
+
return "Product not found in our database. Please provide the image URL of the product."
|
558 |
|
559 |
else:
|
560 |
if ("http:/" in user_input or "https:/" in user_input) and (".jpeg" in user_input or ".jpg" in user_input) and st.session_state.product_shared:
|
561 |
_, msg = chatbot_response(user_input, "", data_extractor_url, system_prompt, extract_info=True)
|
|
|
562 |
st.session_state.product_selected = True
|
563 |
+
return msg
|
|
|
|
|
|
|
|
|
|
|
|
|
564 |
else:
|
565 |
if not st.session_state.product_shared:
|
566 |
+
return "Please provide the product name first"
|
567 |
else:
|
568 |
+
return "Please provide valid image URL of the product."
|
569 |
else:
|
570 |
return "Next Product"
|
571 |
|
572 |
# Show the welcome message at the start
|
573 |
show_welcome_message()
|
574 |
|
575 |
+
# Handle radio button selection
|
576 |
+
handle_radio_selection()
|
577 |
+
|
578 |
+
# Input Box for User Interaction
|
579 |
user_input = st.chat_input("Enter your message:", key="user_input")
|
580 |
|
581 |
if user_input:
|
582 |
+
st.session_state.messages.append({"role": "user", "content": user_input})
|
583 |
+
with st.chat_message("user"):
|
584 |
+
st.markdown(user_input)
|
585 |
+
|
586 |
+
response = chat_response(user_input, system_prompt)
|
587 |
|
588 |
if response != "Next Product":
|
589 |
st.session_state.messages.append({"role": "assistant", "content": response})
|
|
|
596 |
st.session_state.yes_no_choice = None
|
597 |
st.session_state.product_shared = False
|
598 |
st.session_state.welcome_msg = "What is the next product you would like me to analyze today?"
|
599 |
+
st.session_state.similar_products = []
|
600 |
+
st.session_state.awaiting_selection = False
|
601 |
+
st.session_state.current_user_input = ""
|
602 |
st.rerun()
|
603 |
|
604 |
# Option to Clear Chat History
|