akarora93 commited on
Commit
ac863b1
·
verified ·
1 Parent(s): fff8e92

Upload folder using huggingface_hub

Browse files
Files changed (4) hide show
  1. Dockerfile +16 -0
  2. app.py +77 -0
  3. requirements.txt +9 -0
  4. sales_forecast_model_v1_0.joblib +3 -0
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:sales_forecast_api"]
app.py ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ sales_forecast_api = Flask("Sales Forecast Prediction API ")
9
+
10
+ # Load the trained churn prediction model
11
+ model = joblib.load("sales_forecast_model_v1_0.joblib")
12
+
13
+ # Define a route for the home page
14
+ @sales_forecast_api.get('/')
15
+ def home():
16
+ return "Welcome to the Sales Forecast Prediction API!"
17
+
18
+ # Define an endpoint to predict churn for a single customer
19
+ @sales_forecast_api.post('/v1/product')
20
+ def predict_sales_forecast():
21
+ # Get JSON data from the request
22
+ product_data = request.get_json()
23
+
24
+ # Extract relevant customer features from the input data
25
+ sample = {
26
+ 'Product_Weight': product_data['Product_Weight'],
27
+ 'Product_Sugar_Content': product_data['Product_Sugar_Content'],
28
+ 'Product_Allocated_Area': product_data['Product_Allocated_Area'],
29
+ 'Product_Type': product_data['Product_Type'],
30
+ 'Product_MRP': product_data['Product_MRP'],
31
+ 'Store_Id': product_data['Store_Id'],
32
+ 'Store_Establishment_Year': product_data['Store_Establishment_Year'],
33
+ 'Store_Size': product_data['Store_Size'],
34
+ 'Store_Location_City_Type': product_data['Store_Location_City_Type'],
35
+ 'Store_Type': product_data['Store_Type']
36
+ }
37
+
38
+ # Convert the extracted data into a DataFrame
39
+ input_data = pd.DataFrame([sample])
40
+
41
+ # Make prediction (get sales_forecast)
42
+ predicted_log_price = model.predict(input_data)[0]
43
+
44
+ # Calculate actual price
45
+ predicted_price = np.exp(predicted_log_price)
46
+
47
+ # Map prediction result to a human-readable label
48
+ predicted_price = round(float(predicted_price), 2)
49
+
50
+ # Return the prediction as a JSON response
51
+ return jsonify({'Sales Forecast (in dollars)': predicted_price})
52
+
53
+ # Define an endpoint to predict churn for a batch of customers
54
+ @sales_forecast_api.post('/v1/productbatch')
55
+ def predict_sales_forecast_batch():
56
+ # Get the uploaded CSV file from the request
57
+ file = request.files['file']
58
+
59
+ # Read the CSV file into a Pandas DataFrame
60
+ input_data = pd.read_csv(file)
61
+
62
+ # Make predictions for all properties in the DataFrame (get log_prices)
63
+ predicted_log_prices = model.predict(input_data).tolist()
64
+
65
+ # Calculate actual prices
66
+ predicted_sales = [round(float(np.exp(log_price)), 2) for log_price in predicted_log_prices]
67
+
68
+ # Create a dictionary of predictions with property IDs as keys
69
+ product_ids = input_data['Product_ID'].tolist() # Assuming 'id' is the property ID column
70
+ output_dict = dict(zip(product_ids, predicted_sales)) # Use actual prices
71
+
72
+ # Return the predictions dictionary as a JSON response
73
+ return output_dict
74
+
75
+ # Run the Flask application in debug mode if this script is executed directly
76
+ if __name__ == '__main__':
77
+ sales_forecast_api.run(debug=True)
requirements.txt ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ numpy==2.0.2
2
+ pandas==2.2.2
3
+ scikit-learn==1.6.1
4
+ seaborn==0.13.2
5
+ joblib==1.4.2
6
+ xgboost==2.1.4
7
+ requests==2.32.3
8
+ huggingface_hub==0.30.1
9
+ Flask==3.0.3
sales_forecast_model_v1_0.joblib ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:c24befbc23d8e5538fd746cf4710b8edf1ea5cab6bb129398a9a67e6a3133af6
3
+ size 141790218