Spaces:
Running
Running
Commit
·
7d533c9
1
Parent(s):
718887f
bug fix
Browse files- interfaces/productModels.py +38 -1
- routers/analysis.py +2 -1
- services/analysis_service.py +5 -3
- utils/analysis_utils.py +36 -43
interfaces/productModels.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
from typing import List, Dict
|
| 2 |
from pydantic import BaseModel
|
| 3 |
from datetime import datetime
|
| 4 |
|
|
@@ -21,4 +21,41 @@ class ProductCreate(BaseModel):
|
|
| 21 |
timestamp: datetime
|
| 22 |
ingredient_ids: List[int]|str
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
|
|
|
| 1 |
+
from typing import List, Dict, Optional
|
| 2 |
from pydantic import BaseModel
|
| 3 |
from datetime import datetime
|
| 4 |
|
|
|
|
| 21 |
timestamp: datetime
|
| 22 |
ingredient_ids: List[int]|str
|
| 23 |
|
| 24 |
+
class SafetyScore(BaseModel):
|
| 25 |
+
isPresent: bool
|
| 26 |
+
value: Optional[int] = None
|
| 27 |
+
|
| 28 |
+
class ProductInfo(BaseModel):
|
| 29 |
+
id: Optional[int] = None
|
| 30 |
+
name: Optional[str] = None
|
| 31 |
+
barcode: Optional[str] = None
|
| 32 |
+
image_url: Optional[str] = None
|
| 33 |
+
brand: Optional[str] = None
|
| 34 |
+
manufacturing_places: Optional[str] = None
|
| 35 |
+
stores: Optional[str] = None
|
| 36 |
+
countries: Optional[str] = None
|
| 37 |
+
|
| 38 |
+
class IngredientInfo(BaseModel):
|
| 39 |
+
ingredients_text: Optional[str] = None
|
| 40 |
+
ingredients_analysis: Optional[List[Dict[str, Any]]] = None # Adjust type if analysis has a specific structure
|
| 41 |
+
additives: Optional[List[str]] = None
|
| 42 |
+
|
| 43 |
+
class AllergenInfo(BaseModel):
|
| 44 |
+
allergens: Optional[List[str]] = None
|
| 45 |
+
traces: Optional[List[str]] = None
|
| 46 |
+
|
| 47 |
+
class DietInfo(BaseModel):
|
| 48 |
+
vegan: Optional[bool] = None
|
| 49 |
+
vegetarian: Optional[bool] = None
|
| 50 |
+
|
| 51 |
+
class ProductAnalysisResponse(BaseModel):
|
| 52 |
+
found: bool
|
| 53 |
+
safety_score: SafetyScore
|
| 54 |
+
product_info: Optional[ProductInfo] = None
|
| 55 |
+
ingredient_info: Optional[IngredientInfo] = None
|
| 56 |
+
allergen_info: Optional[AllergenInfo] = None
|
| 57 |
+
diet_info: Optional[DietInfo] = None
|
| 58 |
+
nutritional_info: Optional[Dict[str, Any]] = None # Adjust type if nutritional info has a specific structure
|
| 59 |
+
manufacturing_info: Optional[Dict[str, Any]] = None # Adjust type if manufacturing info has a specific structure
|
| 60 |
+
|
| 61 |
|
routers/analysis.py
CHANGED
|
@@ -8,7 +8,7 @@ import pytz
|
|
| 8 |
from sqlalchemy.orm import Session
|
| 9 |
from typing import List, Dict, Any
|
| 10 |
from db.models import User, Ingredient
|
| 11 |
-
from interfaces.ingredientModels import IngredientAnalysisResult, IngredientRequest
|
| 12 |
from interfaces.productModels import ProductIngredientsRequest
|
| 13 |
from logger_manager import log_info, log_error
|
| 14 |
from db.database import get_db,SessionLocal
|
|
@@ -19,6 +19,7 @@ from services.ingredientFinderAgent import IngredientInfoAgentLangGraph
|
|
| 19 |
from services.productAnalyzerAgent import analyze_product_ingredients
|
| 20 |
from services.auth_service import get_current_user
|
| 21 |
from utils.db_utils import ingredient_db_to_pydantic
|
|
|
|
| 22 |
from services.analysis_service import get_product_data_by_marker_id as get_analysis_service_data
|
| 23 |
from utils.ingredient_utils import process_single_ingredient
|
| 24 |
|
|
|
|
| 8 |
from sqlalchemy.orm import Session
|
| 9 |
from typing import List, Dict, Any
|
| 10 |
from db.models import User, Ingredient
|
| 11 |
+
from interfaces.ingredientModels import IngredientAnalysisResult, IngredientRequest
|
| 12 |
from interfaces.productModels import ProductIngredientsRequest
|
| 13 |
from logger_manager import log_info, log_error
|
| 14 |
from db.database import get_db,SessionLocal
|
|
|
|
| 19 |
from services.productAnalyzerAgent import analyze_product_ingredients
|
| 20 |
from services.auth_service import get_current_user
|
| 21 |
from utils.db_utils import ingredient_db_to_pydantic
|
| 22 |
+
from interfaces.productModels import ProductAnalysisResponse
|
| 23 |
from services.analysis_service import get_product_data_by_marker_id as get_analysis_service_data
|
| 24 |
from utils.ingredient_utils import process_single_ingredient
|
| 25 |
|
services/analysis_service.py
CHANGED
|
@@ -2,8 +2,10 @@ from sqlalchemy.orm import Session
|
|
| 2 |
from db.models import Marker, Product
|
| 3 |
from utils.analysis_utils import format_product_analysis_response
|
| 4 |
from utils.logger_manager import log_info, log_error
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
def get_product_data_by_marker_id(db: Session, target_id: str):
|
| 7 |
"""
|
| 8 |
Retrieves product analysis and ingredient information by marker ID.
|
| 9 |
|
|
@@ -12,8 +14,8 @@ def get_product_data_by_marker_id(db: Session, target_id: str):
|
|
| 12 |
target_id: The target ID from the marker table.
|
| 13 |
|
| 14 |
Returns:
|
| 15 |
-
A
|
| 16 |
-
|
| 17 |
"""
|
| 18 |
log_info(f"Attempting to retrieve product data for marker ID: {target_id}")
|
| 19 |
try:
|
|
|
|
| 2 |
from db.models import Marker, Product
|
| 3 |
from utils.analysis_utils import format_product_analysis_response
|
| 4 |
from utils.logger_manager import log_info, log_error
|
| 5 |
+
from interfaces.productModels import ProductAnalysisResponse
|
| 6 |
+
from typing import Optional
|
| 7 |
|
| 8 |
+
def get_product_data_by_marker_id(db: Session, target_id: str) -> Optional[ProductAnalysisResponse]:
|
| 9 |
"""
|
| 10 |
Retrieves product analysis and ingredient information by marker ID.
|
| 11 |
|
|
|
|
| 14 |
target_id: The target ID from the marker table.
|
| 15 |
|
| 16 |
Returns:
|
| 17 |
+
A ProductAnalysisResponse object or None if no product is found.
|
| 18 |
+
|
| 19 |
"""
|
| 20 |
log_info(f"Attempting to retrieve product data for marker ID: {target_id}")
|
| 21 |
try:
|
utils/analysis_utils.py
CHANGED
|
@@ -1,61 +1,54 @@
|
|
| 1 |
-
from typing import Dict, Any, Optional
|
|
|
|
| 2 |
|
| 3 |
-
def format_product_analysis_response(product_data) ->
|
| 4 |
"""
|
| 5 |
Formats the retrieved product analysis data into a consistent response structure.
|
| 6 |
|
| 7 |
Args:
|
| 8 |
-
product_data: A
|
| 9 |
|
| 10 |
Returns:
|
| 11 |
-
A
|
| 12 |
"""
|
| 13 |
if product_data is None:
|
| 14 |
-
return
|
| 15 |
-
"found": False,
|
| 16 |
-
"safety_score": {"isPresent": False, "value": None},
|
| 17 |
-
"product_info": None,
|
| 18 |
-
"ingredient_info": None,
|
| 19 |
-
"allergen_info": None,
|
| 20 |
-
"diet_info": None,
|
| 21 |
-
"nutritional_info": None,
|
| 22 |
-
"manufacturing_info": None,
|
| 23 |
-
}
|
| 24 |
|
| 25 |
-
|
|
|
|
| 26 |
safety_score_isPresent = safety_score_value is not None
|
| 27 |
|
| 28 |
-
return
|
| 29 |
-
|
| 30 |
-
|
| 31 |
"isPresent": safety_score_isPresent,
|
| 32 |
"value": safety_score_value,
|
| 33 |
},
|
| 34 |
-
|
| 35 |
"id": product_data.id,
|
| 36 |
-
"name": product_data.
|
| 37 |
-
"barcode":
|
| 38 |
-
"image_url":
|
| 39 |
-
"brand":
|
| 40 |
-
"manufacturing_places":
|
| 41 |
-
"stores":
|
| 42 |
-
"countries":
|
| 43 |
-
}
|
| 44 |
-
|
| 45 |
-
"ingredients_text": product_data.
|
| 46 |
-
"ingredients_analysis": product_data.
|
| 47 |
-
"additives":
|
| 48 |
-
}
|
| 49 |
-
|
| 50 |
-
"allergens": product_data.
|
| 51 |
-
"traces":
|
| 52 |
-
}
|
| 53 |
-
|
| 54 |
-
"vegan":
|
| 55 |
-
"vegetarian":
|
| 56 |
-
}
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
|
| 61 |
# Add other helper functions as needed for analysis
|
|
|
|
| 1 |
+
from typing import Dict, Any, Optional, Any
|
| 2 |
+
from interfaces.productModels import ProductAnalysisResponse
|
| 3 |
|
| 4 |
+
def format_product_analysis_response(product_data: Optional[Any]) -> Optional[ProductAnalysisResponse]:
|
| 5 |
"""
|
| 6 |
Formats the retrieved product analysis data into a consistent response structure.
|
| 7 |
|
| 8 |
Args:
|
| 9 |
+
product_data: A SQLAlchemy Product object, or None if not found.
|
| 10 |
|
| 11 |
Returns:
|
| 12 |
+
A ProductAnalysisResponse object or None.
|
| 13 |
"""
|
| 14 |
if product_data is None:
|
| 15 |
+
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
# Assuming product_data is a SQLAlchemy Product object
|
| 18 |
+
safety_score_value = product_data.overall_safety_score
|
| 19 |
safety_score_isPresent = safety_score_value is not None
|
| 20 |
|
| 21 |
+
return ProductAnalysisResponse(
|
| 22 |
+
found=True,
|
| 23 |
+
safety_score={
|
| 24 |
"isPresent": safety_score_isPresent,
|
| 25 |
"value": safety_score_value,
|
| 26 |
},
|
| 27 |
+
product_info= {
|
| 28 |
"id": product_data.id,
|
| 29 |
+
"name": product_data.product_name,
|
| 30 |
+
"barcode": None, # Assuming barcode is not in Product model
|
| 31 |
+
"image_url": None, # Assuming image_url is not in Product model
|
| 32 |
+
"brand": None, # Assuming brand is not in Product model
|
| 33 |
+
"manufacturing_places": None, # Assuming manufacturing_places is not in Product model
|
| 34 |
+
"stores": None, # Assuming stores is not in Product model
|
| 35 |
+
"countries": None, # Assuming countries is not in Product model
|
| 36 |
+
},
|
| 37 |
+
ingredient_info={
|
| 38 |
+
"ingredients_text": product_data.ingredients,
|
| 39 |
+
"ingredients_analysis": product_data.ingredient_interactions, # Assuming ingredient_interactions maps to analysis
|
| 40 |
+
"additives": None, # Assuming additives are not directly in Product model
|
| 41 |
+
},
|
| 42 |
+
allergen_info= {
|
| 43 |
+
"allergens": product_data.allergy_warnings,
|
| 44 |
+
"traces": None, # Assuming traces are not directly in Product model
|
| 45 |
+
},
|
| 46 |
+
diet_info={
|
| 47 |
+
"vegan": None, # Assuming vegan is not directly in Product model
|
| 48 |
+
"vegetarian": None, # Assuming vegetarian is not directly in Product model
|
| 49 |
+
},
|
| 50 |
+
nutritional_info=None, # Assuming nutritional_info is not directly in Product model
|
| 51 |
+
manufacturing_info=None # Assuming manufacturing_info is not directly in Product model
|
| 52 |
+
)
|
| 53 |
|
| 54 |
# Add other helper functions as needed for analysis
|