GeoGhi/news-bias-classifier
News Bias Classification API
Minimal FastAPI inference service for a free-first Hugging Face Docker Space demo.
Why the model is not stored in the Space repo
Hugging Face Space repos can hit a 1 GB repository storage limit. This project's model.safetensors is about 1.74 GB, so the Space repo should contain only the API code.
Store the model in a separate Hugging Face model repo, then set the Space variable:
HF_MODEL_ID=your-username/news-bias-classifier-modelFor a free demo, make the model repo public if possible. If the model repo is private, also add HF_TOKEN as a Space secret.
Endpoints
GET /healthPOST /predict
Request:
{
"text": "Full article text"
}Response:
{
"label": "left",
"scores": {
"left": 0.82,
"center": 0.11,
"right": 0.07
}
}Local development
From C:\Projects\NewsBiasClassification\backend:
python -m pip install -r requirements.txt
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000Locally, the backend uses the existing model at ../model/final unless HF_MODEL_ID is set.
Prepare the Space folder
From the repository root:
.\backend\scripts\prepare_hf_space.ps1This creates:
backend/dist/hf-space/with this shape:
.
|- app/
|- model/
|- requirements.txt
|- Dockerfile
|- .dockerignore
`- README.mdThe default script output intentionally does not include model/final, so it stays under the Space repo storage limit.
Deploy to Hugging Face Spaces
- Create a Hugging Face model repo, for example
your-username/news-bias-classifier-model. - Upload the files from
C:\Projects\NewsBiasClassification\model\finalinto that model repo. - Create a new Hugging Face Space.
- Choose Docker as the Space SDK.
- Choose CPU Basic hardware to keep the demo free.
- Upload or push the contents of
backend/dist/hf-spaceto the Space repo root. - In the Space settings, add
HF_MODEL_ID=your-username/news-bias-classifier-model. - Wait for the Space to build.
- Test
https://your-space-name.hf.space/health. - Test
POST https://your-space-name.hf.space/predict.
Optional Space variables:
HF_MODEL_ID=your-username/news-bias-classifier-model
HF_TOKEN=only-needed-for-private-model-repos
MAX_LENGTH=256
REQUEST_MAX_CHARS=20000
CORS_ORIGINS=https://your-frontend-domain.comFor local frontend testing against the deployed Space, set:
NEXT_PUBLIC_API_BASE_URL=https://your-space-name.hf.space