# neelsshah2006/smartstock architecture

> Architecture diagram of the GitHub repository neelsshah2006/smartstock, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-14). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/neelsshah2006/smartstock
- Repository: https://github.com/neelsshah2006/smartstock

## Overview

1. **Overall shape**
   - SmartStock is a two-tier Python system: a **Streamlit frontend** for user interaction and a **FastAPI backend** for validation, persistence, and ML inference.
   - The backend is the real system boundary: the UI never talks to MongoDB or the model files directly.

2. **User-facing layer**
   - `frontend/app.py` is the Streamlit entrypoint.
   - `frontend/pages/add_data.py` handles historical record entry.
   - `frontend/pages/predict.py` drives forecasting and halt checks.
   - `frontend/components/forms.py` and `cards.py` are reusable UI pieces for input and result display.

3. **API layer**
   - `backend/app/main.py` assembles the FastAPI app and mounts routers.
   - `backend/app/routes/data.py` exposes data-ingestion endpoints.
   - `backend/app/routes/prediction.py` exposes inference endpoints.
   - `backend/app/schemas/schemas.py` defines request/response contracts, so the frontend and backend communicate through typed payloads.

4. **Persistence boundary**
   - `backend/app/database/mongodb.py` is the database connection layer.
   - MongoDB is the durable store for historical retail records and likely the source of truth for data entered through the UI.
   - The README makes MongoDB a required runtime dependency, so persistence is part of the online architecture, not just training.

5. **Inference services**
   - `backend/app/services/forecasting_service.py` handles sales prediction.
   - `backend/app/services/halt_service.py` handles the reorder-halt decision.
   - `backend/app/services/preprocessing_service.py` prepares incoming records into the exact model-ready shape before inference.
   - This separation suggests a pipeline: ingest input → preprocess → forecast → decide halt.

6. **Feature engineering and encoding**
   - `backend/app/utils/feature_engineering.py` and `backend/app/utils/encoders.py` encapsulate domain-specific transformations.
   - `backend/app/models/label_encoders.pkl`, `forecast_features.pkl`, and `halt_features.pkl` indicate the runtime relies on persisted feature definitions and categorical encodings, not ad hoc transformation in the UI.
   - `backend/app/utils/validators.py` is the guardrail layer for rejecting malformed or inconsistent data before it reaches the models or database.

7. **ML artifacts and model boundary**
   - The backend ships with pre-trained artifacts: `forecasting_model.pkl` and `halt_model.pkl`.
   - The README says forecasting uses **XGBoost regression**, and the repo’s model files show the online path is artifact-based inference rather than live training.
   - This makes model loading an internal backend concern; the UI only sees prediction results and halt recommendations.

8. **Offline training pipeline**
   - `backend/train_models.py` is the model-building entrypoint.
   - `SmartStock.ipynb` appears to be the exploratory/development notebook used to iterate on the ML approach.
   - Training data lives under `backend/data/` and mirrored top-level `data/` (`train.csv`, `test.csv`, `store.csv`), which are the offline inputs used to build and evaluate the shipped artifacts.

9. **Data ingestion flow**
   - A typical “add historical data” path is: Streamlit form → FastAPI data route → schema validation → preprocessing/validation → MongoDB insert.
   - This keeps raw business data centralized in the backend, where it can later be used for retraining or contextual inference.

10. **Prediction flow**
   - A prediction request is: Streamlit predict page → FastAPI prediction route → preprocessing/feature engineering → forecasting model → halt model.
   - The response likely combines the forecasted sales value with a halt/no-halt recommendation, which the frontend renders as cards or summary output.

11. **Operational separation**
   - The repo cleanly separates **presentation**, **API orchestration**, **data persistence**, **feature engineering**, and **model inference**.
   - That separation is important for architecture diagrams: the frontend is a thin client, the backend is the orchestrator, MongoDB is stateful storage, and the `.pkl` files are deployed ML assets.

12. **Interface and developer tooling**
   - The backend exposes standard FastAPI docs at `/docs`, which is useful for inspecting the prediction and data APIs.
   - `.env.example` files in backend and frontend show environment-based configuration, with MongoDB connection being the main external runtime dependency.

Main entry points: [`backend/train_models.py`](https://github.com/neelsshah2006/smartstock/blob/main/backend/train_models.py) (Train Models), [`backend/app/services/preprocessing_service.py`](https://github.com/neelsshah2006/smartstock/blob/main/backend/app/services/preprocessing_service.py) (Preprocess), [`backend/app/routes/data.py`](https://github.com/neelsshah2006/smartstock/blob/main/backend/app/routes/data.py) (Data API), [`backend/app/main.py`](https://github.com/neelsshah2006/smartstock/blob/main/backend/app/main.py) (API App)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["Frontend"]
  node_frontend_app["App<br/>streamlit entrypoint<br/>[app.py]"]
  node_frontend_add_data["Add Data<br/>ui page<br/>[add_data.py]"]
  node_frontend_predict["Predict<br/>ui page<br/>[predict.py]"]
  node_frontend_forms["Forms<br/>ui components<br/>[forms.py]"]
  node_frontend_cards["Cards<br/>ui components<br/>[cards.py]"]
end

subgraph group_backend_api["Backend API"]
  node_backend_main["API App<br/>[main.py]"]
  node_route_data["Data API<br/>route module<br/>[data.py]"]
  node_route_prediction["Predict API<br/>route module<br/>[prediction.py]"]
  node_schemas["Schemas<br/>contracts<br/>[schemas.py]"]
end

subgraph group_backend_core["Backend Core"]
  node_mongodb[("MongoDB<br/>database layer<br/>[mongodb.py]")]
  node_preprocessing["Preprocess"]
  node_forecasting["Forecasting"]
  node_halt["Halt Logic<br/>[halt_service.py]"]
  node_feature_eng["Features<br/>transform utils"]
  node_encoders["Encoders<br/>transform utils<br/>[encoders.py]"]
  node_validators["Validators<br/>guardrails<br/>[validators.py]"]
end

subgraph group_ml_assets["ML Assets"]
  node_forecast_model(("Forecast Model<br/>artifact"))
  node_halt_model(("Halt Model<br/>artifact<br/>[halt_model.pkl]"))
  node_label_encoders(("Label Encoders<br/>artifact<br/>[label_encoders.pkl]"))
  node_forecast_features(("Forecast Feats<br/>artifact"))
  node_halt_features(("Halt Feats<br/>artifact<br/>[halt_features.pkl]"))
end

subgraph group_offline["Offline Training"]
  node_train_models["Train Models<br/>training entrypoint<br/>[train_models.py]"]
  node_notebook["Notebook<br/>[SmartStock.ipynb]"]
  node_train_data[("Training Data<br/>datasets<br/>[train.csv]")]
end

node_frontend_app -->|"routes to"| node_frontend_add_data
node_frontend_app -->|"routes to"| node_frontend_predict
node_frontend_add_data -->|"uses"| node_frontend_forms
node_frontend_predict -->|"uses"| node_frontend_cards
node_frontend_add_data -->|"calls"| node_backend_main
node_frontend_predict -->|"calls"| node_backend_main
node_backend_main -->|"mounts"| node_route_data
node_backend_main -->|"mounts"| node_route_prediction
node_route_data -->|"validates"| node_schemas
node_route_prediction -->|"validates"| node_schemas
node_route_data -->|"checks"| node_validators
node_route_data -->|"prepares"| node_preprocessing
node_route_data -->|"writes"| node_mongodb
node_route_prediction -->|"prepares"| node_preprocessing
node_preprocessing -->|"derives"| node_feature_eng
node_preprocessing -->|"encodes"| node_encoders
node_preprocessing -->|"feeds"| node_forecast_model
node_preprocessing -->|"feeds"| node_halt_model
node_forecasting -->|"loads"| node_forecast_model
node_halt -->|"loads"| node_halt_model
node_forecasting -->|"depends on"| node_forecast_features
node_halt -->|"depends on"| node_halt_features
node_feature_eng -->|"uses"| node_label_encoders
node_train_models -->|"trains on"| node_train_data
node_train_models -.->|"iterates with"| node_notebook
node_train_models -->|"produces"| node_forecast_model
node_train_models -->|"produces"| node_halt_model
node_train_models -->|"produces"| node_label_encoders
node_train_models -->|"produces"| node_forecast_features
node_train_models -->|"produces"| node_halt_features

click node_frontend_app "https://github.com/neelsshah2006/smartstock/blob/main/frontend/app.py"
click node_frontend_add_data "https://github.com/neelsshah2006/smartstock/blob/main/frontend/pages/add_data.py"
click node_frontend_predict "https://github.com/neelsshah2006/smartstock/blob/main/frontend/pages/predict.py"
click node_frontend_forms "https://github.com/neelsshah2006/smartstock/blob/main/frontend/components/forms.py"
click node_frontend_cards "https://github.com/neelsshah2006/smartstock/blob/main/frontend/components/cards.py"
click node_backend_main "https://github.com/neelsshah2006/smartstock/blob/main/backend/app/main.py"
click node_route_data "https://github.com/neelsshah2006/smartstock/blob/main/backend/app/routes/data.py"
click node_route_prediction "https://github.com/neelsshah2006/smartstock/blob/main/backend/app/routes/prediction.py"
click node_schemas "https://github.com/neelsshah2006/smartstock/blob/main/backend/app/schemas/schemas.py"
click node_mongodb "https://github.com/neelsshah2006/smartstock/blob/main/backend/app/database/mongodb.py"
click node_preprocessing "https://github.com/neelsshah2006/smartstock/blob/main/backend/app/services/preprocessing_service.py"
click node_forecasting "https://github.com/neelsshah2006/smartstock/blob/main/backend/app/services/forecasting_service.py"
click node_halt "https://github.com/neelsshah2006/smartstock/blob/main/backend/app/services/halt_service.py"
click node_feature_eng "https://github.com/neelsshah2006/smartstock/blob/main/backend/app/utils/feature_engineering.py"
click node_encoders "https://github.com/neelsshah2006/smartstock/blob/main/backend/app/utils/encoders.py"
click node_validators "https://github.com/neelsshah2006/smartstock/blob/main/backend/app/utils/validators.py"
click node_forecast_model "https://github.com/neelsshah2006/smartstock/blob/main/backend/app/models/forecasting_model.pkl"
click node_halt_model "https://github.com/neelsshah2006/smartstock/blob/main/backend/app/models/halt_model.pkl"
click node_label_encoders "https://github.com/neelsshah2006/smartstock/blob/main/backend/app/models/label_encoders.pkl"
click node_forecast_features "https://github.com/neelsshah2006/smartstock/blob/main/backend/app/models/forecast_features.pkl"
click node_halt_features "https://github.com/neelsshah2006/smartstock/blob/main/backend/app/models/halt_features.pkl"
click node_train_models "https://github.com/neelsshah2006/smartstock/blob/main/backend/train_models.py"
click node_notebook "https://github.com/neelsshah2006/smartstock/blob/main/SmartStock.ipynb"
click node_train_data "https://github.com/neelsshah2006/smartstock/blob/main/backend/data/train.csv"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_frontend_app,node_frontend_add_data,node_frontend_predict,node_frontend_forms,node_frontend_cards toneBlue
class node_backend_main,node_route_data,node_route_prediction,node_schemas toneAmber
class node_mongodb,node_preprocessing,node_forecasting,node_halt,node_feature_eng,node_encoders,node_validators toneMint
class node_forecast_model,node_halt_model,node_label_encoders,node_forecast_features,node_halt_features toneRose
class node_train_models,node_notebook,node_train_data toneIndigo
```

## Components

### Frontend

Streamlit user interface and reusable UI pieces.

- [App](https://github.com/neelsshah2006/smartstock/blob/main/frontend/app.py): streamlit entrypoint · Main Streamlit application shell. · `frontend/app.py`
- [Add Data](https://github.com/neelsshah2006/smartstock/blob/main/frontend/pages/add_data.py): ui page · Page for entering historical records. · `frontend/pages/add_data.py`
- [Predict](https://github.com/neelsshah2006/smartstock/blob/main/frontend/pages/predict.py): ui page · Page for forecasting and halt checks. · `frontend/pages/predict.py`
- [Forms](https://github.com/neelsshah2006/smartstock/blob/main/frontend/components/forms.py): ui components · Reusable input form components. · `frontend/components/forms.py`
- [Cards](https://github.com/neelsshah2006/smartstock/blob/main/frontend/components/cards.py): ui components · Reusable result display components. · `frontend/components/cards.py`

### Backend API

FastAPI app, routes, schemas, and request validation.

- [API App](https://github.com/neelsshah2006/smartstock/blob/main/backend/app/main.py): FastAPI app · Assembles the FastAPI application and mounts routers. · `backend/app/main.py`
- [Data API](https://github.com/neelsshah2006/smartstock/blob/main/backend/app/routes/data.py): route module · Endpoints for historical data ingestion. · `backend/app/routes/data.py`
- [Predict API](https://github.com/neelsshah2006/smartstock/blob/main/backend/app/routes/prediction.py): route module · Endpoints for inference requests. · `backend/app/routes/prediction.py`
- [Schemas](https://github.com/neelsshah2006/smartstock/blob/main/backend/app/schemas/schemas.py): contracts · Request and response payload definitions. · `backend/app/schemas/schemas.py`

### Backend Core

Persistence, preprocessing, feature engineering, and inference services.

- [MongoDB](https://github.com/neelsshah2006/smartstock/blob/main/backend/app/database/mongodb.py): database layer · Connection and access layer for durable retail records. · `backend/app/database/mongodb.py`
- [Preprocess](https://github.com/neelsshah2006/smartstock/blob/main/backend/app/services/preprocessing_service.py): service · Shapes incoming records for model-ready inference. · `backend/app/services/preprocessing_service.py`
- [Forecasting](https://github.com/neelsshah2006/smartstock/blob/main/backend/app/services/forecasting_service.py): service · Sales prediction service. · `backend/app/services/forecasting_service.py`
- [Halt Logic](https://github.com/neelsshah2006/smartstock/blob/main/backend/app/services/halt_service.py): service · Reorder-halt decision service. · `backend/app/services/halt_service.py`
- [Features](https://github.com/neelsshah2006/smartstock/blob/main/backend/app/utils/feature_engineering.py): transform utils · Domain-specific feature engineering helpers. · `backend/app/utils/feature_engineering.py`
- [Encoders](https://github.com/neelsshah2006/smartstock/blob/main/backend/app/utils/encoders.py): transform utils · Categorical encoding helpers. · `backend/app/utils/encoders.py`
- [Validators](https://github.com/neelsshah2006/smartstock/blob/main/backend/app/utils/validators.py): guardrails · Input validation and consistency checks. · `backend/app/utils/validators.py`

### ML Assets

Persisted model and feature artifacts used at runtime and training data used offline.

- [Forecast Model](https://github.com/neelsshah2006/smartstock/blob/main/backend/app/models/forecasting_model.pkl): artifact · Persisted XGBoost regression model. · `backend/app/models/forecasting_model.pkl`
- [Halt Model](https://github.com/neelsshah2006/smartstock/blob/main/backend/app/models/halt_model.pkl): artifact · Persisted decision model for halt recommendations. · `backend/app/models/halt_model.pkl`
- [Label Encoders](https://github.com/neelsshah2006/smartstock/blob/main/backend/app/models/label_encoders.pkl): artifact · Persisted categorical encoders used by the runtime. · `backend/app/models/label_encoders.pkl`
- [Forecast Feats](https://github.com/neelsshah2006/smartstock/blob/main/backend/app/models/forecast_features.pkl): artifact · Feature definition bundle for forecasting. · `backend/app/models/forecast_features.pkl`
- [Halt Feats](https://github.com/neelsshah2006/smartstock/blob/main/backend/app/models/halt_features.pkl): artifact · Feature definition bundle for halt logic. · `backend/app/models/halt_features.pkl`

### Offline Training

Model-building entrypoints and exploratory analysis.

- [Train Models](https://github.com/neelsshah2006/smartstock/blob/main/backend/train_models.py): training entrypoint · Offline model-building script. · `backend/train_models.py`
- [Notebook](https://github.com/neelsshah2006/smartstock/blob/main/SmartStock.ipynb): analysis notebook · Exploratory development and model iteration. · `SmartStock.ipynb`
- [Training Data](https://github.com/neelsshah2006/smartstock/blob/main/backend/data/train.csv): datasets · CSV inputs used for offline training and evaluation. · `backend/data/train.csv`

## Connections

- App → Add Data: routes to · The app exposes the data-entry page.
- App → Predict: routes to · The app exposes the prediction page.
- Add Data → Forms: uses · Data entry is built from reusable forms.
- Predict → Cards: uses · Prediction output is rendered with cards.
- Add Data → API App: calls · Historical record submission goes through the API.
- Predict → API App: calls · Prediction requests go through the API.
- API App → Data API: mounts · Data endpoints are registered on the app.
- API App → Predict API: mounts · Prediction endpoints are registered on the app.
- Data API → Schemas: validates · Incoming payloads use typed contracts.
- Predict API → Schemas: validates · Inference payloads use typed contracts.
- Data API → Validators: checks · Malformed records are rejected before persistence.
- Data API → Preprocess: prepares · Incoming data is normalized before storage.
- Data API → MongoDB: writes · Validated historical data is persisted.
- Predict API → Preprocess: prepares · Requests are shaped for inference.
- Preprocess → Features: derives · Feature engineering produces model inputs.
- Preprocess → Encoders: encodes · Categorical values are transformed consistently.
- Preprocess → Forecast Model: feeds · Prepared inputs go to the forecasting model.
- Preprocess → Halt Model: feeds · Prepared inputs go to the halt model.
- Forecasting → Forecast Model: loads · Forecast service uses the persisted model artifact.
- Halt Logic → Halt Model: loads · Halt service uses the persisted model artifact.
- Forecasting → Forecast Feats: depends on · Forecast service relies on stored feature definitions.
- Halt Logic → Halt Feats: depends on · Halt service relies on stored feature definitions.
- Features → Label Encoders: uses · Feature logic relies on persisted encoders.
- Train Models → Training Data: trains on · Offline training consumes CSV datasets.
- Train Models → Notebook: iterates with · Notebook supports experimentation and refinement.
- Train Models → Forecast Model: produces · Training generates the forecasting artifact.
- Train Models → Halt Model: produces · Training generates the halt artifact.
- Train Models → Label Encoders: produces · Training persists encoding artifacts.
- Train Models → Forecast Feats: produces · Training persists forecasting feature metadata.
- Train Models → Halt Feats: produces · Training persists halt feature metadata.
