RahulSingh211's picture
Upload folder using huggingface_hub
aeeccde verified
Raw History Blame Contribute Delete
2.25 kB
import joblib
import pandas as pd
from flask import Flask, request, jsonify
# Initialize Flask app with a name
churn_predictor_api = Flask("SuperKart Predictor")
# Load the trained churn prediction model
model = joblib.load("SuperKart_model_v1_0.joblib")
# Define a route for the home page
@churn_predictor_api.get('/')
def home():
return "Welcome to the SuperKart Prediction API!"
# Define an endpoint to predict churn for a single invester
@churn_predictor_api.post('/v1/invester')
def predict_churn():
# Get JSON data from the request
customer_data = request.get_json()
# Extract relevant Store features from the input data
sample = {
'ProductId': customer_data['ProductId'],
'ProductWeight': customer_data['ProductWeight'],
'ProductSugarContent' : customer_data['ProductSugarContent'],
'ProductAllocatedArea': customer_data['ProductAllocatedArea'],
'ProductType': customer_data['ProductType'],
'ProductMRP': customer_data['ProductMRP'],
'StoreId': customer_data['StoreId'],
'StoreEstablishmentYear': customer_data['StoreEstablishmentYear'],
'StoreSize': customer_data['StoreSize'],
'StoreLocationCityType': customer_data['StoreLocationCityType']
}
# Convert the extracted data into a DataFrame
input_data = pd.DataFrame([sample])
# Make a churn 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 churn for a batch of investers
@churn_predictor_api.post('/v1/investerbatch')
def predict_churn_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)
# Make predictions for the batch data and convert raw predictions into a readable format
predictions = model.predict(input_data.drop("CustomerId",axis=1)).tolist()
cust_id_list = input_data.CustomerId.values.tolist()
output_dict = dict(zip(cust_id_list, predictions))
return output_dict
# Run the Flask app in debug mode
if __name__ == '__main__':
app.run(debug=True)