| title | emoji | colorFrom | colorTo | sdk | pinned | license | short_description |
|---|---|---|---|---|---|---|---|
Farmingo API |
🌾 |
green |
blue |
docker |
false |
mit |
Farmingo API (FastAPI) deployed as a Docker Space. |
api.py— main FastAPI application. Must defineapp = FastAPI()at module level and expose the inference endpoint.schema.py— pydantic models used for request validation and response serialization. Update these models to match the inputs/outputs your model expects.requirements.txt— list of pip dependencies (FastAPI, uvicorn, pydantic, plus any ML libs liketorch,tensorflow,scikit-learn).
-
Keep schema simple and explicit
schema.pycurrently definesCropPriceInputandCropPriceOutput. If your model expects different fields, rename or create new pydantic models that reflect the exact inputs and outputs. Example:
from pydantic import BaseModel class CropPriceInput(BaseModel): crop: str region: str date: str # ISO format class CropPriceOutput(BaseModel): crop: str region: str date: str price: float
- Pydantic will validate incoming JSON and produce useful 422 errors when the payload is malformed.
-
Load the model once, at module import time
- Load or initialize the model in
api.pyat 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.
import os MODEL_PATH = os.environ.get("MODEL_PATH") model = load_model(MODEL_PATH) # implement load_model for your model type
- Load or initialize the model in
-
Keep a small adapter function
- 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.
def predict_crop_price(crop: str, region: str, date: str = None): # transform inputs, call model, post-process output return 123.45 # numeric or convertible to float
- Implement a single adapter function (already present as
-
Endpoint matches schema
- The endpoint should accept the input model and return the output model using
response_model=...so responses are validated and documented automatically by FastAPI.
@app.post("/crop_price", response_model=CropPriceOutput) async def predict(data: CropPriceInput): price = predict_crop_price(data.crop, data.region, data.date) return CropPriceOutput(...)
- The endpoint should accept the input model and return the output model using
(optional)Create and activate a virtual environment:
# Step 1: Create a new virtual environment named ".venv"
python -m venv .venv
# Step 2: Activate the virtual environment (Windows PowerShell or Git Bash)
./.venv/Scripts/activate
# Step 3: Export all installed libraries to a requirements file
pip freeze > requirements.txt
- Install dependencies (adjust for your model runtime):
# Install all required dependencies
pip install -r requirements.txt- Run the app from the directory with
api.py:
uvicorn api:app --reload --port 8000- Open interactive docs to test inputs and outputs:
http://127.0.0.1:8000/docs(Swagger UI)http://127.0.0.1:8000/redoc(ReDoc)
Use the input shape defined in schema.py. Example based on the current schema:
{
"crop": "wheat",
"region": "kolhapur",
"date": "2025-11-13"
}Example response:
{
"crop": "wheat",
"region": "kolhapur",
"date": "2025-11-13",
"price": 123.45
}- Model reloaded on each request: Load the model once at module import time.
- Non-serializable output: Ensure outputs are primitives (float, int, str) or pydantic models.
- Missing
appobject:uvicorn api:apprequiresapp = FastAPI()inapi.py. - 422 Unprocessable Entity: Request JSON doesn't match the input pydantic model.
- Port in use: Run on a different port with
--port.
https://github.com/SwapCodesDev/ML-Models---Farmingo