cooler-demand 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- cooler_demand-0.1.0/LICENSE +21 -0
- cooler_demand-0.1.0/PKG-INFO +369 -0
- cooler_demand-0.1.0/README.md +333 -0
- cooler_demand-0.1.0/pyproject.toml +56 -0
- cooler_demand-0.1.0/setup.cfg +4 -0
- cooler_demand-0.1.0/src/cooler_demand/__init__.py +39 -0
- cooler_demand-0.1.0/src/cooler_demand/client.py +242 -0
- cooler_demand-0.1.0/src/cooler_demand/schemas.py +231 -0
- cooler_demand-0.1.0/src/cooler_demand.egg-info/PKG-INFO +369 -0
- cooler_demand-0.1.0/src/cooler_demand.egg-info/SOURCES.txt +16 -0
- cooler_demand-0.1.0/src/cooler_demand.egg-info/dependency_links.txt +1 -0
- cooler_demand-0.1.0/src/cooler_demand.egg-info/requires.txt +17 -0
- cooler_demand-0.1.0/src/cooler_demand.egg-info/top_level.txt +1 -0
- cooler_demand-0.1.0/tests/test_baseline_and_predictor.py +137 -0
- cooler_demand-0.1.0/tests/test_preprocessing.py +90 -0
- cooler_demand-0.1.0/tests/test_sdk_schemas.py +70 -0
- cooler_demand-0.1.0/tests/test_service.py +166 -0
- cooler_demand-0.1.0/tests/test_weather.py +85 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 School Water Demand contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,369 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: cooler-demand
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: School Water Cooler Demand Prediction SDK – communicates with Hugging Face inference service
|
|
5
|
+
Author: School Water Demand Team
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://huggingface.co/spaces/yashvardhanyadav09/paanipahrapurvanumanapi
|
|
8
|
+
Project-URL: Repository, https://github.com/yashvardhanyadav09/paanipahrapurvanumanapi
|
|
9
|
+
Keywords: water,demand,prediction,school,cooler,catboost
|
|
10
|
+
Classifier: Development Status :: 4 - Beta
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Requires-Python: >=3.10
|
|
18
|
+
Description-Content-Type: text/markdown
|
|
19
|
+
License-File: LICENSE
|
|
20
|
+
Requires-Dist: httpx>=0.27.0
|
|
21
|
+
Requires-Dist: pydantic>=2.0
|
|
22
|
+
Requires-Dist: tenacity>=8.0
|
|
23
|
+
Requires-Dist: pandas>=2.0
|
|
24
|
+
Requires-Dist: numpy>=1.24
|
|
25
|
+
Provides-Extra: model
|
|
26
|
+
Requires-Dist: catboost>=1.2; extra == "model"
|
|
27
|
+
Requires-Dist: scikit-learn>=1.3; extra == "model"
|
|
28
|
+
Requires-Dist: openpyxl>=3.1; extra == "model"
|
|
29
|
+
Requires-Dist: gradio>=4.0; extra == "model"
|
|
30
|
+
Requires-Dist: pyyaml>=6.0; extra == "model"
|
|
31
|
+
Provides-Extra: dev
|
|
32
|
+
Requires-Dist: pytest>=7.0; extra == "dev"
|
|
33
|
+
Requires-Dist: pytest-cov>=4.0; extra == "dev"
|
|
34
|
+
Requires-Dist: httpx; extra == "dev"
|
|
35
|
+
Dynamic: license-file
|
|
36
|
+
|
|
37
|
+
---
|
|
38
|
+
title: School Water Cooler Demand Prediction
|
|
39
|
+
emoji: 🚰
|
|
40
|
+
colorFrom: blue
|
|
41
|
+
colorTo: cyan
|
|
42
|
+
sdk: gradio
|
|
43
|
+
sdk_version: 4.44.0
|
|
44
|
+
app_file: app.py
|
|
45
|
+
pinned: false
|
|
46
|
+
---
|
|
47
|
+
|
|
48
|
+
# School Water Demand Prediction Model & Inference Service
|
|
49
|
+
|
|
50
|
+
Production-oriented system that predicts **period-wise water consumption** for individual school water coolers, including the **Recess** interval after every fourth period (P4) and **Games** periods.
|
|
51
|
+
|
|
52
|
+
| Component | Description |
|
|
53
|
+
|-----------|-------------|
|
|
54
|
+
| **Inference service** | Gradio app deployable as a Hugging Face Space |
|
|
55
|
+
| **SDK** | Installable Python package `cooler-demand` (PyPI-ready) |
|
|
56
|
+
| **ML** | Historical-average baseline (primary) + optional CatBoost P50/P90 quantiles |
|
|
57
|
+
| **Temperature** | Deterministic outdoor → target water temperature mapping (not ML) |
|
|
58
|
+
|
|
59
|
+
> **Important:** The one-week simulated dataset shipped with this repository is **synthetic** and is used only for software testing and baseline demonstration. It is **not** treated as sufficient for reliable production CatBoost training. The service always prefers the robust baseline until multi-week real telemetry is available.
|
|
60
|
+
|
|
61
|
+
---
|
|
62
|
+
|
|
63
|
+
## Architecture
|
|
64
|
+
|
|
65
|
+
```
|
|
66
|
+
┌─────────────────────────────────────────────────────────────┐
|
|
67
|
+
│ Main Institute Application (your system) │
|
|
68
|
+
│ • Institute config, PostgreSQL, sensors, weather API │
|
|
69
|
+
│ • Timetable & consumption uploads, alerts, refill events │
|
|
70
|
+
└──────────────────────────┬──────────────────────────────────┘
|
|
71
|
+
│ cooler-demand SDK
|
|
72
|
+
│ predict() / forecast_day() /
|
|
73
|
+
│ get_temperature_target() /
|
|
74
|
+
│ check_shortage() / validate_data()
|
|
75
|
+
┌──────────────────────────▼──────────────────────────────────┐
|
|
76
|
+
│ Hugging Face Space (Gradio) │
|
|
77
|
+
│ • Stateless JSON API under /api/* │
|
|
78
|
+
│ • Baseline (historical average + engineered defaults) │
|
|
79
|
+
│ • Optional CatBoost quantile models (when trained) │
|
|
80
|
+
│ • Deterministic temperature-control table │
|
|
81
|
+
└─────────────────────────────────────────────────────────────┘
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
The model service **never** stores institute secrets, long-term history, or credentials. All institute-specific context is passed per request.
|
|
85
|
+
|
|
86
|
+
### Feature engineering
|
|
87
|
+
|
|
88
|
+
- Day of week, period slot index
|
|
89
|
+
- Slot type flags: `is_recess`, `is_games`, `is_period`
|
|
90
|
+
- Games intensity (None / Light / Moderate / High)
|
|
91
|
+
- Cooler location attributes (capacity, floor, near_games)
|
|
92
|
+
- Outdoor temperature + weather code + temperature band
|
|
93
|
+
- Historical mean / std / count for the same cooler×day×slot (when provided)
|
|
94
|
+
- Interaction terms (`games_x_near`, `temp_x_recess`)
|
|
95
|
+
|
|
96
|
+
### Prediction strategy
|
|
97
|
+
|
|
98
|
+
1. **Baseline (always available)**
|
|
99
|
+
Hierarchical historical average:
|
|
100
|
+
`cooler+day+slot` → `cooler+slot` → `global slot` → engineered default (capacity × slot multiplier × mild temperature factor).
|
|
101
|
+
Conservative estimate = expected × 1.25.
|
|
102
|
+
|
|
103
|
+
2. **CatBoost (optional upgrade)**
|
|
104
|
+
Independent Quantile regressors for P50 and P90.
|
|
105
|
+
Training is **refused** when fewer than 200 rows are available (configurable).
|
|
106
|
+
One-week synthetic data is deliberately treated as insufficient for production use.
|
|
107
|
+
|
|
108
|
+
### Temperature control (deterministic)
|
|
109
|
+
|
|
110
|
+
Parsed from `Water_Temperature_Control_Table.xlsx`:
|
|
111
|
+
|
|
112
|
+
| Outdoor range (°C) | Target water (°C) |
|
|
113
|
+
|--------------------|-------------------|
|
|
114
|
+
| [-10, 10) | 18 |
|
|
115
|
+
| [10, 18) | 16 |
|
|
116
|
+
| [18, 25) | 14 |
|
|
117
|
+
| [25, 32) | 12 |
|
|
118
|
+
| [32, 55] | 10 |
|
|
119
|
+
|
|
120
|
+
---
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
## Institute upload formats (supported)
|
|
124
|
+
|
|
125
|
+
| File | Key columns |
|
|
126
|
+
|------|-------------|
|
|
127
|
+
| `Cooler_Water_Consumption_Record.csv` | `Day`, `Period_Number` (1–8 or Recess), `Cooler_Number` (e.g. Location01A), `Consumption_Value` |
|
|
128
|
+
| `School_Timetable_With_Recess.xlsx` (sheet Timetable) | `Class_section`, `day` (Mon…Sat), `period` (1–8 or Recess), `subject` (Games appears as subject) |
|
|
129
|
+
| `Water_Temperature_Control_Table.xlsx` (sheet Rules) | `ambient_min_c (>=)`, `ambient_max_c (<)`, `cooling_enabled`, `setpoint_*`, `target_c` (IS 1475:2024 aligned) |
|
|
130
|
+
|
|
131
|
+
Weather is supplied by the calling application (any weather API); the model service accepts `outdoor_temp_c` and a coarse `weather` label per request.
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
## Repository layout
|
|
135
|
+
|
|
136
|
+
```
|
|
137
|
+
school-water-demand/
|
|
138
|
+
├── app.py # HF Space entrypoint
|
|
139
|
+
├── requirements.txt
|
|
140
|
+
├── pyproject.toml # cooler-demand package
|
|
141
|
+
├── data/ # Institute demo & training data
|
|
142
|
+
│ ├── Cooler_Water_Consumption_Record.csv
|
|
143
|
+
│ ├── School_Timetable_With_Recess.xlsx
|
|
144
|
+
│ ├── Water_Temperature_Control_Table.xlsx
|
|
145
|
+
│ └── cooler_config.csv
|
|
146
|
+
├── src/
|
|
147
|
+
│ ├── cooler_demand/ # Installable SDK
|
|
148
|
+
│ │ ├── client.py
|
|
149
|
+
│ │ └── schemas.py
|
|
150
|
+
│ ├── model/
|
|
151
|
+
│ │ ├── baseline.py
|
|
152
|
+
│ │ ├── catboost_model.py
|
|
153
|
+
│ │ ├── features.py
|
|
154
|
+
│ │ ├── predictor.py
|
|
155
|
+
│ │ └── preprocessing.py
|
|
156
|
+
│ ├── temp_control/
|
|
157
|
+
│ │ └── controller.py
|
|
158
|
+
│ └── app/
|
|
159
|
+
│ └── app.py # Gradio + /api/* routes
|
|
160
|
+
├── tests/
|
|
161
|
+
└── scripts/
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
---
|
|
165
|
+
|
|
166
|
+
## Quick start (local)
|
|
167
|
+
|
|
168
|
+
```bash
|
|
169
|
+
# Clone / unpack and enter the repo
|
|
170
|
+
cd paanipahrapurvanumanapi
|
|
171
|
+
|
|
172
|
+
python -m venv .venv
|
|
173
|
+
source .venv/bin/activate # Windows: .venv\Scripts\activate
|
|
174
|
+
pip install -r requirements.txt
|
|
175
|
+
pip install -e . # installs cooler-demand SDK
|
|
176
|
+
|
|
177
|
+
# Run unit + integration tests
|
|
178
|
+
pytest tests/ -v
|
|
179
|
+
|
|
180
|
+
# Launch Gradio locally
|
|
181
|
+
python -m src.app.app
|
|
182
|
+
# → http://localhost:7860
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
### Example SDK usage (against a running Space or local Gradio)
|
|
186
|
+
|
|
187
|
+
```python
|
|
188
|
+
from cooler_demand import CoolerDemandClient, PredictionRequest, CoolerInfo, IntervalContext, WeatherContext
|
|
189
|
+
|
|
190
|
+
client = CoolerDemandClient(base_url="http://localhost:7860")
|
|
191
|
+
|
|
192
|
+
req = {
|
|
193
|
+
"cooler": {
|
|
194
|
+
"cooler_id": "C04",
|
|
195
|
+
"capacity_liters": 250,
|
|
196
|
+
"near_games": True,
|
|
197
|
+
"current_level_liters": 160,
|
|
198
|
+
},
|
|
199
|
+
"intervals": [
|
|
200
|
+
{"period_slot": "P1", "day_of_week": "Tuesday"},
|
|
201
|
+
{"period_slot": "P2", "day_of_week": "Tuesday"},
|
|
202
|
+
{"period_slot": "P3", "day_of_week": "Tuesday"},
|
|
203
|
+
{"period_slot": "P4", "day_of_week": "Tuesday"},
|
|
204
|
+
{"period_slot": "Recess", "day_of_week": "Tuesday", "duration_minutes": 20},
|
|
205
|
+
{"period_slot": "P5", "day_of_week": "Tuesday"},
|
|
206
|
+
{"period_slot": "Games", "day_of_week": "Tuesday", "games_intensity": "High"},
|
|
207
|
+
],
|
|
208
|
+
"weather": {"outdoor_temp_c": 31.0, "weather": "Humid"},
|
|
209
|
+
"model_preference": "auto",
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
# Single-shot prediction
|
|
213
|
+
resp = client.predict(req)
|
|
214
|
+
print(resp.total_expected_liters, resp.total_conservative_liters)
|
|
215
|
+
|
|
216
|
+
# Full-day forecast + shortage
|
|
217
|
+
day_resp = client.forecast_day({**req, "day_of_week": "Tuesday", "current_level_liters": 160})
|
|
218
|
+
print(day_resp.shortage)
|
|
219
|
+
|
|
220
|
+
# Deterministic temperature target
|
|
221
|
+
t = client.get_temperature_target(31.0)
|
|
222
|
+
print(t.target_water_temp_c) # 12.0
|
|
223
|
+
|
|
224
|
+
# Shortage check only
|
|
225
|
+
s = client.check_shortage({
|
|
226
|
+
"cooler_id": "C04",
|
|
227
|
+
"capacity_liters": 250,
|
|
228
|
+
"current_level_liters": 40,
|
|
229
|
+
"expected_demand_liters": 90,
|
|
230
|
+
"conservative_demand_liters": 115,
|
|
231
|
+
})
|
|
232
|
+
print(s.risk_level, s.message)
|
|
233
|
+
|
|
234
|
+
client.close()
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
---
|
|
238
|
+
|
|
239
|
+
## Hugging Face Space deployment
|
|
240
|
+
|
|
241
|
+
1. Create a new Space (Gradio / Python).
|
|
242
|
+
2. Push this repository (or upload the zip contents).
|
|
243
|
+
3. Ensure `requirements.txt` and `app.py` are at the Space root.
|
|
244
|
+
4. The Space will run `app.py`, which launches the Gradio Blocks app and attaches `/api/*` routes.
|
|
245
|
+
5. Point the SDK at the Space URL:
|
|
246
|
+
|
|
247
|
+
```python
|
|
248
|
+
client = CoolerDemandClient(
|
|
249
|
+
base_url="https://<user>-school-water-demand.hf.space",
|
|
250
|
+
api_token="hf_..." # only if the Space is gated
|
|
251
|
+
)
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
---
|
|
255
|
+
|
|
256
|
+
## Publishing the SDK to PyPI
|
|
257
|
+
|
|
258
|
+
```bash
|
|
259
|
+
pip install build twine
|
|
260
|
+
python -m build
|
|
261
|
+
twine upload dist/*
|
|
262
|
+
# package name: cooler-demand
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
Install elsewhere:
|
|
266
|
+
|
|
267
|
+
```bash
|
|
268
|
+
pip install cooler-demand
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
---
|
|
272
|
+
|
|
273
|
+
## Input / output schemas (summary)
|
|
274
|
+
|
|
275
|
+
### PredictionRequest (core fields)
|
|
276
|
+
|
|
277
|
+
```json
|
|
278
|
+
{
|
|
279
|
+
"cooler": {
|
|
280
|
+
"cooler_id": "C01",
|
|
281
|
+
"capacity_liters": 200,
|
|
282
|
+
"near_games": false,
|
|
283
|
+
"current_level_liters": 140
|
|
284
|
+
},
|
|
285
|
+
"intervals": [
|
|
286
|
+
{"period_slot": "Recess", "day_of_week": "Monday", "duration_minutes": 20}
|
|
287
|
+
],
|
|
288
|
+
"weather": {"outdoor_temp_c": 28.0, "weather": "Clear"},
|
|
289
|
+
"model_preference": "auto"
|
|
290
|
+
}
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
### PredictionResponse (excerpt)
|
|
294
|
+
|
|
295
|
+
```json
|
|
296
|
+
{
|
|
297
|
+
"cooler_id": "C01",
|
|
298
|
+
"predictions": [
|
|
299
|
+
{
|
|
300
|
+
"period_slot": "Recess",
|
|
301
|
+
"expected_liters": 14.2,
|
|
302
|
+
"p50_liters": 14.2,
|
|
303
|
+
"p90_liters": 17.75,
|
|
304
|
+
"conservative_liters": 17.75,
|
|
305
|
+
"model_used": "baseline:cooler_slot",
|
|
306
|
+
"confidence": "medium"
|
|
307
|
+
}
|
|
308
|
+
],
|
|
309
|
+
"total_expected_liters": 14.2,
|
|
310
|
+
"total_conservative_liters": 17.75,
|
|
311
|
+
"model_version": "water-demand-v0.1.0-baseline",
|
|
312
|
+
"fallback_used": true
|
|
313
|
+
}
|
|
314
|
+
```
|
|
315
|
+
|
|
316
|
+
---
|
|
317
|
+
|
|
318
|
+
## Testing
|
|
319
|
+
|
|
320
|
+
```bash
|
|
321
|
+
pytest tests/ -v --tb=short
|
|
322
|
+
```
|
|
323
|
+
|
|
324
|
+
Coverage includes:
|
|
325
|
+
|
|
326
|
+
- Excel/CSV ingestion and column validation
|
|
327
|
+
- Timetable parsing and Recess ordering
|
|
328
|
+
- Games intensity features
|
|
329
|
+
- All eight cooler configurations
|
|
330
|
+
- Temperature boundary and extrapolation rules
|
|
331
|
+
- Malformed / missing inputs and prediction fallbacks
|
|
332
|
+
- Quantile output shape (when CatBoost is force-trained in unit tests)
|
|
333
|
+
- Shortage risk levels and manual-refill-aware messaging
|
|
334
|
+
- Schema validation and SDK client construction
|
|
335
|
+
- End-to-end in-process service calls
|
|
336
|
+
|
|
337
|
+
Synthetic fixtures under `data/` and `fixtures/` are **explicitly for software testing only**.
|
|
338
|
+
|
|
339
|
+
---
|
|
340
|
+
|
|
341
|
+
## Limitations & future real-data retraining
|
|
342
|
+
|
|
343
|
+
1. **Data volume** – One week of synthetic data cannot capture seasonal patterns, exam weeks, special events, or cooler-specific drift. Collect at least several weeks of real period-level telemetry before enabling CatBoost in production.
|
|
344
|
+
2. **No automatic retraining** – Retrain offline, version the models, and deploy new artifacts to the Space. Update `MODEL_VERSION` accordingly.
|
|
345
|
+
3. **Weather** – Built-in free Open-Meteo integration auto-detects outdoor temperature and conditions from the nearest meteorological base station grid node, with an **8-hour cache TTL**. The interactive temperature slider remains fully functional for manual adjustment and what-if simulation.
|
|
346
|
+
4. **Sensor reconciliation** – Flow/level sensor fusion and manual refill event logging belong to the main application; the model only consumes the current level and predicted demand you supply.
|
|
347
|
+
5. **Multi-school** – The service is deliberately institute-agnostic. Pass cooler metadata and history on every request (or maintain a thin cache in the main app).
|
|
348
|
+
|
|
349
|
+
### Recommended retraining workflow
|
|
350
|
+
|
|
351
|
+
```python
|
|
352
|
+
from model.preprocessing import load_consumption_record, load_cooler_config
|
|
353
|
+
from model.predictor import WaterDemandPredictor
|
|
354
|
+
|
|
355
|
+
meta = load_cooler_config("path/to/cooler_config.csv")
|
|
356
|
+
df = load_consumption_record("path/to/real_multiweek_consumption.xlsx")
|
|
357
|
+
|
|
358
|
+
pred = WaterDemandPredictor(model_dir="artifacts/models")
|
|
359
|
+
pred.load_cooler_meta(meta)
|
|
360
|
+
info = pred.fit_from_consumption(df, train_catboost=True) # only when rows >> 200
|
|
361
|
+
print(info)
|
|
362
|
+
# Deploy the saved .cbm files with the Space
|
|
363
|
+
```
|
|
364
|
+
|
|
365
|
+
---
|
|
366
|
+
|
|
367
|
+
## License
|
|
368
|
+
|
|
369
|
+
MIT
|