Dattaluri commited on
Commit
4da2af4
·
verified ·
1 Parent(s): d69d8fd

Upload backend app files

Browse files
Files changed (3) hide show
  1. Dockerfile +13 -13
  2. app.py +51 -0
  3. requirements.txt +5 -3
Dockerfile CHANGED
@@ -1,20 +1,20 @@
1
- FROM python:3.13.5-slim
 
2
 
 
3
  WORKDIR /app
4
 
5
- RUN apt-get update && apt-get install -y \
6
- build-essential \
7
- curl \
8
- git \
9
- && rm -rf /var/lib/apt/lists/*
10
 
11
- COPY requirements.txt ./
12
- COPY src/ ./src/
13
 
14
- RUN pip3 install -r requirements.txt
 
15
 
16
- EXPOSE 8501
 
17
 
18
- HEALTHCHECK CMD curl --fail http://localhost:8501/_stcore/health
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
- altair
2
- pandas
3
- streamlit
 
 
 
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