Why this matters
A model becomes useful when other software can ask it for predictions: a mobile app, a website, a spreadsheet tool or another service. Ghanaian machine learning adverts mention model serving, secure API endpoints, Docker and keeping models working in production. This work sits between data science and software development, and it is a strong skill to show.
Step one: save the trained model
After training, save the model to a file so you do not retrain it every time. For scikit learn models, joblib is the usual tool:
import joblib
joblib.dump(model, "yield_model.joblib")Only load model files you created or fully trust. Loading a file like this can run code inside it, so never load one from an unknown source.
Step two: wrap it in a web API
A small web framework such as FastAPI turns a Python function into a web address that accepts requests:
import joblib
import pandas as pd
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
model = joblib.load("yield_model.joblib")
class Farm(BaseModel):
rainfall_mm: float
fertiliser_bags: float
@app.post("/predict")
def predict(farm: Farm):
row = pd.DataFrame([farm.model_dump()])
return {"yield_bags": round(float(model.predict(row)[0]), 1)}The Farm class describes the expected input, so a request with missing or non numeric values is rejected automatically before it reaches the model. Other software sends JSON such as rainfall 650 and fertiliser 2, and gets back a predicted yield. You can run it on your computer with a server such as uvicorn and try it in the browser page FastAPI generates.