Upload backend app files
Browse files- Dockerfile +13 -13
- app.py +51 -0
- requirements.txt +5 -3
Dockerfile
CHANGED
|
@@ -1,20 +1,20 @@
|
|
| 1 |
-
|
|
|
|
| 2 |
|
|
|
|
| 3 |
WORKDIR /app
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
curl \
|
| 8 |
-
git \
|
| 9 |
-
&& rm -rf /var/lib/apt/lists/*
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
|
| 14 |
-
|
|
|
|
| 15 |
|
| 16 |
-
|
|
|
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
ENTRYPOINT ["streamlit", "run", "src/streamlit_app.py", "--server.port=8501", "--server.address=0.0.0.0"]
|
|
|
|
| 1 |
+
# Use a minimal base image with Python 3.9 installed
|
| 2 |
+
FROM python:3.9-slim
|
| 3 |
|
| 4 |
+
# Set the working directory inside the container to /app
|
| 5 |
WORKDIR /app
|
| 6 |
|
| 7 |
+
# Copy the requirements.txt file into the working directory
|
| 8 |
+
COPY requirements.txt .
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
# Install Python dependencies
|
| 11 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 12 |
|
| 13 |
+
# Copy the rest of the application files into the working directory
|
| 14 |
+
COPY . .
|
| 15 |
|
| 16 |
+
# Expose port 5000
|
| 17 |
+
EXPOSE 5000
|
| 18 |
|
| 19 |
+
# Define the command to run the Flask application
|
| 20 |
+
CMD ["flask", "run", "--host=0.0.0.0"]
|
|
|
app.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify
|
| 2 |
+
import joblib
|
| 3 |
+
import pandas as pd
|
| 4 |
+
|
| 5 |
+
app = Flask(__name__)
|
| 6 |
+
|
| 7 |
+
# Load the serialized full pipeline
|
| 8 |
+
try:
|
| 9 |
+
full_pipeline = joblib.load('deployment_files/SuperKart_model_v1_0.joblib')
|
| 10 |
+
# Get the list of columns from the training data used by the pipeline
|
| 11 |
+
pipeline_columns = full_pipeline.named_steps['preprocessor'].transformers_[0][1].get_feature_names_out(
|
| 12 |
+
full_pipeline.named_steps['preprocessor'].transformers_[0][2]
|
| 13 |
+
).tolist()
|
| 14 |
+
# Add numerical columns to the pipeline columns list
|
| 15 |
+
numerical_cols_in_pipeline = [col for col in full_pipeline.named_steps['scaler'].feature_names_in_]
|
| 16 |
+
pipeline_columns = numerical_cols_in_pipeline + [col for col in pipeline_columns if col not in numerical_cols_in_pipeline]
|
| 17 |
+
|
| 18 |
+
except Exception as e:
|
| 19 |
+
full_pipeline = None
|
| 20 |
+
print(f"Error loading pipeline: {e}")
|
| 21 |
+
|
| 22 |
+
@app.route('/predict', methods=['POST'])
|
| 23 |
+
def predict():
|
| 24 |
+
if full_pipeline is None:
|
| 25 |
+
return jsonify({'error': 'Model not loaded'}), 500
|
| 26 |
+
|
| 27 |
+
try:
|
| 28 |
+
data = request.get_json(force=True)
|
| 29 |
+
|
| 30 |
+
# Convert input data to DataFrame, ensuring column order matches training data
|
| 31 |
+
input_df = pd.DataFrame([data])
|
| 32 |
+
|
| 33 |
+
# Reorder columns to match the order expected by the pipeline
|
| 34 |
+
# This assumes all expected columns are present in the input data
|
| 35 |
+
input_df = input_df[pipeline_columns]
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
# Make prediction
|
| 39 |
+
prediction = full_pipeline.predict(input_df)
|
| 40 |
+
|
| 41 |
+
# Return prediction as JSON
|
| 42 |
+
return jsonify({'prediction': prediction.tolist()})
|
| 43 |
+
|
| 44 |
+
except Exception as e:
|
| 45 |
+
return jsonify({'error': str(e)}), 400
|
| 46 |
+
|
| 47 |
+
if __name__ == '__main__':
|
| 48 |
+
# Create the backend_app directory if it doesn't exist
|
| 49 |
+
import os
|
| 50 |
+
os.makedirs('backend_app', exist_ok=True)
|
| 51 |
+
app.run(debug=True, host='0.0.0.0', port=5000)
|
requirements.txt
CHANGED
|
@@ -1,3 +1,5 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
Flask==3.0.3
|
| 2 |
+
joblib==1.4.2
|
| 3 |
+
pandas==2.2.2
|
| 4 |
+
scikit-learn==1.6.1
|
| 5 |
+
numpy==2.0.2
|