File size: 2,130 Bytes
5e42426
 
 
 
 
5ab5f11
5e42426
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8a6669e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1bac749
8a6669e
1bac749
 
 
 
 
 
 
 
 
8a6669e
77a64b3
8a6669e
5e42426
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import joblib
import numpy as np
import pandas as pd
from flask import Flask, request, jsonify
super_kart_api=Flask("Superkart_price_prediction")
model=joblib.load('model.joblib')
@super_kart_api.get('/')
def home():
  return "Welcome to SuperKart sales Prediction"
@super_kart_api.post('/v1/spkart_single')
def sale_pred_single():
  sale_data=request.get_json()
  sample={
      'Product_Weight':sale_data['Product_Weight'],
      'Product_Sugar_Content':sale_data['Product_Sugar_Content'],
      'Product_Allocated_Area':sale_data['Product_Allocated_Area'],
      'Product_Type':sale_data['Product_Type'],
      'Product_MRP':sale_data['Product_MRP'],
      'Store_Id':sale_data['Store_Id'],
      'Store_Size':sale_data['Store_Size'],
      'Store_Location_City_Type':sale_data['Store_Location_City_Type'],
      'Store_Type':sale_data['Store_Type'],
      'Store_age':sale_data['Store_age']
  }
  input_data=pd.DataFrame([sample])
  predicted_sale=model.predict(input_data)[0]
  response={'Store_Outlet':sample['Store_Id'],"Sale":round(float(predicted_sale), 2)}
  return jsonify(response)

@super_kart_api.post('/v1/spkart_batch')
def sale_pred_batch():
    file = request.files['file']
    print("File Received:", file.filename)
    
    # Read input data
    input_data = pd.read_csv(file)
    print("Input Data Shape:", input_data.shape)
    print(input_data.head())
    
    # Make predictions
    predicted_sale = model.predict(input_data).tolist()
    print("Predicted Sales Length:", len(predicted_sale))
    
    predicted_sales = [round(float(i)) for i in predicted_sale]
    sale_outlets = input_data['Store_Id'].tolist()
    print("Sale Outlets Length:", len(sale_outlets))
    
    # Create response
    response = dict(zip(sale_outlets, predicted_sales))
    
    print("Response:", response)
    repshape = {'input_shape': input_data.shape, 'predicted_count': len(predicted_sale)}
    
    print("Response Shape:", repshape)
    
    # Return combined response
    return jsonify({
        'predictions': response,
        'metadata': repshape
    })
    
    

if __name__=='__main__':
  super_kart_api.run()