shivanis14 commited on
Commit
2ca7d09
1 Parent(s): 476d625

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -14
app.py CHANGED
@@ -308,9 +308,9 @@ def analyze_claims(claims, assistant_id):
308
 
309
  return claims_analysis_str
310
 
311
- def generate_final_analysis(brand_name, product_name, processing_level, harmful_ingredient_analysis, claims_analysis):
312
  global debug_mode, client
313
- system_prompt = """You are provided with a detailed analysis of a food product. Your task is to generate actionable insights to help the user decide whether to consume the product, at what frequency, and identify any potential harms or benefits. Consider the context of consumption to ensure the advice is personalized and practical.
314
 
315
  Use the following criteria to generate your response:
316
 
@@ -363,7 +363,7 @@ Claims Analysis:
363
  return completion.choices[0].message.content
364
 
365
 
366
- def analyze_product(product_info_raw):
367
  global assistant1, assistant2, assistant3
368
 
369
  if product_info_raw != "{}":
@@ -379,7 +379,7 @@ def analyze_product(product_info_raw):
379
  if len(claims_list) > 0:
380
  claims_analysis = analyze_claims(claims_list, assistant3.id) if claims_list else ""
381
 
382
- final_analysis = generate_final_analysis(brand_name,product_name,processing_level,harmful_ingredient_analysis,claims_analysis)
383
  return final_analysis
384
  else:
385
  return "I'm sorry, product information could not be extracted from the url."
@@ -389,7 +389,7 @@ def analyze_product(product_info_raw):
389
  if 'messages' not in st.session_state:
390
  st.session_state.messages = []
391
 
392
- def chatbot_response(image_urls_str, product_name_by_user, data_extractor_url, extract_info = True):
393
  # Process the user input and generate a response
394
  processing_level = ""
395
  harmful_ingredient_analysis = ""
@@ -407,7 +407,7 @@ def chatbot_response(image_urls_str, product_name_by_user, data_extractor_url, e
407
  with st.spinner("Analyzing the product... This may take a moment."):
408
  product_info_raw = get_product_data_from_db(product_name_by_user, data_extractor_url)
409
  print(f"DEBUG product_info_raw : {product_info_raw}")
410
- final_analysis = analyze_product(product_info_raw)
411
  return [], final_analysis
412
 
413
  else:
@@ -426,7 +426,7 @@ def chatbot_response(image_urls_str, product_name_by_user, data_extractor_url, e
426
  product_info_raw = extract_data_from_product_image(image_urls, data_extractor_url)
427
  print(f"DEBUG product_info_raw : {product_info_raw}")
428
  if product_info_raw:
429
- final_analysis = analyze_product(product_info_raw)
430
  return [], final_analysis
431
  else:
432
  return [], "Information could not be extracted from the image!"
@@ -449,6 +449,8 @@ if "yes_no_choice" not in st.session_state: # Track if Yes/No choice was made
449
  st.session_state.yes_no_choice = None
450
  if "welcome_msg" not in st.session_state: # Track if Yes/No choice was made
451
  st.session_state.welcome_msg = "Welcome to ConsumeWise! What product would you like me to analyze today?"
 
 
452
 
453
  print(f"st.session_state.product_selected : {st.session_state.product_selected}")
454
  print(f"st.session_state.product_shared : {st.session_state.product_shared}")
@@ -459,6 +461,19 @@ print(f"st.session_state.yes_no_choice : {st.session_state.yes_no_choice}")
459
 
460
  st.title("ConsumeWise - Your Food Product Analysis Assistant")
461
 
 
 
 
 
 
 
 
 
 
 
 
 
 
462
  # Chat history in a scrollable container
463
  with st.container():
464
  st.markdown("<div style='height: 400px; overflow-y: auto;'>", unsafe_allow_html=True)
@@ -504,13 +519,13 @@ def get_yes_no_choice(key):
504
 
505
 
506
  # Define chatbot interaction logic
507
- def chat_response(user_input):
508
  """Simulate product selection and analysis logic."""
509
  if not st.session_state.product_selected:
510
  # Fetch similar products
511
  if "http:/" not in user_input and "https:/" not in user_input:
512
  st.session_state.product_shared = True
513
- similar_products, msg = chatbot_response("", user_input, data_extractor_url, extract_info=False)
514
  if len(similar_products) > 0:
515
  st.session_state.messages.append(
516
  {"role": "assistant", "content": "Here are some similar products. Please select one or enter 'None':"}
@@ -522,7 +537,7 @@ def chat_response(user_input):
522
  choice = st.radio("Select a product:", similar_products + ["None"], key="product_choice")
523
  if choice and choice != "None":
524
  st.session_state.product_selected = True
525
- _, msg = chatbot_response("", user_input, data_extractor_url, extract_info=True)
526
  return msg
527
 
528
  elif choice == "None":
@@ -532,7 +547,7 @@ def chat_response(user_input):
532
 
533
  else:
534
  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:
535
- _, msg = chatbot_response(user_input, "", data_extractor_url, extract_info=True)
536
 
537
  st.session_state.product_selected = True
538
 
@@ -556,9 +571,10 @@ show_welcome_message()
556
 
557
  # Input Box for User Interaction
558
  user_input = st.chat_input("Enter your message:", key="user_input")
559
-
560
- if user_input:
561
- response = chat_response(handle_user_input())
 
562
 
563
  if response != "Next Product":
564
  st.session_state.messages.append({"role": "assistant", "content": response})
 
308
 
309
  return claims_analysis_str
310
 
311
+ def generate_final_analysis(brand_name, product_name, processing_level, harmful_ingredient_analysis, claims_analysis, system_prompt):
312
  global debug_mode, client
313
+ system_prompt_orig = """You are provided with a detailed analysis of a food product. Your task is to generate actionable insights to help the user decide whether to consume the product, at what frequency, and identify any potential harms or benefits. Consider the context of consumption to ensure the advice is personalized and practical.
314
 
315
  Use the following criteria to generate your response:
316
 
 
363
  return completion.choices[0].message.content
364
 
365
 
366
+ def analyze_product(product_info_raw, system_prompt):
367
  global assistant1, assistant2, assistant3
368
 
369
  if product_info_raw != "{}":
 
379
  if len(claims_list) > 0:
380
  claims_analysis = analyze_claims(claims_list, assistant3.id) if claims_list else ""
381
 
382
+ final_analysis = generate_final_analysis(brand_name,product_name,processing_level,harmful_ingredient_analysis,claims_analysis, system_prompt)
383
  return final_analysis
384
  else:
385
  return "I'm sorry, product information could not be extracted from the url."
 
389
  if 'messages' not in st.session_state:
390
  st.session_state.messages = []
391
 
392
+ def chatbot_response(image_urls_str, product_name_by_user, data_extractor_url, system_prompt, extract_info = True):
393
  # Process the user input and generate a response
394
  processing_level = ""
395
  harmful_ingredient_analysis = ""
 
407
  with st.spinner("Analyzing the product... This may take a moment."):
408
  product_info_raw = get_product_data_from_db(product_name_by_user, data_extractor_url)
409
  print(f"DEBUG product_info_raw : {product_info_raw}")
410
+ final_analysis = analyze_product(product_info_raw, system_prompt)
411
  return [], final_analysis
412
 
413
  else:
 
426
  product_info_raw = extract_data_from_product_image(image_urls, data_extractor_url)
427
  print(f"DEBUG product_info_raw : {product_info_raw}")
428
  if product_info_raw:
429
+ final_analysis = analyze_product(product_info_raw, system_prompt)
430
  return [], final_analysis
431
  else:
432
  return [], "Information could not be extracted from the image!"
 
449
  st.session_state.yes_no_choice = None
450
  if "welcome_msg" not in st.session_state: # Track if Yes/No choice was made
451
  st.session_state.welcome_msg = "Welcome to ConsumeWise! What product would you like me to analyze today?"
452
+ if "system_prompt" not in st.session_state:
453
+ st.session_state.system_prompt = ""
454
 
455
  print(f"st.session_state.product_selected : {st.session_state.product_selected}")
456
  print(f"st.session_state.product_shared : {st.session_state.product_shared}")
 
461
 
462
  st.title("ConsumeWise - Your Food Product Analysis Assistant")
463
 
464
+ # Add a text area for system prompt input
465
+ st.sidebar.header("System Prompt")
466
+ system_prompt = st.sidebar.text_area(
467
+ "Enter your system prompt here:",
468
+ value=st.session_state.system_prompt,
469
+ height=150,
470
+ key="system_prompt_input"
471
+ )
472
+
473
+ # Update session state when the system prompt changes
474
+ if system_prompt != st.session_state.system_prompt:
475
+ st.session_state.system_prompt = system_prompt
476
+
477
  # Chat history in a scrollable container
478
  with st.container():
479
  st.markdown("<div style='height: 400px; overflow-y: auto;'>", unsafe_allow_html=True)
 
519
 
520
 
521
  # Define chatbot interaction logic
522
+ def chat_response(user_input, system_prompt):
523
  """Simulate product selection and analysis logic."""
524
  if not st.session_state.product_selected:
525
  # Fetch similar products
526
  if "http:/" not in user_input and "https:/" not in user_input:
527
  st.session_state.product_shared = True
528
+ similar_products, msg = chatbot_response("", user_input, data_extractor_url, system_prompt, extract_info=False)
529
  if len(similar_products) > 0:
530
  st.session_state.messages.append(
531
  {"role": "assistant", "content": "Here are some similar products. Please select one or enter 'None':"}
 
537
  choice = st.radio("Select a product:", similar_products + ["None"], key="product_choice")
538
  if choice and choice != "None":
539
  st.session_state.product_selected = True
540
+ _, msg = chatbot_response("", user_input, data_extractor_url, system_prompt, extract_info=True)
541
  return msg
542
 
543
  elif choice == "None":
 
547
 
548
  else:
549
  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:
550
+ _, msg = chatbot_response(user_input, "", data_extractor_url, system_prompt, extract_info=True)
551
 
552
  st.session_state.product_selected = True
553
 
 
571
 
572
  # Input Box for User Interaction
573
  user_input = st.chat_input("Enter your message:", key="user_input")
574
+ #How can I provide a text box to enter system_prompt?
575
+
576
+ if user_input and system_prompt:
577
+ response = chat_response(handle_user_input(), system_prompt)
578
 
579
  if response != "Next Product":
580
  st.session_state.messages.append({"role": "assistant", "content": response})