Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- Dockerfile +9 -9
- app.py +80 -60
Dockerfile
CHANGED
|
@@ -1,16 +1,16 @@
|
|
| 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
|
| 5 |
WORKDIR /app
|
| 6 |
|
| 7 |
-
# Copy all files from the current directory
|
| 8 |
COPY . .
|
| 9 |
|
| 10 |
-
# Install
|
| 11 |
-
RUN
|
| 12 |
|
| 13 |
-
# Define the command to
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
#
|
|
|
|
|
|
|
|
|
| 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
CHANGED
|
@@ -1,62 +1,82 @@
|
|
| 1 |
|
| 2 |
-
|
| 3 |
-
import
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
# Section for online prediction
|
| 8 |
-
st.subheader("Input Product and Store Details for Sales Prediction")
|
| 9 |
-
|
| 10 |
-
# Input fields for product and store data
|
| 11 |
-
Product_Weight = st.number_input("Product Weight", min_value=0.0, value=15.0)
|
| 12 |
-
Product_Sugar_Content = st.selectbox("Product Sugar Content", ["Low Sugar", "Regular", "No Sugar"])
|
| 13 |
-
Product_Allocated_Area = st.number_input("Product Allocated Area", min_value=0.0, value=0.10)
|
| 14 |
-
Product_MRP = st.number_input("Product Maximum Retail Price", min_value=0.10, value=35.00)
|
| 15 |
-
Store_Size = st.selectbox("Store Size", ["Small", "Medium", "High"])
|
| 16 |
-
Store_Location_City_Type = st.selectbox("Store Location City Type", ["Tier 1", "Tier 2", "Tier 3"])
|
| 17 |
-
Store_Type = st.selectbox("Store Type", ["Food Mart","Departmental Store","Supermarket Type1", "Supermarket Type2"])
|
| 18 |
-
Product_Id_char = st.selectbox("Product Id Character", ["FD", "NC", "DR"])
|
| 19 |
-
Store_Age_Years = st.number_input("Store Age - Years", min_value=0, value=15)
|
| 20 |
-
Product_Type_Category = st.selectbox("Product Type Category", ["Perishables", "Non Perishables"])
|
| 21 |
-
|
| 22 |
-
product_data = {
|
| 23 |
-
"Product_Weight": Product_Weight,
|
| 24 |
-
"Product_Sugar_Content": Product_Sugar_Content,
|
| 25 |
-
"Product_Allocated_Area": Product_Allocated_Area,
|
| 26 |
-
"Product_MRP": Product_MRP,
|
| 27 |
-
"Store_Size": Store_Size,
|
| 28 |
-
"Store_Location_City_Type": Store_Location_City_Type,
|
| 29 |
-
"Store_Type": Store_Type,
|
| 30 |
-
"Product_Id_char": Product_Id_char,
|
| 31 |
-
"Store_Age_Years": Store_Age_Years,
|
| 32 |
-
"Product_Type_Cat": Product_Type_Category
|
| 33 |
-
}
|
| 34 |
-
|
| 35 |
-
if st.button("Predict", type='primary'):
|
| 36 |
-
response = requests.post("https://admattew-SuperkartPredictionBackEnd.hf.space/v1/predict", json=product_data) # user name and space name to correctly define the endpoint
|
| 37 |
-
if response.status_code == 200:
|
| 38 |
-
result = response.json()
|
| 39 |
-
predicted_sales = result["Sales"]
|
| 40 |
-
st.write(f"Predicted Product Store Sales Total: ₹{predicted_sales:.2f}")
|
| 41 |
-
else:
|
| 42 |
-
st.error("Error in API request")
|
| 43 |
-
|
| 44 |
-
# Section for batch prediction
|
| 45 |
-
st.subheader("Batch Prediction")
|
| 46 |
-
|
| 47 |
-
# Allow users to upload a CSV file for batch prediction
|
| 48 |
-
uploaded_file = st.file_uploader("Upload CSV file for batch prediction", type=["csv"])
|
| 49 |
-
|
| 50 |
-
# Make batch prediction when the "Predict Batch" button is clicked
|
| 51 |
-
if st.button("Predict Batch"):
|
| 52 |
-
if uploaded_file:
|
| 53 |
-
response = requests.post("https://admattew/SuperkartPredictionBackEnd.hf.space/v1/predictbatch", files={"file": uploaded_file}) # Send file to Flask API
|
| 54 |
-
if response.status_code == 200:
|
| 55 |
-
predictions = response.json()
|
| 56 |
-
st.success("Batch predictions completed!")
|
| 57 |
-
st.write(predictions) # Display the predictions
|
| 58 |
-
else:
|
| 59 |
-
st.error("Error making batch prediction.")
|
| 60 |
-
else:
|
| 61 |
-
st.warning("Please upload a CSV file for batch prediction.")
|
| 62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
|
| 2 |
+
# Import necessary libraries
|
| 3 |
+
import numpy as np
|
| 4 |
+
import joblib # For loading the serialized model
|
| 5 |
+
import pandas as pd # For data manipulation
|
| 6 |
+
from flask import Flask, request, jsonify # For creating the Flask API
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
# Initialize Flask app with a name
|
| 9 |
+
superkart_api = Flask("Babatundes Superkart Sales Predictor") #define the name of the app
|
| 10 |
+
|
| 11 |
+
# Load the trained prediction model
|
| 12 |
+
model = joblib.load("superkart_prediction_model.joblib") #define the location of the serialized model
|
| 13 |
+
|
| 14 |
+
# Define a route for the home page
|
| 15 |
+
@superkart_api.get('/')
|
| 16 |
+
def home():
|
| 17 |
+
"""
|
| 18 |
+
This function handles GET requests to the root URL ('/') of the API.
|
| 19 |
+
It returns a simple welcome message.
|
| 20 |
+
"""
|
| 21 |
+
return "Welcome to Babatunde's Superkart Sales Predictor API!" #define a welcome message
|
| 22 |
+
|
| 23 |
+
# Define an endpoint to predict churn for a single customer
|
| 24 |
+
@superkart_api.post('/v1/predict')
|
| 25 |
+
def predict_sales():
|
| 26 |
+
"""
|
| 27 |
+
This function handles POST requests to the '/v1/predict' endpoint.
|
| 28 |
+
It expects a JSON payload containing property details and returns
|
| 29 |
+
the predicted sales outcome price as a JSON response.
|
| 30 |
+
"""
|
| 31 |
+
# Get JSON data from the request
|
| 32 |
+
data = request.get_json()
|
| 33 |
+
|
| 34 |
+
# Extract relevant product ans store features from the input data. The order of the column names matters.
|
| 35 |
+
sample = {
|
| 36 |
+
'Product_Weight': data['Product_Weight'],
|
| 37 |
+
'Product_Sugar_Content': data['Product_Sugar_Content'],
|
| 38 |
+
'Product_Allocated_Area': data['Product_Allocated_Area'],
|
| 39 |
+
'Product_MRP': data['Product_MRP'],
|
| 40 |
+
'Store_Size': data['Store_Size'],
|
| 41 |
+
'Store_Location_City_Type': data['Store_Location_City_Type'],
|
| 42 |
+
'Store_Type': data['Store_Type'],
|
| 43 |
+
'Product_Id_char': data['Product_Id_char'],
|
| 44 |
+
'Store_Age_Years': data['Store_Age_Years'],
|
| 45 |
+
'Product_Type_Cat': data['Product_Type_Cat']
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
# Convert the extracted data into a DataFrame
|
| 50 |
+
input_data = pd.DataFrame([sample])
|
| 51 |
+
|
| 52 |
+
# Make a sales prediction using the trained model
|
| 53 |
+
prediction = model.predict(input_data).tolist()[0]
|
| 54 |
+
|
| 55 |
+
# Return the prediction as a JSON response
|
| 56 |
+
return jsonify({'Sales': prediction})
|
| 57 |
+
|
| 58 |
+
# Define an endpoint for batch prediction (POST request)
|
| 59 |
+
@superkart_api.post('/v1/predictbatch')
|
| 60 |
+
def predict_sales_batch():
|
| 61 |
+
"""
|
| 62 |
+
This function handles POST requests to the '/v1/predictbatch' endpoint.
|
| 63 |
+
It expects a CSV file containing property details for multiple properties
|
| 64 |
+
and returns the predicted rental prices as a dictionary in the JSON response.
|
| 65 |
+
"""
|
| 66 |
+
# Get the uploaded CSV file from the request
|
| 67 |
+
file = request.files['file']
|
| 68 |
+
|
| 69 |
+
# Read the CSV file into a Pandas DataFrame
|
| 70 |
+
input_data = pd.read_csv(file)
|
| 71 |
+
|
| 72 |
+
# Make predictions for all properties in the DataFrame
|
| 73 |
+
predicted_sales = model.predictbatch(input_data).tolist()
|
| 74 |
+
|
| 75 |
+
# Return the prediction as a JSON response
|
| 76 |
+
return jsonify({'Sales': predicted_sales})
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
# Run the Flask app in debug mode
|
| 81 |
+
if __name__ == '__main__':
|
| 82 |
+
superkart_api.run(debug=True)
|