Fix new services implementation 2

This commit is contained in:
Urtzi Alfaro
2025-08-14 13:26:59 +02:00
parent 262b3dc9c4
commit 0951547e92
39 changed files with 1203 additions and 917 deletions

View File

@@ -5,8 +5,8 @@ AI-powered product classification for onboarding automation
"""
from fastapi import APIRouter, Depends, HTTPException, Path
from typing import List, Dict, Any
from uuid import UUID
from typing import List, Dict, Any, Optional
from uuid import UUID, uuid4
from pydantic import BaseModel, Field
import structlog
@@ -38,12 +38,12 @@ class ProductSuggestionResponse(BaseModel):
category: str
unit_of_measure: str
confidence_score: float
estimated_shelf_life_days: int = None
estimated_shelf_life_days: Optional[int] = None
requires_refrigeration: bool = False
requires_freezing: bool = False
is_seasonal: bool = False
suggested_supplier: str = None
notes: str = None
suggested_supplier: Optional[str] = None
notes: Optional[str] = None
class BusinessModelAnalysisResponse(BaseModel):
@@ -87,7 +87,7 @@ async def classify_single_product(
# Convert to response format
response = ProductSuggestionResponse(
suggestion_id=str(UUID.uuid4()), # Generate unique ID for tracking
suggestion_id=str(uuid4()), # Generate unique ID for tracking
original_name=suggestion.original_name,
suggested_name=suggestion.suggested_name,
product_type=suggestion.product_type.value,
@@ -144,7 +144,7 @@ async def classify_products_batch(
suggestion_responses = []
for suggestion in suggestions:
suggestion_responses.append(ProductSuggestionResponse(
suggestion_id=str(UUID.uuid4()),
suggestion_id=str(uuid4()),
original_name=suggestion.original_name,
suggested_name=suggestion.suggested_name,
product_type=suggestion.product_type.value,
@@ -159,39 +159,58 @@ async def classify_products_batch(
notes=suggestion.notes
))
# Analyze business model
# Analyze business model with enhanced detection
ingredient_count = sum(1 for s in suggestions if s.product_type.value == 'ingredient')
finished_count = sum(1 for s in suggestions if s.product_type.value == 'finished_product')
semi_finished_count = sum(1 for s in suggestions if 'semi' in s.suggested_name.lower() or 'frozen' in s.suggested_name.lower() or 'pre' in s.suggested_name.lower())
total = len(suggestions)
ingredient_ratio = ingredient_count / total if total > 0 else 0
semi_finished_ratio = semi_finished_count / total if total > 0 else 0
# Determine business model
# Enhanced business model determination
if ingredient_ratio >= 0.7:
model = 'production'
model = 'individual_bakery' # Full production from raw ingredients
elif ingredient_ratio <= 0.2 and semi_finished_ratio >= 0.3:
model = 'central_baker_satellite' # Receives semi-finished products from central baker
elif ingredient_ratio <= 0.3:
model = 'retail'
model = 'retail_bakery' # Sells finished products from suppliers
else:
model = 'hybrid'
model = 'hybrid_bakery' # Mixed model
confidence = max(abs(ingredient_ratio - 0.5) * 2, 0.1)
# Calculate confidence based on clear distinction
if model == 'individual_bakery':
confidence = min(ingredient_ratio * 1.2, 0.95)
elif model == 'central_baker_satellite':
confidence = min((semi_finished_ratio + (1 - ingredient_ratio)) / 2 * 1.2, 0.95)
else:
confidence = max(abs(ingredient_ratio - 0.5) * 2, 0.1)
recommendations = {
'production': [
'Focus on ingredient inventory management',
'Set up recipe cost calculation',
'Configure supplier relationships',
'Enable production planning features'
'individual_bakery': [
'Set up raw ingredient inventory management',
'Configure recipe cost calculation and production planning',
'Enable supplier relationships for flour, yeast, sugar, etc.',
'Set up full production workflow with proofing and baking schedules',
'Enable waste tracking for overproduction'
],
'retail': [
'Configure central baker relationships',
'Set up delivery schedule tracking',
'Enable finished product freshness monitoring',
'Focus on sales forecasting'
'central_baker_satellite': [
'Configure central baker delivery schedules',
'Set up semi-finished product inventory (frozen dough, par-baked items)',
'Enable finish-baking workflow and timing optimization',
'Track freshness and shelf-life for received products',
'Focus on customer demand forecasting for final products'
],
'hybrid': [
'Configure both ingredient and finished product management',
'Set up flexible inventory categories',
'Enable both production and retail features'
'retail_bakery': [
'Set up finished product supplier relationships',
'Configure delivery schedule tracking',
'Enable freshness monitoring and expiration management',
'Focus on sales forecasting and customer preferences'
],
'hybrid_bakery': [
'Configure both ingredient and semi-finished product management',
'Set up flexible production workflows',
'Enable both supplier and central baker relationships',
'Configure multi-tier inventory categories'
]
}