yenslife commited on
Commit
dcd4485
·
1 Parent(s): 9397b80

FEAT: Add Dockerized FastAPI inference service

Browse files

新增 FastAPI 人員存在偵測 API、模型載入服務與 uv 相依設定。\n加入 Dockerfile 與 Hugging Face Spaces 設定,並附上測試圖片。

Files changed (10) hide show
  1. .gitignore +216 -0
  2. .python-version +1 -0
  3. Dockerfile +27 -0
  4. app.py +53 -5
  5. main.py +31 -0
  6. model_service.py +65 -0
  7. no_person.jpg +0 -0
  8. person.jpg +0 -0
  9. pyproject.toml +21 -0
  10. uv.lock +0 -0
.gitignore ADDED
@@ -0,0 +1,216 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[codz]
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
+ share/python-wheels/
24
+ *.egg-info/
25
+ .installed.cfg
26
+ *.egg
27
+ MANIFEST
28
+
29
+ # PyInstaller
30
+ # Usually these files are written by a python script from a template
31
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
32
+ *.manifest
33
+ *.spec
34
+
35
+ # Installer logs
36
+ pip-log.txt
37
+ pip-delete-this-directory.txt
38
+
39
+ # Unit test / coverage reports
40
+ htmlcov/
41
+ .tox/
42
+ .nox/
43
+ .coverage
44
+ .coverage.*
45
+ .cache
46
+ nosetests.xml
47
+ coverage.xml
48
+ *.cover
49
+ *.py.cover
50
+ .hypothesis/
51
+ .pytest_cache/
52
+ cover/
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
+ .pybuilder/
76
+ target/
77
+
78
+ # Jupyter Notebook
79
+ .ipynb_checkpoints
80
+
81
+ # IPython
82
+ profile_default/
83
+ ipython_config.py
84
+
85
+ # pyenv
86
+ # For a library or package, you might want to ignore these files since the code is
87
+ # intended to run in multiple environments; otherwise, check them in:
88
+ # .python-version
89
+
90
+ # pipenv
91
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
93
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
94
+ # install all needed dependencies.
95
+ # Pipfile.lock
96
+
97
+ # UV
98
+ # Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
99
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
100
+ # commonly ignored for libraries.
101
+ # uv.lock
102
+
103
+ # poetry
104
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
105
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
106
+ # commonly ignored for libraries.
107
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
108
+ # poetry.lock
109
+ # poetry.toml
110
+
111
+ # pdm
112
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
113
+ # pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python.
114
+ # https://pdm-project.org/en/latest/usage/project/#working-with-version-control
115
+ # pdm.lock
116
+ # pdm.toml
117
+ .pdm-python
118
+ .pdm-build/
119
+
120
+ # pixi
121
+ # Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control.
122
+ # pixi.lock
123
+ # Pixi creates a virtual environment in the .pixi directory, just like venv module creates one
124
+ # in the .venv directory. It is recommended not to include this directory in version control.
125
+ .pixi
126
+
127
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
128
+ __pypackages__/
129
+
130
+ # Celery stuff
131
+ celerybeat-schedule
132
+ celerybeat.pid
133
+
134
+ # Redis
135
+ *.rdb
136
+ *.aof
137
+ *.pid
138
+
139
+ # RabbitMQ
140
+ mnesia/
141
+ rabbitmq/
142
+ rabbitmq-data/
143
+
144
+ # ActiveMQ
145
+ activemq-data/
146
+
147
+ # SageMath parsed files
148
+ *.sage.py
149
+
150
+ # Environments
151
+ .env
152
+ .envrc
153
+ .venv
154
+ env/
155
+ venv/
156
+ ENV/
157
+ env.bak/
158
+ venv.bak/
159
+
160
+ # Spyder project settings
161
+ .spyderproject
162
+ .spyproject
163
+
164
+ # Rope project settings
165
+ .ropeproject
166
+
167
+ # mkdocs documentation
168
+ /site
169
+
170
+ # mypy
171
+ .mypy_cache/
172
+ .dmypy.json
173
+ dmypy.json
174
+
175
+ # Pyre type checker
176
+ .pyre/
177
+
178
+ # pytype static type analyzer
179
+ .pytype/
180
+
181
+ # Cython debug symbols
182
+ cython_debug/
183
+
184
+ # PyCharm
185
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
186
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
187
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
188
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
189
+ # .idea/
190
+
191
+ # Abstra
192
+ # Abstra is an AI-powered process automation framework.
193
+ # Ignore directories containing user credentials, local state, and settings.
194
+ # Learn more at https://abstra.io/docs
195
+ .abstra/
196
+
197
+ # Visual Studio Code
198
+ # Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore
199
+ # that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
200
+ # and can be added to the global gitignore or merged into this file. However, if you prefer,
201
+ # you could uncomment the following to ignore the entire vscode folder
202
+ # .vscode/
203
+
204
+ # Ruff stuff:
205
+ .ruff_cache/
206
+
207
+ # PyPI configuration file
208
+ .pypirc
209
+
210
+ # Marimo
211
+ marimo/_static/
212
+ marimo/_lsp/
213
+ __marimo__/
214
+
215
+ # Streamlit
216
+ .streamlit/secrets.toml
.python-version ADDED
@@ -0,0 +1 @@
 
 
1
+ 3.12
Dockerfile ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.12-slim
2
+
3
+ ENV PYTHONDONTWRITEBYTECODE=1 \
4
+ PYTHONUNBUFFERED=1 \
5
+ UV_LINK_MODE=copy
6
+
7
+ WORKDIR /app
8
+
9
+ # Install system deps commonly needed by Pillow/torchvision runtime on Spaces.
10
+ RUN apt-get update && apt-get install -y --no-install-recommends \
11
+ libglib2.0-0 \
12
+ libsm6 \
13
+ libxext6 \
14
+ libxrender1 \
15
+ curl \
16
+ && rm -rf /var/lib/apt/lists/*
17
+
18
+ RUN pip install --no-cache-dir uv
19
+
20
+ COPY pyproject.toml uv.lock ./
21
+ RUN uv sync --frozen --no-dev
22
+
23
+ COPY . .
24
+
25
+ EXPOSE 7860
26
+
27
+ CMD ["sh", "-c", "uv run uvicorn app:app --host 0.0.0.0 --port ${PORT:-7860}"]
app.py CHANGED
@@ -1,8 +1,56 @@
1
- import gradio as gr
 
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
5
 
6
- demo = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- demo.launch()
8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from contextlib import asynccontextmanager
2
+ from io import BytesIO
3
 
4
+ from fastapi import FastAPI, File, HTTPException, UploadFile
5
+ from PIL import Image, UnidentifiedImageError
6
 
7
+ from model_service import MODEL_PATH, get_model_service
 
8
 
9
+
10
+ @asynccontextmanager
11
+ async def lifespan(_: FastAPI):
12
+ # Warm up model on startup so the first request is not slow.
13
+ get_model_service()
14
+ yield
15
+
16
+
17
+ app = FastAPI(
18
+ title="Presence Detection API",
19
+ description="Detect whether an image contains a person.",
20
+ version="0.1.0",
21
+ lifespan=lifespan,
22
+ )
23
+
24
+
25
+ @app.get("/")
26
+ def root():
27
+ return {
28
+ "message": "Presence Detection API",
29
+ "docs": "/docs",
30
+ "model_path": str(MODEL_PATH.name),
31
+ }
32
+
33
+
34
+ @app.get("/health")
35
+ def health():
36
+ return {"status": "ok", "model_loaded": True}
37
+
38
+
39
+ @app.post("/predict")
40
+ async def predict(file: UploadFile = File(...)):
41
+ if not file.content_type or not file.content_type.startswith("image/"):
42
+ raise HTTPException(status_code=400, detail="Uploaded file must be an image.")
43
+
44
+ data = await file.read()
45
+ if not data:
46
+ raise HTTPException(status_code=400, detail="Uploaded file is empty.")
47
+
48
+ try:
49
+ image = Image.open(BytesIO(data)).convert("RGB")
50
+ except UnidentifiedImageError as exc:
51
+ raise HTTPException(status_code=400, detail="Invalid image file.") from exc
52
+
53
+ result = get_model_service().predict_image(image)
54
+ result["filename"] = file.filename
55
+ result["content_type"] = file.content_type
56
+ return result
main.py ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from pathlib import Path
2
+ from PIL import Image
3
+
4
+ from model_service import MODEL_PATH, get_model_service
5
+
6
+
7
+ IMAGE_PATH = Path("person.jpg")
8
+ # IMAGE_PATH = Path("no_person.jpg")
9
+
10
+
11
+ def main():
12
+ if not IMAGE_PATH.exists():
13
+ raise SystemExit(f"Image not found: {IMAGE_PATH}")
14
+
15
+ service = get_model_service()
16
+ print(f"[INFO] device={service.device}")
17
+ print(f"[INFO] model={MODEL_PATH}")
18
+ print(f"[INFO] image={IMAGE_PATH}")
19
+
20
+ img = Image.open(IMAGE_PATH).convert("RGB")
21
+ result = service.predict_image(img)
22
+
23
+ print("\n========== RESULT ==========")
24
+ print(f"Prediction: {result['label']}")
25
+ print(f"P(no_person) = {result['probabilities']['no_person']:.4f}")
26
+ print(f"P(person) = {result['probabilities']['person']:.4f}")
27
+ print("============================")
28
+
29
+
30
+ if __name__ == "__main__":
31
+ main()
model_service.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from functools import lru_cache
2
+ from pathlib import Path
3
+
4
+ import torch
5
+ import torch.nn as nn
6
+ import torchvision.transforms as T
7
+ from PIL import Image
8
+ from torchvision import models
9
+
10
+
11
+ BASE_DIR = Path(__file__).resolve().parent
12
+ MODEL_PATH = BASE_DIR / "best_global_model_presence.pt"
13
+ CLASS_NAMES = ["no_person", "person"]
14
+
15
+
16
+ def build_resnet18(num_classes: int = 2) -> nn.Module:
17
+ # We load task-specific weights from `best_global_model_presence.pt`, so no
18
+ # pretrained backbone download is needed at runtime.
19
+ model = models.resnet18(weights=None)
20
+ in_features = model.fc.in_features
21
+ model.fc = nn.Linear(in_features, num_classes)
22
+ return model
23
+
24
+
25
+ class PresenceModelService:
26
+ def __init__(self, model_path: Path):
27
+ if not model_path.exists():
28
+ raise FileNotFoundError(f"Model not found: {model_path}")
29
+
30
+ self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
31
+ self.model = build_resnet18(num_classes=2).to(self.device)
32
+
33
+ state = torch.load(model_path, map_location="cpu")
34
+ self.model.load_state_dict(state, strict=True)
35
+ self.model.eval()
36
+
37
+ self.transform = T.Compose(
38
+ [
39
+ T.Resize((224, 224)),
40
+ T.ToTensor(),
41
+ T.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
42
+ ]
43
+ )
44
+
45
+ def predict_image(self, image: Image.Image) -> dict:
46
+ x = self.transform(image).unsqueeze(0).to(self.device)
47
+
48
+ with torch.no_grad():
49
+ logits = self.model(x)
50
+ probs = torch.softmax(logits, dim=-1)[0]
51
+ pred_idx = int(torch.argmax(probs).item())
52
+
53
+ probabilities = {
54
+ CLASS_NAMES[i]: round(float(probs[i].item()), 6) for i in range(len(CLASS_NAMES))
55
+ }
56
+ return {
57
+ "label": CLASS_NAMES[pred_idx],
58
+ "prediction_index": pred_idx,
59
+ "probabilities": probabilities,
60
+ }
61
+
62
+
63
+ @lru_cache(maxsize=1)
64
+ def get_model_service() -> PresenceModelService:
65
+ return PresenceModelService(MODEL_PATH)
no_person.jpg ADDED
person.jpg ADDED
pyproject.toml ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [project]
2
+ name = "securemlapi"
3
+ version = "0.1.0"
4
+ description = "Add your description here"
5
+ readme = "README.md"
6
+ requires-python = ">=3.12"
7
+ dependencies = [
8
+ "fastapi>=0.133.1",
9
+ "uvicorn>=0.41.0",
10
+ "python-multipart>=0.0.20",
11
+ "matplotlib>=3.10.8",
12
+ "numpy>=2.4.2",
13
+ "opencv-python>=4.13.0.92",
14
+ "pandas>=3.0.1",
15
+ "pillow>=12.1.1",
16
+ "scikit-learn>=1.8.0",
17
+ "torch>=2.10.0",
18
+ "torchvision>=0.25.0",
19
+ "tqdm>=4.67.3",
20
+ "ultralytics>=8.4.17",
21
+ ]
uv.lock ADDED
The diff for this file is too large to render. See raw diff