Spaces:
Runtime error
Runtime error
Download main.py from busbusss/content-mod-api: direct link, hf CLI and curl.
- Browser
- Download file 892 Bytes
-
https://huggingface.co/spaces/busbusss/content-mod-api/resolve/main/main.py
- Command line
-
hf download hf://spaces/busbusss/content-mod-api/main.py
-
curl -L -o main.py https://huggingface.co/spaces/busbusss/content-mod-api/resolve/main/main.py
892 Bytes
| from fastapi import FastAPI | |
| from pydantic import BaseModel | |
| from transformers import pipeline | |
| app = FastAPI(title="Content Moderation API") | |
| # Load model once when server starts | |
| classifier = pipeline("text-classification", | |
| model="unitary/toxic-bert", | |
| top_k=None) | |
| class TextInput(BaseModel): | |
| text: str | |
| def home(): | |
| return {"message": "Content Moderation API is running"} | |
| def moderate_text(input: TextInput): | |
| # Get prediction | |
| result = classifier(input.text)[0] | |
| # Find highest scoring label | |
| top_prediction = max(result, key=lambda x: x['score']) | |
| return { | |
| "text": input.text, | |
| "prediction": top_prediction['label'], | |
| "confidence": top_prediction['score'], | |
| "all_scores": result | |
| } | |
| def health(): | |
| return {"status": "healthy"} |