Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- Dockerfile +13 -0
- app.py +94 -0
- requirements.txt +11 -0
- superkart_prediction.joblib +3 -0
Dockerfile
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.9-slim
|
| 2 |
+
|
| 3 |
+
# Set the working directory inside the container
|
| 4 |
+
WORKDIR /app
|
| 5 |
+
|
| 6 |
+
# Copy all files from the current directory to the container's working directory
|
| 7 |
+
COPY . .
|
| 8 |
+
|
| 9 |
+
# Install dependencies from the requirements file without using cache to reduce image size
|
| 10 |
+
RUN pip install -- no-cache-dir -- upgrade -r requirements.txt
|
| 11 |
+
|
| 12 |
+
# Define the command to start the application using Gunicorn with 4 worker processes
|
| 13 |
+
CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:7860", "app: sales_predictor_api"]
|
app.py
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import numpy as np
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import joblib
|
| 5 |
+
from flask import Flask, request, jsonify
|
| 6 |
+
|
| 7 |
+
#initate flask application
|
| 8 |
+
sales_price_prediction_api = Flask("SuperKart Sales Price Prediction API")
|
| 9 |
+
|
| 10 |
+
#Load the trained model
|
| 11 |
+
model = joblib.load("superkart_prediction.joblib")
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
# Define a route for the home page (GET request)
|
| 15 |
+
@SalesPredictionBackend_api.get('/')
|
| 16 |
+
def home():
|
| 17 |
+
"""
|
| 18 |
+
This function handles GET requests to the root URL ('/') of the API.
|
| 19 |
+
It returns a simple welcome message.
|
| 20 |
+
"""
|
| 21 |
+
return "Welcome to the SuperKart Sales Prediction API!"
|
| 22 |
+
|
| 23 |
+
# Define an endpoint for single property prediction (POST request)
|
| 24 |
+
@SalesPredictionBackend_api.post('/v1/sales')
|
| 25 |
+
def predict_rental_prife():
|
| 26 |
+
"""
|
| 27 |
+
This function handles POST requests to the '/v1/sales' endpoint.
|
| 28 |
+
It expects a JSON payload containing producr details and returns
|
| 29 |
+
the predicted sales amount as a JSON response.
|
| 30 |
+
"""
|
| 31 |
+
# Get the JSON data from the request body
|
| 32 |
+
sales_data = request.get_json()
|
| 33 |
+
|
| 34 |
+
# Extract relevant features from the JSON data
|
| 35 |
+
sample = {
|
| 36 |
+
'Product_Id': sales_data['Product_Id'],
|
| 37 |
+
'Product_Weight': sales_data['Product_Weight'],
|
| 38 |
+
'Product_Sugar_Content': sales_data['Product_Sugar_Content'],
|
| 39 |
+
'Product_Allocated_Area': sales_data['Product_Allocated_Area'],
|
| 40 |
+
'Product_Allocated_Area': sales_data['Product_Allocated_Area'],
|
| 41 |
+
'Product_Type': sales_data['Product_Type'],
|
| 42 |
+
'Product_MRP': sales_data['Product_MRP'],
|
| 43 |
+
'Store_Id': sales_data['Store_Id'],
|
| 44 |
+
'Store_Establishment_Year': sales_data['Store_Establishment_Year'],
|
| 45 |
+
'Store_Size': sales_data['Store_Size'],
|
| 46 |
+
'Store_Location_City_Type': sales_data['Store_Location_City_Type'],
|
| 47 |
+
'Store_Type': sales_data['Store_Type']
|
| 48 |
+
}
|
| 49 |
+
#Convert the extracted sata into a Panda Dataframe
|
| 50 |
+
input_data = pd.DataFrame([sample])
|
| 51 |
+
|
| 52 |
+
#Make prediction (get sales_prediction)
|
| 53 |
+
prediction_log_sale = model.predict(input_data)
|
| 54 |
+
|
| 55 |
+
#calculated_sale
|
| 56 |
+
predicted_sale = np.exp(prediction_log_sale[0])
|
| 57 |
+
|
| 58 |
+
#convert predicted price to Pyton float
|
| 59 |
+
predicted_sale = round(float(predicted_sale), 2)
|
| 60 |
+
|
| 61 |
+
#return the sales
|
| 62 |
+
return jsonify({'predicted Sales (indollars)': predicted_sale})
|
| 63 |
+
|
| 64 |
+
#define an endpoint for batch predictions (POST request)
|
| 65 |
+
@SalesPredictionBackend_api.post('/v1/sales/batch')
|
| 66 |
+
def predict_sales_batch():
|
| 67 |
+
"""
|
| 68 |
+
This function handles POST requests to the '/v1/rentalbatch' endpoint.
|
| 69 |
+
It expects a CSV file containing property details for multiple properties
|
| 70 |
+
and returns the predicted rental prices as a dictionary in the JSON response.
|
| 71 |
+
"""
|
| 72 |
+
|
| 73 |
+
# Get the uploaded CSV file from the requqst
|
| 74 |
+
file = request. files['file']
|
| 75 |
+
|
| 76 |
+
# Read the CSV file into a Pandas DataFrame
|
| 77 |
+
input_data = pd.read_csv(file)
|
| 78 |
+
|
| 79 |
+
# Make predictions for all properties in the DataFrame (get log_prices)
|
| 80 |
+
predicted_log_sales = model.predict(input_data).tolist()
|
| 81 |
+
|
| 82 |
+
#calculate actual prices
|
| 83 |
+
predicted_prices = [round(float(np.exp(log_price)), 2) for log_price in predicted_log_prices]
|
| 84 |
+
|
| 85 |
+
# Create a dictionary of predictions with property IDs as keys
|
| 86 |
+
property_ids = input_data['id'].tolist() # Assuming 'id' is the property ID column
|
| 87 |
+
output_dict = dict(zip(property_ids, predicted_prices) ) # Use actual prices
|
| 88 |
+
# Return the predictions dictionary as
|
| 89 |
+
return output_dict
|
| 90 |
+
|
| 91 |
+
# Run the Flask application in debug mode if this script is executed directly
|
| 92 |
+
if __name__ == '_main_':
|
| 93 |
+
sales_price_prediction_api.run(debug=True)
|
| 94 |
+
|
requirements.txt
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
pandas == 2.2.2
|
| 2 |
+
numpy == 2.0.2
|
| 3 |
+
scikit-learn == 1.6.1
|
| 4 |
+
xgboost == 2.1.4
|
| 5 |
+
joblib == 1.4.2
|
| 6 |
+
Werkzeug == 2.2.2
|
| 7 |
+
flask == 2.2.2
|
| 8 |
+
gunicorn == 20.1.0
|
| 9 |
+
requests == 2.28.1
|
| 10 |
+
uvicorn[standard]
|
| 11 |
+
streamlit == 1.43.2
|
superkart_prediction.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:2a1c875817ca94e6944732721953770e476548d571e8e99af8e6cee658bbe433
|
| 3 |
+
size 1015101
|