Upload folder using huggingface_hub
Browse files- Dockerfile +16 -0
- app.py +62 -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 -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:app`: Runs the Flask app (assuming `app.py` contains the Flask instance named `app`)
|
| 16 |
+
CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:7860", "app:app"]
|
app.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import joblib
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from flask import Flask, request, jsonify
|
| 4 |
+
|
| 5 |
+
# Initialize Flask app with a name
|
| 6 |
+
app = Flask("Product Sales Predictor")
|
| 7 |
+
|
| 8 |
+
# Load the trained product sales prediction model
|
| 9 |
+
model = joblib.load("/app/product_sales_predictor_v1_0.joblib")
|
| 10 |
+
|
| 11 |
+
# Define a route for the home page
|
| 12 |
+
@app.get('/')
|
| 13 |
+
def home():
|
| 14 |
+
return "Welcome to the Product Sales Prediction API"
|
| 15 |
+
|
| 16 |
+
# Define an endpoint to predict sales for a single product
|
| 17 |
+
@app.post('/v1/product')
|
| 18 |
+
def predict_sales():
|
| 19 |
+
# Get JSON data from the request
|
| 20 |
+
product_data = request.get_json()
|
| 21 |
+
|
| 22 |
+
# Extract relevant product features from the input data
|
| 23 |
+
sample = {
|
| 24 |
+
'Product_Weight': product_data['Product_Weight'],
|
| 25 |
+
'Product_Sugar_Content': product_data['Product_Sugar_Content'],
|
| 26 |
+
'Product_Allocated_Area': product_data['Product_Allocated_Area'],
|
| 27 |
+
'Product_Type': product_data['Product_Type'],
|
| 28 |
+
'Product_MRP': product_data['Product_MRP'],
|
| 29 |
+
'Store_Id': product_data['Store_Id'],
|
| 30 |
+
'Store_Establishment_Year': product_data['Store_Establishment_Year'],
|
| 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 |
+
|
| 39 |
+
# Make a sales 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({'Predicted_Sales': prediction})
|
| 44 |
+
|
| 45 |
+
# Define an endpoint to predict sales for a batch of products
|
| 46 |
+
@app.post('/v1/productbatch')
|
| 47 |
+
def predict_sales_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 |
+
|
| 54 |
+
# Make predictions for the batch data
|
| 55 |
+
predictions = model.predict(input_data).tolist()
|
| 56 |
+
|
| 57 |
+
# Return the predictions as a JSON response
|
| 58 |
+
return jsonify({'Predicted_Sales': predictions})
|
| 59 |
+
|
| 60 |
+
# Run the Flask app in debug mode
|
| 61 |
+
if __name__ == '__main__':
|
| 62 |
+
app.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==2.2.2
|
| 7 |
+
flask==2.2.2
|
| 8 |
+
gunicorn==20.1.0
|
| 9 |
+
requests==2.28.1
|
| 10 |
+
uvicorn[standard]
|