176 lines
4.8 KiB
Markdown
176 lines
4.8 KiB
Markdown
# GitHub Copilot Instructions — Prediksi Harga Cabai
|
|
|
|
## Project
|
|
This repository is the FastAPI deployment backend for the D4 thesis:
|
|
"Analisis Model Long Short-Term Memory dalam Sistem Prediksi Harga Cabai dengan Visualisasi Peta Digital Interaktif di Kabupaten Jember"
|
|
|
|
The API serves one-day-ahead LSTM predictions for cabai rawit at:
|
|
- Pasar_Tanjung
|
|
- Pasar_Kreongan
|
|
- Pasar_Tegal_Besar
|
|
- Pasar_Mangli
|
|
- Pasar_Wirolegi
|
|
|
|
## Environment
|
|
Use the existing Anaconda environment:
|
|
- Conda: tf_gpu39
|
|
- Python: 3.9.23
|
|
- Interpreter: C:\Users\alfia\anaconda3\envs\tf_gpu39\python.exe
|
|
|
|
Do not create another Conda environment unless explicitly requested.
|
|
|
|
## ML artifacts
|
|
The models are already trained and deployed. Do NOT retrain them in the API.
|
|
|
|
Each market has:
|
|
- model.h5
|
|
- scaler.pkl
|
|
- config.json
|
|
|
|
Final deployment configuration:
|
|
- neurons: 64
|
|
- activation: tanh
|
|
- learning_rate: 0.05
|
|
- batch_size: 32
|
|
- epochs: 50
|
|
- optimizer: Adam
|
|
- time_step: 7
|
|
- seed: 42
|
|
- shuffle: true
|
|
|
|
These values are reference metadata. Do not hard-code predictions.
|
|
|
|
## Inference rules
|
|
For each request:
|
|
1. Accept exactly 7 recent prices.
|
|
2. Convert to numeric shape (7, 1).
|
|
3. Use the saved market-specific scaler.
|
|
4. Transform the 7 values.
|
|
5. Reshape to (1, 7, 1).
|
|
6. Run the saved .h5 model once.
|
|
7. Inverse-transform the output.
|
|
8. Return the predicted next price.
|
|
|
|
The model unit is thousand rupiah:
|
|
- 22.0 means approximately Rp22,000.
|
|
|
|
Do not fit a new scaler on request data.
|
|
|
|
## Model loading
|
|
Load all five model/scaler/config artifacts once during FastAPI startup/lifespan and keep them in memory.
|
|
|
|
Do NOT:
|
|
- load models on every request
|
|
- call model.fit()
|
|
- retrain on startup
|
|
- recreate a different LSTM architecture when the saved model can be loaded directly
|
|
|
|
For inference, loading Keras .h5 with compile=False is preferred.
|
|
|
|
## Suggested project structure
|
|
C:\PrediksiHargaCabe\
|
|
├── api/
|
|
│ ├── main.py
|
|
│ ├── requirements.txt
|
|
│ └── models/
|
|
│ └── rawit/
|
|
│ ├── pasar_tanjung/
|
|
│ │ ├── model.h5
|
|
│ │ ├── scaler.pkl
|
|
│ │ └── config.json
|
|
│ ├── pasar_kreongan/
|
|
│ ├── pasar_tegal_besar/
|
|
│ ├── pasar_mangli/
|
|
│ └── pasar_wirolegi/
|
|
├── website/
|
|
└── .github/
|
|
└── copilot-instructions.md
|
|
|
|
Inspect the workspace first and reuse existing files instead of creating duplicates.
|
|
|
|
## Required API
|
|
Implement at least:
|
|
|
|
GET /
|
|
Basic status response.
|
|
|
|
GET /api/markets
|
|
Return Rawit and the five supported markets.
|
|
|
|
GET /api/health
|
|
Report API status and whether all five model artifacts loaded successfully.
|
|
|
|
POST /api/prediction/rawit/{market}
|
|
Request example:
|
|
{
|
|
"prices": [18, 16, 19, 19, 22, 22, 22]
|
|
}
|
|
|
|
Response should include:
|
|
- chili
|
|
- market
|
|
- time_step
|
|
- input_prices
|
|
- predicted_price_thousand
|
|
- predicted_price_rupiah
|
|
|
|
Use Pydantic validation.
|
|
|
|
Reject:
|
|
- unknown markets
|
|
- fewer/more than 7 prices
|
|
- non-numeric values
|
|
- NaN/infinite values
|
|
- negative prices
|
|
|
|
## CORS
|
|
Prepare CORS so a React/React Native frontend can call the API during development. Keep allowed origins configurable and avoid recommending unrestricted wildcard CORS for production.
|
|
|
|
## Testing
|
|
Before declaring the API complete:
|
|
1. Start from the `api` directory with:
|
|
python -m uvicorn main:app --reload
|
|
2. Verify /.
|
|
3. Verify /api/markets.
|
|
4. Verify /api/health.
|
|
5. Verify /docs.
|
|
6. Test prediction for all five markets.
|
|
7. Compare at least one known API prediction with the validated notebook inference, allowing normal floating-point differences.
|
|
|
|
## Thesis safety
|
|
Do not silently:
|
|
- change hyperparameters
|
|
- change time_step
|
|
- change the scaler method
|
|
- retrain models
|
|
- introduce another ML model
|
|
- modify the validated ML methodology
|
|
|
|
Never call the final configuration a "global optimum". Use wording such as:
|
|
"konfigurasi terbaik yang diperoleh dari skema eksperimen yang diterapkan pada dataset penelitian."
|
|
|
|
## Agent workflow
|
|
Before editing:
|
|
1. Inspect the workspace.
|
|
2. Inspect existing files and model paths.
|
|
3. Reuse correct code.
|
|
4. Avoid duplicate entry points or model directories.
|
|
|
|
When editing:
|
|
1. Make small coherent changes.
|
|
2. Preserve validated ML inference logic.
|
|
3. Use clear Python/FastAPI structure.
|
|
4. Run a practical validation after each important change.
|
|
5. If a command fails, inspect and fix the actual error; do not fake success.
|
|
|
|
The immediate priority is Rawit only:
|
|
1. Build the FastAPI backend.
|
|
2. Load the five saved artifacts.
|
|
3. Verify startup.
|
|
4. Implement /api/markets, /api/health, and /api/prediction/rawit/{market}.
|
|
5. Test the API.
|
|
6. Prepare it for connection to the thesis frontend.
|
|
|
|
Communication preference:
|
|
Explain important decisions briefly in Indonesian, give exact commands, state which files are changed, and avoid unrelated technologies.
|