shivanis14
commited on
Commit
•
ae67f00
1
Parent(s):
1f54b96
Update app.py (#2)
Browse files- Update app.py (ae87d076b81cff6841a1c1007ab882afad3d1505)
app.py
CHANGED
@@ -4,6 +4,7 @@ import json, os
|
|
4 |
import requests, time
|
5 |
from data_extractor import extract_data, find_product, get_product
|
6 |
from nutrient_analyzer import analyze_nutrients
|
|
|
7 |
|
8 |
#Used the @st.cache_resource decorator on this function.
|
9 |
#This Streamlit decorator ensures that the function is only executed once and its result (the OpenAI client) is cached.
|
@@ -35,7 +36,41 @@ def get_product_list(product_name_by_user, data_extractor_url):
|
|
35 |
response = find_product(product_name_by_user)
|
36 |
return response
|
37 |
|
38 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
#GET Response: {'_id': '6714f0487a0e96d7aae2e839',
|
40 |
#'brandName': 'Parle', 'claims': ['This product does not contain gold'],
|
41 |
#'fssaiLicenseNumbers': [10013022002253],
|
@@ -56,6 +91,21 @@ def find_product_type(product_info_from_db):
|
|
56 |
#'servingsPerPack': 3.98,
|
57 |
#'shelfLife': '7 months from packaging'}
|
58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
|
60 |
product_type = None
|
61 |
calories = None
|
@@ -64,14 +114,13 @@ def find_product_type(product_info_from_db):
|
|
64 |
added_sugar = None
|
65 |
salt = None
|
66 |
serving_size = None
|
67 |
-
|
68 |
if product_info_from_db["servingSize"]["unit"] == "g":
|
69 |
product_type = "solid"
|
70 |
elif product_info_from_db["servingSize"]["unit"] == "ml":
|
71 |
product_type = "liquid"
|
72 |
serving_size = product_info_from_db["servingSize"]["quantity"]
|
73 |
-
|
74 |
-
for item in product_info_from_db["nutritionalInformation"]:
|
75 |
if 'energy' in item['name'].lower():
|
76 |
calories = item['values'][0]['value']
|
77 |
if 'total sugar' in item['name'].lower():
|
@@ -86,6 +135,7 @@ def find_product_type(product_info_from_db):
|
|
86 |
sugar = added_sugar
|
87 |
elif total_sugar is not None and total_sugar > 0 and added_sugar is None and sugar is None:
|
88 |
sugar = total_sugar
|
|
|
89 |
return product_type, calories, sugar, salt, serving_size
|
90 |
|
91 |
# Initialize assistants and vector stores
|
@@ -391,13 +441,23 @@ def analyze_product(product_info_raw, system_prompt):
|
|
391 |
product_name = product_info_from_db.get("productName", "")
|
392 |
ingredients_list = [ingredient["name"] for ingredient in product_info_from_db.get("ingredients", [])]
|
393 |
claims_list = product_info_from_db.get("claims", [])
|
|
|
|
|
394 |
|
395 |
-
if
|
396 |
product_type, calories, sugar, salt, serving_size = find_product_type(product_info_from_db)
|
397 |
nutrient_analysis = analyze_nutrients(product_type, calories, sugar, salt, serving_size)
|
398 |
print(f"DEBUG ! nutrient analysis is {nutrient_analysis}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
399 |
processing_level = analyze_processing_level(ingredients_list, brand_name, product_name, assistant1.id) if ingredients_list else ""
|
400 |
harmful_ingredient_analysis = analyze_harmful_ingredients(ingredients_list, brand_name, product_name, assistant2.id) if ingredients_list else ""
|
|
|
401 |
if len(claims_list) > 0:
|
402 |
claims_analysis = analyze_claims(claims_list, ingredients_list, product_name, assistant3.id) if claims_list else ""
|
403 |
|
|
|
4 |
import requests, time
|
5 |
from data_extractor import extract_data, find_product, get_product
|
6 |
from nutrient_analyzer import analyze_nutrients
|
7 |
+
from rda import find_nutrition
|
8 |
|
9 |
#Used the @st.cache_resource decorator on this function.
|
10 |
#This Streamlit decorator ensures that the function is only executed once and its result (the OpenAI client) is cached.
|
|
|
36 |
response = find_product(product_name_by_user)
|
37 |
return response
|
38 |
|
39 |
+
def rda_analysis(product_info_from_db_nutritionalInformation, product_info_from_db_servingSize):
|
40 |
+
data_for_rda_analysis = {'nutritionPerServing' : {}, 'userServingSize' : None}
|
41 |
+
nutrient_name_list = ['energy', 'protein', 'carbohydrates', 'addedSugars', 'dietaryFiber', 'totalFat', 'saturatedFat', 'monounsaturatedFat', 'polyunsaturatedFat', 'transFat', 'sodium']
|
42 |
+
|
43 |
+
completion = client.chat.completions.create(
|
44 |
+
model="gpt-4o",
|
45 |
+
messages=[
|
46 |
+
{"role": "system", "content": f"You will be given nutritional information of a food product. Find values of {', '.join(nutrient_name_list)} in the user-defined format"},
|
47 |
+
{"role": "user", "content":
|
48 |
+
"Nutritional content of food product is " + json.dumps(product_info_from_db_nutritionalInformation) + """. Generate output in this JSON format
|
49 |
+
{'energy': <value>,
|
50 |
+
'protein': <value>,
|
51 |
+
'carbohydrates': <value>,
|
52 |
+
'addedSugars': <value>,
|
53 |
+
'dietaryFiber': <value>,
|
54 |
+
'totalFat': <value>,
|
55 |
+
'saturatedFat': <value>,
|
56 |
+
'monounsaturatedFat': <value>,
|
57 |
+
'polyunsaturatedFat': <value>,
|
58 |
+
'transFat': <value>,
|
59 |
+
'sodium': <value>,
|
60 |
+
'servingSize': <Serving size of the provided nutrition value>} """}
|
61 |
+
]
|
62 |
+
)
|
63 |
+
|
64 |
+
data_for_rda_analysis_part_1 = completion.choices[0].message.content
|
65 |
+
print(f"data_for_rda_analysis_part_1 : {data_for_rda_analysis_part_1}")
|
66 |
+
|
67 |
+
data_for_rda_analysis = {}
|
68 |
+
data_for_rda_analysis.update({'nutritionPerServing' : data_for_rda_analysis_part_1.replace("```", "").replace("json", "")})
|
69 |
+
data_for_rda_analysis.update({'userServingSize' : product_info_from_db_servingSize})
|
70 |
+
|
71 |
+
return data_for_rda_analysis
|
72 |
+
|
73 |
+
def find_product_nutrients(product_info_from_db):
|
74 |
#GET Response: {'_id': '6714f0487a0e96d7aae2e839',
|
75 |
#'brandName': 'Parle', 'claims': ['This product does not contain gold'],
|
76 |
#'fssaiLicenseNumbers': [10013022002253],
|
|
|
91 |
#'servingsPerPack': 3.98,
|
92 |
#'shelfLife': '7 months from packaging'}
|
93 |
|
94 |
+
###rda###
|
95 |
+
#nutrition_data = {
|
96 |
+
#'energy': 250,
|
97 |
+
#'protein': 10,
|
98 |
+
#'carbohydrates': 30,
|
99 |
+
#'addedSugars': 5,
|
100 |
+
#'dietaryFiber': 3,
|
101 |
+
#'totalFat': 10,
|
102 |
+
#'saturatedFat': 3,
|
103 |
+
#'monounsaturatedFat': 2,
|
104 |
+
#'polyunsaturatedFat': 1,
|
105 |
+
#'transFat': 0,
|
106 |
+
#'sodium': 200,
|
107 |
+
#'servingSize': 100 # Serving size of the provided nutrition values
|
108 |
+
#}
|
109 |
|
110 |
product_type = None
|
111 |
calories = None
|
|
|
114 |
added_sugar = None
|
115 |
salt = None
|
116 |
serving_size = None
|
117 |
+
|
118 |
if product_info_from_db["servingSize"]["unit"] == "g":
|
119 |
product_type = "solid"
|
120 |
elif product_info_from_db["servingSize"]["unit"] == "ml":
|
121 |
product_type = "liquid"
|
122 |
serving_size = product_info_from_db["servingSize"]["quantity"]
|
123 |
+
|
|
|
124 |
if 'energy' in item['name'].lower():
|
125 |
calories = item['values'][0]['value']
|
126 |
if 'total sugar' in item['name'].lower():
|
|
|
135 |
sugar = added_sugar
|
136 |
elif total_sugar is not None and total_sugar > 0 and added_sugar is None and sugar is None:
|
137 |
sugar = total_sugar
|
138 |
+
|
139 |
return product_type, calories, sugar, salt, serving_size
|
140 |
|
141 |
# Initialize assistants and vector stores
|
|
|
441 |
product_name = product_info_from_db.get("productName", "")
|
442 |
ingredients_list = [ingredient["name"] for ingredient in product_info_from_db.get("ingredients", [])]
|
443 |
claims_list = product_info_from_db.get("claims", [])
|
444 |
+
nutritional_information = product_info_from_db['nutritionalInformation']
|
445 |
+
serving_size = product_info_from_db["servingSize"]["quantity"]
|
446 |
|
447 |
+
if nutritional_information:
|
448 |
product_type, calories, sugar, salt, serving_size = find_product_type(product_info_from_db)
|
449 |
nutrient_analysis = analyze_nutrients(product_type, calories, sugar, salt, serving_size)
|
450 |
print(f"DEBUG ! nutrient analysis is {nutrient_analysis}")
|
451 |
+
|
452 |
+
nutrient_analysis_rda_data = rda_analysis(nutritional_information, serving_size)
|
453 |
+
print(f"DEBUG ! Data for RDA nutrient analysis is {nutrient_analysis_rda_data}")
|
454 |
+
nutrient_analysis_rda = find_nutrition(nutrient_analysis_rda_data)
|
455 |
+
print(f"DEBUG ! RDA nutrient analysis is {nutrient_analysis_rda}")
|
456 |
+
|
457 |
+
if len(ingredients_list) > 0:
|
458 |
processing_level = analyze_processing_level(ingredients_list, brand_name, product_name, assistant1.id) if ingredients_list else ""
|
459 |
harmful_ingredient_analysis = analyze_harmful_ingredients(ingredients_list, brand_name, product_name, assistant2.id) if ingredients_list else ""
|
460 |
+
|
461 |
if len(claims_list) > 0:
|
462 |
claims_analysis = analyze_claims(claims_list, ingredients_list, product_name, assistant3.id) if claims_list else ""
|
463 |
|