Spaces:
Sleeping
Sleeping
Upload 4 files
Browse files- Dockerfile +21 -0
- app.py +45 -0
- requirements.txt +7 -0
- superkart_model.pkl +3 -0
Dockerfile
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Use an official Python runtime as a parent image
|
| 2 |
+
FROM python:3.9-slim
|
| 3 |
+
|
| 4 |
+
# Set the working directory in the container
|
| 5 |
+
WORKDIR /app
|
| 6 |
+
|
| 7 |
+
# Copy the requirements file into the container
|
| 8 |
+
COPY requirements.txt .
|
| 9 |
+
|
| 10 |
+
# Install any needed packages specified in requirements.txt
|
| 11 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 12 |
+
|
| 13 |
+
# Copy the rest of the application code into the container
|
| 14 |
+
COPY . .
|
| 15 |
+
|
| 16 |
+
# Expose the port the app runs on
|
| 17 |
+
EXPOSE 5000
|
| 18 |
+
|
| 19 |
+
EXPOSE 7860
|
| 20 |
+
HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 CMD curl -f http://localhost:7860/ || exit 1
|
| 21 |
+
CMD ["gunicorn", "--bind", "0.0.0.0:7860", "app:app"]
|
app.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify
|
| 2 |
+
import joblib
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
# Initialize the Flask application
|
| 7 |
+
app = Flask(__name__)
|
| 8 |
+
|
| 9 |
+
# Load the trained model pipeline
|
| 10 |
+
try:
|
| 11 |
+
model = joblib.load('superkart_model.pkl')
|
| 12 |
+
print("Model loaded successfully.")
|
| 13 |
+
except Exception as e:
|
| 14 |
+
print(f"Error loading model: {e}")
|
| 15 |
+
model = None
|
| 16 |
+
|
| 17 |
+
@app.route('/')
|
| 18 |
+
def home():
|
| 19 |
+
return "SuperKart Sales Prediction API is running!"
|
| 20 |
+
|
| 21 |
+
@app.route('/predict', methods=['POST'])
|
| 22 |
+
def predict():
|
| 23 |
+
if model is None:
|
| 24 |
+
return jsonify({'error': 'Model not loaded'}), 500
|
| 25 |
+
|
| 26 |
+
try:
|
| 27 |
+
# Get data from the POST request
|
| 28 |
+
data = request.get_json(force=True)
|
| 29 |
+
|
| 30 |
+
# Convert the incoming JSON data into a pandas DataFrame
|
| 31 |
+
# The model pipeline expects a DataFrame with the original column names
|
| 32 |
+
input_data = pd.DataFrame(data, index=[0])
|
| 33 |
+
|
| 34 |
+
# Make a prediction
|
| 35 |
+
prediction = model.predict(input_data)
|
| 36 |
+
|
| 37 |
+
# Return the prediction as JSON
|
| 38 |
+
return jsonify({'predicted_sales': np.round(prediction[0], 2)})
|
| 39 |
+
|
| 40 |
+
except Exception as e:
|
| 41 |
+
return jsonify({'error': str(e)}), 400
|
| 42 |
+
|
| 43 |
+
if __name__ == '__main__':
|
| 44 |
+
# Run the app on port 5000
|
| 45 |
+
app.run(host='0.0.0.0', port=5000)
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Flask==3.0.0
|
| 2 |
+
scikit-learn==1.3.2
|
| 3 |
+
pandas==2.0.3
|
| 4 |
+
numpy==1.25.2
|
| 5 |
+
xgboost==2.0.3
|
| 6 |
+
joblib==1.3.2
|
| 7 |
+
gunicorn==21.2.0
|
superkart_model.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:157d7f1c6b8e66278bbb080eed840ed33a2bb64501562b96bd02eb6d02fc7ce9
|
| 3 |
+
size 31487338
|