DataCareerHub / MLOps and Model Deployment

Deploying Models Through APIs

Fictional practice · Reference code executed locally · The supplied local reference was executed against the included fixture. This does not verify a production service or the learner’s own environment.

Package a model without executable deserialization

Package a model without executable deserialization

A model artifact needs schema, feature order, target units, preprocessing and version identity, not just coefficients. The small duration model is stored as JSON with three numeric coefficients and supported input bounds. This avoids loading arbitrary executable pickle objects from unknown sources. JSON alone does not prove integrity: validate its structure, source and hash before use. The new 60-row telemetry fixture is fictional and separate from the earlier protected ML course. Its first 40 rows fit the fixed linear model; later partitions support evaluation.

Follow a concrete example

The artifact stores a fixed feature order[distance_km,load_ratio] and three coefficients including an intercept. Its schema version, bounds, units and training count make the prediction formula inspectable. Validate these fields before serving. A model file that merely parses as JSON can still be malformed or incompatible with the request contract.

Further reading: Docker build guidance, Python HTTP server reference. The case and exercises are authored for DataCareerHub.

Your task

Run the packaging lab and inspect model.json. List the feature order, units, training population and supported bounds.

Use learner_worksheet.txt and the provided source/code files. Save your own artifact and compare the separate solution after attempting the task.

Worked solution

The model uses distance_km and load_ratio and predicts hours. The artifact includes schema_version, model_id, coefficients, training_rows and bounds. It is a transparent learning package, not an approved operational model.

Validate request values and response meaning

Validate request values and response meaning

An API contract must reject unsupported input rather than silently coercing it. The reference requires exactly two keys, finite numeric values, no booleans and values within declared teaching bounds. It returns model_id, prediction_hours, unit and automated_decision=false. Validation checks do not establish that an input is representative or that the prediction is calibrated. Unknown extra fields should not change roles, model versions or paid-access flags. The optional server binds only loopback and is never installed on the PHP website.

Follow a concrete example

Try the valid100km/0.5load request, then a missing field, an extra field, True as distance and a negative distance. Reject before prediction when the contract fails. Returning an estimate is not approval for an automated action; the response explicitly includes automated_decision=false. A request never chooses a model file or alters course permissions.

Inspect the reference implementation

Language: Python 3.12. The complete downloadable file includes the named helpers and the fresh-output command-line runner. Trace the source, transformation and result against this lesson’s contract before running it locally.

def mlops_api(source,out):
 model=fit_service(source);(Path(out)/'model.json').write_text(json.dumps(model,indent=2)+'\n');cases=[]
 for payload in [{'distance_km':100,'load_ratio':.5},{'distance_km':-1,'load_ratio':.5},{'distance_km':True,'load_ratio':.5},{'distance_km':100}]:
  try:cases.append({'input':payload,'response':predict_payload(payload,model)})
  except ValueError as e:cases.append({'input':payload,'error':str(e)})
 return {'cases':cases,'model':'model.json','binding':'Optional server example binds only 127.0.0.1; never installed on this website.'}

Further reading: Docker build guidance, Python HTTP server reference. The case and exercises are authored for DataCareerHub.

Your task

Execute valid, negative-distance, boolean and missing-key cases. Explain each observed response or rejection.

Use learner_worksheet.txt and the provided source/code files. Save your own artifact and compare the separate solution after attempting the task.

Worked solution

The 100 km/0.5 load input returns a numeric prediction. Negative distance, boolean distance and missing load are rejected. No request can select another model or change an entitlement. The response is an estimate for a learning exercise.

Separate local serving from website deployment

Separate local serving from website deployment

Model serving introduces a runtime, request boundary, logging and operating responsibilities separate from lesson delivery. The optional HTTP example has bounded body size, a health route, one prediction route and generic error responses. It requires a separately generated model artifact and a local learner command. Do not expose it publicly without authentication, transport, rate/timeout, monitoring and operating review. Uploading course files to Aruba must not start a Python service or open a port. A code example is not a production service claim.

Follow a concrete example

The optional server requires a model path supplied by the local learner and binds127.0.0.1. It is distinct from the course’s PHP download controller. Review health/predict routing, body-size limits, generic error responses and shutdown. Do not move that server into the hosting upload directory or claim public authentication/availability has been implemented.

Further reading: Docker build guidance, Python HTTP server reference. The case and exercises are authored for DataCareerHub.

Your task

Inspect server_example.py.txt and write a local-versus-production boundary checklist. Run only if choosing the optional loopback lab.

Use learner_worksheet.txt and the provided source/code files. Save your own artifact and compare the separate solution after attempting the task.

Worked solution

The default binding is 127.0.0.1, not a public interface. The course PHP runtime serves learning files only. Public serving needs an independent approved deployment; no cloud workload or model endpoint is created by this task.

Practice evidence