mirror of
https://github.com/mealie-recipes/mealie.git
synced 2025-05-24 01:12:54 -04:00
* add basic pre-commit file * add flake8 * add isort * add pep585-upgrade (typing upgrades) * use namespace for import * add mypy * update ci for backend * flake8 scope * fix version format * update makefile * disable strict option (temporary) * fix mypy issues * upgrade type hints (pre-commit) * add vscode typing check * add types to dev deps * remote container draft * update setup script * update compose version * run setup on create * dev containers update * remove unused pages * update setup tips * expose ports * Update pre-commit to include flask8-print (#1053) * Add in flake8-print to pre-commit * pin version of flake8-print * formatting * update getting strated docs * add mypy to pre-commit * purge .mypy_cache on clean * drop mypy Co-authored-by: zackbcom <zackbcom@users.noreply.github.com>
61 lines
1.7 KiB
Python
61 lines
1.7 KiB
Python
import subprocess
|
|
import tempfile
|
|
from fractions import Fraction
|
|
from pathlib import Path
|
|
|
|
from pydantic import BaseModel, validator
|
|
|
|
from mealie.schema._mealie.types import NoneFloat
|
|
|
|
from . import utils
|
|
from .pre_processor import pre_process_string
|
|
|
|
CWD = Path(__file__).parent
|
|
MODEL_PATH = CWD / "model.crfmodel"
|
|
|
|
|
|
class CRFConfidence(BaseModel):
|
|
average: float = 0.0
|
|
comment: NoneFloat = None
|
|
name: NoneFloat = None
|
|
unit: NoneFloat = None
|
|
qty: NoneFloat = None
|
|
|
|
|
|
class CRFIngredient(BaseModel):
|
|
input: str = ""
|
|
name: str = ""
|
|
other: str = ""
|
|
qty: str = ""
|
|
comment: str = ""
|
|
unit: str = ""
|
|
confidence: CRFConfidence
|
|
|
|
@validator("qty", always=True, pre=True)
|
|
def validate_qty(qty, values): # sourcery skip: merge-nested-ifs
|
|
if qty is None or qty == "":
|
|
# Check if other contains a fraction
|
|
try:
|
|
if values["other"] is not None and values["other"].find("/") != -1:
|
|
return float(Fraction(values["other"])).__round__(1)
|
|
else:
|
|
return 1
|
|
except Exception:
|
|
pass
|
|
|
|
return qty
|
|
|
|
|
|
def _exec_crf_test(input_text):
|
|
with tempfile.NamedTemporaryFile(mode="w") as input_file:
|
|
input_file.write(utils.export_data(input_text))
|
|
input_file.flush()
|
|
return subprocess.check_output(["crf_test", "--verbose=1", "--model", MODEL_PATH, input_file.name]).decode(
|
|
"utf-8"
|
|
)
|
|
|
|
|
|
def convert_list_to_crf_model(list_of_ingrdeint_text: list[str]):
|
|
crf_output = _exec_crf_test([pre_process_string(x) for x in list_of_ingrdeint_text])
|
|
return [CRFIngredient(**ingredient) for ingredient in utils.import_data(crf_output.split("\n"))]
|