python-tools
FastAPI service powering the ML-backed tools on smitparekh.co.in — currently background removal, with room for upscaling, OCR, transcription, etc.
The frontmatter above is what Hugging Face Spaces reads when you push this folder as a Docker Space.
Endpoints
| Method |
Path |
Description |
| GET |
/ |
Service info + docs link |
| GET |
/api/health |
Liveness probe — returns active model name |
| POST |
/api/remove-bg |
multipart/form-data file → transparent PNG |
| POST |
/api/transcribe |
multipart/form-data file → transcript JSON |
| GET |
/docs |
Swagger UI |
POST /api/remove-bg accepts image/png, image/jpeg, image/webp (default 10 MB cap) and returns a transparent image/png.
POST /api/transcribe accepts audio (mp3, wav, m4a, aac, flac, ogg, webm; default 10 MB cap) and returns { "text": "...", "language": "en", "duration": 312.4 }. Transcription runs on CPU via faster-whisper — expect roughly 1–3× audio length on the free HF CPU tier.
Local development
cd python-tools
python -m venv .venv
.venv\Scripts\activate # Windows
# .venv/bin/activate # macOS / Linux
pip install -r requirements.txt
uvicorn app.main:app --reload --port 7860
Visit http://localhost:7860/docs.
Configuration
Copy .env.example to .env and adjust as needed.
| Variable |
Default |
Purpose |
ALLOWED_ORIGINS |
http://localhost:3000,http://localhost:3001 |
CORS allowlist (comma-separated list or *) |
REMBG_MODEL |
u2net |
rembg model: u2net, u2netp, u2net_human_seg, isnet-general-use, birefnet-general |
MAX_UPLOAD_MB |
10 |
Per-request image upload size cap |
WHISPER_MODEL |
base |
faster-whisper model: tiny, base, small, medium, large-v3 (bigger = slower on CPU) |
MAX_AUDIO_MB |
10 |
Per-request audio upload size cap for /api/transcribe |
Folder structure
python-tools/
├── Dockerfile ← Bakes the rembg + whisper models into the image at build time
├── requirements.txt
├── .env.example
└── app/
├── main.py ← FastAPI app: CORS, health, router wiring
├── config.py ← Settings (pydantic-settings)
├── logging_config.py ← Structured stdout logging
└── routers/
├── remove_bg.py ← POST /api/remove-bg
└── transcribe.py ← POST /api/transcribe
Deploy to Hugging Face Spaces (Docker)
Create a new Space → SDK Docker, hardware CPU basic (free, 16 GB RAM).
From this folder:
git init
git remote add hf https://huggingface.co/spaces/<your-user>/python-tools
git add .
git commit -m "feat: initial python tools service"
git push hf main
Wait for the build (~5–8 min — the model is baked into the image).
Set ALLOWED_ORIGINS in Settings → Variables and secrets, e.g.:
https://smitparekh.co.in,https://smitparekh-web.vercel.app
Point the API at it — in smitparekh-api/.env:
PYTHON_API_URL=https://<your-user>-python-tools.hf.space
And in smitparekh-web/.env.local:
NEXT_PUBLIC_PYTHON_API_URL=https://<your-user>-python-tools.hf.space
Docker (anywhere else)
docker build -t portfolio-python-tools .
docker run --rm -p 7860:7860 \
-e ALLOWED_ORIGINS="http://localhost:3000" \
portfolio-python-tools
Adding more tools
- Drop a router file in
app/routers/<name>.py exposing an APIRouter.
- Wire it up in
app/main.py with app.include_router(...).
- Add the corresponding typed API call in
smitparekh-web/lib/api/<name>.ts and a React Query hook in smitparekh-web/hooks/api/use-<name>.ts.
Notes
- Cold starts on HF Spaces: ~30–60 s after 48 h idle. The model is pre-downloaded into the Docker image (see
Dockerfile) so requests served by a warm container are fast.
- u2net model lives at
/tmp/.u2net inside the container (writable layer).
- Logs go to stdout; HF Spaces captures them in the Space logs panel.
- The Node API (
smitparekh-api) uses this service as its primary remove-bg provider and falls back to the Remove.bg commercial API when PYTHON_API_URL is unset or the request fails.