AishwaryaNJ commited on
Commit
a0f349e
·
0 Parent(s):

first commit

Browse files
Files changed (1) hide show
  1. README.md +277 -0
README.md ADDED
@@ -0,0 +1,277 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Dynamic Pricing Engine for E-commerce Website
2
+
3
+ An end-to-end intermediate MVP for real-time dynamic pricing with:
4
+
5
+ - Synthetic data generation for 50,000+ SKU style scenarios
6
+ - Random Forest and XGBoost training pipelines
7
+ - FastAPI inference API for real-time price recommendations across synthetic and Kaggle retail model profiles
8
+ - Streamlit dashboard for simulation and monitoring
9
+ - Competitor price blending, inventory adjustments, and flash sale detection
10
+ - Optional Redis and Kafka integrations
11
+
12
+ ## Architecture
13
+
14
+ 1. `scripts/generate_sample_data.py` creates synthetic click, order, inventory, and competitor signals.
15
+ 2. `scripts/train_model.py` trains a baseline Random Forest and an XGBoost regressor, then saves the best model.
16
+ 3. `app/api.py` serves real-time pricing recommendations through FastAPI.
17
+ 4. `app/dashboard.py` provides a Streamlit control panel for scenario analysis and monitoring.
18
+ 5. `app/streaming.py` provides optional Kafka event producer and consumer helpers.
19
+
20
+ ## Project Structure
21
+
22
+ ```text
23
+ app/
24
+ api.py
25
+ config.py
26
+ dashboard.py
27
+ feature_engineering.py
28
+ modeling.py
29
+ pricing_engine.py
30
+ schemas.py
31
+ streaming.py
32
+ data/
33
+ processed/
34
+ raw/
35
+ models/
36
+ scripts/
37
+ generate_sample_data.py
38
+ train_model.py
39
+ tests/
40
+ smoke_test.py
41
+ ```
42
+
43
+ ## Quick Start
44
+
45
+ ### 1. Create a virtual environment
46
+
47
+ ```powershell
48
+ python -m venv .venv
49
+ .venv\Scripts\Activate.ps1
50
+ pip install -r requirements.txt
51
+ ```
52
+
53
+ ### 2. Generate sample data
54
+
55
+ ```powershell
56
+ python scripts/generate_sample_data.py --rows 25000
57
+ ```
58
+
59
+ ### 3. Train the model
60
+
61
+ ```powershell
62
+ python scripts/train_model.py
63
+ ```
64
+
65
+ ### Train on a real Kaggle dataset
66
+
67
+ Recommended dataset:
68
+
69
+ - Kaggle `Retail Price Optimization`: https://www.kaggle.com/datasets/suddharshan/retail-price-optimization
70
+
71
+ Download `retail_price.csv` and place it at:
72
+
73
+ ```text
74
+ data/raw/kaggle/retail_price.csv
75
+ ```
76
+
77
+ Then train with:
78
+
79
+ ```powershell
80
+ python scripts/train_model.py --profile kaggle_retail --data-path data/raw/kaggle/retail_price.csv
81
+ ```
82
+
83
+ Notes:
84
+
85
+ - This trains a real-dataset pricing model using the Kaggle retail schema.
86
+ - The FastAPI service now exposes a dedicated Kaggle request schema at `/price/recommend/kaggle` when a `kaggle_retail` model is loaded.
87
+
88
+ ### 4. Run the FastAPI service
89
+
90
+ ```powershell
91
+ uvicorn app.api:app --reload
92
+ ```
93
+
94
+ Docs will be available at `http://127.0.0.1:8000/docs`.
95
+
96
+ ### 5. Run the Streamlit dashboard
97
+
98
+ ```powershell
99
+ streamlit run app/dashboard.py
100
+ ```
101
+
102
+ ## API Endpoints
103
+
104
+ - `GET /health`: Service health and artifact status
105
+ - `POST /price/recommend`: Returns a recommended price, confidence score, and pricing signals for the synthetic profile
106
+ - `POST /price/recommend/kaggle`: Returns a recommended price and price gap analysis for the Kaggle retail profile
107
+ - `POST /events/order`: Registers an order event for flash sale detection
108
+ - `GET /monitoring/summary`: Returns pricing and event summary metrics
109
+
110
+ ## Core Pricing Logic
111
+
112
+ The engine combines:
113
+
114
+ - ML-predicted baseline price
115
+ - Competitor blending: default `70% model / 30% competitor`
116
+ - Inventory pressure adjustments
117
+ - Demand surge adjustments
118
+ - Flash sale emergency multiplier
119
+ - Floor and ceiling guardrails
120
+
121
+ ## Example Request
122
+
123
+ ```json
124
+ {
125
+ "sku_id": "SKU-1024",
126
+ "category": "electronics",
127
+ "brand": "brand_b",
128
+ "customer_segment": "premium",
129
+ "hour_of_day": 20,
130
+ "day_of_week": 5,
131
+ "is_weekend": 1,
132
+ "is_festival": 1,
133
+ "inventory_level": 23,
134
+ "inventory_days_cover": 4.1,
135
+ "competitor_price": 1849.0,
136
+ "click_through_rate": 0.074,
137
+ "conversion_rate": 0.038,
138
+ "units_sold_last_5m": 7,
139
+ "units_sold_last_1h": 33,
140
+ "base_cost": 1210.0,
141
+ "current_price": 1799.0
142
+ }
143
+ ```
144
+
145
+ ## AWS Deployment Notes
146
+
147
+ - EC2: Host FastAPI and Streamlit on one instance for a demo, or split them later if needed.
148
+ - S3: Good fit for model artifacts and price history snapshots.
149
+ - Lambda: Optional competitor polling and scheduled retraining.
150
+ - CloudWatch: Use for API logs, dashboard process logs, and alert thresholds.
151
+ - Redis: Cache recent recommendations and event counters.
152
+ - Kafka: Use self-managed Kafka for demos, or Amazon MSK only after checking cost.
153
+
154
+ Current AWS note:
155
+
156
+ - AWS Free Tier changed on July 15, 2025. Official AWS docs say accounts created before July 15, 2025 can use `t2.micro` free tier eligibility in supported regions, while accounts created on or after July 15, 2025 use a newer credit-based/free-plan model with eligible instance types such as `t3.micro`. Verify your account type before launch.
157
+ - Amazon S3 is covered through the newer AWS Free Tier credit/free-plan model, not a simple permanent 5 GB rule for all new accounts.
158
+ - Amazon MSK pricing is pay-as-you-go on the official AWS pricing page. Treat MSK as a potential paid service unless your account credits cover it.
159
+
160
+ ## Docker Deployment
161
+
162
+ Build and run the full stack locally or on a VM:
163
+
164
+ ```powershell
165
+ docker compose up --build
166
+ ```
167
+
168
+ Then open:
169
+
170
+ - API docs: `http://127.0.0.1:8000/docs`
171
+ - Streamlit dashboard: `http://127.0.0.1:8501`
172
+
173
+ Notes:
174
+
175
+ - Compose mounts `data/` and `models/` from the host so retrained artifacts persist outside the containers.
176
+ - Replace `.env.example` with a real `.env` file before production deployment.
177
+
178
+ If you want to build each image separately:
179
+
180
+ ```powershell
181
+ docker build -t dynamic-pricing-api -f Dockerfile .
182
+ docker build -t dynamic-pricing-dashboard -f Dockerfile.streamlit .
183
+ ```
184
+
185
+ ## EC2 Deployment
186
+
187
+ Recommended target path:
188
+
189
+ ```text
190
+ /opt/dynamic-pricing-engine
191
+ ```
192
+
193
+ Quick setup outline on Ubuntu EC2:
194
+
195
+ ```bash
196
+ sudo apt update
197
+ sudo apt install -y python3.13-venv
198
+ cd /opt
199
+ sudo mkdir -p dynamic-pricing-engine
200
+ sudo chown ubuntu:ubuntu dynamic-pricing-engine
201
+ ```
202
+
203
+ Copy the repo to `/opt/dynamic-pricing-engine`, then:
204
+
205
+ ```bash
206
+ cd /opt/dynamic-pricing-engine
207
+ python3 -m venv .venv
208
+ source .venv/bin/activate
209
+ pip install --upgrade pip
210
+ pip install -r requirements.txt
211
+ cp .env.example .env
212
+ chmod +x deploy/ec2/start_api.sh
213
+ chmod +x deploy/ec2/start_dashboard.sh
214
+ python scripts/generate_sample_data.py --rows 25000
215
+ python scripts/train_model.py --profile kaggle_retail --data-path data/raw/kaggle/retail_price.csv
216
+ ```
217
+
218
+ Important:
219
+
220
+ - The current Streamlit dashboard expects a `kaggle_retail` model artifact.
221
+ - If you deploy both API and dashboard together, train the `kaggle_retail` profile and use `POST /price/recommend/kaggle`.
222
+ - If you want the synthetic API profile in production, deploy the API by itself or refactor the dashboard to use the synthetic schema.
223
+
224
+ Install the included `systemd` services:
225
+
226
+ ```bash
227
+ sudo cp deploy/ec2/dynamic-pricing-api.service /etc/systemd/system/
228
+ sudo cp deploy/ec2/dynamic-pricing-dashboard.service /etc/systemd/system/
229
+ sudo systemctl daemon-reload
230
+ sudo systemctl enable dynamic-pricing-api
231
+ sudo systemctl enable dynamic-pricing-dashboard
232
+ sudo systemctl start dynamic-pricing-api
233
+ sudo systemctl start dynamic-pricing-dashboard
234
+ sudo systemctl status dynamic-pricing-api
235
+ sudo systemctl status dynamic-pricing-dashboard
236
+ ```
237
+
238
+ Open these after the instance is up and the EC2 security group allows inbound `8000` and `8501`:
239
+
240
+ ```text
241
+ http://<your-ec2-public-ip>:8000/docs
242
+ http://<your-ec2-public-ip>:8501
243
+ ```
244
+
245
+ Suggested EC2 security group inbound rules for a demo:
246
+
247
+ - `22` from your IP only
248
+ - `8000` from your IP or a narrow trusted range
249
+ - `8501` from your IP or a narrow trusted range
250
+
251
+ For public production access, put Nginx in front and expose only `80` and `443`.
252
+
253
+ ## EC2 With Docker
254
+
255
+ If you prefer containers on EC2 instead of `systemd`, install Docker on Ubuntu and run:
256
+
257
+ ```bash
258
+ cd /opt/dynamic-pricing-engine
259
+ docker compose up --build -d
260
+ docker compose ps
261
+ ```
262
+
263
+ Then open:
264
+
265
+ ```text
266
+ http://<your-ec2-public-ip>:8000/docs
267
+ http://<your-ec2-public-ip>:8501
268
+ ```
269
+
270
+ ## Extension Ideas
271
+
272
+ - Replace synthetic data with marketplace telemetry
273
+ - Add Flink or Spark Structured Streaming for event windows
274
+ - Add SHAP explainability for pricing decisions
275
+ - Introduce RL policy optimization for multi-step pricing
276
+ - Connect to Amazon Personalize for customer-level contextual pricing
277
+ "# Dynamic-Pricing-Engine"