|
|
| |
| import numpy as np |
| import joblib |
| import pandas as pd |
| from flask import Flask, request, jsonify |
| from huggingface_hub import hf_hub_download |
| import joblib |
|
|
|
|
| model_path = hf_hub_download( |
| repo_id="PR118/SalesForecastModel", |
| filename="superkart_sales_forecast_prediction_model_v1_0.joblib" |
| ) |
|
|
|
|
| |
| superkart_api = Flask("Sales Forecast Predictor Model") |
|
|
| |
| model = joblib.load(model_path) |
|
|
| |
| @superkart_api.get('/') |
| def home(): |
| return "Welcome to the Sales Forecast Prediction API!" |
|
|
| |
| @superkart_api.post('/v1/predict') |
| def predict_sales(): |
| |
| data = request.get_json() |
|
|
| |
| sample = { |
| 'Product_Weight': data['Product_Weight'], |
| 'Product_Sugar_Content': data['Product_Sugar_Content'], |
| 'Product_Allocated_Area': data['Product_Allocated_Area'], |
| 'Product_MRP': data['Product_MRP'], |
| 'Store_Size': data['Store_Size'], |
| 'Store_Location_City_Type': data['Store_Location_City_Type'], |
| 'Store_Type': data['Store_Type'], |
| |
| 'Store_Age_Years': data['Store_Age_Years'], |
| 'Product_Type_Category': data['Product_Type_Category'] |
| } |
|
|
| |
| input_data = pd.DataFrame([sample]) |
|
|
| |
| prediction = model.predict(input_data).tolist()[0] |
|
|
| |
| return jsonify({'Sales': prediction}) |
|
|
|
|
| |
| @superkart_api.post('/v1/predictbatch') |
| def predict_sales_batch(): |
| """ |
| This function handles POST requests to the '/v1/predictbatch' endpoint. |
| It expects a CSV file containing property details for multiple properties |
| and returns the predicted rental prices as a dictionary in the JSON response. |
| """ |
| |
| file = request.files['file'] |
|
|
| |
| input_data = pd.read_csv(file) |
|
|
| |
| predicted_sales = model.predict(input_data).tolist() |
|
|
| |
| product_ids = input_data['Product_Id'].tolist() |
| output_dict = dict(zip(product_ids, predicted_sales)) |
|
|
| |
| return output_dict |
|
|
|
|
|
|
| |
| if __name__ == '__main__': |
| superkart_api.run(debug=True) |
|
|