Prashantbhat1607's picture
Upload folder using huggingface_hub
52bee35 verified
import numpy as np
import joblib
import pandas as pd
from flask import Flask, request, jsonify
# Initialize the Flask app
app = Flask("Revenue Predictor")
# Load trained model
model = joblib.load("backend_files/Superkart_prediction_model_v1_0.joblib")
# Root endpoint
@app.route('/', methods=['GET'])
def home():
return "Welcome to the Revenue Prediction API!"
# Single record prediction
@app.route('/v1/revenue', methods=['POST'])
def predict_revenue():
customer_data = request.get_json()
# Prepare input data
sample = {
"Product_Type": customer_data['Product_Type'],
"Product_MRP": customer_data['Product_MRP'],
"Product_Weight": customer_data['Product_Weight'],
"Product_Sugar_Content": customer_data['Product_Sugar_Content'],
"Store_Size": customer_data['Store_Size'],
"Store_Location_City": customer_data['Store_Location_City'],
"Store_Type": customer_data['Store_Type'],
"Product_Allocated_Area": customer_data['Product_Allocated_Area'],
"Store_Establishment_Year": customer_data['Store_Establishment_Year']
}
# Convert to DataFrame
input_data = pd.DataFrame([sample])
# Predict log revenue
predicted_log_revenue = model.predict(input_data).tolist()[0]
# Convert log to actual revenue
predicted_revenue = np.exp(predicted_log_revenue)
predicted_revenue = round(float(predicted_revenue), 2)
return jsonify({'Predicted revenue (in dollars)': predicted_revenue})
# Batch prediction endpoint
@app.route('/v1/revenuebatch', methods=['POST'])
def predict_revenue_batch():
file = request.files['file']
input_data = pd.read_csv(file)
# Predict log revenue
predicted_log_revenue = model.predict(input_data.drop(["Product_Id", "Store_Id"], axis=1)).tolist()
# Convert to actual revenue
predicted_revenue = [round(float(np.exp(x)), 2) for x in predicted_log_revenue]
# Create output dictionary
product_ids = input_data["Product_Id"].astype(str) + "_" + input_data["Store_Id"].astype(str)
output_dict = dict(zip(product_ids, predicted_revenue))
return jsonify(output_dict)
# Run the app
if __name__ == '__main__':
app.run(debug=True)