Abhishek0323 commited on
Commit
cac4f58
·
verified ·
1 Parent(s): 74fc0a9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -16
app.py CHANGED
@@ -1,23 +1,41 @@
1
  import gradio as gr
2
- import requests
 
3
 
4
- # URL of your FastAPI endpoint
5
- API_URL = "http://4.186.39.227/predict"
 
6
 
7
- # Function to call the FastAPI endpoint with customer data
 
 
 
 
 
 
8
  def predict_customer_segment(data):
9
- response = requests.post(API_URL, json=data)
10
- if response.status_code == 200:
11
- cluster_label = response.json()["cluster_label"]
12
- cluster_descriptions = {
13
- 0: "Low Value Customer",
14
- 1: "Medium Value Customer",
15
- 2: "High Value Customer",
16
- 3: "Premium Customer"
17
- }
18
- return cluster_descriptions.get(cluster_label, "Unknown Cluster")
19
- else:
20
- return f"Error: {response.json()['detail']}"
 
 
 
 
 
 
 
 
 
 
21
 
22
  # Function to handle predictions from form input
23
  def predict_from_form(CUST_ID, BALANCE, BALANCE_FREQUENCY, PURCHASES, ONEOFF_PURCHASES, INSTALLMENTS_PURCHASES, CASH_ADVANCE, PURCHASES_FREQUENCY, ONEOFF_PURCHASES_FREQUENCY, PURCHASES_INSTALLMENTS_FREQUENCY, CASH_ADVANCE_FREQUENCY, CASH_ADVANCE_TRX, PURCHASES_TRX, CREDIT_LIMIT, PAYMENTS, MINIMUM_PAYMENTS, PRC_FULL_PAYMENT, TENURE):
 
1
  import gradio as gr
2
+ import pickle
3
+ import numpy as np
4
 
5
+ # Load the preprocessor and model
6
+ with open("processor.pkl", "rb") as f:
7
+ processor = pickle.load(f)
8
 
9
+ with open("model.pkl", "rb") as f:
10
+ model = pickle.load(f)
11
+
12
+ # The original FastAPI endpoint URL (not active now)
13
+ # API_URL = "http://4.186.39.227/predict"
14
+
15
+ # Function to preprocess input data and predict customer segment
16
  def predict_customer_segment(data):
17
+ # Preprocess the data
18
+ processed_data = processor.transform(np.array([[
19
+ data["CUST_ID"], data["BALANCE"], data["BALANCE_FREQUENCY"], data["PURCHASES"],
20
+ data["ONEOFF_PURCHASES"], data["INSTALLMENTS_PURCHASES"], data["CASH_ADVANCE"],
21
+ data["PURCHASES_FREQUENCY"], data["ONEOFF_PURCHASES_FREQUENCY"], data["PURCHASES_INSTALLMENTS_FREQUENCY"],
22
+ data["CASH_ADVANCE_FREQUENCY"], data["CASH_ADVANCE_TRX"], data["PURCHASES_TRX"],
23
+ data["CREDIT_LIMIT"], data["PAYMENTS"], data["MINIMUM_PAYMENTS"], data["PRC_FULL_PAYMENT"],
24
+ data["TENURE"]
25
+ ]]))
26
+
27
+ # Predict the cluster label using the model
28
+ cluster_label = model.predict(processed_data)[0]
29
+
30
+ # Map the cluster label to a human-readable description
31
+ cluster_descriptions = {
32
+ 0: "Low Value Customer",
33
+ 1: "Medium Value Customer",
34
+ 2: "High Value Customer",
35
+ 3: "Premium Customer"
36
+ }
37
+
38
+ return cluster_descriptions.get(cluster_label, "Unknown Cluster")
39
 
40
  # Function to handle predictions from form input
41
  def predict_from_form(CUST_ID, BALANCE, BALANCE_FREQUENCY, PURCHASES, ONEOFF_PURCHASES, INSTALLMENTS_PURCHASES, CASH_ADVANCE, PURCHASES_FREQUENCY, ONEOFF_PURCHASES_FREQUENCY, PURCHASES_INSTALLMENTS_FREQUENCY, CASH_ADVANCE_FREQUENCY, CASH_ADVANCE_TRX, PURCHASES_TRX, CREDIT_LIMIT, PAYMENTS, MINIMUM_PAYMENTS, PRC_FULL_PAYMENT, TENURE):