generated from guardrails-ai/validator-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
90 lines (71 loc) · 2.31 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from typing import List
import detoxify
import torch
import os
app = FastAPI()
# Initialize the detoxify model once
env = os.environ.get("env", "dev")
torch_device = "cuda" if env == "prod" else "cpu"
model = detoxify.Detoxify("unbiased-small", device=torch.device(torch_device))
class InferenceData(BaseModel):
name: str
shape: List[int]
data: List
datatype: str
class InputRequest(BaseModel):
inputs: List[InferenceData]
class OutputResponse(BaseModel):
modelname: str
modelversion: str
outputs: List[InferenceData]
@app.get("/")
async def hello_world():
return "toxic_language"
@app.post("/validate", response_model=OutputResponse)
async def check_toxicity(input_request: InputRequest):
threshold = None
for inp in input_request.inputs:
if inp.name == "text":
text_vals = inp.data
elif inp.name == "threshold":
threshold = float(inp.data[0])
if text_vals is None or threshold is None:
raise HTTPException(status_code=400, detail="Invalid input format")
return ToxicLanguage.infer(text_vals, threshold)
class ToxicLanguage:
model_name = "unbiased-small"
validation_method = "sentence"
device = torch.device(torch_device)
model = detoxify.Detoxify(model_name, device=device)
labels = [
"toxicity",
"severe_toxicity",
"obscene",
"threat",
"insult",
"identity_attack",
"sexual_explicit",
]
def infer(text_vals, threshold) -> OutputResponse:
outputs = []
for idx, text in enumerate(text_vals):
results = ToxicLanguage.model.predict(text)
pred_labels = [
label for label, score in results.items() if score > threshold
]
outputs.append(
InferenceData(
name=f"result{idx}",
datatype="BYTES",
shape=[len(pred_labels)],
data=[pred_labels],
)
)
output_data = OutputResponse(
modelname="unbiased-small", modelversion="1", outputs=outputs
)
return output_data
# Run the app with uvicorn
# Save this script as app.py and run with: uvicorn app:app --reload