Spaces:
Sleeping
Sleeping
File size: 2,523 Bytes
91db89f 7f73f05 91db89f 7f73f05 91db89f 7f73f05 91db89f 7f73f05 91db89f 7f73f05 91db89f |
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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
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
@backend_predictor_api.get('/')
def home():
return "Welcome to the Backend Prediction API!"
# Define an endpoint to predict for a single data
@backend_predictor_api.post('/v1/dijakbn')
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
@backend_predictor_api.post('/v1/dijakbnbatch')
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)
|