Propvia commited on
Commit
54bcbcc
·
1 Parent(s): 086bd34
Files changed (3) hide show
  1. Dockerfile +16 -0
  2. app.py +80 -0
  3. requirments.txt +7 -0
Dockerfile ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.10-slim
2
+
3
+ ENV PYTHONUNBUFFERED=1
4
+ ENV HF_HOME=/tmp
5
+
6
+ WORKDIR /code
7
+
8
+ COPY requirements.txt .
9
+ RUN pip install --no-cache-dir --upgrade pip \
10
+ && pip install --no-cache-dir -r requirements.txt
11
+
12
+ COPY . .
13
+
14
+ EXPOSE 7860
15
+
16
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860", "--log-level", "info"]
app.py ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI
2
+ from pydantic import BaseModel
3
+ from transformers import pipeline
4
+ import os
5
+
6
+
7
+ os.environ["HF_HOME"] = "/tmp"
8
+
9
+ SPAM_MODEL = "valurank/distilroberta-spam-comments-detection"
10
+ TOXIC_MODEL = "s-nlp/roberta_toxicity_classifier"
11
+ SENTIMENT_MODEL = "nlptown/bert-base-multilingual-uncased-sentiment"
12
+ NSFW_MODEL = "michellejieli/NSFW_text_classifier"
13
+
14
+ spam = pipeline("text-classification", model=SPAM_MODEL)
15
+
16
+ toxic = pipeline("text-classification", model=TOXIC_MODEL)
17
+
18
+ sentiment = pipeline("text-classification", model = SENTIMENT_MODEL)
19
+
20
+ nsfw = pipeline("text-classification", model = NSFW_MODEL)
21
+
22
+
23
+ app = FastAPI()
24
+
25
+ @app.get("/")
26
+ def root():
27
+ return {"status": "ok"}
28
+
29
+ class Query(BaseModel):
30
+ text: str
31
+
32
+ @app.post("/spam")
33
+ def predict_spam(query: Query):
34
+ result = spam(query.text)[0]
35
+ return {"label": result["label"], "score": result["score"]}
36
+
37
+ @app.post("/toxic")
38
+ def predict_toxic(query: Query):
39
+ result = toxic(query.text)[0]
40
+ return {"label": result["label"], "score": result["score"]}
41
+
42
+ @app.post("/sentiment")
43
+ def predict_sentiment(query: Query):
44
+ result = sentiment(query.text)[0]
45
+ return {"label": result["label"], "score": result["score"]}
46
+
47
+ @app.post("/nsfw")
48
+ def predict_nsfw(query: Query):
49
+ result = nsfw(query.text)[0]
50
+ return {"label": result["label"], "score": result["score"]}
51
+
52
+ @app.get("/health")
53
+ def health_check():
54
+
55
+ status = {
56
+ "server": "running",
57
+ "models": {}
58
+ }
59
+
60
+ models = {
61
+ "spam": (SPAM_MODEL, spam),
62
+ "toxic": (TOXIC_MODEL, toxic),
63
+ "sentiment": (SENTIMENT_MODEL, sentiment),
64
+ "nsfw": (NSFW_MODEL, nsfw),
65
+ }
66
+
67
+ for key, (model_name, model_pipeline) in models.items():
68
+ try:
69
+ model_pipeline("test")
70
+ status["models"][key] = {
71
+ "model_name": model_name,
72
+ "status": "running"
73
+ }
74
+ except Exception as e:
75
+ status["models"][key] = {
76
+ "model_name": model_name,
77
+ "status": f"error: {str(e)}"
78
+ }
79
+
80
+ return status
requirments.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ fastapi
2
+ uvicorn[standard]
3
+ transformers
4
+ torch --index-url https://download.pytorch.org/whl/cpu
5
+ pydantic>=2.0
6
+ huggingface_hub
7
+ requests