SujayAery commited on
Commit
270b06d
·
verified ·
1 Parent(s): 8a678f2

Upload folder using huggingface_hub

Browse files
Files changed (4) hide show
  1. Dockerfile +23 -0
  2. SuperKart_v1_0.joblib +3 -0
  3. app.py +110 -0
  4. requirements.txt +11 -0
Dockerfile ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Use slim Python image
2
+ FROM python:3.9-slim
3
+
4
+ # Set working directory inside the container
5
+ WORKDIR /app
6
+
7
+ # Copy project files into the container
8
+ COPY . .
9
+
10
+ # Install dependencies and print package list to verify gunicorn is installed
11
+ RUN pip install --no-cache-dir --upgrade pip \
12
+ && pip install --no-cache-dir -r requirements.txt \
13
+ && echo "Installed packages:" \
14
+ && pip list
15
+
16
+ # Expose the port Hugging Face expects
17
+ EXPOSE 7860
18
+
19
+ # Start the Flask app using gunicorn
20
+ # - app: refers to app.py
21
+ # - app: the Flask app object in app.py (corrected from rental_price_predictor_api)
22
+ CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:7860", "app:app"]
23
+
SuperKart_v1_0.joblib ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:24da6828e3b20a1c6b5341217ea94c563b2f0e091b64def76aff316904ca3e87
3
+ size 687933
app.py ADDED
@@ -0,0 +1,110 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ # Import necessary libraries
3
+ import numpy as np
4
+ import joblib
5
+ import pandas as pd
6
+ from flask import Flask, request, jsonify
7
+ import traceback
8
+ import math
9
+
10
+ # Define the path where the model is saved
11
+ model_file_name = "SuperKart_v1_0.joblib"
12
+
13
+ try:
14
+ # Load the trained machine learning model
15
+ model = joblib.load(model_file_name)
16
+ except FileNotFoundError:
17
+ print(f"Error: Model file not found at {model_file_name}")
18
+ model = None
19
+ except Exception as e:
20
+ print(f"Error loading model: {e}")
21
+ traceback.print_exc()
22
+ model = None
23
+
24
+ # Initialize the Flask app
25
+ app = Flask(__name__)
26
+
27
+ @app.route('/')
28
+ def home():
29
+ return "Welcome to the Super Kart Product Sales Price Prediction API!"
30
+
31
+ # ---------------- single Prediction Endpoint ----------------
32
+ @app.route('/v1/salesprice', methods=['POST'])
33
+ def predict_sales_price():
34
+ if model is None:
35
+ return jsonify({"error": "Model not loaded. Cannot make predictions."}), 500
36
+
37
+ try:
38
+ property_data = request.get_json(force=True)
39
+
40
+ expected_keys = [
41
+ 'Product_Weight', 'Product_Sugar_Content', 'Product_Allocated_Area',
42
+ 'Product_Type', 'Product_MRP', 'Store_Size',
43
+ 'Store_Location_City_Type', 'Store_Type', 'Store_Age'
44
+ ]
45
+ if not all(key in property_data for key in expected_keys):
46
+ missing_keys = [key for key in expected_keys if key not in property_data]
47
+ return jsonify({"error": f"Missing keys in input data: {missing_keys}"}), 400
48
+
49
+ sample = {key: property_data.get(key) for key in expected_keys}
50
+ input_data = pd.DataFrame([sample])
51
+
52
+ predicted_sales_price = model.predict(input_data)
53
+ predicted_price = round(float(predicted_sales_price[0]), 2)
54
+
55
+ if math.isinf(predicted_price) or math.isnan(predicted_price):
56
+ return jsonify({"error": "Prediction resulted in an invalid value."}), 400
57
+
58
+ return jsonify({'Predicted Price': predicted_price}), 200
59
+
60
+ except Exception as e:
61
+ print(f"Error during single prediction: {e}")
62
+ traceback.print_exc()
63
+ return jsonify({"error": "Internal server error", "details": str(e)}), 500
64
+
65
+ # ---------------- Batch Prediction Endpoint ----------------
66
+ @app.route('/v1/salespricebatch', methods=['POST'])
67
+ def predict_sales_price_batch():
68
+ """
69
+ Expects a CSV file with one product per row.
70
+ Returns JSON: a list of dicts with `row_id` and predicted price.
71
+ """
72
+ if model is None:
73
+ return jsonify({"error": "Model not loaded. Cannot make predictions."}), 500
74
+
75
+ if 'file' not in request.files:
76
+ return jsonify({"error": "No file uploaded"}), 400
77
+
78
+ try:
79
+ file = request.files['file']
80
+ input_data = pd.read_csv(file)
81
+
82
+ expected_columns = [
83
+ 'Product_Weight', 'Product_Sugar_Content', 'Product_Allocated_Area',
84
+ 'Product_Type', 'Product_MRP', 'Store_Size',
85
+ 'Store_Location_City_Type', 'Store_Type', 'Store_Age'
86
+ ]
87
+ missing_columns = [col for col in expected_columns if col not in input_data.columns]
88
+ if missing_columns:
89
+ return jsonify({"error": f"Missing required columns: {missing_columns}"}), 400
90
+
91
+ input_data.reset_index(inplace=True)
92
+ input_data.rename(columns={'index': 'row_id'}, inplace=True)
93
+
94
+ predictions = model.predict(input_data[expected_columns])
95
+ predicted_prices = [round(float(p), 2) for p in predictions]
96
+
97
+ results = [
98
+ {"row_id": row_id, "Predicted Price": price}
99
+ for row_id, price in zip(input_data['row_id'], predicted_prices)
100
+ ]
101
+
102
+ return jsonify(results), 200
103
+
104
+ except Exception as e:
105
+ print(f"Error during batch prediction: {e}")
106
+ traceback.print_exc()
107
+ return jsonify({"error": "Internal server error during batch prediction.", "details": str(e)}), 500
108
+
109
+ if __name__ == '__main__':
110
+ pass
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
+ streamlit==1.43.2
11
+ flask-cors==3.0.10