Spaces:
Running
Running
Commit
·
718887f
1
Parent(s):
d5e35e8
bug fix
Browse files- routers/analysis.py +2 -1
- services/analysis_service.py +3 -4
- utils/analysis_utils.py +19 -21
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
|
|
@@ -17,6 +17,7 @@ from dotenv import load_dotenv
|
|
| 17 |
from langsmith import traceable
|
| 18 |
from services.ingredientFinderAgent import IngredientInfoAgentLangGraph
|
| 19 |
from services.productAnalyzerAgent import analyze_product_ingredients
|
|
|
|
| 20 |
from utils.db_utils import ingredient_db_to_pydantic
|
| 21 |
from services.analysis_service import get_product_data_by_marker_id as get_analysis_service_data
|
| 22 |
from utils.ingredient_utils import process_single_ingredient
|
|
|
|
| 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, ProductAnalysisResponse
|
| 12 |
from interfaces.productModels import ProductIngredientsRequest
|
| 13 |
from logger_manager import log_info, log_error
|
| 14 |
from db.database import get_db,SessionLocal
|
|
|
|
| 17 |
from langsmith import traceable
|
| 18 |
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
|
services/analysis_service.py
CHANGED
|
@@ -22,14 +22,14 @@ def get_product_data_by_marker_id(db: Session, target_id: str):
|
|
| 22 |
|
| 23 |
if not marker:
|
| 24 |
log_info(f"No marker found for target ID: {target_id}")
|
| 25 |
-
return
|
| 26 |
|
| 27 |
# Get the product associated with the marker
|
| 28 |
product = db.query(Product).filter(Product.id == marker.product_id).first()
|
| 29 |
|
| 30 |
if not product:
|
| 31 |
log_info(f"No product found for product_id: {marker.product_id} linked to marker ID: {target_id}")
|
| 32 |
-
return
|
| 33 |
|
| 34 |
log_info(f"Product found for marker ID {target_id}: {product.name}")
|
| 35 |
|
|
@@ -40,5 +40,4 @@ def get_product_data_by_marker_id(db: Session, target_id: str):
|
|
| 40 |
|
| 41 |
except Exception as e:
|
| 42 |
log_error(f"Error retrieving product data for marker ID {target_id}: {str(e)}", e)
|
| 43 |
-
|
| 44 |
-
return {"found": False, "message": "An error occurred while retrieving product data."}
|
|
|
|
| 22 |
|
| 23 |
if not marker:
|
| 24 |
log_info(f"No marker found for target ID: {target_id}")
|
| 25 |
+
return None
|
| 26 |
|
| 27 |
# Get the product associated with the marker
|
| 28 |
product = db.query(Product).filter(Product.id == marker.product_id).first()
|
| 29 |
|
| 30 |
if not product:
|
| 31 |
log_info(f"No product found for product_id: {marker.product_id} linked to marker ID: {target_id}")
|
| 32 |
+
return None
|
| 33 |
|
| 34 |
log_info(f"Product found for marker ID {target_id}: {product.name}")
|
| 35 |
|
|
|
|
| 40 |
|
| 41 |
except Exception as e:
|
| 42 |
log_error(f"Error retrieving product data for marker ID {target_id}: {str(e)}", e)
|
| 43 |
+
return None
|
|
|
utils/analysis_utils.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
from typing import Dict, Any, Optional
|
| 2 |
|
| 3 |
-
def
|
| 4 |
"""
|
| 5 |
Formats the retrieved product analysis data into a consistent response structure.
|
| 6 |
|
|
@@ -22,36 +22,34 @@ def format_analysis_response(product_data: Optional[Dict[str, Any]]) -> Dict[str
|
|
| 22 |
"manufacturing_info": None,
|
| 23 |
}
|
| 24 |
|
| 25 |
-
|
| 26 |
-
# Adjust field names based on your actual database schema and query results.
|
| 27 |
-
safety_score_value = product_data.get("safety_score")
|
| 28 |
safety_score_isPresent = safety_score_value is not None
|
| 29 |
|
| 30 |
return {
|
| 31 |
"found": True,
|
| 32 |
"safety_score": {
|
| 33 |
-
"
|
| 34 |
"value": safety_score_value,
|
| 35 |
},
|
| 36 |
"product_info": {
|
| 37 |
-
"id": product_data.
|
| 38 |
-
"name": product_data.
|
| 39 |
-
"barcode": product_data.
|
| 40 |
-
"image_url": product_data.
|
| 41 |
-
"brand": product_data.
|
| 42 |
-
"manufacturing_places": product_data.
|
| 43 |
-
"stores": product_data.
|
| 44 |
-
"countries": product_data.
|
| 45 |
-
} if product_data.
|
| 46 |
"ingredient_info": {
|
| 47 |
-
"ingredients_text": product_data.
|
| 48 |
-
"ingredients_analysis": product_data.
|
| 49 |
-
"additives": product_data.
|
| 50 |
-
} if product_data.
|
| 51 |
"allergen_info": {
|
| 52 |
-
"allergens": product_data.
|
| 53 |
-
"traces": product_data.
|
| 54 |
-
} if product_data.
|
| 55 |
"diet_info": {
|
| 56 |
"vegan": product_data.get("vegan"),
|
| 57 |
"vegetarian": product_data.get("vegetarian"),
|
|
|
|
| 1 |
from typing import Dict, Any, Optional
|
| 2 |
|
| 3 |
+
def format_product_analysis_response(product_data) -> Dict[str, Any]:
|
| 4 |
"""
|
| 5 |
Formats the retrieved product analysis data into a consistent response structure.
|
| 6 |
|
|
|
|
| 22 |
"manufacturing_info": None,
|
| 23 |
}
|
| 24 |
|
| 25 |
+
safety_score_value = product_data.safety_score
|
|
|
|
|
|
|
| 26 |
safety_score_isPresent = safety_score_value is not None
|
| 27 |
|
| 28 |
return {
|
| 29 |
"found": True,
|
| 30 |
"safety_score": {
|
| 31 |
+
"isPresent": safety_score_isPresent,
|
| 32 |
"value": safety_score_value,
|
| 33 |
},
|
| 34 |
"product_info": {
|
| 35 |
+
"id": product_data.id,
|
| 36 |
+
"name": product_data.name,
|
| 37 |
+
"barcode": product_data.barcode,
|
| 38 |
+
"image_url": product_data.image_url,
|
| 39 |
+
"brand": product_data.brand,
|
| 40 |
+
"manufacturing_places": product_data.manufacturing_places,
|
| 41 |
+
"stores": product_data.stores,
|
| 42 |
+
"countries": product_data.countries,
|
| 43 |
+
} if product_data.id is not None else None,
|
| 44 |
"ingredient_info": {
|
| 45 |
+
"ingredients_text": product_data.ingredients_text,
|
| 46 |
+
"ingredients_analysis": product_data.ingredients_analysis,
|
| 47 |
+
"additives": product_data.additives,
|
| 48 |
+
} if product_data.ingredients_text is not None else None,
|
| 49 |
"allergen_info": {
|
| 50 |
+
"allergens": product_data.allergens,
|
| 51 |
+
"traces": product_data.traces,
|
| 52 |
+
} if product_data.allergens is not None or product_data.traces is not None else None,
|
| 53 |
"diet_info": {
|
| 54 |
"vegan": product_data.get("vegan"),
|
| 55 |
"vegetarian": product_data.get("vegetarian"),
|