Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- Dockerfile +16 -0
- app.py +64 -0
- requirements.txt +10 -0
Dockerfile
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
# - `-w 4`: Uses 4 worker processes for handling requests
|
| 14 |
+
# - `-b 0.0.0.0:7860`: Binds the server to port 7860 on all network interfaces
|
| 15 |
+
# - `app:sales_prediction_api`: Runs the Flask app (assuming `app.py` contains the Flask instance named `sales_prediction_api`)
|
| 16 |
+
CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:7860", "app:sales_prediction_api"]
|
app.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import joblib
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from flask import Flask, request, jsonify
|
| 4 |
+
|
| 5 |
+
# Initialize Flask app with a name
|
| 6 |
+
sales_prediction_api = Flask("Customer Churn Predictor")
|
| 7 |
+
|
| 8 |
+
# Load the trained prediction model
|
| 9 |
+
model = joblib.load("sales_prediction_model_v1_0.joblib")
|
| 10 |
+
pipeline = joblib.load("sales_prediction_pipeline_v1_0.joblib")
|
| 11 |
+
|
| 12 |
+
# Define a route for the home page
|
| 13 |
+
@sales_prediction_api.get('/')
|
| 14 |
+
def home():
|
| 15 |
+
return "Welcome to the SuperKart Sales Prediction API!"
|
| 16 |
+
|
| 17 |
+
# Define an endpoint to predict for a single product
|
| 18 |
+
@sales_prediction_api.post('/v1/product')
|
| 19 |
+
def predict_sales():
|
| 20 |
+
# Get JSON data from the request
|
| 21 |
+
product_data = request.get_json()
|
| 22 |
+
|
| 23 |
+
# Extract relevant features from the input data
|
| 24 |
+
sample = {
|
| 25 |
+
'Product_Id': product_data['Product_Id'],
|
| 26 |
+
'Product_Weight': product_data['Product_Weight'],
|
| 27 |
+
'Product_Sugar_Content': product_data['Product_Sugar_Content'],
|
| 28 |
+
'Product_Allocated_Area': product_data['Product_Allocated_Area'],
|
| 29 |
+
'Product_Type': product_data['Product_Type'],
|
| 30 |
+
'Product_MRP': product_data['Product_MRP'],
|
| 31 |
+
'Store_Size': product_data['Store_Size'],
|
| 32 |
+
'Store_Location_City_Type': product_data['Store_Location_City_Type'],
|
| 33 |
+
'Store_Type': product_data['Store_Type']
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
# Convert the extracted data into a DataFrame
|
| 37 |
+
input_data = pd.DataFrame([sample])
|
| 38 |
+
input_data = pipeline.transform(input_data)
|
| 39 |
+
# Make a prediction using the trained model
|
| 40 |
+
prediction = model.predict(input_data).tolist()[0]
|
| 41 |
+
|
| 42 |
+
# Return the prediction as a JSON response
|
| 43 |
+
return jsonify({'Prediction': {"Product_Id": Product_Id, "Sales": prediction}})
|
| 44 |
+
|
| 45 |
+
# Define an endpoint to predict sales for a batch of products
|
| 46 |
+
@sales_prediction_api.post('/v1/productbatch')
|
| 47 |
+
def predict_batch():
|
| 48 |
+
# Get the uploaded CSV file from the request
|
| 49 |
+
file = request.files['file']
|
| 50 |
+
|
| 51 |
+
# Read the file into a DataFrame
|
| 52 |
+
input_data = pd.read_csv(file)
|
| 53 |
+
input_data = pipeline.transform(input_data)
|
| 54 |
+
# Make predictions for the batch data:
|
| 55 |
+
predictions = model.predict(input_data).tolist()
|
| 56 |
+
|
| 57 |
+
id_list = input_data.Product_Id.values.tolist()
|
| 58 |
+
output_dict = dict(zip(id_list, predictions))
|
| 59 |
+
|
| 60 |
+
return output_dict
|
| 61 |
+
|
| 62 |
+
# Run the Flask app in debug mode
|
| 63 |
+
if __name__ == '__main__':
|
| 64 |
+
sales_prediction_api.run(debug=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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==3.1.3
|
| 7 |
+
flask==3.1.2
|
| 8 |
+
gunicorn==20.1.0
|
| 9 |
+
requests==2.32.3
|
| 10 |
+
uvicorn[standard]
|