Spaces:
Sleeping
Sleeping
| import pandas as pd | |
| import numpy as np | |
| import joblib | |
| from flask import Flask,jsonify,request | |
| # Initialize flask app | |
| sales_prediction_api=Flask("Forecasted Product Sales Predictor") | |
| # load the model | |
| model=joblib.load('sales_prediction_model_v1_0.joblib') | |
| # create home endpoint | |
| def home(): | |
| return "Welcome to the Superkart product sales forecast API" | |
| # create health check endpoint | |
| #@sales_prediction_api.get('/health') | |
| #def health_check(): | |
| # return jsonify({"status": "ok"}), 200 | |
| # create endpoint for single row data processing | |
| def predict_data(): | |
| data=request.get_json() | |
| user_input={ | |
| 'Product_Weight':data['Product_Weight'], | |
| 'Product_Sugar_Content':data['Product_Sugar_Content'], | |
| 'Product_Allocated_Area':data['Product_Allocated_Area'], | |
| 'Product_Type':data['Product_Type'], | |
| 'Product_MRP':data['Product_MRP'], | |
| 'Store_Id':data['Store_Id'], | |
| 'Store_Establishment_Year':data['Store_Establishment_Year'], | |
| 'Store_Size':data['Store_Size'], | |
| 'Store_Location_City_Type':data['Store_Location_City_Type'], | |
| 'Store_Type':data['Store_Type'] | |
| } | |
| df=pd.DataFrame([user_input]) | |
| prediction=model.predict(df).tolist()[0] | |
| return jsonify({'prediction':prediction}) | |
| # create endpoint for batch processing | |
| def predict_data_batch(): | |
| file1=request.files['file'] | |
| df_input=pd.read_csv(file1) | |
| predictionlist=model.predict(df_input.drop(['Product_Id'],axis=1)).tolist() | |
| idlist=df_input.Product_Id.values.tolist() | |
| dictionary1= dict(zip(idlist,predictionlist)) | |
| return dictionary1 | |
| if __name__=='__main__': | |
| app.run(debug=True) | |