manasranjanpani commited on
Commit
9b2f059
·
verified ·
1 Parent(s): 8061dcb

Upload folder using huggingface_hub

Browse files
Dockerfile ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.9-slim
2
+
3
+ # Set the working directory inside the container
4
+ WORKDIR /app
5
+
6
+ # Copy dependency file first (better layer caching)
7
+ COPY requirements.txt .
8
+
9
+ # Install dependencies without cache
10
+ RUN pip install --no-cache-dir --upgrade -r requirements.txt
11
+
12
+ # Copy the rest of the code
13
+ COPY . .
14
+
15
+ # Environment variable for Hugging Face Spaces (or Docker run)
16
+ ENV PORT=7860
17
+
18
+ # Expose the port
19
+ EXPOSE $PORT
20
+
21
+ # Start the Flask app using Gunicorn with 4 worker processes
22
+ # - "app:sales_revenue_predictor_api" → app.py has Flask instance named sales_revenue_predictor_api
23
+ CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:7860", "app:sales_revenue_predictor_api"]
app.py ADDED
@@ -0,0 +1,124 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ extraaLearn_predictor_api = Flask("ExtraaLearn paid customers Predictor")
9
+
10
+ # Load the trained machine learning model
11
+ model = joblib.load("extraaLearn_model_prediction_model_v1_0.joblib")
12
+
13
+ # -----------------------------
14
+ # Feature mapping (incoming JSON -> model column names)
15
+ # -----------------------------
16
+ feature_mapping = {
17
+ "id": "ID",
18
+ "age": "age",
19
+ "currentOccupation": "current_occupation",
20
+ "firstInteraction": "first_interaction",
21
+ "profileCompleted": "profile_completed",
22
+ "websiteVisits": "website_visits",
23
+ "timeSpentOnWebsite": "time_spent_on_website",
24
+ "pageViewsPerVisit": "page_views_per_visit",
25
+ "lastActivity": "last_activity",
26
+ "printMediaType1": "print_media_type1",
27
+ "printMediaType2": "print_media_type2",
28
+ "digitalMedia": "digital_media",
29
+ "educationalChannels": "educational_channels",
30
+ "referral": "referral",
31
+ "status": "status" # (target) include if present in input; otherwise leave out at inference
32
+ }
33
+
34
+
35
+ # -----------------------------
36
+ # Routes
37
+ # -----------------------------
38
+
39
+ # Health check
40
+ @extraaLearn_predictor_api.get("/ping")
41
+ def ping():
42
+ """Simple health check endpoint."""
43
+ return jsonify({"status": "ok"})
44
+
45
+
46
+ # Home route
47
+ @extraaLearn_predictor_api.get("/")
48
+ def home():
49
+ """Welcome message for the API."""
50
+ return "Welcome to the ExtraaLearn customers Prediction API!"
51
+
52
+
53
+ # Single prediction
54
+ @extraaLearn_predictor_api.post("/v1/customers")
55
+ def predict_sales_revenue():
56
+ """
57
+ Handles POST requests to predict sales revenue for a single product/store.
58
+ Expects a JSON payload with features.
59
+ """
60
+ # try:
61
+ # Get the JSON data from the request body
62
+ property_data = request.get_json()
63
+
64
+ # Map input keys to model feature names
65
+ sample = {}
66
+ for api_key, model_key in feature_mapping.items():
67
+ if api_key not in property_data:
68
+ return jsonify({"error": f"Missing required field: {api_key}"}), 400
69
+ sample[model_key] = property_data[api_key]
70
+
71
+ # Convert the extracted data into a Pandas DataFrame
72
+ input_data = pd.DataFrame([sample])
73
+
74
+ # Make prediction (log-transformed sales total)
75
+ predicted_customer = model.predict(input_data)[0]
76
+
77
+
78
+
79
+ return jsonify({"Predicted_Sales": predicted_customer})
80
+
81
+ # except Exception as e:
82
+ # return jsonify({"error": str(e)}), 500
83
+
84
+
85
+ # Batch prediction
86
+ @extraaLearn_predictor_api.post("/v1/customersbatch")
87
+ def predict_sales_batch():
88
+ """
89
+ Handles POST requests for batch prediction.
90
+ Expects a CSV file with multiple records.
91
+ """
92
+ try:
93
+ # Get the uploaded CSV file
94
+ file = request.files.get("file")
95
+ if file is None:
96
+ return jsonify({"error": "CSV file is required"}), 400
97
+
98
+ # Read the CSV file into a Pandas DataFrame
99
+ input_data = pd.read_csv(file)
100
+
101
+ # Make predictions
102
+ predicted_extraaLearn_customers_totals = model.predict(input_data).tolist()
103
+
104
+ # Convert predictions back from log scale
105
+ predicted_customers = [
106
+ round(float(np.exp(p)), 2) for p in predicted_extraaLearn_customers_totals
107
+ ]
108
+
109
+ # If an "id" column exists, return mapping {id: prediction}
110
+ if "id" in input_data.columns:
111
+ property_ids = input_data["id"].tolist()
112
+ output_dict = dict(zip(property_ids, predicted_customers))
113
+ else:
114
+ output_dict = {"predictions": predicted_customers}
115
+
116
+ return jsonify(output_dict)
117
+
118
+ except Exception as e:
119
+ return jsonify({"error": str(e)}), 500
120
+
121
+
122
+ # Run the Flask application in debug mode if this script is executed directly
123
+ if __name__ == "__main__":
124
+ extraaLearn_predictor_api.run(host="0.0.0.0", port=7860, debug=True)
extraaLearn_model_prediction_model_v1_0.joblib ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:0180b83fd6065d069fc94df4b2b954846d086d14e9f116f7f431898ad745472a
3
+ size 89891
requirements.txt ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ gunicorn
2
+ pandas==2.2.2
3
+ numpy==2.0.2
4
+ scikit-learn==1.6.1
5
+ xgboost==2.1.4
6
+ joblib==1.4.2
7
+ Werkzeug==2.2.2
8
+ flask==2.2.2
9
+ gunicorn==20.1.0
10
+ requests==2.28.1
11
+ uvicorn[standard]
12
+ streamlit==1.43.2