Spaces:
Sleeping
Sleeping
Add Docker support and FastAPI inference endpoint
Browse files- Create .dockerignore to exclude unnecessary files from the Docker context
- Update Dockerfile for optimized image building and non-privileged user
- Add README.Docker.md for building and deploying instructions
- Implement FastAPI application with a text classification endpoint
- Update requirements.txt to include transformers and PyTorch dependencies
- .dockerignore +34 -0
- Dockerfile +47 -8
- README.Docker.md +22 -0
- app.py +12 -2
- requirements.txt +3 -0
.dockerignore
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Include any files or directories that you don't want to be copied to your
|
2 |
+
# container here (e.g., local build artifacts, temporary files, etc.).
|
3 |
+
#
|
4 |
+
# For more help, visit the .dockerignore file reference guide at
|
5 |
+
# https://docs.docker.com/go/build-context-dockerignore/
|
6 |
+
|
7 |
+
**/.DS_Store
|
8 |
+
**/__pycache__
|
9 |
+
**/.venv
|
10 |
+
**/.classpath
|
11 |
+
**/.dockerignore
|
12 |
+
**/.env
|
13 |
+
**/.git
|
14 |
+
**/.gitignore
|
15 |
+
**/.project
|
16 |
+
**/.settings
|
17 |
+
**/.toolstarget
|
18 |
+
**/.vs
|
19 |
+
**/.vscode
|
20 |
+
**/*.*proj.user
|
21 |
+
**/*.dbmdl
|
22 |
+
**/*.jfm
|
23 |
+
**/bin
|
24 |
+
**/charts
|
25 |
+
**/docker-compose*
|
26 |
+
**/compose.y*ml
|
27 |
+
**/Dockerfile*
|
28 |
+
**/node_modules
|
29 |
+
**/npm-debug.log
|
30 |
+
**/obj
|
31 |
+
**/secrets.dev.yaml
|
32 |
+
**/values.dev.yaml
|
33 |
+
LICENSE
|
34 |
+
README.md
|
Dockerfile
CHANGED
@@ -1,16 +1,55 @@
|
|
1 |
-
#
|
2 |
-
# you will also find guides on how best to write your Dockerfile
|
3 |
|
4 |
-
|
|
|
|
|
5 |
|
6 |
-
|
7 |
-
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
WORKDIR /app
|
11 |
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
COPY --chown=user . /app
|
|
|
|
|
|
|
|
|
|
|
16 |
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
|
|
1 |
+
# syntax=docker/dockerfile:1
|
|
|
2 |
|
3 |
+
# Comments are provided throughout this file to help you get started.
|
4 |
+
# If you need more help, visit the Dockerfile reference guide at
|
5 |
+
# https://docs.docker.com/go/dockerfile-reference/
|
6 |
|
7 |
+
# Want to help us make this template better? Share your feedback here: https://forms.gle/ybq9Krt8jtBL3iCk7
|
8 |
+
|
9 |
+
ARG PYTHON_VERSION=3.9.0
|
10 |
+
FROM python:${PYTHON_VERSION}-slim as base
|
11 |
+
|
12 |
+
# Prevents Python from writing pyc files.
|
13 |
+
|
14 |
+
# Keeps Python from buffering stdout and stderr to avoid situations where
|
15 |
+
# the application crashes without emitting any logs due to buffering.
|
16 |
+
ENV PYTHONUNBUFFERED=1
|
17 |
|
18 |
WORKDIR /app
|
19 |
|
20 |
+
# Create a non-privileged user that the app will run under.
|
21 |
+
# See https://docs.docker.com/go/dockerfile-user-best-practices/
|
22 |
+
ARG UID=10001
|
23 |
+
RUN adduser \
|
24 |
+
--disabled-password \
|
25 |
+
--gecos "" \
|
26 |
+
--home "/nonexistent" \
|
27 |
+
--shell "/sbin/nologin" \
|
28 |
+
--no-create-home \
|
29 |
+
--uid "${UID}" \
|
30 |
+
appuser
|
31 |
+
|
32 |
+
ENV PATH="/home/appuser/.local/bin:$PATH"
|
33 |
+
|
34 |
+
COPY --chown=appuser ./requirements.txt requirements.txt
|
35 |
+
RUN mkdir -p /app/.cache \
|
36 |
+
&& chown -R appuser:appuser /app/.cache
|
37 |
+
ENV TRANSFORMERS_CACHE=/app/.cache/huggingface/hub
|
38 |
|
39 |
+
# Download dependencies as a separate step to take advantage of Docker's caching.
|
40 |
+
# Leverage a cache mount to /root/.cache/pip to speed up subsequent builds.
|
41 |
+
# Leverage a bind mount to requirements.txt to avoid having to copy them into
|
42 |
+
# into this layer.
|
43 |
+
RUN python -m pip install -r requirements.txt
|
44 |
+
|
45 |
+
# Switch to the non-privileged user to run the application.
|
46 |
+
USER appuser
|
47 |
+
|
48 |
+
# Copy the source code into the container.
|
49 |
COPY --chown=user . /app
|
50 |
+
|
51 |
+
# Expose the port that the application listens on.
|
52 |
+
EXPOSE 7860
|
53 |
+
|
54 |
+
# Run the application.
|
55 |
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
README.Docker.md
ADDED
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
### Building and running your application
|
2 |
+
|
3 |
+
When you're ready, start your application by running:
|
4 |
+
`docker compose up --build`.
|
5 |
+
|
6 |
+
Your application will be available at http://localhost:6300.
|
7 |
+
|
8 |
+
### Deploying your application to the cloud
|
9 |
+
|
10 |
+
First, build your image, e.g.: `docker build -t myapp .`.
|
11 |
+
If your cloud uses a different CPU architecture than your development
|
12 |
+
machine (e.g., you are on a Mac M1 and your cloud provider is amd64),
|
13 |
+
you'll want to build the image for that platform, e.g.:
|
14 |
+
`docker build --platform=linux/amd64 -t myapp .`.
|
15 |
+
|
16 |
+
Then, push it to your registry, e.g. `docker push myregistry.com/myapp`.
|
17 |
+
|
18 |
+
Consult Docker's [getting started](https://docs.docker.com/go/get-started-sharing/)
|
19 |
+
docs for more detail on building and pushing.
|
20 |
+
|
21 |
+
### References
|
22 |
+
* [Docker's Python guide](https://docs.docker.com/language/python/)
|
app.py
CHANGED
@@ -1,7 +1,17 @@
|
|
1 |
from fastapi import FastAPI
|
|
|
|
|
|
|
2 |
|
3 |
app = FastAPI()
|
4 |
|
5 |
@app.get("/")
|
6 |
-
def
|
7 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
from fastapi import FastAPI
|
2 |
+
from transformers import pipeline
|
3 |
+
|
4 |
+
pipe = pipeline("text-classification", model="JungleLee/bert-toxic-comment-classification")
|
5 |
|
6 |
app = FastAPI()
|
7 |
|
8 |
@app.get("/")
|
9 |
+
async def welcome():
|
10 |
+
return "Welcome to Hopeline - AI Inference API"
|
11 |
+
|
12 |
+
@app.get('/predict')
|
13 |
+
async def predict(text: str):
|
14 |
+
prediction = pipe(text)
|
15 |
+
return prediction
|
16 |
+
|
17 |
+
|
requirements.txt
CHANGED
@@ -1,2 +1,5 @@
|
|
1 |
fastapi
|
2 |
uvicorn[standard]
|
|
|
|
|
|
|
|
1 |
fastapi
|
2 |
uvicorn[standard]
|
3 |
+
transformers
|
4 |
+
torch
|
5 |
+
torchvision
|