shivanis14 commited on
Commit
7381e90
1 Parent(s): 5760d4f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -10
app.py CHANGED
@@ -178,7 +178,6 @@ def find_product_nutrients(product_info_from_db):
178
  if 'sodium' in item['name'].lower():
179
  salt += item['values'][0]['value']
180
 
181
- #How to get Salt?
182
  if added_sugar is not None and added_sugar > 0 and sugar is None:
183
  sugar = added_sugar
184
  elif total_sugar is not None and total_sugar > 0 and added_sugar is None and sugar is None:
@@ -355,9 +354,24 @@ def analyze_processing_level(ingredients, assistant_id):
355
  assistant_id=assistant_id,
356
  include=["step_details.tool_calls[*].file_search.results[*].content"]
357
  )
358
-
359
- messages = list(client.beta.threads.messages.list(thread_id=thread.id, run_id=run.id))
360
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
361
  message_content = messages[0].content[0].text
362
  annotations = message_content.annotations
363
  #citations = []
@@ -388,8 +402,24 @@ def analyze_harmful_ingredients(ingredients, assistant_id):
388
  assistant_id=assistant_id,
389
  include=["step_details.tool_calls[*].file_search.results[*].content"]
390
  )
391
-
392
- messages = list(client.beta.threads.messages.list(thread_id=thread.id, run_id=run.id))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
393
  message_content = messages[0].content[0].text
394
  annotations = message_content.annotations
395
 
@@ -444,8 +474,23 @@ The output must be in JSON format as follows:
444
  include=["step_details.tool_calls[*].file_search.results[*].content"]
445
  )
446
 
447
- messages = list(client.beta.threads.messages.list(thread_id=thread.id, run_id=run.id))
448
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
449
  message_content = messages[0].content[0].text
450
 
451
 
@@ -534,10 +579,11 @@ Claims Analysis:
534
  ]
535
  )
536
 
537
- return f"{brand_name} {product_name} -- " + completion.choices[0].message.content
538
 
539
 
540
  def analyze_product(product_info_raw, system_prompt):
 
541
  global assistant1, assistant2, assistant3
542
 
543
  if product_info_raw != "{}":
@@ -559,6 +605,10 @@ def analyze_product(product_info_raw, system_prompt):
559
  if nutritional_information:
560
  product_type, calories, sugar, salt, serving_size = find_product_nutrients(product_info_from_db)
561
  nutrient_analysis = analyze_nutrients(product_type, calories, sugar, salt, serving_size)
 
 
 
 
562
  print(f"DEBUG ! nutrient analysis is {nutrient_analysis}")
563
 
564
  nutrient_analysis_rda_data = rda_analysis(nutritional_information, serving_size)
@@ -566,8 +616,7 @@ def analyze_product(product_info_raw, system_prompt):
566
  print(f"DEBUG : nutrient_analysis_rda_data['nutritionPerServing'] : {nutrient_analysis_rda_data['nutritionPerServing']}")
567
  print(f"DEBUG : nutrient_analysis_rda_data['userServingSize'] : {nutrient_analysis_rda_data['userServingSize']}")
568
 
569
- if nutrient_analysis_rda_data:
570
- nutrient_analysis_rda = find_nutrition(nutrient_analysis_rda_data)
571
  print(f"DEBUG ! RDA nutrient analysis is {nutrient_analysis_rda}")
572
 
573
  #Call GPT for nutrient analysis
@@ -581,6 +630,7 @@ def analyze_product(product_info_raw, system_prompt):
581
  claims_analysis = analyze_claims(claims_list, ingredients_list, assistant3.id) if claims_list else ""
582
 
583
  final_analysis = generate_final_analysis(brand_name, product_name, nutritional_level, processing_level, harmful_ingredient_analysis, claims_analysis, system_prompt)
 
584
  return final_analysis
585
  else:
586
  return "I'm sorry, product information could not be extracted from the url."
@@ -612,6 +662,8 @@ def chatbot_response(image_urls_str, product_name_by_user, data_extractor_url, s
612
  with st.spinner("Analyzing the product... This may take a moment."):
613
  product_info_raw = get_product_data_from_db(product_name_by_user, data_extractor_url)
614
  print(f"DEBUG product_info_raw from name: {product_info_raw}")
 
 
615
  if 'error' not in json.loads(product_info_raw).keys():
616
  final_analysis = analyze_product(product_info_raw, system_prompt)
617
  return [], final_analysis
 
178
  if 'sodium' in item['name'].lower():
179
  salt += item['values'][0]['value']
180
 
 
181
  if added_sugar is not None and added_sugar > 0 and sugar is None:
182
  sugar = added_sugar
183
  elif total_sugar is not None and total_sugar > 0 and added_sugar is None and sugar is None:
 
354
  assistant_id=assistant_id,
355
  include=["step_details.tool_calls[*].file_search.results[*].content"]
356
  )
 
 
357
 
358
+ # Polling loop to wait for a response in the thread
359
+ messages = []
360
+ max_retries = 10 # You can set a maximum retry limit
361
+ retries = 0
362
+ wait_time = 2 # Seconds to wait between retries
363
+
364
+ while retries < max_retries:
365
+ messages = list(client.beta.threads.messages.list(thread_id=thread.id, run_id=run.id))
366
+ if messages: # If we receive any messages, break the loop
367
+ break
368
+ retries += 1
369
+ time.sleep(wait_time)
370
+
371
+ # Check if we got the message content
372
+ if not messages:
373
+ raise TimeoutError("Processing Claims : No messages were returned after polling.")
374
+
375
  message_content = messages[0].content[0].text
376
  annotations = message_content.annotations
377
  #citations = []
 
402
  assistant_id=assistant_id,
403
  include=["step_details.tool_calls[*].file_search.results[*].content"]
404
  )
405
+
406
+ # Polling loop to wait for a response in the thread
407
+ messages = []
408
+ max_retries = 10 # You can set a maximum retry limit
409
+ retries = 0
410
+ wait_time = 2 # Seconds to wait between retries
411
+
412
+ while retries < max_retries:
413
+ messages = list(client.beta.threads.messages.list(thread_id=thread.id, run_id=run.id))
414
+ if messages: # If we receive any messages, break the loop
415
+ break
416
+ retries += 1
417
+ time.sleep(wait_time)
418
+
419
+ # Check if we got the message content
420
+ if not messages:
421
+ raise TimeoutError("Processing Claims : No messages were returned after polling.")
422
+
423
  message_content = messages[0].content[0].text
424
  annotations = message_content.annotations
425
 
 
474
  include=["step_details.tool_calls[*].file_search.results[*].content"]
475
  )
476
 
477
+ # Polling loop to wait for a response in the thread
478
+ messages = []
479
+ max_retries = 10 # You can set a maximum retry limit
480
+ retries = 0
481
+ wait_time = 2 # Seconds to wait between retries
482
+
483
+ while retries < max_retries:
484
+ messages = list(client.beta.threads.messages.list(thread_id=thread.id, run_id=run.id))
485
+ if messages: # If we receive any messages, break the loop
486
+ break
487
+ retries += 1
488
+ time.sleep(wait_time)
489
+
490
+ # Check if we got the message content
491
+ if not messages:
492
+ raise TimeoutError("Processing Claims : No messages were returned after polling.")
493
+
494
  message_content = messages[0].content[0].text
495
 
496
 
 
579
  ]
580
  )
581
 
582
+ return f"Brand: {brand_name}\n\nProduct: {product_name}\n\nAnalysis:\n\n{completion.choices[0].message.content}"
583
 
584
 
585
  def analyze_product(product_info_raw, system_prompt):
586
+
587
  global assistant1, assistant2, assistant3
588
 
589
  if product_info_raw != "{}":
 
605
  if nutritional_information:
606
  product_type, calories, sugar, salt, serving_size = find_product_nutrients(product_info_from_db)
607
  nutrient_analysis = analyze_nutrients(product_type, calories, sugar, salt, serving_size)
608
+ if product_type is not None and serving_size is not None:
609
+ nutrient_analysis = analyze_nutrients(product_type, calories, sugar, salt, serving_size)
610
+ else:
611
+ return "product not found because product information in the db is corrupt"
612
  print(f"DEBUG ! nutrient analysis is {nutrient_analysis}")
613
 
614
  nutrient_analysis_rda_data = rda_analysis(nutritional_information, serving_size)
 
616
  print(f"DEBUG : nutrient_analysis_rda_data['nutritionPerServing'] : {nutrient_analysis_rda_data['nutritionPerServing']}")
617
  print(f"DEBUG : nutrient_analysis_rda_data['userServingSize'] : {nutrient_analysis_rda_data['userServingSize']}")
618
 
619
+ nutrient_analysis_rda = find_nutrition(nutrient_analysis_rda_data)
 
620
  print(f"DEBUG ! RDA nutrient analysis is {nutrient_analysis_rda}")
621
 
622
  #Call GPT for nutrient analysis
 
630
  claims_analysis = analyze_claims(claims_list, ingredients_list, assistant3.id) if claims_list else ""
631
 
632
  final_analysis = generate_final_analysis(brand_name, product_name, nutritional_level, processing_level, harmful_ingredient_analysis, claims_analysis, system_prompt)
633
+
634
  return final_analysis
635
  else:
636
  return "I'm sorry, product information could not be extracted from the url."
 
662
  with st.spinner("Analyzing the product... This may take a moment."):
663
  product_info_raw = get_product_data_from_db(product_name_by_user, data_extractor_url)
664
  print(f"DEBUG product_info_raw from name: {product_info_raw}")
665
+ if product_info_raw == "{}":
666
+ return [], "product not found because product information in the db is corrupt"
667
  if 'error' not in json.loads(product_info_raw).keys():
668
  final_analysis = analyze_product(product_info_raw, system_prompt)
669
  return [], final_analysis