Spaces:
Runtime error
Runtime error
Initial working API
Browse files- .idea/.gitignore +8 -0
- __pycache__/main.cpython-313.pyc +0 -0
- main.py +39 -0
.idea/.gitignore
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Default ignored files
|
| 2 |
+
/shelf/
|
| 3 |
+
/workspace.xml
|
| 4 |
+
# Editor-based HTTP Client requests
|
| 5 |
+
/httpRequests/
|
| 6 |
+
# Datasource local storage ignored files
|
| 7 |
+
/dataSources/
|
| 8 |
+
/dataSources.local.xml
|
__pycache__/main.cpython-313.pyc
ADDED
|
Binary file (1.8 kB). View file
|
|
|
main.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
|
| 5 |
+
app = FastAPI(title="Content Moderation API")
|
| 6 |
+
|
| 7 |
+
# Load model once when server starts
|
| 8 |
+
classifier = pipeline("text-classification",
|
| 9 |
+
model="unitary/toxic-bert",
|
| 10 |
+
top_k=None)
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
class TextInput(BaseModel):
|
| 14 |
+
text: str
|
| 15 |
+
|
| 16 |
+
@app.get("/")
|
| 17 |
+
def home():
|
| 18 |
+
return {"message": "Content Moderation API is running"}
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
@app.post("/moderate")
|
| 22 |
+
def moderate_text(input: TextInput):
|
| 23 |
+
# Get prediction
|
| 24 |
+
result = classifier(input.text)[0]
|
| 25 |
+
|
| 26 |
+
# Find highest scoring label
|
| 27 |
+
top_prediction = max(result, key=lambda x: x['score'])
|
| 28 |
+
|
| 29 |
+
return {
|
| 30 |
+
"text": input.text,
|
| 31 |
+
"prediction": top_prediction['label'],
|
| 32 |
+
"confidence": top_prediction['score'],
|
| 33 |
+
"all_scores": result
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
@app.get("/health")
|
| 38 |
+
def health():
|
| 39 |
+
return {"status": "healthy"}
|