Spaces:
Running
Running
Prathamesh Sable
commited on
Commit
·
cb44dfc
1
Parent(s):
c43a7a2
added recommendation and removed ingredient info
Browse files- interfaces/productModels.py +7 -1
- utils/analysis_utils.py +48 -2
interfaces/productModels.py
CHANGED
|
@@ -37,7 +37,7 @@ class SafetyInfo(BaseModel):
|
|
| 37 |
|
| 38 |
class IngredientInfo(BaseModel):
|
| 39 |
ingredients_list: List[str] = []
|
| 40 |
-
ingredients_analysis: List[Dict[str, Any]] = []
|
| 41 |
ingredient_count: int = 0
|
| 42 |
|
| 43 |
class AllergenInfo(BaseModel):
|
|
@@ -49,6 +49,11 @@ class DietaryInfo(BaseModel):
|
|
| 49 |
is_vegetarian: bool = False
|
| 50 |
is_vegan: bool = False
|
| 51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
class ProductAnalysisResponse(BaseModel):
|
| 53 |
"""Response model for product analysis by marker ID"""
|
| 54 |
found: bool = Field(..., description="Whether the product was found")
|
|
@@ -57,6 +62,7 @@ class ProductAnalysisResponse(BaseModel):
|
|
| 57 |
ingredient_info: IngredientInfo = Field(..., description="Information about ingredients")
|
| 58 |
allergen_info: AllergenInfo = Field(..., description="Information about allergens")
|
| 59 |
dietary_info: DietaryInfo = Field(..., description="Dietary information")
|
|
|
|
| 60 |
timestamp: str = Field(..., description="Timestamp of the response")
|
| 61 |
|
| 62 |
|
|
|
|
| 37 |
|
| 38 |
class IngredientInfo(BaseModel):
|
| 39 |
ingredients_list: List[str] = []
|
| 40 |
+
# ingredients_analysis: List[Dict[str, Any]] = []
|
| 41 |
ingredient_count: int = 0
|
| 42 |
|
| 43 |
class AllergenInfo(BaseModel):
|
|
|
|
| 49 |
is_vegetarian: bool = False
|
| 50 |
is_vegan: bool = False
|
| 51 |
|
| 52 |
+
class RecommendationsInfo(BaseModel):
|
| 53 |
+
usage_recommendations: str = ""
|
| 54 |
+
ingredient_interactions: List[str] = []
|
| 55 |
+
key_takeaway: str = ""
|
| 56 |
+
|
| 57 |
class ProductAnalysisResponse(BaseModel):
|
| 58 |
"""Response model for product analysis by marker ID"""
|
| 59 |
found: bool = Field(..., description="Whether the product was found")
|
|
|
|
| 62 |
ingredient_info: IngredientInfo = Field(..., description="Information about ingredients")
|
| 63 |
allergen_info: AllergenInfo = Field(..., description="Information about allergens")
|
| 64 |
dietary_info: DietaryInfo = Field(..., description="Dietary information")
|
| 65 |
+
recommendations_info: RecommendationsInfo = Field(..., description="Recommendations and key insights")
|
| 66 |
timestamp: str = Field(..., description="Timestamp of the response")
|
| 67 |
|
| 68 |
|
utils/analysis_utils.py
CHANGED
|
@@ -97,6 +97,42 @@ def format_product_analysis_response(product):
|
|
| 97 |
except Exception as e:
|
| 98 |
log_error(f"Error parsing ingredients analysis: {e}")
|
| 99 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
# Construct the final response
|
| 101 |
return ProductAnalysisResponse(
|
| 102 |
found=True,
|
|
@@ -116,7 +152,7 @@ def format_product_analysis_response(product):
|
|
| 116 |
},
|
| 117 |
ingredient_info={
|
| 118 |
"ingredients_list": ingredients_list,
|
| 119 |
-
"ingredients_analysis": ingredients_analysis,
|
| 120 |
"ingredient_count": getattr(product, 'ingredients_count', 0)
|
| 121 |
},
|
| 122 |
allergen_info={
|
|
@@ -128,6 +164,11 @@ def format_product_analysis_response(product):
|
|
| 128 |
"is_vegetarian": any(flag.lower() == 'vegetarian' for flag in dietary_flags),
|
| 129 |
"is_vegan": any(flag.lower() == 'vegan' for flag in dietary_flags)
|
| 130 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
timestamp=datetime.now(tz=pytz.timezone('Asia/Kolkata')).isoformat()
|
| 132 |
)
|
| 133 |
except Exception as e:
|
|
@@ -151,7 +192,7 @@ def format_product_analysis_response(product):
|
|
| 151 |
},
|
| 152 |
ingredient_info={
|
| 153 |
"ingredients_list": [],
|
| 154 |
-
"ingredients_analysis": [],
|
| 155 |
"ingredient_count": 0
|
| 156 |
},
|
| 157 |
allergen_info={
|
|
@@ -163,5 +204,10 @@ def format_product_analysis_response(product):
|
|
| 163 |
"is_vegetarian": False,
|
| 164 |
"is_vegan": False
|
| 165 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
timestamp=datetime.now(tz=pytz.timezone('Asia/Kolkata')).isoformat()
|
| 167 |
)
|
|
|
|
| 97 |
except Exception as e:
|
| 98 |
log_error(f"Error parsing ingredients analysis: {e}")
|
| 99 |
|
| 100 |
+
# Parse ingredient interactions
|
| 101 |
+
ingredient_interactions = []
|
| 102 |
+
try:
|
| 103 |
+
interactions_str = getattr(product, 'ingredient_interactions', None)
|
| 104 |
+
if interactions_str:
|
| 105 |
+
ingredient_interactions = safe_parse_json(interactions_str, [])
|
| 106 |
+
except Exception as e:
|
| 107 |
+
log_error(f"Error parsing ingredient interactions: {e}")
|
| 108 |
+
ingredient_interactions = []
|
| 109 |
+
|
| 110 |
+
# Get usage recommendations
|
| 111 |
+
usage_recommendations = ""
|
| 112 |
+
try:
|
| 113 |
+
usage_recommendations = getattr(product, 'usage_recommendations', "")
|
| 114 |
+
if usage_recommendations and isinstance(usage_recommendations, str):
|
| 115 |
+
if usage_recommendations.startswith('"') and usage_recommendations.endswith('"'):
|
| 116 |
+
usage_recommendations = safe_parse_json(usage_recommendations, "")
|
| 117 |
+
if not isinstance(usage_recommendations, str):
|
| 118 |
+
usage_recommendations = str(usage_recommendations)
|
| 119 |
+
except Exception as e:
|
| 120 |
+
log_error(f"Error parsing usage recommendations: {e}")
|
| 121 |
+
usage_recommendations = ""
|
| 122 |
+
|
| 123 |
+
# Get key takeaway
|
| 124 |
+
key_takeaway = ""
|
| 125 |
+
try:
|
| 126 |
+
key_takeaway = getattr(product, 'key_takeaway', "")
|
| 127 |
+
if key_takeaway and isinstance(key_takeaway, str):
|
| 128 |
+
if key_takeaway.startswith('"') and key_takeaway.endswith('"'):
|
| 129 |
+
key_takeaway = safe_parse_json(key_takeaway, "")
|
| 130 |
+
if not isinstance(key_takeaway, str):
|
| 131 |
+
key_takeaway = str(key_takeaway)
|
| 132 |
+
except Exception as e:
|
| 133 |
+
log_error(f"Error parsing key takeaway: {e}")
|
| 134 |
+
key_takeaway = ""
|
| 135 |
+
|
| 136 |
# Construct the final response
|
| 137 |
return ProductAnalysisResponse(
|
| 138 |
found=True,
|
|
|
|
| 152 |
},
|
| 153 |
ingredient_info={
|
| 154 |
"ingredients_list": ingredients_list,
|
| 155 |
+
# "ingredients_analysis": ingredients_analysis,
|
| 156 |
"ingredient_count": getattr(product, 'ingredients_count', 0)
|
| 157 |
},
|
| 158 |
allergen_info={
|
|
|
|
| 164 |
"is_vegetarian": any(flag.lower() == 'vegetarian' for flag in dietary_flags),
|
| 165 |
"is_vegan": any(flag.lower() == 'vegan' for flag in dietary_flags)
|
| 166 |
},
|
| 167 |
+
recommendations_info={
|
| 168 |
+
"usage_recommendations": usage_recommendations,
|
| 169 |
+
"ingredient_interactions": ingredient_interactions,
|
| 170 |
+
"key_takeaway": key_takeaway
|
| 171 |
+
},
|
| 172 |
timestamp=datetime.now(tz=pytz.timezone('Asia/Kolkata')).isoformat()
|
| 173 |
)
|
| 174 |
except Exception as e:
|
|
|
|
| 192 |
},
|
| 193 |
ingredient_info={
|
| 194 |
"ingredients_list": [],
|
| 195 |
+
# "ingredients_analysis": [],
|
| 196 |
"ingredient_count": 0
|
| 197 |
},
|
| 198 |
allergen_info={
|
|
|
|
| 204 |
"is_vegetarian": False,
|
| 205 |
"is_vegan": False
|
| 206 |
},
|
| 207 |
+
recommendations_info={
|
| 208 |
+
"usage_recommendations": "",
|
| 209 |
+
"ingredient_interactions": [],
|
| 210 |
+
"key_takeaway": ""
|
| 211 |
+
},
|
| 212 |
timestamp=datetime.now(tz=pytz.timezone('Asia/Kolkata')).isoformat()
|
| 213 |
)
|