moushmim's picture
Upload folder using huggingface_hub
37909cb verified
import sys
import os
import pandas as pd
import numpy as np
import joblib
import pandas as pd
from flask import Flask, request, jsonify
# Initialize Flask app with a name
app = Flask("SK Sales Forecast")
# Load the trained sales forecast model
model = joblib.load('SuperKart_salesprediction_model_v1_0-2.joblib')
# Define a route for the home page
@app.get('/')
def home():
return "Welcome to the SuperKart Sales Forecast API!"
# Define an endpoint to predict forecast for a single product store sales total
@app.post('/v1/singleproduct')
def sales_forecast():
# Get JSON data from the request
sales_data = request.get_json()
# Extract relevant sales features from the input data
sample = {
'Product_Id': sales_data['Product_Id'],
'Product_Weight': sales_data['Product_Weight'],
'Product_Sugar_Content': sales_data['Product_Sugar_Content'],
'Product_Allocated_Area': sales_data['Product_Allocated_Area'],
'Product_Type': sales_data['Product_Type'],
'Product_MRP': sales_data['Product_MRP'],
'Store_Id': sales_data['Store_Id'],
'Store_Establishment_Year': sales_data['Store_Establishment_Year'],
'Store_Size': sales_data['Store_Size'],
'Store_Location_City_Type': sales_data['Store_Location_City_Type'],
'Store_Type': sales_data['Store_Type']
}
# 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 customers
@app.post('/v1/productbatch')
def predict_sales_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).tolist()
product_id_list = input_data['Product_Id'].tolist()
output_dict = dict(zip(product_id_list, predictions))
return output_dict
# Run the Flask app in debug mode
if __name__ == '__main__':
app.run(debug=True)