sebastiangv's picture
Upload folder using huggingface_hub
7d34d7a verified
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)