diff --git a/.gitignore b/.gitignore index 82f9275..c5655d2 100644 --- a/.gitignore +++ b/.gitignore @@ -160,3 +160,5 @@ cython_debug/ # and can be added to the global gitignore or merged into this file. For a more nuclear # option (not recommended) you can uncomment the following to ignore the entire idea folder. #.idea/ + +cache/ \ No newline at end of file diff --git a/app.py b/app.py index 4f2fa4d..d84d2f2 100644 --- a/app.py +++ b/app.py @@ -10,12 +10,13 @@ from fastapi.security import OAuth2PasswordBearer from concurrent.futures import ThreadPoolExecutor from dotenv import load_dotenv +from cachier import cachier load_dotenv() app = FastAPI() -# auth with a bearer api key, whoose hash is stored in the environment variable API_KEY_HASH +# auth with a bearer api key, whose hash is stored in the environment variable API_KEY_HASH oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token") API_KEY_HASH = os.getenv("API_KEY_HASH") assert API_KEY_HASH, "API_KEY_HASH environment variable must be set" @@ -42,6 +43,7 @@ class Classification(BaseModel): scores: list[float] = [0.0] * len(DEFAULT_LABELS) +@cachier(cache_dir="./cache") def classify_sync(message: str, labels: list[str]) -> dict: result = classifier(message, candidate_labels=labels) return result diff --git a/pdm.lock b/pdm.lock index 5c13760..36d91dc 100644 --- a/pdm.lock +++ b/pdm.lock @@ -5,7 +5,7 @@ groups = ["default", "dev"] strategy = ["cross_platform", "inherit_metadata"] lock_version = "4.4.1" -content_hash = "sha256:6ab1d5745cf5db39b13df46c9d2be4b7222939a3bfbf1fe8d8b0e3824ff7110a" +content_hash = "sha256:84c1e9832cb3d77a4d85bb82e83998ee81adc41e563e25a2dd1f2a96db7a7063" [[package]] name = "annotated-types" @@ -55,6 +55,21 @@ files = [ {file = "black-24.4.2.tar.gz", hash = "sha256:c872b53057f000085da66a19c55d68f6f8ddcac2642392ad3a355878406fbd4d"}, ] +[[package]] +name = "cachier" +version = "3.0.0" +summary = "Persistent, stale-free, local and cross-machine caching for Python functions." +groups = ["default"] +dependencies = [ + "portalocker>=2.3.2", + "setuptools>=67.6.0", + "watchdog>=2.3.1", +] +files = [ + {file = "cachier-3.0.0-py3-none-any.whl", hash = "sha256:627455077d3619a27b1c63368d49533ffe6807a1b19fe6944e4c8f3ac895d1bc"}, + {file = "cachier-3.0.0.tar.gz", hash = "sha256:f756ebeabf8b397f75fbbb9a5841972d4c85a81b7313e2812b3448e80d74c66d"}, +] + [[package]] name = "certifi" version = "2024.6.2" @@ -669,6 +684,20 @@ files = [ {file = "pluggy-1.5.0.tar.gz", hash = "sha256:2cffa88e94fdc978c4c574f15f9e59b7f4201d439195c3715ca9e2486f1d0cf1"}, ] +[[package]] +name = "portalocker" +version = "2.8.2" +requires_python = ">=3.8" +summary = "Wraps the portalocker recipe for easy usage" +groups = ["default"] +dependencies = [ + "pywin32>=226; platform_system == \"Windows\"", +] +files = [ + {file = "portalocker-2.8.2-py3-none-any.whl", hash = "sha256:cfb86acc09b9aa7c3b43594e19be1345b9d16af3feb08bf92f23d4dce513a28e"}, + {file = "portalocker-2.8.2.tar.gz", hash = "sha256:2b035aa7828e46c58e9b31390ee1f169b98e1066ab10b9a6a861fe7e25ee4f33"}, +] + [[package]] name = "pydantic" version = "2.7.4" @@ -777,6 +806,18 @@ files = [ {file = "python_multipart-0.0.9.tar.gz", hash = "sha256:03f54688c663f1b7977105f021043b0793151e4cb1c1a9d4a11fc13d622c4026"}, ] +[[package]] +name = "pywin32" +version = "306" +summary = "Python for Window Extensions" +groups = ["default"] +marker = "platform_system == \"Windows\"" +files = [ + {file = "pywin32-306-cp311-cp311-win32.whl", hash = "sha256:e65028133d15b64d2ed8f06dd9fbc268352478d4f9289e69c190ecd6818b6407"}, + {file = "pywin32-306-cp311-cp311-win_amd64.whl", hash = "sha256:a7639f51c184c0272e93f244eb24dafca9b1855707d94c192d4a0b4c01e1100e"}, + {file = "pywin32-306-cp311-cp311-win_arm64.whl", hash = "sha256:70dba0c913d19f942a2db25217d9a1b726c278f483a919f1abfed79c9cf64d3a"}, +] + [[package]] name = "pyyaml" version = "6.0.1" @@ -901,6 +942,17 @@ files = [ {file = "safetensors-0.4.3.tar.gz", hash = "sha256:2f85fc50c4e07a21e95c24e07460fe6f7e2859d0ce88092838352b798ce711c2"}, ] +[[package]] +name = "setuptools" +version = "70.1.0" +requires_python = ">=3.8" +summary = "Easily download, build, install, upgrade, and uninstall Python packages" +groups = ["default"] +files = [ + {file = "setuptools-70.1.0-py3-none-any.whl", hash = "sha256:d9b8b771455a97c8a9f3ab3448ebe0b29b5e105f1228bba41028be116985a267"}, + {file = "setuptools-70.1.0.tar.gz", hash = "sha256:01a1e793faa5bd89abc851fa15d0a0db26f160890c7102cd8dce643e886b47f5"}, +] + [[package]] name = "shellingham" version = "1.5.4" @@ -1230,6 +1282,35 @@ files = [ {file = "uvloop-0.19.0.tar.gz", hash = "sha256:0246f4fd1bf2bf702e06b0d45ee91677ee5c31242f39aab4ea6fe0c51aedd0fd"}, ] +[[package]] +name = "watchdog" +version = "4.0.1" +requires_python = ">=3.8" +summary = "Filesystem events monitoring" +groups = ["default"] +files = [ + {file = "watchdog-4.0.1-cp311-cp311-macosx_10_9_universal2.whl", hash = "sha256:17e32f147d8bf9657e0922c0940bcde863b894cd871dbb694beb6704cfbd2fb5"}, + {file = "watchdog-4.0.1-cp311-cp311-macosx_10_9_x86_64.whl", hash = "sha256:03e70d2df2258fb6cb0e95bbdbe06c16e608af94a3ffbd2b90c3f1e83eb10767"}, + {file = "watchdog-4.0.1-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:123587af84260c991dc5f62a6e7ef3d1c57dfddc99faacee508c71d287248459"}, + {file = "watchdog-4.0.1-pp310-pypy310_pp73-macosx_10_9_x86_64.whl", hash = "sha256:0144c0ea9997b92615af1d94afc0c217e07ce2c14912c7b1a5731776329fcfc7"}, + {file = "watchdog-4.0.1-pp310-pypy310_pp73-macosx_11_0_arm64.whl", hash = "sha256:998d2be6976a0ee3a81fb8e2777900c28641fb5bfbd0c84717d89bca0addcdc5"}, + {file = "watchdog-4.0.1-pp38-pypy38_pp73-macosx_10_9_x86_64.whl", hash = "sha256:e7921319fe4430b11278d924ef66d4daa469fafb1da679a2e48c935fa27af193"}, + {file = "watchdog-4.0.1-pp38-pypy38_pp73-macosx_11_0_arm64.whl", hash = "sha256:f0de0f284248ab40188f23380b03b59126d1479cd59940f2a34f8852db710625"}, + {file = "watchdog-4.0.1-pp39-pypy39_pp73-macosx_10_9_x86_64.whl", hash = "sha256:bca36be5707e81b9e6ce3208d92d95540d4ca244c006b61511753583c81c70dd"}, + {file = "watchdog-4.0.1-pp39-pypy39_pp73-macosx_11_0_arm64.whl", hash = "sha256:ab998f567ebdf6b1da7dc1e5accfaa7c6992244629c0fdaef062f43249bd8dee"}, + {file = "watchdog-4.0.1-py3-none-manylinux2014_aarch64.whl", hash = "sha256:dddba7ca1c807045323b6af4ff80f5ddc4d654c8bce8317dde1bd96b128ed253"}, + {file = "watchdog-4.0.1-py3-none-manylinux2014_armv7l.whl", hash = "sha256:4513ec234c68b14d4161440e07f995f231be21a09329051e67a2118a7a612d2d"}, + {file = "watchdog-4.0.1-py3-none-manylinux2014_i686.whl", hash = "sha256:4107ac5ab936a63952dea2a46a734a23230aa2f6f9db1291bf171dac3ebd53c6"}, + {file = "watchdog-4.0.1-py3-none-manylinux2014_ppc64.whl", hash = "sha256:6e8c70d2cd745daec2a08734d9f63092b793ad97612470a0ee4cbb8f5f705c57"}, + {file = "watchdog-4.0.1-py3-none-manylinux2014_ppc64le.whl", hash = "sha256:f27279d060e2ab24c0aa98363ff906d2386aa6c4dc2f1a374655d4e02a6c5e5e"}, + {file = "watchdog-4.0.1-py3-none-manylinux2014_s390x.whl", hash = "sha256:f8affdf3c0f0466e69f5b3917cdd042f89c8c63aebdb9f7c078996f607cdb0f5"}, + {file = "watchdog-4.0.1-py3-none-manylinux2014_x86_64.whl", hash = "sha256:ac7041b385f04c047fcc2951dc001671dee1b7e0615cde772e84b01fbf68ee84"}, + {file = "watchdog-4.0.1-py3-none-win32.whl", hash = "sha256:206afc3d964f9a233e6ad34618ec60b9837d0582b500b63687e34011e15bb429"}, + {file = "watchdog-4.0.1-py3-none-win_amd64.whl", hash = "sha256:7577b3c43e5909623149f76b099ac49a1a01ca4e167d1785c76eb52fa585745a"}, + {file = "watchdog-4.0.1-py3-none-win_ia64.whl", hash = "sha256:d7b9f5f3299e8dd230880b6c55504a1f69cf1e4316275d1b215ebdd8187ec88d"}, + {file = "watchdog-4.0.1.tar.gz", hash = "sha256:eebaacf674fa25511e8867028d281e602ee6500045b57f43b08778082f7f8b44"}, +] + [[package]] name = "watchfiles" version = "0.22.0" diff --git a/pyproject.toml b/pyproject.toml index 950e27d..242f605 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -10,6 +10,7 @@ dependencies = [ "numpy<2", "transformers>=4.41.2", "fastapi>=0.111.0", + "cachier>=3.0.0", ] requires-python = "==3.11.*" readme = "README.md" diff --git a/requiements.txt b/requiements.txt deleted file mode 100644 index 1ddca93..0000000 --- a/requiements.txt +++ /dev/null @@ -1,70 +0,0 @@ -annotated-types==0.7.0 ; python_version >= "3.11" and python_version < "4.0" -anyio==4.4.0 ; python_version >= "3.11" and python_version < "4.0" -certifi==2024.6.2 ; python_version >= "3.11" and python_version < "4.0" -charset-normalizer==3.3.2 ; python_version >= "3.11" and python_version < "4.0" -click==8.1.7 ; python_version >= "3.11" and python_version < "4.0" -colorama==0.4.6 ; python_version >= "3.11" and python_version < "4.0" and (sys_platform == "win32" or platform_system == "Windows") -dnspython==2.6.1 ; python_version >= "3.11" and python_version < "4.0" -email-validator==2.1.2 ; python_version >= "3.11" and python_version < "4.0" -fastapi-cli==0.0.4 ; python_version >= "3.11" and python_version < "4.0" -fastapi==0.111.0 ; python_version >= "3.11" and python_version < "4.0" -filelock==3.15.3 ; python_version >= "3.11" and python_version < "4.0" -fsspec==2024.6.0 ; python_version >= "3.11" and python_version < "4.0" -h11==0.14.0 ; python_version >= "3.11" and python_version < "4.0" -httpcore==1.0.5 ; python_version >= "3.11" and python_version < "4.0" -httptools==0.6.1 ; python_version >= "3.11" and python_version < "4.0" -httpx==0.27.0 ; python_version >= "3.11" and python_version < "4.0" -huggingface-hub==0.23.4 ; python_version >= "3.11" and python_version < "4.0" -idna==3.7 ; python_version >= "3.11" and python_version < "4.0" -intel-openmp==2021.4.0 ; python_version >= "3.11" and python_version < "4.0" and platform_system == "Windows" -jinja2==3.1.4 ; python_version >= "3.11" and python_version < "4.0" -markdown-it-py==3.0.0 ; python_version >= "3.11" and python_version < "4.0" -markupsafe==2.1.5 ; python_version >= "3.11" and python_version < "4.0" -mdurl==0.1.2 ; python_version >= "3.11" and python_version < "4.0" -mkl==2021.4.0 ; python_version >= "3.11" and python_version < "4.0" and platform_system == "Windows" -mpmath==1.3.0 ; python_version >= "3.11" and python_version < "4.0" -networkx==3.3 ; python_version >= "3.11" and python_version < "4.0" -numpy==1.26.4 ; python_version >= "3.11" and python_version < "4.0" -nvidia-cublas-cu12==12.1.3.1 ; platform_system == "Linux" and platform_machine == "x86_64" and python_version >= "3.11" and python_version < "4.0" -nvidia-cuda-cupti-cu12==12.1.105 ; platform_system == "Linux" and platform_machine == "x86_64" and python_version >= "3.11" and python_version < "4.0" -nvidia-cuda-nvrtc-cu12==12.1.105 ; platform_system == "Linux" and platform_machine == "x86_64" and python_version >= "3.11" and python_version < "4.0" -nvidia-cuda-runtime-cu12==12.1.105 ; platform_system == "Linux" and platform_machine == "x86_64" and python_version >= "3.11" and python_version < "4.0" -nvidia-cudnn-cu12==8.9.2.26 ; platform_system == "Linux" and platform_machine == "x86_64" and python_version >= "3.11" and python_version < "4.0" -nvidia-cufft-cu12==11.0.2.54 ; platform_system == "Linux" and platform_machine == "x86_64" and python_version >= "3.11" and python_version < "4.0" -nvidia-curand-cu12==10.3.2.106 ; platform_system == "Linux" and platform_machine == "x86_64" and python_version >= "3.11" and python_version < "4.0" -nvidia-cusolver-cu12==11.4.5.107 ; platform_system == "Linux" and platform_machine == "x86_64" and python_version >= "3.11" and python_version < "4.0" -nvidia-cusparse-cu12==12.1.0.106 ; platform_system == "Linux" and platform_machine == "x86_64" and python_version >= "3.11" and python_version < "4.0" -nvidia-nccl-cu12==2.20.5 ; platform_system == "Linux" and platform_machine == "x86_64" and python_version >= "3.11" and python_version < "4.0" -nvidia-nvjitlink-cu12==12.5.40 ; platform_system == "Linux" and platform_machine == "x86_64" and python_version >= "3.11" and python_version < "4.0" -nvidia-nvtx-cu12==12.1.105 ; platform_system == "Linux" and platform_machine == "x86_64" and python_version >= "3.11" and python_version < "4.0" -orjson==3.10.5 ; python_version >= "3.11" and python_version < "4.0" -packaging==24.1 ; python_version >= "3.11" and python_version < "4.0" -pybind11==2.12.0 ; python_version >= "3.11" and python_version < "4.0" -pydantic-core==2.18.4 ; python_version >= "3.11" and python_version < "4.0" -pydantic==2.7.4 ; python_version >= "3.11" and python_version < "4.0" -pygments==2.18.0 ; python_version >= "3.11" and python_version < "4.0" -python-dotenv==1.0.1 ; python_version >= "3.11" and python_version < "4.0" -python-multipart==0.0.9 ; python_version >= "3.11" and python_version < "4.0" -pyyaml==6.0.1 ; python_version >= "3.11" and python_version < "4.0" -regex==2024.5.15 ; python_version >= "3.11" and python_version < "4.0" -requests==2.32.3 ; python_version >= "3.11" and python_version < "4.0" -rich==13.7.1 ; python_version >= "3.11" and python_version < "4.0" -safetensors==0.4.3 ; python_version >= "3.11" and python_version < "4.0" -shellingham==1.5.4 ; python_version >= "3.11" and python_version < "4.0" -sniffio==1.3.1 ; python_version >= "3.11" and python_version < "4.0" -starlette==0.37.2 ; python_version >= "3.11" and python_version < "4.0" -sympy==1.12.1 ; python_version >= "3.11" and python_version < "4.0" -tbb==2021.12.0 ; python_version >= "3.11" and python_version < "4.0" and platform_system == "Windows" -tokenizers==0.19.1 ; python_version >= "3.11" and python_version < "4.0" -torch==2.3.1 ; python_version >= "3.11" and python_version < "4.0" -tqdm==4.66.4 ; python_version >= "3.11" and python_version < "4.0" -transformers==4.41.2 ; python_version >= "3.11" and python_version < "4.0" -triton==2.3.1 ; platform_system == "Linux" and platform_machine == "x86_64" and python_version < "3.12" and python_version >= "3.11" -typer==0.12.3 ; python_version >= "3.11" and python_version < "4.0" -typing-extensions==4.12.2 ; python_version >= "3.11" and python_version < "4.0" -ujson==5.10.0 ; python_version >= "3.11" and python_version < "4.0" -urllib3==2.2.2 ; python_version >= "3.11" and python_version < "4.0" -uvicorn[standard]==0.30.1 ; python_version >= "3.11" and python_version < "4.0" -uvloop==0.19.0 ; (sys_platform != "win32" and sys_platform != "cygwin") and platform_python_implementation != "PyPy" and python_version >= "3.11" and python_version < "4.0" -watchfiles==0.22.0 ; python_version >= "3.11" and python_version < "4.0" -websockets==12.0 ; python_version >= "3.11" and python_version < "4.0" diff --git a/requirements.txt b/requirements.txt index 58b79c3..98195ef 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,6 +3,7 @@ annotated-types==0.7.0 anyio==4.4.0 +cachier==3.0.0 certifi==2024.6.2 charset-normalizer==3.3.2 click==8.1.7 @@ -42,16 +43,19 @@ nvidia-nvjitlink-cu12==12.5.40; platform_system == "Linux" and platform_machine nvidia-nvtx-cu12==12.1.105; platform_system == "Linux" and platform_machine == "x86_64" orjson==3.10.5 packaging==24.1 +portalocker==2.8.2 pydantic==2.7.4 pydantic-core==2.18.4 pygments==2.18.0 python-dotenv==1.0.1 python-multipart==0.0.9 +pywin32==306; platform_system == "Windows" pyyaml==6.0.1 regex==2024.5.15 requests==2.32.3 rich==13.7.1 safetensors==0.4.3 +setuptools==70.1.0 shellingham==1.5.4 sniffio==1.3.1 starlette==0.37.2 @@ -68,5 +72,6 @@ ujson==5.10.0 urllib3==2.2.2 uvicorn==0.30.1 uvloop==0.19.0; (sys_platform != "cygwin" and sys_platform != "win32") and platform_python_implementation != "PyPy" +watchdog==4.0.1 watchfiles==0.22.0 websockets==12.0