4.8 KiB
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:
- Accept exactly 7 recent prices.
- Convert to numeric shape (7, 1).
- Use the saved market-specific scaler.
- Transform the 7 values.
- Reshape to (1, 7, 1).
- Run the saved .h5 model once.
- Inverse-transform the output.
- 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:
- Start from the
apidirectory with: python -m uvicorn main:app --reload - Verify /.
- Verify /api/markets.
- Verify /api/health.
- Verify /docs.
- Test prediction for all five markets.
- 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:
- Inspect the workspace.
- Inspect existing files and model paths.
- Reuse correct code.
- Avoid duplicate entry points or model directories.
When editing:
- Make small coherent changes.
- Preserve validated ML inference logic.
- Use clear Python/FastAPI structure.
- Run a practical validation after each important change.
- If a command fails, inspect and fix the actual error; do not fake success.
The immediate priority is Rawit only:
- Build the FastAPI backend.
- Load the five saved artifacts.
- Verify startup.
- Implement /api/markets, /api/health, and /api/prediction/rawit/{market}.
- Test the API.
- 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.