import joblib import numpy as np import pandas as pd from fastapi import FastAPI, Request, UploadFile, File superkart_api = FastAPI(title="Superkart Sales Prediction", description="API for predicting Superkart sales", version="0.116.1" ) model = joblib.load('model_1.joblib') @superkart_api.get('/') def home(): return "Welcome to SuperKart Sales Prediction API!" @superkart_api.post('/v1/superkart_single') async def salepred_single(request: Request): sales_data = await request.json() # Read input data sample = { '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_Size':sales_data['Store_Size'], 'Store_Location_City_Type':sales_data['Store_Location_City_Type'], 'Store_Type':sales_data['Store_Type'], } input_data = pd.DataFrame([sample]) # Make predictions predicted_sale = model.predict(input_data)[0] # Create response response = {'Store_Outlet':sample['Store_Id'],"Sale":round(float(predicted_sale), 2)} return response @superkart_api.post('/v1/superkart_batch') async def salepred_batch(file: UploadFile = File(...)): # Read input data input_data = pd.read_csv(file.file) # Make predictions predicted_sale = model.predict(input_data).tolist() # Add predictions to input data input_data['Predicted_Sale'] = predicted_sale # Group by Store_Id and sum the predicted sales grouped_sales = input_data.groupby('Store_Id')['Predicted_Sale'].sum().to_dict() # Create response response = { 'store_sales': {store_id: round(float(sale), 2) for store_id, sale in grouped_sales.items()} } print("Final Response:", response) return response