How neelsshah2006/smartstock works
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: train_models.py Train Models · preprocessing_service.py Preprocess · data.py Data API · main.py API App
Components 24
Frontend
Streamlit user interface and reusable UI pieces.
- App · Main Streamlit application shell.
frontend/app.py - Add Data · Page for entering historical records.
frontend/pages/add_data.py - Predict · Page for forecasting and halt checks.
frontend/pages/predict.py - Forms · Reusable input form components.
frontend/components/forms.py - Cards · Reusable result display components.
frontend/components/cards.py
Backend API
FastAPI app, routes, schemas, and request validation.
- API App · Assembles the FastAPI application and mounts routers.
backend/app/main.py - Data API · Endpoints for historical data ingestion.
backend/app/routes/data.py - Predict API · Endpoints for inference requests.
backend/app/routes/prediction.py - Schemas · Request and response payload definitions.
backend/app/schemas/schemas.py
Backend Core
Persistence, preprocessing, feature engineering, and inference services.
- MongoDB · Connection and access layer for durable retail records.
backend/app/database/mongodb.py - Preprocess · Shapes incoming records for model-ready inference.
backend/app/services/preprocessing_service.py - Forecasting · Sales prediction service.
backend/app/services/forecasting_service.py - Halt Logic · Reorder-halt decision service.
backend/app/services/halt_service.py - Features · Domain-specific feature engineering helpers.
backend/app/utils/feature_engineering.py - Encoders · Categorical encoding helpers.
backend/app/utils/encoders.py - Validators · 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 · Persisted XGBoost regression model.
backend/app/models/forecasting_model.pkl - Halt Model · Persisted decision model for halt recommendations.
backend/app/models/halt_model.pkl - Label Encoders · Persisted categorical encoders used by the runtime.
backend/app/models/label_encoders.pkl - Forecast Feats · Feature definition bundle for forecasting.
backend/app/models/forecast_features.pkl - Halt Feats · Feature definition bundle for halt logic.
backend/app/models/halt_features.pkl
Offline Training
Model-building entrypoints and exploratory analysis.
- Train Models · Offline model-building script.
backend/train_models.py - Notebook · Exploratory development and model iteration.
SmartStock.ipynb - Training Data · CSV inputs used for offline training and evaluation.
backend/data/train.csv
Connections 30
- 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.