shivanis14 commited on
Commit
6980695
1 Parent(s): 5e09799

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -22
app.py CHANGED
@@ -36,40 +36,57 @@ def get_product_list(product_name_by_user, data_extractor_url):
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'],
 
36
  response = find_product(product_name_by_user)
37
  return response
38
 
39
+
40
  def rda_analysis(product_info_from_db_nutritionalInformation, product_info_from_db_servingSize):
41
+ data_for_rda_analysis = {'nutritionPerServing': {}, 'userServingSize': None}
42
+ nutrient_name_list = ['energy', 'protein', 'carbohydrates', 'addedSugars', 'dietaryFiber', 'totalFat',
43
+ 'saturatedFat', 'monounsaturatedFat', 'polyunsaturatedFat', 'transFat', 'sodium']
44
 
45
+ # Using the response_format to request output in structured JSON format
46
  completion = client.chat.completions.create(
47
  model="gpt-4o",
48
  messages=[
49
+ {"role": "system", "content": "You will be given nutritional information of a food product."},
50
+ {"role": "user", "content": f"Nutritional content of food product is {json.dumps(product_info_from_db_nutritionalInformation)}. Extract the values of the following nutrients: {', '.join(nutrient_name_list)}."}
51
+ ],
52
+ response_format="json", # This specifies we want structured JSON in return
53
+ functions=[
54
+ {
55
+ "name": "rda_analysis",
56
+ "description": "Return the extracted nutrient information in the correct format",
57
+ "parameters": {
58
+ "type": "object",
59
+ "properties": {
60
+ "energy": {"type": "number"},
61
+ "protein": {"type": "number"},
62
+ "carbohydrates": {"type": "number"},
63
+ "addedSugars": {"type": "number"},
64
+ "dietaryFiber": {"type": "number"},
65
+ "totalFat": {"type": "number"},
66
+ "saturatedFat": {"type": "number"},
67
+ "monounsaturatedFat": {"type": "number"},
68
+ "polyunsaturatedFat": {"type": "number"},
69
+ "transFat": {"type": "number"},
70
+ "sodium": {"type": "number"},
71
+ "servingSize": {"type": "number"} # Ensures you get serving size too
72
+ },
73
+ "required": nutrient_name_list + ["servingSize"]
74
+ }
75
+ }
76
  ]
77
  )
78
 
79
+ data_for_rda_analysis_part_1 = completion.choices[0].message["content"]
80
+ print(f"data_for_rda_analysis_part_1: {data_for_rda_analysis_part_1}")
81
 
82
+ data_for_rda_analysis = {
83
+ 'nutritionPerServing': data_for_rda_analysis_part_1,
84
+ 'userServingSize': product_info_from_db_servingSize
85
+ }
86
 
87
  return data_for_rda_analysis
88
 
89
+
90
  def find_product_nutrients(product_info_from_db):
91
  #GET Response: {'_id': '6714f0487a0e96d7aae2e839',
92
  #'brandName': 'Parle', 'claims': ['This product does not contain gold'],