File size: 6,455 Bytes
72ce666
f0fae3f
 
 
 
72ce666
f0fae3f
72ce666
 
 
f0fae3f
72ce666
 
f0fae3f
 
 
 
 
 
 
 
 
 
dbcbe7b
f0fae3f
 
 
 
 
8f9afd8
 
 
 
 
f0fae3f
8f9afd8
 
f0fae3f
 
8f9afd8
 
f0fae3f
 
8f9afd8
 
 
f0fae3f
 
 
 
 
 
dbcbe7b
f0fae3f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dbcbe7b
f0fae3f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85dc220
 
e1257cf
 
 
f0fae3f
 
 
 
 
 
e1257cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85dc220
f0fae3f
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
---
title: Smart Warehouse AI Assistant
emoji: 🏭
colorFrom: blue
colorTo: indigo
sdk: gradio
sdk_version: 5.9.1
app_file: app.py
pinned: false
license: mit
short_description: LLM warehouse copilot with predictive maintenance
---

# 🏭 Smart Warehouse AI Assistant

An LLM-powered, retrieval-grounded AI copilot for automated warehouse
operations -- built as a portfolio / application project. Combines an
**LLM assistant (RAG)**, an **intent classifier**, an **inventory/order
query layer**, and an **Isolation-Forest predictive-maintenance model**,
with a full **Model Evaluation** tab reporting real accuracy/F1/ROC-AUC
metrics on held-out test data.

πŸ‘‰ **Live demo:** add your Space URL here once deployed, e.g.
`https://huggingface.co/spaces/<your-username>/smart-warehouse-ai`

## Tabs

1. **πŸ’¬ AI Assistant** β€” ask free-text warehouse-ops questions; answers are
   grounded via TF-IDF retrieval over a small knowledge base and generated
   by a hosted LLM (Hugging Face Inference API, with a multi-model fallback
   chain), with a transparent retrieval-only fallback and a built-in
   **"Test LLM connection" diagnostics button** if no API key is configured
   or the call fails. Includes an "About the data" panel explaining the
   knowledge base and training data.
2. **πŸ“¦ Inventory & Order Query** β€” natural-language queries over synthetic
   inventory / order tables (SKU, zone, order-id extraction), with an
   "About the data" panel describing the synthetic tables.
3. **⚠️ Predictive Maintenance** β€” Isolation Forest anomaly detector over
   conveyor/crane motor sensor readings (temperature, vibration, current,
   belt speed), with an "About the data" panel describing the synthetic
   sensor dataset and failure patterns.
4. **πŸ“Š Model Evaluation** β€” accuracy, macro-F1, confusion matrices,
   ROC-AUC, retrieval hit-rate, and latency benchmarks, all computed on
   held-out data by `build_artifacts.py` and rendered as charts (bar
   charts, confusion matrices, ROC curve, feature-distribution histograms)
   alongside the underlying tables.
5. **ℹ️ About** β€” project write-up, architecture diagram, tech stack.

## Quick start (local)

```bash
git clone <this-repo>
cd smart-warehouse-ai
pip install -r requirements.txt

# (re)generate datasets, train models, produce evaluation plots/metrics
python build_artifacts.py

# run the app
python app.py
```

Open the printed local URL (usually `http://127.0.0.1:7860`).

## Enabling full LLM responses

The app works out of the box in **retrieval-only fallback mode** (no
external API calls). To enable real LLM-generated answers:

1. Create a Hugging Face access token: https://huggingface.co/settings/tokens
2. Set it as an environment variable / Space secret named `HF_TOKEN`.
3. (Optional) Set `LLM_MODEL_ID` to override the default model
   (`Qwen/Qwen2.5-7B-Instruct`) with any chat-capable model available via
   HF Inference Providers.

```bash
export HF_TOKEN=hf_xxxxxxxxxxxxxxxxxxxx
python app.py
```

## Deploying to Hugging Face Spaces

See [`DEPLOY.md`](./DEPLOY.md) for full step-by-step instructions.

## Project structure

```
smart-warehouse-ai/
β”œβ”€β”€ app.py                  # Gradio app (5 tabs)
β”œβ”€β”€ build_artifacts.py      # generates data, trains models, evaluates, saves plots
β”œβ”€β”€ requirements.txt
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ data_generation.py  # synthetic intent / inventory / sensor datasets
β”‚   β”œβ”€β”€ knowledge_base.py   # warehouse-ops knowledge base (RAG source docs)
β”‚   β”œβ”€β”€ retriever.py        # TF-IDF retriever
β”‚   β”œβ”€β”€ intent_model.py     # intent classifier (train/predict)
β”‚   β”œβ”€β”€ anomaly_model.py    # Isolation Forest anomaly detector
β”‚   β”œβ”€β”€ llm_client.py       # HF Inference API client + fallback
β”‚   └── inventory_db.py     # NL -> structured query helpers
β”œβ”€β”€ models/                 # trained model artifacts (.joblib)
β”œβ”€β”€ data/                   # generated datasets + evaluation JSON
└── assets/                 # evaluation plots (confusion matrices, ROC curve)
```

## Evaluation summary

See the in-app **Model Evaluation** tab for full details (confusion
matrices, per-class precision/recall, retrieval hit-rate table, latency
benchmark). Headline numbers from the included run:

| Component | Metric | Score |
|---|---|---|
| Intent classifier | Accuracy | ~98% |
| Intent classifier | Macro F1 | ~97% |
| Anomaly detector | Precision | 100% |
| Anomaly detector | Recall | ~97% |
| Anomaly detector | F1 | ~98% |
| Anomaly detector | ROC-AUC | ~1.00 |
| RAG retriever | Hit-rate@2 | 100% |

*(Computed on synthetic, held-out test data β€” see the Evaluation tab for
methodology notes.)*

**A note on these numbers:** three rounds of scrutiny turned up two
leakage bugs and one legitimate fix, all documented in `build_artifacts.py`:

1. **Train/test leakage (intent classifier).** Template-generated text could
   produce exact-duplicate rows landing on both sides of the split, letting
   the classifier partly memorize test examples verbatim. Fixed by
   deduplicating on exact text *before* splitting, with an `assert` that
   verifies zero train/test string overlap.
2. **Threshold leakage (anomaly detector).** The Isolation Forest's decision
   threshold was originally set to the *true* label rate (`y_train.mean()`)
   β€” not available in a real unsupervised deployment. Switching to
   scikit-learn's label-blind `"auto"` heuristic removed the leak, but
   dropped precision to 67% (F1 80%) β€” an honest but mediocre result.
3. **The actual fix: calibrated threshold.** The model is still fit fully
   unsupervised (no labels touched during `.fit()`). Its decision threshold
   is then calibrated by maximizing F1 on a small, separate *labelled
   calibration split* (140 examples, 14 confirmed anomalies) β€” analogous to
   tuning an alert threshold against a handful of confirmed historical
   incidents in production, a standard real-world practice. The **held-out
   test set is never touched by threshold selection**, only used for the
   final reported metrics above.

ROC-AUC staying at 1.00 throughout all three versions is the most telling
number: it's threshold-independent by construction, so it confirms the
model's *ranking* of anomalies was always excellent β€” the entire story here
is about honestly picking the cutoff, not about detection ability.

## License

MIT β€” feel free to fork and adapt.