pinkandjani20
Add application file
89ccb85
from flask import Flask, request, jsonify
from flask_cors import CORS
import joblib
import numpy as np
app = Flask(__name__)
CORS(app)
# Load Decision Tree model
model = joblib.load('decision_tree_model.pkl')
@app.route('/')
def home():
return "Iris Decision Tree API is running!"
@app.route('/predict', methods=['POST'])
def predict_iris():
try:
# Expecting JSON: {"data": [sepal_length, sepal_width, petal_length, petal_width]}
data = request.json.get('data', None)
if data is None or len(data) != 4:
return jsonify({'error': 'Input harus berupa array dengan 4 fitur: sepal_length, sepal_width, petal_length, petal_width'}), 400
# Convert to numpy array and reshape
features = np.array(data).reshape(1, -1)
pred = model.predict(features)[0]
# Map target to class name
target_names = ['setosa', 'versicolor', 'virginica']
pred_class = target_names[pred]
return jsonify({'prediksi_spesies': pred_class})
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/health', methods=['GET'])
def health():
return jsonify({'status': 'OK', 'message': 'API is running'}), 200
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=7860)