Spaces:
Sleeping
Sleeping
Enhance FastAPI application with text preprocessing and update Dockerfile for pip installation
Browse files- Dockerfile +2 -1
- app.py +26 -4
- requirements.txt +1 -1
Dockerfile
CHANGED
@@ -40,7 +40,8 @@ ENV TRANSFORMERS_CACHE=/app/.cache/huggingface/hub
|
|
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
|
|
|
44 |
|
45 |
# Switch to the non-privileged user to run the application.
|
46 |
USER appuser
|
|
|
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 --upgrade pip \
|
44 |
+
&& python -m pip install --no-cache-dir -r requirements.txt
|
45 |
|
46 |
# Switch to the non-privileged user to run the application.
|
47 |
USER appuser
|
app.py
CHANGED
@@ -1,17 +1,39 @@
|
|
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.
|
13 |
-
async def
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
return prediction
|
16 |
|
17 |
|
|
|
1 |
from fastapi import FastAPI
|
2 |
from transformers import pipeline
|
3 |
+
import re
|
4 |
|
5 |
pipe = pipeline("text-classification", model="JungleLee/bert-toxic-comment-classification")
|
6 |
|
7 |
+
app = FastAPI(
|
8 |
+
title="Hopeline - AI Inference API",
|
9 |
+
description="API for detecting toxic comments",
|
10 |
+
version="0.1"
|
11 |
+
)
|
12 |
+
|
13 |
+
def preprocess_text(text: str) -> str:
|
14 |
+
# Remove special characters and extra whitespace
|
15 |
+
text = re.sub(r'[^\w\s]', '', text)
|
16 |
+
# Convert to lowercase
|
17 |
+
text = text.lower()
|
18 |
+
# Remove extra whitespaces
|
19 |
+
text = ' '.join(text.split())
|
20 |
+
return text
|
21 |
|
22 |
@app.get("/")
|
23 |
async def welcome():
|
24 |
return "Welcome to Hopeline - AI Inference API"
|
25 |
|
26 |
+
@app.post('/predict')
|
27 |
+
async def predict_post(request_body: dict):
|
28 |
+
text = request_body.get('text', '')
|
29 |
+
if not text:
|
30 |
+
return {"error": "No text provided"}
|
31 |
+
|
32 |
+
# Preprocess text
|
33 |
+
processed_text = preprocess_text(text)
|
34 |
+
|
35 |
+
# Get prediction
|
36 |
+
prediction = pipe(processed_text)
|
37 |
return prediction
|
38 |
|
39 |
|
requirements.txt
CHANGED
@@ -2,4 +2,4 @@ fastapi
|
|
2 |
uvicorn[standard]
|
3 |
transformers
|
4 |
torch
|
5 |
-
torchvision
|
|
|
2 |
uvicorn[standard]
|
3 |
transformers
|
4 |
torch
|
5 |
+
torchvision
|