shivanis14 commited on
Commit
cc89461
1 Parent(s): cfe23fd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -5
app.py CHANGED
@@ -283,6 +283,37 @@ def initialize_assistants_and_vector_stores():
283
 
284
  assistant1, assistant2, assistant3 = initialize_assistants_and_vector_stores()
285
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
286
  def analyze_processing_level(ingredients, brand_name, product_name, assistant_id):
287
  global debug_mode, client
288
  thread = client.beta.threads.create(
@@ -410,7 +441,7 @@ def analyze_claims(claims, ingredients, product_name, assistant_id):
410
 
411
  return claims_analysis_str
412
 
413
- def generate_final_analysis(brand_name, product_name, nutrient_analysis, nutrient_analysis_rda, processing_level, harmful_ingredient_analysis, claims_analysis, system_prompt):
414
  global debug_mode, client
415
  system_prompt_orig = """You are provided with a detailed analysis of a food product. Your task is to generate actionable insights to help the user decide whether to consume the product, at what frequency, and identify any potential harms or benefits. Consider the context of consumption to ensure the advice is personalized and practical.
416
 
@@ -445,8 +476,7 @@ Additionally, consider the following while generating insights:
445
  Product Name: {brand_name} {product_name}
446
 
447
  Nutrition Analysis :
448
- {nutrient_analysis}
449
- {nutrient_analysis_rda}
450
 
451
  Processing Level:
452
  {processing_level}
@@ -495,7 +525,9 @@ def analyze_product(product_info_raw, system_prompt):
495
 
496
  nutrient_analysis_rda = find_nutrition(nutrient_analysis_rda_data)
497
  print(f"DEBUG ! RDA nutrient analysis is {nutrient_analysis_rda}")
498
-
 
 
499
  if len(ingredients_list) > 0:
500
  processing_level = analyze_processing_level(ingredients_list, brand_name, product_name, assistant1.id) if ingredients_list else ""
501
  harmful_ingredient_analysis = analyze_harmful_ingredients(ingredients_list, brand_name, product_name, assistant2.id) if ingredients_list else ""
@@ -503,7 +535,7 @@ def analyze_product(product_info_raw, system_prompt):
503
  if len(claims_list) > 0:
504
  claims_analysis = analyze_claims(claims_list, ingredients_list, product_name, assistant3.id) if claims_list else ""
505
 
506
- final_analysis = generate_final_analysis(brand_name, product_name, nutrient_analysis, nutrient_analysis_rda, processing_level, harmful_ingredient_analysis, claims_analysis, system_prompt)
507
  return final_analysis
508
  else:
509
  return "I'm sorry, product information could not be extracted from the url."
 
283
 
284
  assistant1, assistant2, assistant3 = initialize_assistants_and_vector_stores()
285
 
286
+ def analyze_nutrition_icmr_rda(nutrient_analysis, nutrient_analysis_rda):
287
+ global debug_mode, client
288
+ system_prompt = """Analyze the nutritional content of the food item, focusing on nutrients that significantly exceed the Recommended Daily Allowance (RDA) or threshold limits defined by ICMR.
289
+ Provide contextual insights for users as explained below:
290
+
291
+ Calories: If the calorie content is very high, compare it to a well-balanced nutritional meal. Indicate how many such meals' worth of calories the product contains.
292
+ Sugar & Salt: Present the amounts in teaspoons to make it easier to understand daily intake levels.
293
+ Fat & Calories: Offer practical context for these nutrients, explaining the implications of their levels and how they relate to balanced eating.
294
+
295
+ Ensure the response is clear, accurate, and provides actionable recommendations for healthier choices."""
296
+
297
+ user_prompt = f"""
298
+ Product Name: {brand_name} {product_name}
299
+
300
+ Nutrition Analysis :
301
+ {nutrient_analysis}
302
+ {nutrient_analysis_rda}
303
+ """
304
+ if debug_mode:
305
+ print(f"\nuser_prompt : \n {user_prompt}")
306
+
307
+ completion = client.chat.completions.create(
308
+ model="gpt-4o", # Make sure to use an appropriate model
309
+ messages=[
310
+ {"role": "system", "content": system_prompt},
311
+ {"role": "user", "content": user_prompt}
312
+ ]
313
+ )
314
+
315
+ return completion.choices[0].message.content
316
+
317
  def analyze_processing_level(ingredients, brand_name, product_name, assistant_id):
318
  global debug_mode, client
319
  thread = client.beta.threads.create(
 
441
 
442
  return claims_analysis_str
443
 
444
+ def generate_final_analysis(brand_name, product_name, nutritional_level, processing_level, harmful_ingredient_analysis, claims_analysis, system_prompt):
445
  global debug_mode, client
446
  system_prompt_orig = """You are provided with a detailed analysis of a food product. Your task is to generate actionable insights to help the user decide whether to consume the product, at what frequency, and identify any potential harms or benefits. Consider the context of consumption to ensure the advice is personalized and practical.
447
 
 
476
  Product Name: {brand_name} {product_name}
477
 
478
  Nutrition Analysis :
479
+ {nutritional_level}
 
480
 
481
  Processing Level:
482
  {processing_level}
 
525
 
526
  nutrient_analysis_rda = find_nutrition(nutrient_analysis_rda_data)
527
  print(f"DEBUG ! RDA nutrient analysis is {nutrient_analysis_rda}")
528
+ #Call GPT for nutrient analysis
529
+ nutritional_level = analyze_nutrition_icmr_rda(nutrient_analysis, nutrient_analysis_rda)
530
+
531
  if len(ingredients_list) > 0:
532
  processing_level = analyze_processing_level(ingredients_list, brand_name, product_name, assistant1.id) if ingredients_list else ""
533
  harmful_ingredient_analysis = analyze_harmful_ingredients(ingredients_list, brand_name, product_name, assistant2.id) if ingredients_list else ""
 
535
  if len(claims_list) > 0:
536
  claims_analysis = analyze_claims(claims_list, ingredients_list, product_name, assistant3.id) if claims_list else ""
537
 
538
+ final_analysis = generate_final_analysis(brand_name, product_name, nutritional_level, processing_level, harmful_ingredient_analysis, claims_analysis, system_prompt)
539
  return final_analysis
540
  else:
541
  return "I'm sorry, product information could not be extracted from the url."