abhishek1504 commited on
Commit
be2098d
·
verified ·
1 Parent(s): 73b58c2

Upload folder using huggingface_hub

Browse files
Files changed (4) hide show
  1. Dockerfile +16 -0
  2. app.py +83 -0
  3. requirements.txt +10 -0
  4. sales_prediction_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:house_price_api"]
app.py ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import joblib
2
+ import pandas as pd
3
+ from flask import Flask, request, jsonify
4
+
5
+ # Initialize Flask app
6
+ sales_api = Flask("Product Store Sales Predictor")
7
+
8
+ # Load the trained sales prediction model
9
+ # model = joblib.load("deployment_files/sales_prediction_model_v1_0.pkl")
10
+ model = saved_model
11
+
12
+ # Define a route for the home page
13
+ @sales_api.get('/')
14
+ def home():
15
+ return "Welcome to the Product Store Sales Prediction API!"
16
+
17
+ # ==============================
18
+ # Single record prediction endpoint
19
+ # ==============================
20
+ @sales_api.post('/v1/sales')
21
+ def predict_sales():
22
+ try:
23
+ # Get JSON data from the request
24
+ data = request.get_json()
25
+
26
+ # Extract all required product/store features from the input data
27
+ sample = {
28
+ 'Product_Id': data['Product_Id'],
29
+ 'Product_Weight': data['Product_Weight'],
30
+ 'Product_Sugar_Content': data['Product_Sugar_Content'],
31
+ 'Product_Allocated_Area': data['Product_Allocated_Area'],
32
+ 'Product_Type': data['Product_Type'],
33
+ 'Product_MRP': data['Product_MRP'],
34
+ 'Store_Id': data['Store_Id'],
35
+ 'Store_Establishment_Year': data['Store_Establishment_Year'],
36
+ 'Store_Size': data['Store_Size'],
37
+ 'Store_Location_City_Type': data['Store_Location_City_Type'],
38
+ 'Store_Type': data['Store_Type']
39
+ }
40
+
41
+ # Convert dictionary to DataFrame
42
+ input_df = pd.DataFrame([sample])
43
+
44
+ # Make prediction using the trained model
45
+ prediction = model.predict(input_df).tolist()[0]
46
+
47
+ # Return JSON response
48
+ return jsonify({'Predicted_Product_Store_Sales_Total': prediction})
49
+
50
+ except Exception as e:
51
+ return jsonify({'error': str(e)}), 400
52
+
53
+ # ==============================
54
+ # Batch prediction endpoint
55
+ # ==============================
56
+ @sales_api.post('/v1/salesbatch')
57
+ def predict_sales_batch():
58
+ try:
59
+ # Get uploaded CSV file from the request
60
+ file = request.files['file']
61
+
62
+ # Read CSV file into DataFrame
63
+ input_data = pd.read_csv(file)
64
+
65
+ # Make predictions on the batch
66
+ predictions = model.predict(input_data).tolist()
67
+
68
+ # Add predictions as new column
69
+ input_data['Predicted_Product_Store_Sales_Total'] = predictions
70
+
71
+ # Convert DataFrame to list of dicts for JSON output
72
+ result = input_data.to_dict(orient='records')
73
+
74
+ return jsonify(result)
75
+
76
+ except Exception as e:
77
+ return jsonify({'error': str(e)}), 400
78
+
79
+ # ==============================
80
+ # Run Flask app
81
+ # ==============================
82
+ if __name__ == '__main__':
83
+ sales_api.run(debug=True)
requirements.txt ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
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]
sales_prediction_model_v1_0.joblib ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:4657187149681115b7fe62798bb261be5dcfaa2831d4028d46d7da0afc9da490
3
+ size 647912