Spaces:
Sleeping
Sleeping
| import joblib | |
| import pandas as pd | |
| from flask import Flask, request, jsonify | |
| # Initialize Flask app with a name | |
| #revenue_predictor_api = Flask("SuperKart Revenue Predictor") | |
| backend_predictor_api = Flask("Backend Predictor") | |
| # Load the trained prediction model | |
| model = joblib.load("personal_loan.joblib") | |
| # Define a route for the home page | |
| def home(): | |
| return "Welcome to the Backend Prediction API!" | |
| # Define an endpoint to predict for a single data | |
| def predict_dijak_backend(): | |
| # Get JSON data from the request | |
| backend_data = request.get_json() | |
| # Extract relevant backend features from the input data | |
| sample = { | |
| 'ID': backend_data['ID'], | |
| 'Age': backend_data['Age'], | |
| 'Experience': backend_data['Experience'], | |
| 'Income': backend_data['Income'], | |
| 'ZIPCode': backend_data['ZIPCode'], | |
| 'Family': backend_data['Family'], | |
| 'CCAvg': backend_data['CCAvg'], | |
| 'Education': backend_data['Education'], | |
| 'Mortgage': backend_data['Mortgage'], | |
| 'Securities_Account': backend_data['Securities_Account'], | |
| 'CD_Account': backend_data['CD_Account'], | |
| 'Online': backend_data['Online'], | |
| 'CreditCard': backend_data['CreditCard'] | |
| } | |
| # Convert the extracted data into a DataFrame | |
| input_data = pd.DataFrame([sample]) | |
| # Make a prediction using the trained model | |
| prediction = model.predict(input_data).tolist()[0] | |
| # Return the prediction as a JSON response | |
| return jsonify({'Prediction': prediction}) | |
| # Define an endpoint to predict for a batch of input | |
| def predict_dijak_backend_batch(): | |
| # Get the uploaded CSV file from the request | |
| file = request.files['file'] | |
| # Read the file into a DataFrame | |
| input_data = pd.read_csv(file) | |
| # Drop Product_Id before prediction | |
| features = input_data.drop("Personal_Loan", axis=1) | |
| # Make predictions | |
| predictions = model.predict(features).tolist() | |
| # Build structured output with Product_Id, Store_Id, and rounded output | |
| output_list = [] | |
| for i in range(len(predictions)): | |
| output_list.append({ | |
| #"Product_Id": input_data.loc[i, "Product_Id"], | |
| #"Store_Id": input_data.loc[i, "Store_Id"], | |
| "Prediction": round(predictions[i], 2) | |
| }) | |
| return jsonify(output_list) | |
| # Run the Flask app in debug mode | |
| if __name__ == '__main__': | |
| app.run(debug=True) | |