Angshuman28/ampere-hf-backend
AMPERE HF Space Backend (Phase B Skeleton)
Minimal FastAPI backend that wraps the Phase A inference bundle (outputs/inference_bundle/appliance_8ch/ in the research repo, duplicated under models/appliance_8ch/ here) and exposes:
This is Phase B. It is intended to live inside the research repo for now; the directory can be lifted out and pushed to a Hugging Face Space once the encoder has been golden-tested (see app/inference/encoder.py for the TODO).
Layout
ampere-hf-space/
app/ FastAPI app + routes + inference glue
main.py create_app(), lifespan loads the bundle
schemas.py Pydantic request/response models
routes/ One module per endpoint
inference/ bundle.py, encoder.py, runner.py, ...
state/device_state.py Per-device ring buffer + EWMA stats
recommendations/rules.py Day-1 rule engine
scheduler/adaptive.py Day-1 balanced_transition_change policy
util/ config, logging, executor helpers
ampere_lib/ Vendored minimal slice of ampere.neural
neural/observer_models.py DwellObserver architecture (verbatim)
neural/dwell_dataset.py DwellNormalizer
neural/utils.py require_torch + resolve_device
models/appliance_8ch/ Bundle copied from outputs/inference_bundle/
tests/ pytest + TestClient
README.md
requirements.txt
pyproject.tomlRunning locally
# from this directory
python -m pip install -r requirements.txt
python -m uvicorn app.main:app --host 0.0.0.0 --port 7860Environment variables:
No Turso, no Hugging Face, no third-party tokens are read. Secrets stay outside this Space.
Quick smoke
curl -s http://localhost:7860/health
curl -s http://localhost:7860/version
curl -s -X POST http://localhost:7860/reconstruct \
-H "Content-Type: application/json" \
-d '{"device_id":"home-01","timestamp":"2026-05-13T15:30:25Z",
"selected_branch_id":3,"observed_power_w":1247.3,"dwell_seconds":0.96}'
curl -s -X POST http://localhost:7860/schedule/next \
-H "Content-Type: application/json" \
-d '{"device_id":"home-01","current_time":"2026-05-13T15:30:25Z"}'
curl -s http://localhost:7860/recommendations/home-01Running the tests
python -m pytest -qTests cover: health, bundle loading, encoder shape/finiteness, the reconstruct flow end-to-end, device state, the recommendation engine, and the adaptive scheduler.
Phase B golden-parity TODO
app/inference/encoder.py rebuilds the 23 features documented in the bundle's feature_contract.json, but does not golden-test against the research repo's DwellWindowDataset.__getitem__. See the module docstring for details and the explicit follow-up before this skeleton ships to production.
What this backend does NOT do (Phase B scope)
- No deployment to Hugging Face Spaces yet.
- No Turso writes (Flutter owns the durable layer).
- No Flutter or Digital Twin edits.
- No RL scheduler (Day-2 will add a separate service).
- No model retraining.
- No user auth beyond an optional admin token on
/device/reset. - No background jobs.
