File size: 1,260 Bytes
d7b327e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
from flask import Flask, request, jsonify
import joblib
import numpy as np
app = Flask(__name__)
# Load models (make sure these files exist)
xgb = joblib.load("xgb_model.pkl")
rf = joblib.load("rf_model.pkl")
@app.route("/recommend", methods=["POST"])
def recommend():
data = request.get_json()
# Extract input features
length = float(data["length"])
width = float(data["width"])
price = float(data["price"])
coverage = float(data["coverage"])
area_range = float(data["area_range"])
tile_type = data["tile_type"].lower()
# Feature engineering
tile_type_num = 0 if tile_type == "floor" else 1
tile_area = length * width
price_per_sqft = price / coverage
budget_eff = coverage / price
features = np.array([[tile_type_num, length, width, price, coverage,
area_range, tile_area, price_per_sqft, budget_eff]])
# Predict using both models and average
prob = (xgb.predict_proba(features)[0][1] + rf.predict_proba(features)[0][1]) / 2
result = "β
Recommended" if prob >= 0.5 else "β Not Recommended"
return jsonify({"result": result, "score": round(float(prob), 3)})
if __name__ == "__main__":
app.run(debug=True)
|