shivanis14 commited on
Commit
7f3a649
1 Parent(s): f7edc34

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -50
app.py CHANGED
@@ -24,66 +24,69 @@ debug_mode, client = get_openai_client()
24
  data_extractor_url = get_backend_urls()
25
 
26
  def extract_data_from_product_image(image_links, data_extractor_url):
27
- #Send product label image url to data extractor
28
- #url = data_extractor_url + "extract"
29
- #data = {
30
- # "image_links": image_links
31
- #}
32
- #try:
33
- # response = requests.post(url, json=data)
34
- # if response.status_code == 200 or response.status_code == 201:
35
- # print("POST Response:", response.json()) # Assuming JSON response
36
- # return response.json()
37
- # else:
38
- # print(f"POST Request failed with status code: {response.status_code}")
39
- # return {}
40
- #except requests.exceptions.RequestException as e:
41
- # print(f"Error occurred: {e}")
42
- # return {}
43
-
44
  response = extract_data(image_links)
45
  return response
46
 
47
  def get_product_data_from_db(product_name, data_extractor_url):
48
- #Extract data for a product by calling data extractor's API : https://data-extractor-3cn8or2tc-sonikas-projects-9936eaad.vercel.app/
49
- #url = data_extractor_url + "product"
50
- #params = {"name": product_name}
51
-
52
- #try:
53
- # response = requests.get(url, params = params)
54
- # # Check if the request was successful
55
- # if response.status_code == 200:
56
- # print("GET Response:", response.json()) # Assuming the response is JSON
57
- # return response.json()
58
- # else:
59
- # print(f"GET Request failed with status code: {response.status_code}")
60
- # return {}
61
- #except requests.exceptions.RequestException as e:
62
- # print(f"Error occurred: {e}")
63
- # return {}
64
  response = get_product(product_name)
65
  return response
66
 
67
  def get_product_list(product_name_by_user, data_extractor_url):
68
- #url = data_extractor_url + "find_product"
69
- #params = {"name": product_name_by_user}
70
-
71
- #try:
72
- # response = requests.get(url, params = params)
73
- # # Check if the request was successful
74
- # if response.status_code == 200:
75
- # print("GET Response:", response.json()) # Assuming the response is JSON
76
- # return response.json()
77
- # else:
78
- # print(f"GET Request failed with status code: {response.status_code}")
79
- # return {}
80
- #except requests.exceptions.RequestException as e:
81
- # print(f"Error occurred: {e}")
82
- # return {}
83
-
84
  response = find_product(product_name_by_user)
85
  return response
86
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87
  # Initialize assistants and vector stores
88
  # Function to initialize vector stores and assistants
89
  @st.cache_resource
@@ -384,7 +387,8 @@ def analyze_product(product_info_raw, system_prompt):
384
  claims_list = product_info_from_db.get("claims", [])
385
 
386
  if len(ingredients_list) > 0:
387
- #nutrient_analysis = analyze_nutrients(product_type, calories, sugar, salt, serving_size, added_fat)
 
388
  processing_level = analyze_processing_level(ingredients_list, brand_name, product_name, assistant1.id) if ingredients_list else ""
389
  harmful_ingredient_analysis = analyze_harmful_ingredients(ingredients_list, brand_name, product_name, assistant2.id) if ingredients_list else ""
390
  if len(claims_list) > 0:
 
24
  data_extractor_url = get_backend_urls()
25
 
26
  def extract_data_from_product_image(image_links, data_extractor_url):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  response = extract_data(image_links)
28
  return response
29
 
30
  def get_product_data_from_db(product_name, data_extractor_url):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  response = get_product(product_name)
32
  return response
33
 
34
  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 find_product_type(product_info_from_db):
39
+ #GET Response: {'_id': '6714f0487a0e96d7aae2e839',
40
+ #'brandName': 'Parle', 'claims': ['This product does not contain gold'],
41
+ #'fssaiLicenseNumbers': [10013022002253],
42
+ #'ingredients': [{'metadata': '', 'name': 'Refined Wheat Flour (Maida)', 'percent': '63%'}, {'metadata': '', 'name': 'Sugar', 'percent': ''}, {'metadata': '', 'name': 'Refined Palm Oil', 'percent': ''}, {'metadata': '(Glucose, Levulose)', 'name': 'Invert Sugar Syrup', 'percent': ''}, {'metadata': 'I', 'name': 'Sugar Citric Acid', 'percent': ''}, {'metadata': '', 'name': 'Milk Solids', 'percent': '1%'}, {'metadata': '', 'name': 'Iodised Salt', 'percent': ''}, {'metadata': '503(I), 500 (I)', 'name': 'Raising Agents', 'percent': ''}, {'metadata': '1101 (i)', 'name': 'Flour Treatment Agent', 'percent': ''}, {'metadata': 'Diacetyl Tartaric and Fatty Acid Esters of Glycerol (of Vegetable Origin)', 'name': 'Emulsifier', 'percent': ''}, {'metadata': 'Vanilla', 'name': 'Artificial Flavouring Substances', 'percent': ''}],
43
+
44
+ #'nutritionalInformation': [{'name': 'Energy', 'unit': 'kcal', 'values': [{'base': 'per 100 g','value': 462}]},
45
+ #{'name': 'Protein', 'unit': 'g', 'values': [{'base': 'per 100 g', 'value': 6.7}]},
46
+ #{'name': 'Carbohydrate', 'unit': 'g', 'values': [{'base': 'per 100 g', 'value': 76.0}, {'base': 'of which sugars', 'value': 26.9}]},
47
+ #{'name': 'Fat', 'unit': 'g', 'values': [{'base': 'per 100 g', 'value': 14.6}, {'base': 'Saturated Fat', 'value': 6.8}, {'base': 'Trans Fat', 'value': 0}]},
48
+ #{'name': 'Total Sugars', 'unit': 'g', 'values': [{'base': 'per 100 g', 'value': 27.7}]},
49
+ #{'name': 'Added Sugars', 'unit': 'g', 'values': [{'base': 'per 100 g', 'value': 26.9}]},
50
+ #{'name': 'Cholesterol', 'unit': 'mg', 'values': [{'base': 'per 100 g', 'value': 0}]},
51
+ #{'name': 'Sodium', 'unit': 'mg', 'values': [{'base': 'per 100 g', 'value': 281}]}],
52
+
53
+ #'packagingSize': {'quantity': 82, 'unit': 'g'},
54
+ #'productName': 'Parle-G Gold Biscuits',
55
+ #'servingSize': {'quantity': 18.8, 'unit': 'g'},
56
+ #'servingsPerPack': 3.98,
57
+ #'shelfLife': '7 months from packaging'}
58
+
59
+
60
+ product_type = None
61
+ calories = -1
62
+ sugar = -1
63
+ total_sugar = -1
64
+ added_sugar = -1
65
+ salt = -1
66
+ serving_size = -1
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():
78
+ total_sugar = item['values'][0]['value']
79
+ if 'added sugar' in item['name'].lower():
80
+ added_sugar = item['values'][0]['value']
81
+ if 'sugar' in item['name'].lower() and 'added sugar' not in item['name'].lower() and 'total sugar' not in item['name'].lower():
82
+ sugar = item['values'][0]['value']
83
+
84
+ #How to get Salt?
85
+ if added_sugar > 0 and sugar == -1:
86
+ sugar = added_sugar
87
+ elif total_sugar > 0 and added_sugar == -1 and sugar == -1:
88
+ sugar = total_sugar
89
+ return product_type, calories, sugar, salt, serving_size
90
  # Initialize assistants and vector stores
91
  # Function to initialize vector stores and assistants
92
  @st.cache_resource
 
387
  claims_list = product_info_from_db.get("claims", [])
388
 
389
  if len(ingredients_list) > 0:
390
+ product_type, calories, sugar, salt, serving_size = find_product_type(product_info_from_db)
391
+ nutrient_analysis = analyze_nutrients(product_type, calories, sugar, salt, serving_size)
392
  processing_level = analyze_processing_level(ingredients_list, brand_name, product_name, assistant1.id) if ingredients_list else ""
393
  harmful_ingredient_analysis = analyze_harmful_ingredients(ingredients_list, brand_name, product_name, assistant2.id) if ingredients_list else ""
394
  if len(claims_list) > 0: