[nix] flake config added and working

parent c72f5d97
*
!requirements.txt
!src/main.py
!src/s2v_old/
\ No newline at end of file
...@@ -44,7 +44,7 @@ docker-hub: ...@@ -44,7 +44,7 @@ docker-hub:
stage: deploy stage: deploy
image: docker:dind image: docker:dind
script: sh scripts/deploy.sh script: sh scripts/deploy.sh
timeout: 2h #timeout: 2h
only: [master] only: [master]
pages: pages:
...@@ -54,4 +54,4 @@ pages: ...@@ -54,4 +54,4 @@ pages:
- cp redoc-static.html public/index.html - cp redoc-static.html public/index.html
artifacts: artifacts:
paths: [public] paths: [public]
only: [master] only: [master]
\ No newline at end of file
FROM python:3.9-slim-bullseye
ENV VIRTUAL_ENV=/opt/venv
RUN python3 -m venv $VIRTUAL_ENV
ENV PATH="$VIRTUAL_ENV/bin:$PATH"
WORKDIR /tmp
# Install dependencies:
COPY ./requirements.txt .
RUN pip install -r requirements.txt -f https://download.pytorch.org/whl/torch_stable.html
# Build args
# https://docs.docker.com/build/guide/build-args/
#ARG PORT 8001
# https://spacy.io/usage/models/
ARG MODEL en_core_web_trf
RUN python -m spacy download ${MODEL}
#RUN python -m spacy download en_core_web_trf
#RUN python -m spacy download fr_dep_news_trf # 8001
#RUN python -m spacy download de_dep_news_trf # 8002
#RUN python -m spacy download uk_core_news_trf # 8003
#RUN python -m spacy download ru_core_news_trf # 8004
#RUN python -m spacy download pl_core_news_trf # 8004
#RUN python -m spacy download es_core_news_trf # 8005
#RUN python -m spacy download pt_core_news_trf # 8006
#RUN python -m spacy download it_core_news_trf # 8007
#RUN python -m spacy download zh_core_news_trf # 8008
#RUN python -m spacy download el_core_news_trf # 8009 (greek)
#RUN python -m spacy download xx_ent_wiki_trf # 8010
# nl_core_news_sm
WORKDIR /code
COPY src/main.py .
ENV SPACY_MODEL=${MODEL}
CMD uvicorn main:app --host 0.0.0.0 --port 8001
...@@ -25,6 +25,14 @@ README! ...@@ -25,6 +25,14 @@ README!
2. Optionally, generate a wrapper for the HTTP API using [OpenAPI Generator](https://openapi-generator.tech/) on the 2. Optionally, generate a wrapper for the HTTP API using [OpenAPI Generator](https://openapi-generator.tech/) on the
file [`docs/spec/openapi.yaml`](https://github.com/neelkamath/spacy-server/raw/master/docs/spec/openapi.yaml). file [`docs/spec/openapi.yaml`](https://github.com/neelkamath/spacy-server/raw/master/docs/spec/openapi.yaml).
### Nix
One can run spacy server directly via:
```shell
nix -L develop .#en
```
(we need to use `develop`, see https://blog.ysndr.de/posts/guides/2021-12-01-nix-shells/#shell-hooks).
## [Usage](https://hub.docker.com/r/neelkamath/spacy-server) ## [Usage](https://hub.docker.com/r/neelkamath/spacy-server)
## [Contributing](docs/CONTRIBUTING.md) ## [Contributing](docs/CONTRIBUTING.md)
......
with (import <nixpkgs> {});
stdenv.mkDerivation rec {
name = "env";
env = buildEnv {
name = name;
paths = buildInputs;
};
buildInputs = [
docker-compose
];
}
services:
spacy-server:
build:
context: ./
args:
MODEL: en_core_web_trf
ports:
- 8003:8001
spacy-server-fr:
build:
context: ./
args:
MODEL: fr_dep_news_trf
ports:
- 8002:8001
# spacy-server-de:
# build:
# context: ./
# args:
# MODEL: de_dep_news_trf
# ports:
# - 8002:8000
# spacy-server-uk:
# build:
# context: ./
# args:
# MODEL: uk_core_news_trf
# ports:
# - 8003:8000
# spacy-server-ru:
# build:
# context: ./
# args:
# MODEL: ru_core_news_trf
# ports:
# - 8004:8000
# spacy-server-pl:
# build:
# context: ./
# args:
# PORT: 8004
# MODEL: pl_core_news_trf
# ports:
# - 8004:8000
# spacy-server-es:
# build:
# context: ./
# args:
# PORT: 8005
# MODEL: es_core_news_trf
# ports:
# - 8005:8000
# spacy-server-pt:
# build:
# context: ./
# args:
# PORT: 8006
# MODEL: es_core_news_trf
# ports:
# - 8006:8000
# spacy-server-it:
# build:
# context: ./
# args:
# PORT: 8007
# MODEL: it_core_news_trf
# ports:
# - 8007:8000
# spacy-server-zh:
# build:
# context: ./
# args:
# PORT: 8008
# MODEL: it_core_news_trf
# ports:
# - 8008:8000
# spacy-server-el:
# build:
# context: ./
# args:
# PORT: 8009
# MODEL: el_core_news_trf
# ports:
# - 8009:8000
# spacy-server-xx:
# build:
# context: ./
# args:
# PORT: 8010
# MODEL: xx_ent_wiki_trf
# ports:
# - 8010:8000
FROM python:3.8 AS base
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
ARG SPACY_MODEL
ENV PYTHONBUFFERED=1 SENSE2VEC=0 SPACY_MODEL=$SPACY_MODEL
RUN python -m spacy download $SPACY_MODEL
COPY src/main.py src/main.py
EXPOSE 8000
HEALTHCHECK --timeout=2s --start-period=2s --retries=1 \
CMD curl -f http://localhost:8000/health_check
RUN useradd user
USER user
CMD ["uvicorn", "src.main:app", "--host", "0.0.0.0"]
FROM base
ENV SENSE2VEC 1
COPY src/s2v_old/ src/s2v_old/
\ No newline at end of file
version: '3.7'
services:
app:
command: sh -c '. scripts/setup.sh && uvicorn src.main:app --host 0.0.0.0 --reload'
ports: ['8000:8000']
environment:
SPACY_MODEL:
SENSE2VEC:
\ No newline at end of file
version: '3.7'
services:
app:
environment:
SENSE2VEC: 1
# Since any model will do, tests have been written only for the en_core_web_sm model because it has a small size
# and has every necessary pipeline component.
SPACY_MODEL: en_core_web_sm
\ No newline at end of file
# A virtual environment caches dependencies instead of a Docker volume because the volume randomly gets corrupted.
version: '3.7'
services:
app:
image: python:3.8
working_dir: /app
volumes:
- type: bind
source: .
target: /app
\ No newline at end of file
{
"nodes": {
"flake-utils": {
"inputs": {
"systems": "systems"
},
"locked": {
"lastModified": 1731533236,
"narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "11707dc2f618dd54ca8739b309ec4fc024de578b",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1741246872,
"narHash": "sha256-Q6pMP4a9ed636qilcYX8XUguvKl/0/LGXhHcRI91p0U=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "10069ef4cf863633f57238f179a0297de84bd8d3",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixos-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"root": {
"inputs": {
"flake-utils": "flake-utils",
"nixpkgs": "nixpkgs"
}
},
"systems": {
"locked": {
"lastModified": 1681028828,
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
"owner": "nix-systems",
"repo": "default",
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
"type": "github"
},
"original": {
"owner": "nix-systems",
"repo": "default",
"type": "github"
}
}
},
"root": "root",
"version": 7
}
{
description = "Spacy-server flake";
inputs.flake-utils.url = "github:numtide/flake-utils";
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
outputs = { self, nixpkgs, flake-utils }:
flake-utils.lib.eachDefaultSystem (system:
let pkgs = nixpkgs.legacyPackages.${system};
python = (pkgs.python3.override {
packageOverrides = self: super: {
sense2vec = self.buildPythonPackage rec {
pname = "sense2vec" ;
version = "2.0.2" ;
src = pkgs.fetchFromGitHub {
owner = "explosion" ;
repo = "sense2vec";
rev = "v${version}";
sha256 = "sha256-o862emhbYEaIhzI3H4XDc7SjpDAQ05gL8BhSSW/KNE0=";
};
};
};
});
ppkgs = model: pp: [ pp.fastapi
pp.ipython
pp.sense2vec
pp.spacy
pp.spacy-models."${model}"
pp.uvicorn ];
pythonWithModel = model:
python.withPackages (ppkgs model);
server = model: (
let pwm = pythonWithModel model;
in
pkgs.stdenv.mkDerivation rec {
name = "server-${model}";
src = ./.;
buildInputs = [
pkgs.openblas
pwm
];
nativeBuildInputs = [ pkgs.makeWrapper ];
installPhase = with pwm.pkgs; ''
mkdir -p $out/bin
makeWrapper ${src}/startServer.sh $out/bin/startServer.sh \
--set PYTHONPATH "${pwm}/${pwm.sitePackages}"
'';
}
);
langMap = {
de = "de_dep_news_trf";
el = "el_core_web_trf";
en = "en_core_web_trf";
es = "es_core_news_trf";
fr = "fr_dep_news_trf";
it = "it_core_news_trf";
pl = "pl_core_news_trf";
pt = "pt_core_news_trf";
ru = "ru_core_news_trf";
uk = "uk_core_news_trf";
zh = "zh_core_news_trf";
};
mkDevShell = lang: pkgs.mkShell {
buildInputs = [
(pythonWithModel (langMap."${lang}"))
(server (langMap."${lang}"))
];
shellHook = ''
${(server (langMap."${lang}"))}/bin/startServer.sh
'';
};
in
{
devShells.en = mkDevShell "en";
devShells.fr = mkDevShell "fr";
}
);
}
...@@ -133,7 +133,7 @@ class PhraseInSentence(BaseModel): ...@@ -133,7 +133,7 @@ class PhraseInSentence(BaseModel):
@model_validator(mode='after') @model_validator(mode='after')
def phrase_must_be_in_sentence(cls, values): def phrase_must_be_in_sentence(cls, values):
if values.get('phrase') not in values.get('sentence'): if values.phrase not in values.sentence:
raise ValueError('phrase must be in sentence') raise ValueError('phrase must be in sentence')
return values return values
......
#!/bin/bash
uvicorn src.main:app --host 0.0.0.0 "$@"
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment