INNER CODE UNIT · Python
home
sachelsout/infra-lab · week-1/flask-ml-docker-app/app.py:13
def home():
return "Welcome to the ML Model API!"
# Map class index to species
species = ["setosa", "versicolor", "virginica"]
@app.route("/predict", methods=["POST"])
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]