Spaces:
Runtime error
Runtime error
Upload folder using huggingface_hub
Browse files- Dockerfile +9 -7
- app.py +76 -99
- requirements.txt +8 -1
- superkart_sales_model_v1_0.joblib +3 -0
Dockerfile
CHANGED
|
@@ -1,14 +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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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:house_price_api"]
|
app.py
CHANGED
|
@@ -1,100 +1,77 @@
|
|
| 1 |
-
import
|
| 2 |
import pandas as pd
|
| 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 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
#
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
#
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
try:
|
| 79 |
-
files = {"file": (file.name, file, "text/csv")}
|
| 80 |
-
response = requests.post(api_url, files=files, timeout=60)
|
| 81 |
-
if response.status_code == 200:
|
| 82 |
-
result = response.json()
|
| 83 |
-
result_df = pd.DataFrame(result)
|
| 84 |
-
st.header("Batch Prediction Results")
|
| 85 |
-
st.dataframe(result_df)
|
| 86 |
-
|
| 87 |
-
# Download button
|
| 88 |
-
csv = result_df.to_csv(index=False).encode('utf-8')
|
| 89 |
-
st.download_button(
|
| 90 |
-
label="Download Predictions as CSV",
|
| 91 |
-
data=csv,
|
| 92 |
-
file_name="predictions.csv",
|
| 93 |
-
mime="text/csv"
|
| 94 |
-
)
|
| 95 |
-
else:
|
| 96 |
-
st.error(f"Error in API request: Status Code {response.status_code}")
|
| 97 |
-
st.error(f"Response: {response.text}")
|
| 98 |
-
except requests.exceptions.RequestException as e:
|
| 99 |
-
st.error(f"Error connecting to API: {str(e)}")
|
| 100 |
-
st.info("Please ensure your backend API is deployed and the URL is correct.")
|
|
|
|
| 1 |
+
import joblib
|
| 2 |
import pandas as pd
|
| 3 |
+
from flask import Flask, request, jsonify
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
# Initialize Flask app
|
| 7 |
+
sales_forecast_api = Flask("SuperKart Sales Forecast Predictor")
|
| 8 |
+
|
| 9 |
+
# Load the trained SuperKart sales model
|
| 10 |
+
model = joblib.load("superkart_sales_model_v1_0.joblib")
|
| 11 |
+
|
| 12 |
+
# Define a route for the home page
|
| 13 |
+
@sales_forecast_api.get('/')
|
| 14 |
+
def home():
|
| 15 |
+
return "Welcome to the SuperKart Sales Revenue Forecasting API!"
|
| 16 |
+
|
| 17 |
+
# Define an endpoint to predict sales for a single product-store combination
|
| 18 |
+
@sales_forecast_api.post('/v1/sales')
|
| 19 |
+
def predict_sales():
|
| 20 |
+
# Get JSON data from the request
|
| 21 |
+
sales_data = request.get_json()
|
| 22 |
+
|
| 23 |
+
# Extract relevant features from the input data
|
| 24 |
+
# Note: Store_Age will be calculated from Store_Establishment_Year
|
| 25 |
+
current_year = 2024
|
| 26 |
+
store_age = current_year - sales_data['Store_Establishment_Year']
|
| 27 |
+
|
| 28 |
+
sample = {
|
| 29 |
+
'Product_Weight': sales_data['Product_Weight'],
|
| 30 |
+
'Product_Sugar_Content': sales_data['Product_Sugar_Content'],
|
| 31 |
+
'Product_Allocated_Area': sales_data['Product_Allocated_Area'],
|
| 32 |
+
'Product_Type': sales_data['Product_Type'],
|
| 33 |
+
'Product_MRP': sales_data['Product_MRP'],
|
| 34 |
+
'Store_Establishment_Year': sales_data['Store_Establishment_Year'],
|
| 35 |
+
'Store_Size': sales_data['Store_Size'],
|
| 36 |
+
'Store_Location_City_Type': sales_data['Store_Location_City_Type'],
|
| 37 |
+
'Store_Type': sales_data['Store_Type'],
|
| 38 |
+
'Store_Age': store_age
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
# Convert the extracted data into a DataFrame
|
| 42 |
+
input_data = pd.DataFrame([sample])
|
| 43 |
+
|
| 44 |
+
# Make a prediction using the trained model
|
| 45 |
+
prediction = model.predict(input_data).tolist()[0]
|
| 46 |
+
|
| 47 |
+
# Return the prediction as a JSON response
|
| 48 |
+
return jsonify({'Predicted_Sales_Total': prediction})
|
| 49 |
+
|
| 50 |
+
# Define an endpoint to predict sales for a batch of product-store combinations
|
| 51 |
+
@sales_forecast_api.post('/v1/salesbatch')
|
| 52 |
+
def predict_sales_batch():
|
| 53 |
+
# Get the uploaded CSV file from the request
|
| 54 |
+
file = request.files['file']
|
| 55 |
+
|
| 56 |
+
# Read the file into a DataFrame
|
| 57 |
+
input_data = pd.read_csv(file)
|
| 58 |
+
|
| 59 |
+
# Calculate Store_Age if not present
|
| 60 |
+
if 'Store_Age' not in input_data.columns:
|
| 61 |
+
current_year = 2024
|
| 62 |
+
input_data['Store_Age'] = current_year - input_data['Store_Establishment_Year']
|
| 63 |
+
|
| 64 |
+
# Make predictions for the batch data
|
| 65 |
+
predictions = model.predict(input_data).tolist()
|
| 66 |
+
|
| 67 |
+
# Add predictions to the DataFrame
|
| 68 |
+
input_data['Predicted_Sales_Total'] = predictions
|
| 69 |
+
|
| 70 |
+
# Convert results to dictionary
|
| 71 |
+
result = input_data.to_dict(orient="records")
|
| 72 |
+
|
| 73 |
+
return jsonify(result)
|
| 74 |
+
|
| 75 |
+
# Run the Flask app in debug mode
|
| 76 |
+
if __name__ == '__main__':
|
| 77 |
+
sales_forecast_api.run(debug=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
requirements.txt
CHANGED
|
@@ -1,3 +1,10 @@
|
|
| 1 |
pandas==2.2.2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
requests==2.28.1
|
| 3 |
-
|
|
|
|
| 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]
|
superkart_sales_model_v1_0.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:200c1855aa4114e035256f3c29e366623272deda030dc23af2613f70315a8f4b
|
| 3 |
+
size 23837399
|