Spaces:
Sleeping
Sleeping
Upload 6 files
Browse files- .dockerignore +34 -0
- Dockerfile +57 -0
- README.Docker.md +22 -0
- app.py +37 -0
- compose.yaml +49 -0
- requirements.txt +5 -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
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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.11.8
|
| 10 |
+
FROM python:${PYTHON_VERSION}-slim as base
|
| 11 |
+
|
| 12 |
+
# Prevents Python from writing pyc files.
|
| 13 |
+
ENV PYTHONDONTWRITEBYTECODE=1
|
| 14 |
+
|
| 15 |
+
# Keeps Python from buffering stdout and stderr to avoid situations where
|
| 16 |
+
# the application crashes without emitting any logs due to buffering.
|
| 17 |
+
ENV PYTHONUNBUFFERED=1
|
| 18 |
+
|
| 19 |
+
WORKDIR /app
|
| 20 |
+
|
| 21 |
+
# Create a non-privileged user that the app will run under.
|
| 22 |
+
# See https://docs.docker.com/go/dockerfile-user-best-practices/
|
| 23 |
+
ARG UID=10001
|
| 24 |
+
RUN adduser \
|
| 25 |
+
--disabled-password \
|
| 26 |
+
--gecos "" \
|
| 27 |
+
--home "/nonexistent" \
|
| 28 |
+
--shell "/sbin/nologin" \
|
| 29 |
+
--no-create-home \
|
| 30 |
+
--uid "${UID}" \
|
| 31 |
+
appuser
|
| 32 |
+
|
| 33 |
+
# Download dependencies as a separate step to take advantage of Docker's caching.
|
| 34 |
+
# Leverage a cache mount to /root/.cache/pip to speed up subsequent builds.
|
| 35 |
+
# Leverage a bind mount to requirements.txt to avoid having to copy them into
|
| 36 |
+
# into this layer.
|
| 37 |
+
RUN --mount=type=cache,target=/root/.cache/pip \
|
| 38 |
+
--mount=type=bind,source=requirements.txt,target=requirements.txt \
|
| 39 |
+
python -m pip install -r requirements.txt
|
| 40 |
+
|
| 41 |
+
# Switch to the non-privileged user to run the application.
|
| 42 |
+
USER appuser
|
| 43 |
+
|
| 44 |
+
# TRANSFORMERS_CACHE 환경 변수 설정
|
| 45 |
+
ENV TRANSFORMERS_CACHE=/tmp/.cache/huggingface
|
| 46 |
+
|
| 47 |
+
# 적절한 권한으로 캐시 폴더 생성
|
| 48 |
+
RUN mkdir -p $TRANSFORMERS_CACHE && chmod -R 777 $TRANSFORMERS_CACHE
|
| 49 |
+
|
| 50 |
+
# Copy the source code into the container.
|
| 51 |
+
COPY . .
|
| 52 |
+
|
| 53 |
+
# Expose the port that the application listens on.
|
| 54 |
+
EXPOSE 7860
|
| 55 |
+
|
| 56 |
+
# Run the application.
|
| 57 |
+
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:7860.
|
| 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
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
import os
|
| 4 |
+
import torch
|
| 5 |
+
from transformers import BartTokenizer, BartForSequenceClassification, pipeline
|
| 6 |
+
|
| 7 |
+
app = FastAPI()
|
| 8 |
+
|
| 9 |
+
tokens = os.getenv("HF_TOKEN")
|
| 10 |
+
model_name = "iconcube/BART-large_classifier"
|
| 11 |
+
classifier_tokenizer = BartTokenizer.from_pretrained(model_name)
|
| 12 |
+
classifier_model = BartForSequenceClassification.from_pretrained(model_name)
|
| 13 |
+
|
| 14 |
+
classifier = pipeline(
|
| 15 |
+
"text-classification",
|
| 16 |
+
model=classifier_model,
|
| 17 |
+
tokenizer=classifier_tokenizer,
|
| 18 |
+
token=tokens
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
class RequestText(BaseModel):
|
| 22 |
+
text: str
|
| 23 |
+
|
| 24 |
+
class ResponseLabel(BaseModel):
|
| 25 |
+
label: str
|
| 26 |
+
|
| 27 |
+
@app.post("/predict", response_model=ResponseLabel)
|
| 28 |
+
async def predict(request: RequestText):
|
| 29 |
+
result = classifier(request.text)[0]
|
| 30 |
+
label = result["label"]
|
| 31 |
+
if label == "LABEL_0":
|
| 32 |
+
message = "safe_response"
|
| 33 |
+
elif label == "LABEL_1":
|
| 34 |
+
message = "unsafe_response"
|
| 35 |
+
else:
|
| 36 |
+
message = "error"
|
| 37 |
+
return ResponseLabel(label=message)
|
compose.yaml
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Comments are provided throughout this file to help you get started.
|
| 2 |
+
# If you need more help, visit the Docker Compose reference guide at
|
| 3 |
+
# https://docs.docker.com/go/compose-spec-reference/
|
| 4 |
+
|
| 5 |
+
# Here the instructions define your application as a service called "server".
|
| 6 |
+
# This service is built from the Dockerfile in the current directory.
|
| 7 |
+
# You can add other services your application may depend on here, such as a
|
| 8 |
+
# database or a cache. For examples, see the Awesome Compose repository:
|
| 9 |
+
# https://github.com/docker/awesome-compose
|
| 10 |
+
services:
|
| 11 |
+
server:
|
| 12 |
+
build:
|
| 13 |
+
context: .
|
| 14 |
+
ports:
|
| 15 |
+
- 7860:7860
|
| 16 |
+
|
| 17 |
+
# The commented out section below is an example of how to define a PostgreSQL
|
| 18 |
+
# database that your application can use. `depends_on` tells Docker Compose to
|
| 19 |
+
# start the database before your application. The `db-data` volume persists the
|
| 20 |
+
# database data between container restarts. The `db-password` secret is used
|
| 21 |
+
# to set the database password. You must create `db/password.txt` and add
|
| 22 |
+
# a password of your choosing to it before running `docker compose up`.
|
| 23 |
+
# depends_on:
|
| 24 |
+
# db:
|
| 25 |
+
# condition: service_healthy
|
| 26 |
+
# db:
|
| 27 |
+
# image: postgres
|
| 28 |
+
# restart: always
|
| 29 |
+
# user: postgres
|
| 30 |
+
# secrets:
|
| 31 |
+
# - db-password
|
| 32 |
+
# volumes:
|
| 33 |
+
# - db-data:/var/lib/postgresql/data
|
| 34 |
+
# environment:
|
| 35 |
+
# - POSTGRES_DB=example
|
| 36 |
+
# - POSTGRES_PASSWORD_FILE=/run/secrets/db-password
|
| 37 |
+
# expose:
|
| 38 |
+
# - 5432
|
| 39 |
+
# healthcheck:
|
| 40 |
+
# test: [ "CMD", "pg_isready" ]
|
| 41 |
+
# interval: 10s
|
| 42 |
+
# timeout: 5s
|
| 43 |
+
# retries: 5
|
| 44 |
+
# volumes:
|
| 45 |
+
# db-data:
|
| 46 |
+
# secrets:
|
| 47 |
+
# db-password:
|
| 48 |
+
# file: db/password.txt
|
| 49 |
+
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn
|
| 3 |
+
transformers
|
| 4 |
+
torch
|
| 5 |
+
pydantic
|