Spaces:
Runtime error
Runtime error
File size: 1,603 Bytes
4970bf5 | 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 41 42 43 44 45 46 47 |
from flask import Flask, request, jsonify
import pandas as pd
import joblib
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
app = Flask(__name__)
try:
model = joblib.load('model.joblib')
columns = joblib.load('columns.joblib')
logger.info("Model and columns loaded successfully in hosting script.")
except Exception as e:
logger.error(f"Failed to load model or columns: {e}")
raise
@app.route('/', methods=['GET'])
def index():
logger.info("Root endpoint called.")
return jsonify({'status': 'ok'})
@app.route('/health', methods=['GET'])
def health():
logger.info("Health check endpoint called.")
return jsonify({'status': 'healthy'})
@app.route('/predict', methods=['POST'])
def predict():
try:
logger.info("Predict endpoint called with data: %s", request.get_json(force=True))
data = request.get_json(force=True)
input_df = pd.DataFrame(data)
categorical_columns = ['Occupation', 'Gender', 'ProductPitched', 'MaritalStatus', 'Designation']
input_encoded = pd.get_dummies(input_df, columns=categorical_columns, drop_first=True)
input_encoded = input_encoded.reindex(columns=columns, fill_value=0)
prediction = model.predict(input_encoded)
logger.info("Prediction made: %s", prediction.tolist())
return jsonify({'prediction': prediction.tolist()})
except Exception as e:
logger.error(f"Prediction failed: %s", str(e))
return jsonify({'error': str(e)}), 400
# Do not call app.run(); waitress will serve it via Docker CMD
|