Spaces:
Building
Building
Commit ·
104d5d9
1
Parent(s): 8086823
docs: streamline README with natural technical overview
Browse files
README.md
CHANGED
|
@@ -1,8 +1,90 @@
|
|
| 1 |
---
|
| 2 |
-
title: HF Backend
|
| 3 |
emoji: ⚡
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: docker
|
| 7 |
app_port: 7860
|
| 8 |
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: HF Backend
|
| 3 |
emoji: ⚡
|
| 4 |
+
colorFrom: purple
|
| 5 |
+
colorTo: indigo
|
| 6 |
sdk: docker
|
| 7 |
app_port: 7860
|
| 8 |
---
|
| 9 |
+
|
| 10 |
+
# HF Backend
|
| 11 |
+
|
| 12 |
+
FastAPI host running on a Hugging Face Docker Space. It mounts backend routers from separate project repositories under specific URL prefixes.
|
| 13 |
+
|
| 14 |
+
## How it works
|
| 15 |
+
|
| 16 |
+
The host application does not define business logic. Each subproject lives in its own repository, exposes a router in `<package>/router.py`, and is installed as a Python package.
|
| 17 |
+
|
| 18 |
+
The host imports each router dynamically from `registry.py` and mounts it under a prefix. If an individual router fails to import, the host logs the error, records the failure, and keeps running for the other services.
|
| 19 |
+
|
| 20 |
+
```
|
| 21 |
+
/project-1/* -> HEARTFLOW_OS backend router
|
| 22 |
+
/project-2/* -> Semantic Comment Analyzer backend router
|
| 23 |
+
/health -> Host health status
|
| 24 |
+
```
|
| 25 |
+
|
| 26 |
+
## Adding a project
|
| 27 |
+
|
| 28 |
+
Adding a new service takes two steps:
|
| 29 |
+
|
| 30 |
+
1. Add the prefix and router path to `registry.py`:
|
| 31 |
+
```python
|
| 32 |
+
PROJECT_REGISTRY = {
|
| 33 |
+
"/project-1": "heartflow_backend.router:router",
|
| 34 |
+
"/project-2": "semantic_backend.router:router",
|
| 35 |
+
"/project-3": "new_project.router:router",
|
| 36 |
+
}
|
| 37 |
+
```
|
| 38 |
+
|
| 39 |
+
2. Add the pinned Git tag to `requirements.txt`:
|
| 40 |
+
```text
|
| 41 |
+
git+https://github.com/<user>/<repo>.git@v1.0#subdirectory=backend
|
| 42 |
+
```
|
| 43 |
+
|
| 44 |
+
Pushing to `main` syncs the change to Hugging Face and rebuilds the Space container.
|
| 45 |
+
|
| 46 |
+
## Configuration
|
| 47 |
+
|
| 48 |
+
Set these in the Space settings under **Variables and secrets**:
|
| 49 |
+
|
| 50 |
+
| Variable | Required | Default | Notes |
|
| 51 |
+
| :--- | :--- | :--- | :--- |
|
| 52 |
+
| `CORS_ALLOWED_ORIGINS` | No | None | Comma-separated allowed origins. Wildcards (`*`) are ignored. |
|
| 53 |
+
| `DISABLE_PROJECTS` | No | None | Comma-separated prefixes to skip loading (e.g. `project-2`). |
|
| 54 |
+
| `MAX_BODY_SIZE_BYTES` | No | 10485760 (10 MB) | Max request body size before returning 413. |
|
| 55 |
+
| `REQUEST_TIMEOUT_SECONDS` | No | 60.0 | Request timeout before returning 504. |
|
| 56 |
+
| `GITHUB_TOKEN` | Only for private repos | None | GitHub token passed as a build secret to clone private dependencies. |
|
| 57 |
+
|
| 58 |
+
GitHub repository secrets (used by GitHub Actions):
|
| 59 |
+
|
| 60 |
+
| Secret | Notes |
|
| 61 |
+
| :--- | :--- |
|
| 62 |
+
| `HF_BACKEND_SPACE` | Hugging Face write token used to push code and trigger Space restart. |
|
| 63 |
+
| `HF_SPACE_URL` | Optional. Health URL for the keep-alive workflow. |
|
| 64 |
+
|
| 65 |
+
## Endpoints
|
| 66 |
+
|
| 67 |
+
### `GET /health`
|
| 68 |
+
Returns the status of the host and each registered project.
|
| 69 |
+
|
| 70 |
+
```json
|
| 71 |
+
{
|
| 72 |
+
"status": "healthy",
|
| 73 |
+
"loaded": {
|
| 74 |
+
"/project-1": "heartflow_backend.router:router",
|
| 75 |
+
"/project-2": "semantic_backend.router:router"
|
| 76 |
+
},
|
| 77 |
+
"failed": {}
|
| 78 |
+
}
|
| 79 |
+
```
|
| 80 |
+
|
| 81 |
+
If any service fails to load, `status` changes to `"degraded"` or `"unhealthy"` and the error is listed under `"failed"`.
|
| 82 |
+
|
| 83 |
+
Interactive documentation (`/docs`, `/redoc`, `/openapi.json`) is disabled.
|
| 84 |
+
|
| 85 |
+
## Release workflow
|
| 86 |
+
|
| 87 |
+
1. Test changes in the project repository using its `dev.py`.
|
| 88 |
+
2. Tag the release (`git tag v1.0`, `git push origin v1.0`).
|
| 89 |
+
3. Update the tag in this repo's `requirements.txt`.
|
| 90 |
+
4. Push to `main`. The GitHub Actions workflow pushes to Hugging Face and restarts the Space.
|