sahilsingla commited on
Commit
773cc50
·
verified ·
1 Parent(s): 7724041

Upload folder using huggingface_hub

Browse files
Dockerfile ADDED
@@ -0,0 +1,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:rental_price_predictor_api"]
app.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 the Flask application
9
+ sales_forecast_api = Flask("SuperKart Sales Forecast API")
10
+
11
+ # Load the trained machine learning model
12
+ model = joblib.load("superkart_sales_prediction_model_v1_0.joblib")
13
+
14
+ # Define a route for the home page (GET request)
15
+ @sales_forecast_api.get('/')
16
+ def home():
17
+ return "Welcome to the SuperKart Sales Forecast API!"
18
+
19
+ # Define an endpoint for single prediction (POST request)
20
+ @sales_forecast_api.post('/v1/sales')
21
+ def predict_sales():
22
+ """
23
+ This function handles POST requests to the '/v1/sales' endpoint.
24
+ It expects a JSON payload with product and store attributes and returns
25
+ the predicted product-store sales revenue.
26
+ """
27
+ # Get the JSON data
28
+ data = request.get_json()
29
+
30
+ # Extract features based on the data dictionary
31
+ sample = {
32
+ 'Product_Weight': data['Product_Weight'],
33
+ 'Product_Sugar_Content': data['Product_Sugar_Content'],
34
+ 'Product_Allocated_Area': data['Product_Allocated_Area'],
35
+ 'Product_Type': data['Product_Type'],
36
+ 'Product_MRP': data['Product_MRP'],
37
+ 'Store_Establishment_Year': data['Store_Establishment_Year'],
38
+ 'Store_Size': data['Store_Size'],
39
+ 'Store_Location_City_Type': data['Store_Location_City_Type'],
40
+ 'Store_Type': data['Store_Type']
41
+ }
42
+
43
+ # Convert to DataFrame
44
+ input_df = pd.DataFrame([sample])
45
+
46
+ # Predict sales
47
+ predicted_sales = model.predict(input_df)[0]
48
+ predicted_sales = round(float(predicted_sales), 2)
49
+
50
+ return jsonify({'Predicted Product-Store Sales Total': predicted_sales})
51
+
52
+ # Define an endpoint for batch prediction (POST request)
53
+ @sales_forecast_api.post('/v1/salesbatch')
54
+ def predict_sales_batch():
55
+ """
56
+ Handles POST requests to '/v1/salesbatch'.
57
+ Accepts a CSV file and returns predicted sales totals for each product-store.
58
+ """
59
+ # Read the uploaded file
60
+ file = request.files['file']
61
+ input_df = pd.read_csv(file)
62
+
63
+ # Predict sales for the batch
64
+ predictions = model.predict(input_df).tolist()
65
+ predictions = [round(float(pred), 2) for pred in predictions]
66
+
67
+ # Use Product_Id and Store_Id as combined key if available
68
+ if 'Product_Id' in input_df.columns and 'Store_Id' in input_df.columns:
69
+ keys = input_df['Product_Id'] + "_" + input_df['Store_Id']
70
+ else:
71
+ keys = list(range(len(predictions)))
72
+
73
+ results = dict(zip(keys, predictions))
74
+
75
+ return jsonify(results)
76
+
77
+ # Run the application
78
+ if __name__ == '__main__':
79
+ sales_forecast_api.run(debug=True)
requirements.txt ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
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
superkart_sales_prediction_model_v1_0.joblib ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b8fb7aa467fdbb38a4c2e1f19e93b6f7163c9f3c16dff8ad370aa013e12b38db
3
+ size 107883