|
|
| from flask import Flask, request, jsonify |
| import joblib |
| import pandas as pd |
|
|
| |
| app = Flask(__name__) |
|
|
| |
| try: |
| model = joblib.load("extraa_learn_project.pkl") |
| print("Model loaded successfully.") |
| except Exception as e: |
| print(f"Error loading model: {e}") |
| model = None |
|
|
| |
| FEATURES = [ |
| "time_spent_on_website", |
| "first_interaction", |
| "profile_completed", |
| "age", |
| "page_views_per_visit", |
| "last_activity" |
| ] |
|
|
| @app.route("/", methods=["GET"]) |
| def home(): |
| return jsonify({ |
| "message": "Backend is running!", |
| "status": "ok", |
| "required_features": FEATURES |
| }) |
|
|
| @app.route("/predict", methods=["POST"]) |
| def predict(): |
| try: |
| data = request.get_json() |
|
|
| |
| missing = [f for f in FEATURES if f not in data] |
| if missing: |
| return jsonify({"error": f"Missing features: {missing}"}), 400 |
|
|
| |
| df = pd.DataFrame([data], columns=FEATURES) |
|
|
| |
| prediction = model.predict(df)[0] |
| proba = model.predict_proba(df).max() |
|
|
| return jsonify({ |
| "prediction": int(prediction), |
| "confidence": float(proba) |
| }) |
|
|
| except Exception as e: |
| return jsonify({"error": str(e)}), 500 |
|
|
| if __name__ == "__main__": |
| app.run(host="0.0.0.0", port=8501) |
|
|
|
|
|
|
|
|
|
|