Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -2,6 +2,8 @@ from fastapi import FastAPI, HTTPException
|
|
| 2 |
from pydantic import BaseModel
|
| 3 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline
|
| 4 |
import torch
|
|
|
|
|
|
|
| 5 |
|
| 6 |
|
| 7 |
class Guardrail:
|
|
@@ -48,6 +50,30 @@ class ClassificationResult(BaseModel):
|
|
| 48 |
|
| 49 |
app = FastAPI()
|
| 50 |
guardrail = Guardrail()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
|
| 53 |
@app.post("/api/models/PromptInjection/classify", response_model=ClassificationResult)
|
|
|
|
| 2 |
from pydantic import BaseModel
|
| 3 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline
|
| 4 |
import torch
|
| 5 |
+
from detoxify import Detoxify
|
| 6 |
+
|
| 7 |
|
| 8 |
|
| 9 |
class Guardrail:
|
|
|
|
| 50 |
|
| 51 |
app = FastAPI()
|
| 52 |
guardrail = Guardrail()
|
| 53 |
+
toxicity_classifier = Detoxify('original')
|
| 54 |
+
|
| 55 |
+
class ToxicityResult(BaseModel):
|
| 56 |
+
toxicity: float
|
| 57 |
+
severe_toxicity: float
|
| 58 |
+
obscene: float
|
| 59 |
+
threat: float
|
| 60 |
+
insult: float
|
| 61 |
+
identity_attack: float
|
| 62 |
+
|
| 63 |
+
@app.post("/api/models/toxicity/classify", response_model=ToxicityResult)
|
| 64 |
+
def classify_toxicity(text_prompt: TextPrompt):
|
| 65 |
+
try:
|
| 66 |
+
result = toxicity_classifier.predict(text_prompt.prompt)
|
| 67 |
+
return {
|
| 68 |
+
"toxicity": result['toxicity'],
|
| 69 |
+
"severe_toxicity": result['severe_toxicity'],
|
| 70 |
+
"obscene": result['obscene'],
|
| 71 |
+
"threat": result['threat'],
|
| 72 |
+
"insult": result['insult'],
|
| 73 |
+
"identity_attack": result['identity_attack']
|
| 74 |
+
}
|
| 75 |
+
except Exception as e:
|
| 76 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 77 |
|
| 78 |
|
| 79 |
@app.post("/api/models/PromptInjection/classify", response_model=ClassificationResult)
|