feat: Add brute strategy to ingredient processor (#744)

* fix UI column width

* words

* update parser to support diff strats

* add new model url

* make button more visible

* fix nutrition error

* feat(backend):  add 'brute' strategy for parsing ingredients

* satisfy linter

* update UI for creation page

* feat(backend):  log 422 errors in detail when not in PRODUCTION

* add strategy selector

Co-authored-by: Hayden <hay-kot@pm.me>
This commit is contained in:
Hayden
2021-10-16 16:06:13 -08:00
committed by GitHub
parent 60908e5a88
commit 3b920babe3
25 changed files with 961 additions and 131 deletions

33
mealie/routes/handlers.py Normal file
View File

@@ -0,0 +1,33 @@
from fastapi import FastAPI, Request, status
from fastapi.exceptions import RequestValidationError
from fastapi.responses import JSONResponse
from mealie.core.config import get_app_settings
from mealie.core.root_logger import get_logger
logger = get_logger(__name__)
def log_wrapper(request: Request, e):
logger.error("Start 422 Error".center(60, "-"))
logger.error(f"{request.method} {request.url}")
logger.error(f"error is {e}")
logger.error("End 422 Error".center(60, "-"))
def register_debug_handler(app: FastAPI):
settings = get_app_settings()
if settings.PRODUCTION:
return
@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request: Request, exc: RequestValidationError):
exc_str = f"{exc}".replace("\n", " ").replace(" ", " ")
log_wrapper(request, exc)
content = {"status_code": status.HTTP_422_UNPROCESSABLE_ENTITY, "message": exc_str, "data": None}
return JSONResponse(content=content, status_code=status.HTTP_422_UNPROCESSABLE_ENTITY)
return validation_exception_handler

View File

@@ -1,31 +1,25 @@
from fastapi import APIRouter, Depends
from pydantic import BaseModel
from mealie.schema.recipe import RecipeIngredient
from mealie.schema.recipe import ParsedIngredient
from mealie.schema.recipe.recipe_ingredient import IngredientRequest, IngredientsRequest
from mealie.services.parser_services import IngredientParserService
public_router = APIRouter(prefix="/parser")
class IngredientsRequest(BaseModel):
ingredients: list[str]
class IngredientRequest(BaseModel):
ingredient: str
@public_router.post("/ingredients", response_model=list[RecipeIngredient])
@public_router.post("/ingredients", response_model=list[ParsedIngredient])
def parse_ingredients(
ingredients: IngredientsRequest,
p_service: IngredientParserService = Depends(IngredientParserService.private),
):
return {"ingredients": p_service.parse_ingredients(ingredients.ingredients)}
p_service.set_parser(parser=ingredients.parser)
return p_service.parse_ingredients(ingredients.ingredients)
@public_router.post("/ingredient")
@public_router.post("/ingredient", response_model=ParsedIngredient)
def parse_ingredient(
ingredient: IngredientRequest,
p_service: IngredientParserService = Depends(IngredientParserService.private),
):
return {"ingredient": p_service.parse_ingredient(ingredient.ingredient)}
p_service.set_parser(parser=ingredient.parser)
return p_service.parse_ingredient(ingredient.ingredient)