|
| 1 | +## Files |
| 2 | + |
| 3 | +* `api.py` — main FastAPI application. Must define `app = FastAPI()` at module level and expose the inference endpoint. |
| 4 | +* `schema.py` — pydantic models used for request validation and response serialization. Update these models to match the inputs/outputs your model expects. |
| 5 | +* `requirements.txt` — list of pip dependencies (FastAPI, uvicorn, pydantic, plus any ML libs like `torch`, `tensorflow`, `scikit-learn`). |
| 6 | + |
| 7 | +--- |
| 8 | + |
| 9 | +## Design principles |
| 10 | + |
| 11 | +1. **Keep schema simple and explicit** |
| 12 | + |
| 13 | + * `schema.py` currently defines `CropPriceInput` and `CropPriceOutput`. If your model expects different fields, rename or create new pydantic models that reflect the exact inputs and outputs. Example: |
| 14 | + |
| 15 | + ```python |
| 16 | + from pydantic import BaseModel |
| 17 | + |
| 18 | + class CropPriceInput(BaseModel): |
| 19 | + crop: str |
| 20 | + region: str |
| 21 | + date: str # ISO format |
| 22 | + |
| 23 | + class CropPriceOutput(BaseModel): |
| 24 | + crop: str |
| 25 | + region: str |
| 26 | + date: str |
| 27 | + price: float |
| 28 | + ``` |
| 29 | + |
| 30 | + * Pydantic will validate incoming JSON and produce useful 422 errors when the payload is malformed. |
| 31 | + |
| 32 | +2. **Load the model once, at module import time** |
| 33 | + |
| 34 | + * Load or initialize the model in `api.py` at module level so it is reused across requests instead of being loaded per-request. Use environment variables or configuration to point to model files or endpoints. |
| 35 | + |
| 36 | + ```python |
| 37 | + import os |
| 38 | + MODEL_PATH = os.environ.get("MODEL_PATH") |
| 39 | + model = load_model(MODEL_PATH) # implement load_model for your model type |
| 40 | + ``` |
| 41 | + |
| 42 | +3. **Keep a small adapter function** |
| 43 | + |
| 44 | + * Implement a single adapter function (already present as `predict_crop_price`) that accepts the validated fields from the pydantic model, transforms them as needed, runs model inference, and returns a primitive (number or dict) that the route can convert into the response model. |
| 45 | + |
| 46 | + ```python |
| 47 | + def predict_crop_price(crop: str, region: str, date: str = None): |
| 48 | + # transform inputs, call model, post-process output |
| 49 | + return 123.45 # numeric or convertible to float |
| 50 | + ``` |
| 51 | + |
| 52 | +4. **Endpoint matches schema** |
| 53 | + |
| 54 | + * The endpoint should accept the input model and return the output model using `response_model=...` so responses are validated and documented automatically by FastAPI. |
| 55 | + |
| 56 | + ```python |
| 57 | + @app.post("/crop_price", response_model=CropPriceOutput) |
| 58 | + async def predict(data: CropPriceInput): |
| 59 | + price = predict_crop_price(data.crop, data.region, data.date) |
| 60 | + return CropPriceOutput(...) |
| 61 | + ``` |
| 62 | + |
| 63 | + |
| 64 | +--- |
| 65 | + |
| 66 | +## Quick start (local development) |
| 67 | + |
| 68 | +1. `(optional)` Create and activate a virtual environment: |
| 69 | + |
| 70 | +```bash |
| 71 | +# Step 1: Create a new virtual environment named ".venv" |
| 72 | +python -m venv .venv |
| 73 | + |
| 74 | +# Step 2: Activate the virtual environment (Windows PowerShell or Git Bash) |
| 75 | +./.venv/Scripts/activate |
| 76 | + |
| 77 | +# Step 3: Export all installed libraries to a requirements file |
| 78 | +pip freeze > requirements.txt |
| 79 | + |
| 80 | +``` |
| 81 | + |
| 82 | +2. Install dependencies (adjust for your model runtime): |
| 83 | + |
| 84 | +```bash |
| 85 | +# Install all required dependencies |
| 86 | +pip install -r requirements.txt |
| 87 | +``` |
| 88 | + |
| 89 | +3. Run the app from the directory with `api.py`: |
| 90 | + |
| 91 | +```bash |
| 92 | +uvicorn api:app --reload --port 8000 |
| 93 | +``` |
| 94 | + |
| 95 | +4. Open interactive docs to test inputs and outputs: |
| 96 | + |
| 97 | +* `http://127.0.0.1:8000/docs` (Swagger UI) |
| 98 | +* `http://127.0.0.1:8000/redoc` (ReDoc) |
| 99 | + |
| 100 | +--- |
| 101 | + |
| 102 | +## Example request (JSON) |
| 103 | + |
| 104 | +Use the input shape defined in `schema.py`. Example based on the current schema: |
| 105 | + |
| 106 | +```json |
| 107 | +{ |
| 108 | + "crop": "wheat", |
| 109 | + "region": "kolhapur", |
| 110 | + "date": "2025-11-13" |
| 111 | +} |
| 112 | +``` |
| 113 | + |
| 114 | +Example response: |
| 115 | + |
| 116 | +```json |
| 117 | +{ |
| 118 | + "crop": "wheat", |
| 119 | + "region": "kolhapur", |
| 120 | + "date": "2025-11-13", |
| 121 | + "price": 123.45 |
| 122 | +} |
| 123 | +``` |
| 124 | + |
| 125 | +--- |
| 126 | + |
| 127 | +## Common pitfalls |
| 128 | + |
| 129 | +* **Model reloaded on each request:** Load the model once at module import time. |
| 130 | +* **Non-serializable output:** Ensure outputs are primitives (float, int, str) or pydantic models. |
| 131 | +* **Missing `app` object:** `uvicorn api:app` requires `app = FastAPI()` in `api.py`. |
| 132 | +* **422 Unprocessable Entity:** Request JSON doesn't match the input pydantic model. |
| 133 | +* **Port in use:** Run on a different port with `--port`. |
| 134 | + |
| 135 | +--- |
| 136 | + |
| 137 | +## For source code of Crop Prediction Model |
| 138 | +``` |
| 139 | +https://github.com/SwapCodesDev/ML-Models---Farmingo |
| 140 | +``` |
0 commit comments