shivanis14 commited on
Commit
c6ea484
1 Parent(s): c4e9056

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -1
app.py CHANGED
@@ -331,6 +331,7 @@ Claims Analysis:
331
 
332
  return completion.choices[0].message.content
333
 
 
334
  # Streamlit app
335
  def main():
336
  global data_extractor_url
@@ -423,4 +424,78 @@ def main():
423
  st.rerun()
424
 
425
  if __name__ == "__main__":
426
- main()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
331
 
332
  return completion.choices[0].message.content
333
 
334
+ '''
335
  # Streamlit app
336
  def main():
337
  global data_extractor_url
 
424
  st.rerun()
425
 
426
  if __name__ == "__main__":
427
+ main()
428
+ '''
429
+
430
+ # Initialize session state
431
+ if 'messages' not in st.session_state:
432
+ st.session_state.messages = []
433
+
434
+ def chatbot_response(user_input):
435
+ # Process the user input and generate a response
436
+ processing_level = ""
437
+ harmful_ingredient_analysis = ""
438
+ claims_analysis = ""
439
+
440
+ if "analyze" in user_input.lower() and "http" in user_input.lower():
441
+ # Extract image URL from user input
442
+ image_url = user_input.split("http")[1].split()[0]
443
+ image_url = "http" + image_url
444
+
445
+ with st.spinner("Analyzing the product... This may take a moment."):
446
+ product_info_from_db = json.loads(extract_data_from_product_image([image_url], data_extractor_url))
447
+
448
+ if product_info_from_db:
449
+ brand_name = product_info_from_db.get("brandName", "")
450
+ product_name = product_info_from_db.get("productName", "")
451
+ ingredients_list = [ingredient["name"] for ingredient in product_info_from_db.get("ingredients", [])]
452
+ claims_list = product_info_from_db.get("claims", [])
453
+
454
+ if len(ingredients_list) > 0:
455
+ processing_level = analyze_processing_level(ingredients_list, brand_name, product_name, assistant1.id) if ingredients_list else ""
456
+ harmful_ingredient_analysis = analyze_harmful_ingredients(ingredients_list, brand_name, product_name, assistant2.id) if ingredients_list else ""
457
+ if len(claims_list) > 0:
458
+ claims_analysis = analyze_claims(claims_list, assistant3.id) if claims_list else ""
459
+
460
+ final_analysis = generate_final_analysis(
461
+ brand_name,
462
+ product_name,
463
+ processing_level,
464
+ harmful_ingredient_analysis,
465
+ claims_analysis
466
+ )
467
+ return final_analysis
468
+ else:
469
+ return "I'm sorry, I couldn't extract any information from that image. Could you try another image or provide more details about the product?"
470
+ else:
471
+ return "I'm here to analyze food products based on their image. Please provide an image URL and ask me to analyze it. For example: 'Can you analyze this product? http://example.com/image.jpg'"
472
+
473
+ # Streamlit app
474
+ st.title("Food Product Analysis Chatbot")
475
+
476
+ st.write("Hello! I'm your food product analysis assistant. I can analyze food products based on their image. Just send me an image URL and ask me to analyze it.")
477
+
478
+ # Display chat messages
479
+ for message in st.session_state.messages:
480
+ with st.chat_message(message["role"]):
481
+ st.markdown(message["content"])
482
+
483
+ # User input
484
+ user_input = st.chat_input("Type your message here...")
485
+
486
+ if user_input:
487
+ # Add user message to chat history
488
+ st.session_state.messages.append({"role": "user", "content": user_input})
489
+ with st.chat_message("user"):
490
+ st.markdown(user_input)
491
+
492
+ # Generate and display assistant response
493
+ response = chatbot_response(user_input)
494
+ st.session_state.messages.append({"role": "assistant", "content": response})
495
+ with st.chat_message("assistant"):
496
+ st.markdown(response)
497
+
498
+ # Option to clear chat history
499
+ if st.button("Clear Chat History"):
500
+ st.session_state.messages = []
501
+ st.rerun()