This commit is contained in:
bbh
2025-08-13 00:03:21 +08:00
commit f2fe78cce4
6 changed files with 63 additions and 0 deletions

7
.gitignore vendored Normal file
View File

@@ -0,0 +1,7 @@
app.egg-info
*.pyc
.mypy_cache
.coverage
htmlcov
.cache
.venv

14
docker-compose.yml Normal file
View File

@@ -0,0 +1,14 @@
services:
bentoml-serve:
build: ./
container_name: bentoml_serve
ports:
- "3000:3000"
networks:
- traefik-public
- default
networks:
traefik-public:
# Allow setting it to false for testing
external: false

9
dockerfile Normal file
View File

@@ -0,0 +1,9 @@
FROM python:3.10
WORKDIR /app/
COPY ./src /app/
RUN pip install --no-cache-dir scikit-learn bentoml -i https://mirrors.aliyun.com/pypi/simple/
CMD ["bentoml", "serve"]

Binary file not shown.

Binary file not shown.

33
src/service.py Normal file
View File

@@ -0,0 +1,33 @@
import bentoml
import joblib
my_image = bentoml.images.Image(python_version="3.10") \
.python_packages("scikit-learn", "numpy")
@bentoml.service(
image=my_image
)
class NewsClassifierService:
model_logistic_path = "./models/logistic_model_0810.pkl"
model_vectorizer_path = "./models/vectorizer_0810.pkl"
def __init__(self) -> None:
self.model_logistic = joblib.load(self.model_logistic_path)
self.model_vectorizer = joblib.load(self.model_vectorizer_path)
@bentoml.api
def classify(self,text: str) -> dict:
categories = ['Competition News','financial news','Medical news','sports news']
token_text =self.model_vectorizer.transform([text])
prediction = self.model_logistic.predict(token_text)
print(f"Prediction: {prediction}")
predict = categories[prediction[0]]
json_response = {
"text": text,
"predict": predict
}
return json_response