from flask import Flask, request, jsonify
import joblib
import os

# load the model
model = joblib.load("model.pkl")

# create the flask app
app = Flask(__name__)


# app route for the predict endpoint
@app.route("/predict", methods=["POST"])
def predict():
    try:
        # get the input query from the request
        data = request.json
        query = data.get("query", "")

        if not query:
            return jsonify({"error": "No query provided."}), 400

        # make a prediction
        # the model expects an array
        prediction = model.predict([query])[0]
        bad = bool(prediction)

        return jsonify({"bad": bad}), 200

    except Exception as error:
        return jsonify({"error": str(error)}), 500


if __name__ == "__main__":
    app.run(debug=True, host="0.0.0.0", port=5000)
