Fitjv commited on
Commit
72501c9
·
verified ·
1 Parent(s): 3fab4e4

Upload folder using huggingface_hub

Browse files
Files changed (4) hide show
  1. Dockerfile +16 -21
  2. app.py +57 -0
  3. best_random_forest_model.joblib +3 -0
  4. requirements.txt +11 -3
Dockerfile CHANGED
@@ -1,21 +1,16 @@
1
- FROM python:3.9-slim
2
-
3
- WORKDIR /app
4
-
5
- RUN apt-get update && apt-get install -y \
6
- build-essential \
7
- curl \
8
- software-properties-common \
9
- git \
10
- && rm -rf /var/lib/apt/lists/*
11
-
12
- COPY requirements.txt ./
13
- COPY src/ ./src/
14
-
15
- RUN pip3 install -r requirements.txt
16
-
17
- EXPOSE 8501
18
-
19
- HEALTHCHECK CMD curl --fail http://localhost:8501/_stcore/health
20
-
21
- ENTRYPOINT ["streamlit", "run", "src/streamlit_app.py", "--server.port=8501", "--server.address=0.0.0.0"]
 
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: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:superkart_api"]
 
 
 
 
 
app.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import joblib
3
+ import pandas as pd
4
+ from flask import Flask, request, jsonify
5
+
6
+ # Initialize the Flask app
7
+ superkart_api = Flask("SuperKart Sales Prediction API")
8
+
9
+ # Load the trained model (assumed to include preprocessing pipeline)
10
+ model = joblib.load("deployment_files/best_random_forest_model.joblib")
11
+
12
+ @superkart_api.get('/')
13
+ def home():
14
+ return "Welcome to the SuperKart Sales Prediction API!"
15
+
16
+ @superkart_api.post('/v1/sales')
17
+ def predict_sales():
18
+ """
19
+ Handle POST request with a JSON payload for single prediction.
20
+ """
21
+ try:
22
+ input_data = request.get_json()
23
+
24
+ # Wrap in list to convert to DataFrame
25
+ df = pd.DataFrame([input_data])
26
+
27
+ # Predict using the trained pipeline
28
+ prediction = model.predict(df)[0]
29
+
30
+ return jsonify({'Predicted Quarterly Sales (in INR)': round(float(prediction), 2)})
31
+
32
+ except Exception as e:
33
+ return jsonify({'error': str(e)}), 400
34
+
35
+ @superkart_api.post('/v1/salesbatch')
36
+ def predict_sales_batch():
37
+ """
38
+ Handle POST request with a CSV file for batch predictions.
39
+ """
40
+ try:
41
+ file = request.files['file']
42
+ input_df = pd.read_csv(file)
43
+
44
+ # Predict using the trained pipeline
45
+ predictions = model.predict(input_df)
46
+ predictions = [round(float(val), 2) for val in predictions]
47
+
48
+ # Add to original DataFrame
49
+ input_df['Predicted_Quarterly_Sales'] = predictions
50
+
51
+ return input_df.to_dict(orient='records')
52
+
53
+ except Exception as e:
54
+ return jsonify({'error': str(e)}), 400
55
+
56
+ if __name__ == '__main__':
57
+ superkart_api.run(debug=True)
best_random_forest_model.joblib ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:5a46f3144128240a6cd8795f05a9034ed47f3d10dd977b06b5b0d454f127719e
3
+ size 45505863
requirements.txt CHANGED
@@ -1,3 +1,11 @@
1
- altair
2
- pandas
3
- streamlit
 
 
 
 
 
 
 
 
 
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]
11
+ streamlit==1.43.2