File size: 1,619 Bytes
5d0ee50 7d34d7a 5d0ee50 7d34d7a 5d0ee50 | 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 | import json
import joblib
import pandas as pd
from flask import Flask, request, jsonify
app = Flask("SuperKart Predictor")
model = joblib.load("model.joblib")
with open("model.json", "r") as f:
model_info = json.load(f)
@app.get("/")
def home():
return app.response_class(response=json.dumps(model_info, indent=4), status=200, mimetype="application/json")
@app.post("/v1/product")
def predict():
product_data = request.get_json()
sample = {
"Product_Id": product_data["Product_Id"],
"Product_Weight": product_data["Product_Weight"],
"Product_Sugar_Content": product_data["Product_Sugar_Content"],
"Product_Allocated_Area": product_data["Product_Allocated_Area"],
"Product_Type": product_data["Product_Type"],
"Product_MRP": product_data["Product_MRP"],
"Store_Id": product_data["Store_Id"],
"Store_Establishment_Year": product_data["Store_Establishment_Year"],
"Store_Size": product_data["Store_Size"],
"Store_Location_City_Type": product_data["Store_Location_City_Type"],
"Store_Type": product_data["Store_Type"],
}
input_data = pd.DataFrame([sample])
prediction = model.predict(input_data).tolist()[0]
return jsonify({"Prediction": prediction})
@app.post("/v1/productbatch")
def predict_batch():
file = request.files["file"]
input_data = pd.read_csv(file)
predictions = model.predict(input_data).tolist()
input_data["Prediction"] = predictions
result = input_data.to_dict(orient="records")
return jsonify(result)
if __name__ == "__main__":
app.run(debug=True)
|