Xmaster6y commited on
Commit
ab8b5a4
1 Parent(s): 653093a

initial setup

Browse files
.gitignore ADDED
@@ -0,0 +1,141 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[cod]
4
+ *$py.class
5
+
6
+ # C extensions
7
+ *.so
8
+
9
+ # Distribution / packaging
10
+ .Python
11
+ build/
12
+ develop-eggs/
13
+ dist/
14
+ downloads/
15
+ eggs/
16
+ .eggs/
17
+ lib/
18
+ lib64/
19
+ parts/
20
+ sdist/
21
+ var/
22
+ wheels/
23
+ pip-wheel-metadata/
24
+ share/python-wheels/
25
+ *.egg-info/
26
+ .installed.cfg
27
+ *.egg
28
+ MANIFEST
29
+
30
+ # PyInstaller
31
+ # Usually these files are written by a python script from a template
32
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
33
+ *.manifest
34
+ *.spec
35
+
36
+ # Installer logs
37
+ pip-log.txt
38
+ pip-delete-this-directory.txt
39
+
40
+ # Unit test / coverage reports
41
+ htmlcov/
42
+ .tox/
43
+ .nox/
44
+ .coverage
45
+ .coverage.*
46
+ .cache
47
+ nosetests.xml
48
+ coverage.xml
49
+ *.cover
50
+ *.py,cover
51
+ .hypothesis/
52
+ .pytest_cache/
53
+
54
+ # Translations
55
+ *.mo
56
+ *.pot
57
+
58
+ # Django stuff:
59
+ *.log
60
+ local_settings.py
61
+ db.sqlite3
62
+ db.sqlite3-journal
63
+
64
+ # Flask stuff:
65
+ instance/
66
+ .webassets-cache
67
+
68
+ # Scrapy stuff:
69
+ .scrapy
70
+
71
+ # Sphinx documentation
72
+ docs/_build/
73
+
74
+ # PyBuilder
75
+ target/
76
+
77
+ # Jupyter Notebook
78
+ .ipynb_checkpoints
79
+
80
+ # IPython
81
+ profile_default/
82
+ ipython_config.py
83
+
84
+ # pipenv
85
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
86
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
87
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
88
+ # install all needed dependencies.
89
+ #Pipfile.lock
90
+
91
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow
92
+ __pypackages__/
93
+
94
+ # Celery stuff
95
+ celerybeat-schedule
96
+ celerybeat.pid
97
+
98
+ # SageMath parsed files
99
+ *.sage.py
100
+
101
+ # Environments
102
+ .env
103
+ .venv
104
+ env/
105
+ venv/
106
+ ENV/
107
+ env.bak/
108
+ venv.bak/
109
+
110
+ # Spyder project settings
111
+ .spyderproject
112
+ .spyproject
113
+
114
+ # Rope project settings
115
+ .ropeproject
116
+
117
+ # mkdocs documentation
118
+ /site
119
+
120
+ # mypy
121
+ .mypy_cache/
122
+ .dmypy.json
123
+ dmypy.json
124
+
125
+ # Pyre type checker
126
+ .pyre/
127
+
128
+ # Pickle files
129
+ *.pkl
130
+
131
+ # Various files
132
+ ignored
133
+ debug
134
+ *.zip
135
+ lc0
136
+ !bin/lc0
137
+ wandb
138
+ **/.DS_Store
139
+
140
+ *secret*
141
+ .artifacts
.python-version ADDED
@@ -0,0 +1 @@
 
 
1
+ 3.9
Dockerfile ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Read the doc: https://huggingface.co/docs/hub/spaces-sdks-docker
2
+ # you will also find guides on how best to write your Dockerfile
3
+
4
+ FROM python:3.9
5
+
6
+ RUN useradd -m -u 1000 user
7
+ USER user
8
+ ENV PATH="/home/user/.local/bin:$PATH"
9
+
10
+ WORKDIR /app
11
+
12
+ RUN pip install uv --no-cache-dir
13
+ COPY --chown=user ./pyproject.toml pyproject.toml
14
+ COPY --chown=user ./uv.lock uv.lock
15
+ RUN uv sync
16
+
17
+ COPY --chown=user . /app
18
+ ENTRYPOINT ["/app/entrypoint.sh"]
Makefile ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ .PHONY dev:
2
+ dev:
3
+ @echo "Starting the application..."
4
+ @uv run uvicorn api.main:app --reload
5
+
6
+ .PHONY start:
7
+ start:
8
+ @echo "Starting the application..."
9
+ @uv run uvicorn api.main:app --host 0.0.0.0 --port 8000
README.md CHANGED
@@ -1,10 +1,10 @@
1
  ---
2
- title: Team13.4
3
- emoji: 😻
4
  colorFrom: red
5
  colorTo: yellow
6
  sdk: docker
7
  pinned: false
8
  ---
9
 
10
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: Team 13.4
3
+ emoji: 🛰️
4
  colorFrom: red
5
  colorTo: yellow
6
  sdk: docker
7
  pinned: false
8
  ---
9
 
10
+ # Team 13.4
api/__init__.py ADDED
File without changes
api/auth.py ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Authentication module for API
3
+ """
4
+
5
+ from fastapi import HTTPException, Security
6
+ from fastapi.security import api_key
7
+
8
+ from api import constants
9
+
10
+ api_key_header = api_key.APIKeyHeader(name="X-API-KEY")
11
+
12
+
13
+ async def validate_api_key(key: str = Security(api_key_header)):
14
+ if constants.X_API_KEY == "":
15
+ raise HTTPException(status_code=500, detail="API Key is not set in the server")
16
+ if key != constants.X_API_KEY:
17
+ raise HTTPException(status_code=401, detail="Unauthorized - API Key is wrong")
18
+ return None
api/constants.py ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ """
2
+ Constants for the whole API.
3
+ """
4
+
5
+ import os
6
+
7
+ X_API_KEY = os.environ.get("X_API_KEY", "")
api/global_variables.py ADDED
File without changes
api/inference/__init__.py ADDED
File without changes
api/inference/router.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Generate router
3
+ """
4
+
5
+ import logging
6
+ from contextlib import asynccontextmanager
7
+
8
+ from fastapi import APIRouter
9
+
10
+ from api.schema import SuccessDetail
11
+
12
+
13
+ @asynccontextmanager
14
+ async def lifespan(app: APIRouter):
15
+ logger = logging.getLogger("uvicorn")
16
+ logger.info("Starting inference router...")
17
+ yield
18
+ logger.info("Shutting down inference router...")
19
+
20
+
21
+ router = APIRouter(
22
+ lifespan=lifespan,
23
+ )
24
+
25
+
26
+ @router.get(
27
+ "/",
28
+ status_code=200,
29
+ response_model=SuccessDetail,
30
+ )
31
+ async def home():
32
+ """
33
+ Submodule home page.
34
+ """
35
+ return {"success": "Welcome to the inference submodule!"}
api/inference/schema.py ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Schema for the inference endpoint
3
+ """
4
+
5
+ from pydantic import BaseModel, validator
6
+ from enum import Enum
7
+
8
+ class Sex(str, Enum):
9
+ male = "m"
10
+ female = "f"
11
+
12
+
13
+ class Patient(BaseModel):
14
+ age: int
15
+ sex: Sex
16
+
17
+ @validator('age')
18
+ def patient_age_must_be_legit(cls, v):
19
+ if v < 18:
20
+ raise ValueError('age must be at least 18')
21
+ if v > 100:
22
+ raise ValueError('age must be at most 100')
23
+ return v
24
+
25
+ class InferenceRequest(BaseModel):
26
+
27
+ model_name: str
28
+ patient: Patient
api/main.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Main API module.
3
+ """
4
+
5
+ import logging
6
+ from contextlib import asynccontextmanager
7
+
8
+ from fastapi import FastAPI, Depends
9
+ from fastapi.middleware.cors import CORSMiddleware
10
+
11
+ from api.inference import router as inference_router
12
+ from api.requirements import router as requirements_router
13
+ from api import auth
14
+ from api.schema import SuccessDetail
15
+
16
+
17
+ @asynccontextmanager
18
+ async def lifespan(app: FastAPI):
19
+ logger = logging.getLogger("uvicorn")
20
+ logger.info(f"Starting up...")
21
+ yield
22
+ logger.info(f"Shutting down...")
23
+
24
+
25
+ app = FastAPI(
26
+ title="Team 13 API",
27
+ lifespan=lifespan,
28
+ )
29
+
30
+ origins = ["*"]
31
+ app.add_middleware(
32
+ CORSMiddleware,
33
+ allow_origins=origins,
34
+ allow_credentials=True,
35
+ allow_methods=["*"],
36
+ allow_headers=["*"],
37
+ )
38
+
39
+
40
+ @app.get(
41
+ "/",
42
+ tags=["HOME"],
43
+ summary="Home page",
44
+ status_code=200,
45
+ response_model=SuccessDetail,
46
+ )
47
+ async def home():
48
+ """
49
+ Home page.
50
+ """
51
+ return {"success": "Welcome to yet another API!"}
52
+
53
+
54
+ app.include_router(
55
+ requirements_router.router,
56
+ prefix="/requirements",
57
+ tags=["REQUIREMENTS"],
58
+ dependencies=[Depends(auth.validate_api_key)],
59
+ )
60
+
61
+ app.include_router(
62
+ inference_router.router,
63
+ prefix="/inference",
64
+ tags=["INFERENCE"],
65
+ dependencies=[Depends(auth.validate_api_key)],
66
+ )
api/requirements/__init__.py ADDED
File without changes
api/requirements/router.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Generate router
3
+ """
4
+
5
+ import logging
6
+ from contextlib import asynccontextmanager
7
+
8
+ from fastapi import APIRouter
9
+
10
+ from api.schema import SuccessDetail
11
+
12
+
13
+ @asynccontextmanager
14
+ async def lifespan(app: APIRouter):
15
+ logger = logging.getLogger("uvicorn")
16
+ logger.info("Starting requirements router...")
17
+ yield
18
+ logger.info("Shutting down requirements router...")
19
+
20
+
21
+ router = APIRouter(
22
+ lifespan=lifespan,
23
+ )
24
+
25
+
26
+ @router.get(
27
+ "/",
28
+ status_code=200,
29
+ response_model=SuccessDetail,
30
+ )
31
+ async def home():
32
+ """
33
+ Submodule home page.
34
+ """
35
+ return {"success": "Welcome to the requirements submodule!"}
api/requirements/schema.py ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ from pydantic import BaseModel
2
+
3
+
4
+ class RequirementsRequest(BaseModel):
5
+
6
+ model_name: str
api/schema.py ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Global API Schema
3
+ """
4
+
5
+ from pydantic import BaseModel
6
+
7
+
8
+ class ErrorDetail(BaseModel):
9
+ error: str
10
+
11
+ model_config = {
12
+ "json_schema_extra": {
13
+ "examples": [
14
+ {
15
+ "error": "description of the error",
16
+ }
17
+ ],
18
+ }
19
+ }
20
+
21
+
22
+ class SuccessDetail(BaseModel):
23
+ success: str
24
+
25
+ model_config = {
26
+ "json_schema_extra": {
27
+ "examples": [
28
+ {
29
+ "success": "description of the success",
30
+ }
31
+ ],
32
+ }
33
+ }
entry.sh ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ # Entrypoint
2
+
3
+ echo "Starting the application..."
4
+ uv run uvicorn api.main:app --host 0.0.0.0 --port 8000
pyproject.toml ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [project]
2
+ name = "fhe"
3
+ version = "0.0.0-dev"
4
+ description = "Add your description here"
5
+ readme = "README.md"
6
+ requires-python = ">=3.9"
7
+ dependencies = [
8
+ "fastapi>=0.103.2",
9
+ "pydantic>=1.10.18",
10
+ "uvicorn[standard]>=0.30.6",
11
+ ]
uv.lock ADDED
The diff for this file is too large to render. See raw diff