shivanis14
commited on
Commit
•
7eb358f
1
Parent(s):
4ede868
Update app.py
Browse files
app.py
CHANGED
@@ -363,103 +363,6 @@ Claims Analysis:
|
|
363 |
return completion.choices[0].message.content
|
364 |
|
365 |
|
366 |
-
# Streamlit app
|
367 |
-
def old_main():
|
368 |
-
global data_extractor_url
|
369 |
-
|
370 |
-
st.title("Food Product Analysis")
|
371 |
-
|
372 |
-
# Create a form for all inputs
|
373 |
-
with st.form("product_analysis_form"):
|
374 |
-
st.write("Please enter the following information about the product:")
|
375 |
-
#brand_name = st.text_input("Brand Name")
|
376 |
-
#product_name = st.text_input("Product Name")
|
377 |
-
image_links = st.text_area("Image Links (separated by commas)")
|
378 |
-
|
379 |
-
|
380 |
-
submitted = st.form_submit_button("Analyze Product")
|
381 |
-
|
382 |
-
if submitted:
|
383 |
-
# Process inputs
|
384 |
-
|
385 |
-
image_links_list = []
|
386 |
-
if image_links:
|
387 |
-
image_links_list = [image_link.strip() for image_link in image_links.split(',')]
|
388 |
-
|
389 |
-
# Display a message while analyzing
|
390 |
-
with st.spinner("Analyzing the product... This may take a moment."):
|
391 |
-
# Perform analysis
|
392 |
-
processing_level = ""
|
393 |
-
harmful_ingredient_analysis = ""
|
394 |
-
claims_analysis = ""
|
395 |
-
ingredients_list = []
|
396 |
-
claims_list = []
|
397 |
-
product_info = {}
|
398 |
-
brand_name = ""
|
399 |
-
brand_product = ""
|
400 |
-
|
401 |
-
if len(image_links_list) > 0:
|
402 |
-
product_info_raw = extract_data_from_product_image(image_links_list, data_extractor_url)
|
403 |
-
if product_info_raw != "{}":
|
404 |
-
product_info_from_db = json.loads(product_info_raw)
|
405 |
-
print(f"DEBUG : product_info_from_db : {product_info_from_db}\n\n")
|
406 |
-
|
407 |
-
if product_info_from_db:
|
408 |
-
try:
|
409 |
-
brand_name = product_info_from_db["brandName"]
|
410 |
-
except:
|
411 |
-
print(f"Brand Name not found in product_info {product_info_from_db}")
|
412 |
-
|
413 |
-
try:
|
414 |
-
product_name = product_info_from_db["productName"]
|
415 |
-
except:
|
416 |
-
print(f"Product Name not found in product_info {product_info_from_db}")
|
417 |
-
|
418 |
-
try:
|
419 |
-
ingredients_list = [ingredient["name"] for ingredient in product_info_from_db["ingredients"]]
|
420 |
-
except:
|
421 |
-
print(f"Ingredient list not found in product_info {product_info_from_db}")
|
422 |
-
|
423 |
-
try:
|
424 |
-
claims_list = product_info_from_db["claims"]
|
425 |
-
except:
|
426 |
-
print(f"Claims list not found in product_info {product_info_from_db}")
|
427 |
-
|
428 |
-
if len(ingredients_list) > 0:
|
429 |
-
processing_level = analyze_processing_level(ingredients_list, brand_name, product_name, assistant1.id)
|
430 |
-
harmful_ingredient_analysis = analyze_harmful_ingredients(ingredients_list, brand_name, product_name, assistant2.id)
|
431 |
-
else:
|
432 |
-
print("No ingredients specified by the user!")
|
433 |
-
|
434 |
-
if len(claims_list) > 0:
|
435 |
-
claims_analysis = analyze_claims(claims_list, assistant3.id)
|
436 |
-
else:
|
437 |
-
print("No claims specified by the user!")
|
438 |
-
|
439 |
-
# Generate final analysis
|
440 |
-
if processing_level != "" or harmful_ingredient_analysis != "" or claims_analysis != "":
|
441 |
-
final_analysis = generate_final_analysis(
|
442 |
-
brand_name,
|
443 |
-
product_name,
|
444 |
-
processing_level,
|
445 |
-
harmful_ingredient_analysis,
|
446 |
-
claims_analysis
|
447 |
-
)
|
448 |
-
else:
|
449 |
-
final_analysis = "Sorry, No product information found! Please re-try"
|
450 |
-
|
451 |
-
# Display results
|
452 |
-
st.success("Analysis complete!")
|
453 |
-
st.subheader("Final Analysis:")
|
454 |
-
st.write(final_analysis)
|
455 |
-
|
456 |
-
# Option to start over
|
457 |
-
if st.button("Analyze Another Product"):
|
458 |
-
st.rerun()
|
459 |
-
|
460 |
-
#if __name__ == "__main__":
|
461 |
-
# main()
|
462 |
-
|
463 |
def analyze_product(product_info_raw):
|
464 |
global assistant1, assistant2, assistant3
|
465 |
|
@@ -480,7 +383,8 @@ def analyze_product(product_info_raw):
|
|
480 |
return final_analysis
|
481 |
else:
|
482 |
return "I'm sorry, product information could not be extracted from the url."
|
483 |
-
|
|
|
484 |
# Initialize session state
|
485 |
if 'messages' not in st.session_state:
|
486 |
st.session_state.messages = []
|
@@ -494,6 +398,7 @@ def chatbot_response(image_urls_str, product_name_by_user, data_extractor_url, e
|
|
494 |
if product_name_by_user != "":
|
495 |
similar_product_list_json = get_product_list(product_name_by_user, data_extractor_url)
|
496 |
if similar_product_list_json != "{}" and extract_info == False:
|
|
|
497 |
similar_product_list = json.loads(similar_product_list_json)['product_list']
|
498 |
return similar_product_list
|
499 |
|
@@ -537,21 +442,8 @@ st.write("Hello! I'm your food product analysis assistant. Select one of the fol
|
|
537 |
#If user selects 1, ask user to enter image_urls separated by commas else do nothing. Repeat the options after response is generated.
|
538 |
#Do not allow user to send prompts while chatbot is still generating a response
|
539 |
|
540 |
-
options = [
|
541 |
-
"I want to know more about a product",
|
542 |
-
"How much % of my daily allowance of calorie, fat, protein, carb, sugar or any other nutrient will I exhaust if I eat this?",
|
543 |
-
"I want to understand what an ingredient name means",
|
544 |
-
"I want to understand what different nutrients mean",
|
545 |
-
]
|
546 |
-
choice = st.radio("Choose an option:", options)
|
547 |
-
|
548 |
-
|
549 |
# User input
|
550 |
-
|
551 |
-
product_name_by_user = st.text_input("Enter product name")
|
552 |
-
#image_urls_str = st.text_input("Enter image URLs separated by commas")
|
553 |
-
else:
|
554 |
-
product_name_by_user = ""
|
555 |
|
556 |
# Display chat messages
|
557 |
for message in st.session_state.messages:
|
@@ -590,7 +482,7 @@ if product_name_by_user != "":
|
|
590 |
st.markdown(response)
|
591 |
|
592 |
st.session_state["response_pending"] = False
|
593 |
-
|
594 |
# Option to clear chat history
|
595 |
if st.button("Clear Chat History"):
|
596 |
st.session_state.messages = []
|
|
|
363 |
return completion.choices[0].message.content
|
364 |
|
365 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
366 |
def analyze_product(product_info_raw):
|
367 |
global assistant1, assistant2, assistant3
|
368 |
|
|
|
383 |
return final_analysis
|
384 |
else:
|
385 |
return "I'm sorry, product information could not be extracted from the url."
|
386 |
+
|
387 |
+
# Streamlit app
|
388 |
# Initialize session state
|
389 |
if 'messages' not in st.session_state:
|
390 |
st.session_state.messages = []
|
|
|
398 |
if product_name_by_user != "":
|
399 |
similar_product_list_json = get_product_list(product_name_by_user, data_extractor_url)
|
400 |
if similar_product_list_json != "{}" and extract_info == False:
|
401 |
+
print(f"similar_product_list_json : {similar_product_list_json}")
|
402 |
similar_product_list = json.loads(similar_product_list_json)['product_list']
|
403 |
return similar_product_list
|
404 |
|
|
|
442 |
#If user selects 1, ask user to enter image_urls separated by commas else do nothing. Repeat the options after response is generated.
|
443 |
#Do not allow user to send prompts while chatbot is still generating a response
|
444 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
445 |
# User input
|
446 |
+
product_name_by_user = st.text_input("Enter product name")
|
|
|
|
|
|
|
|
|
447 |
|
448 |
# Display chat messages
|
449 |
for message in st.session_state.messages:
|
|
|
482 |
st.markdown(response)
|
483 |
|
484 |
st.session_state["response_pending"] = False
|
485 |
+
|
486 |
# Option to clear chat history
|
487 |
if st.button("Clear Chat History"):
|
488 |
st.session_state.messages = []
|