from flask import Flask, request, jsonify import joblib import pandas as pd app = Flask(__name__) # Load the serialized model # Ensure 'extraalearn_lead_model.joblib' is in the same directory model = joblib.load('extraalearn_lead_model.joblib') @app.route('/predict', methods=['POST']) def predict(): try: # Get JSON data from the request data = request.get_json() # Convert the dictionary to a DataFrame # The keys in the JSON must match the column names expected by the model df = pd.DataFrame([data]) # Make a prediction prediction = model.predict(df) # Return the result as JSON return jsonify({'prediction': int(prediction[0])}) except Exception as e: return jsonify({'error': str(e)}) if __name__ == '__main__': # Hugging Face Spaces defaults to port 7860 app.run(host='0.0.0.0', port=7860)