Spaces:
No application file
No application file
Update Streamlit app
Browse files- Dockerfile +10 -14
- app.py +66 -0
- requirements.txt +9 -3
Dockerfile
CHANGED
|
@@ -1,20 +1,16 @@
|
|
| 1 |
-
FROM python:3.
|
| 2 |
|
|
|
|
| 3 |
WORKDIR /app
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 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 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
| 1 |
+
FROM python:3.11.7-slim
|
| 2 |
|
| 3 |
+
# Set the working directory inside the container to /app
|
| 4 |
WORKDIR /app
|
| 5 |
|
| 6 |
+
# Copy all files from the current directory on the host to the container's /app directory
|
| 7 |
+
COPY . .
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
# Install Python dependencies listed in requirements
|
| 10 |
RUN pip3 install -r requirements.txt
|
| 11 |
|
| 12 |
+
#Define the command to run the Flask application on port 7860 and make it accessible externally
|
| 13 |
+
# - '-w 4' : Uses 4 worker processes to handle requests.
|
| 14 |
+
# - '-b 0.0.0.0:7860': Binds the Flask app to the specified host and port.
|
| 15 |
+
# - 'app:sales_prediction_api': Specifies the entry point of the Flask app.
|
| 16 |
+
CMD ["gunicorn", "w", "4", "-b", "0.0.0.0:7860" "app:sales_prediction_api"]
|
app.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
import numpy as np
|
| 3 |
+
from flask import Flask, request, jsonify
|
| 4 |
+
import joblib
|
| 5 |
+
import pandas as pd
|
| 6 |
+
|
| 7 |
+
# Inititialize Flask app with name
|
| 8 |
+
sales_prediction_api = Flask("Sales Predictor")
|
| 9 |
+
|
| 10 |
+
# Load the trained model predictor model
|
| 11 |
+
dt_model = joblib.load("decision_tree_model.pkl")
|
| 12 |
+
xgb_model = joblib.load("xgboost_model.pkl")
|
| 13 |
+
|
| 14 |
+
# Define a route for the home page
|
| 15 |
+
@sales_prediction_api.route('/')
|
| 16 |
+
def home():
|
| 17 |
+
return "Sales Prediction API"
|
| 18 |
+
|
| 19 |
+
# Define an endpoint to predict sales
|
| 20 |
+
@sales_prediction_api.post('/predict')
|
| 21 |
+
def predict():
|
| 22 |
+
# Get the data from the request
|
| 23 |
+
data = request.get_json()
|
| 24 |
+
|
| 25 |
+
# Extract relevant features from the input data
|
| 26 |
+
sample = {
|
| 27 |
+
'Product_Weight' = data['Product_Weight'],
|
| 28 |
+
'Product_Sugar_Content' = data['Product_Sugar_Content'],
|
| 29 |
+
'Product_Allocated_Area ' = data['Product_Allocated_Area'],
|
| 30 |
+
'Product_Type' = data['Product_Type'],
|
| 31 |
+
'Product_MRP' = data['Product_MRP'],
|
| 32 |
+
'Store_Size' = data['Store_Size'],
|
| 33 |
+
'Store_Location_City_Type' = data['Store_Location_City_Type'],
|
| 34 |
+
'Store_Type' = data['Store_Type'],
|
| 35 |
+
'Store_Age' = data['Store_Age']
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
#convert the extracted data into a dataframe
|
| 39 |
+
sample_df = pd.DataFrame(sample, index=[0])
|
| 40 |
+
|
| 41 |
+
# --------------------------------
|
| 42 |
+
# Model selection logic (FIXED)
|
| 43 |
+
# --------------------------------
|
| 44 |
+
model_choice = data.get("model", "dt")
|
| 45 |
+
|
| 46 |
+
if model_choice == "dt":
|
| 47 |
+
prediction = dt_model.predict(sample_df)[0]
|
| 48 |
+
|
| 49 |
+
elif model_choice == "xgb":
|
| 50 |
+
prediction = xgb_model.predict(sample_df)[0]
|
| 51 |
+
|
| 52 |
+
else:
|
| 53 |
+
return jsonify({"error": "Invalid model specified. Use 'dt' or 'xgb'"}), 400
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
# --------------------------------
|
| 58 |
+
# Response
|
| 59 |
+
# --------------------------------
|
| 60 |
+
return jsonify({
|
| 61 |
+
"model_used": model_choice,
|
| 62 |
+
"prediction": float(prediction)
|
| 63 |
+
})
|
| 64 |
+
|
| 65 |
+
if __name__ == '__main__':
|
| 66 |
+
sales_prediction_api.run(host="0.0.0.0", port=7860,debug=True)
|
requirements.txt
CHANGED
|
@@ -1,3 +1,9 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
pandas==2.1.4
|
| 2 |
+
flask==3.0.1
|
| 3 |
+
joblib==1.3.2
|
| 4 |
+
numpy==1.26.4
|
| 5 |
+
scikit-learn==1.4.0
|
| 6 |
+
xgboost==2.0.3
|
| 7 |
+
streamlit==1.30.0
|
| 8 |
+
requests==2.31.0
|
| 9 |
+
gunicorn==21.2.0
|