Spaces:
Build error
Build error
Upload folder using huggingface_hub
Browse files- Dockerfile +8 -10
- app.py +86 -69
- requirements.txt +8 -0
Dockerfile
CHANGED
@@ -1,16 +1,14 @@
|
|
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
|
8 |
-
COPY
|
9 |
|
10 |
-
# Install
|
11 |
-
RUN
|
12 |
|
13 |
-
#
|
14 |
-
|
15 |
-
|
16 |
-
# NOTE: Disable XSRF protection for easier external access in order to make batch predictions
|
|
|
|
|
1 |
FROM python:3.9-slim
|
2 |
|
3 |
+
# Set the working directory inside the container
|
4 |
WORKDIR /app
|
5 |
|
6 |
+
# Copy all files from deployment_files into the container
|
7 |
+
COPY deployment_files/ ./
|
8 |
|
9 |
+
# Install dependencies
|
10 |
+
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
11 |
|
12 |
+
# Run the Flask app with Gunicorn
|
13 |
+
# Here: app.py is in deployment_files, Flask instance = store_sales_api
|
14 |
+
CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:7860", "app:store_sales_api"]
|
|
app.py
CHANGED
@@ -1,70 +1,87 @@
|
|
1 |
-
|
2 |
-
import
|
3 |
-
import
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
)
|
46 |
-
|
47 |
-
|
48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
else:
|
50 |
-
|
51 |
-
|
52 |
-
#
|
53 |
-
|
54 |
-
|
55 |
-
#
|
56 |
-
|
57 |
-
|
58 |
-
#
|
59 |
-
if
|
60 |
-
|
61 |
-
|
62 |
-
"https://Disha252001-SuperKart-Frontend.hf.space/v1/salebatch",
|
63 |
-
files={"file": uploaded_file}
|
64 |
-
)
|
65 |
-
if response.status_code == 200:
|
66 |
-
predictions = response.json()
|
67 |
-
st.success("Batch predictions completed!")
|
68 |
-
st.write(predictions) # Display the predictions
|
69 |
-
else:
|
70 |
-
st.error("Error making batch prediction.")
|
|
|
1 |
+
# Import necessary libraries
|
2 |
+
import numpy as np
|
3 |
+
import joblib # For loading the serialized model
|
4 |
+
import pandas as pd # For data manipulation
|
5 |
+
from flask import Flask, request, jsonify # For creating the Flask API
|
6 |
+
|
7 |
+
# Initialize the Flask application
|
8 |
+
store_sales_api = Flask("SuperKart Store Sales Predictor")
|
9 |
+
|
10 |
+
# Load the trained machine learning model
|
11 |
+
model = joblib.load("store_sales_prediction_model_v1_0.joblib")
|
12 |
+
|
13 |
+
# Define a route for the home page (GET request)
|
14 |
+
@store_sales_api.get('/')
|
15 |
+
def home():
|
16 |
+
"""
|
17 |
+
Root endpoint to check if the API is running.
|
18 |
+
"""
|
19 |
+
return "Welcome to the SuperKart Store Sales Prediction API!"
|
20 |
+
|
21 |
+
# Define an endpoint for single store sales prediction (POST request)
|
22 |
+
@store_sales_api.post('/v1/sale')
|
23 |
+
def predict_store_sales():
|
24 |
+
"""
|
25 |
+
Handles POST requests for predicting sales of a single store.
|
26 |
+
Expects JSON input with store details.
|
27 |
+
"""
|
28 |
+
# Get JSON input
|
29 |
+
store_data = request.get_json()
|
30 |
+
|
31 |
+
# Extract relevant features (update according to your dataset)
|
32 |
+
sample = {
|
33 |
+
'Store_Type': store_data['Store_Type'],
|
34 |
+
'Location_Type': store_data['Location_Type'],
|
35 |
+
'Region_Code': store_data['Region_Code'],
|
36 |
+
'Holiday': store_data['Holiday'],
|
37 |
+
'Discount': store_data['Discount']
|
38 |
+
# Add any other features you used during training
|
39 |
+
}
|
40 |
+
|
41 |
+
# Convert into DataFrame
|
42 |
+
input_data = pd.DataFrame([sample])
|
43 |
+
|
44 |
+
# Make prediction (direct Store_Sales prediction)
|
45 |
+
predicted_sales = model.predict(input_data)[0]
|
46 |
+
|
47 |
+
# Convert to Python float and round
|
48 |
+
predicted_sales = round(float(predicted_sales), 2)
|
49 |
+
|
50 |
+
# Return JSON response
|
51 |
+
return jsonify({'Predicted Store Sales': predicted_sales})
|
52 |
+
|
53 |
+
# Define an endpoint for batch prediction (POST request)
|
54 |
+
@store_sales_api.post('/v1/salebatch')
|
55 |
+
def predict_store_sales_batch():
|
56 |
+
"""
|
57 |
+
Handles batch predictions for multiple stores.
|
58 |
+
Expects a CSV file with store details.
|
59 |
+
"""
|
60 |
+
# Get uploaded CSV file
|
61 |
+
file = request.files['file']
|
62 |
+
|
63 |
+
# Read CSV into DataFrame
|
64 |
+
input_data = pd.read_csv(file)
|
65 |
+
|
66 |
+
# Make predictions
|
67 |
+
predicted_sales = model.predict(input_data).tolist()
|
68 |
+
|
69 |
+
# Convert to float and round
|
70 |
+
predicted_sales = [round(float(sale), 2) for sale in predicted_sales]
|
71 |
+
|
72 |
+
# Use Store IDs if available
|
73 |
+
if 'Store_ID' in input_data.columns:
|
74 |
+
store_ids = input_data['Store_ID'].tolist()
|
75 |
else:
|
76 |
+
store_ids = list(range(1, len(predicted_sales)+1)) # Fallback index
|
77 |
+
|
78 |
+
# Create dictionary of predictions
|
79 |
+
output_dict = dict(zip(store_ids, predicted_sales))
|
80 |
+
|
81 |
+
# Return JSON
|
82 |
+
return jsonify(output_dict)
|
83 |
+
|
84 |
+
# Run Flask app
|
85 |
+
if __name__ == '__main__':
|
86 |
+
store_sales_api.run(debug=True)
|
87 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
requirements.txt
CHANGED
@@ -1,3 +1,11 @@
|
|
1 |
pandas==2.2.2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
requests==2.28.1
|
|
|
3 |
streamlit==1.43.2
|
|
|
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
|