INNER CODE UNIT · Python
predict
sachelsout/infra-lab · week-1/flask-ml-docker-app/app.py:20
def predict():
data = request.get_json(force=True)
# Example input format: {"features": [5.1, 3.5, 1.4, 0.2]}
features = np.array(data['features']).reshape(1, -1)
if features is None:
return jsonify({"error": "Missing 'features' in request"}), 400
try:
prediction = model.predict(features)[0]
species_name = species[prediction]
return jsonify({"prediction": species_name})
except Exception as e:
return jsonify({"error": str(e)}), 500
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000)