-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathapp.py
More file actions
212 lines (171 loc) · 9.17 KB
/
app.py
File metadata and controls
212 lines (171 loc) · 9.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
from flask import Flask, render_template, request, jsonify
import pandas as pd
import joblib
import json
import re
import torch
import transformers
from transformers import AutoModelForCausalLM, AutoTokenizer
from langchain.llms import HuggingFacePipeline
from functools import lru_cache
app = Flask(__name__)
# Load models from disk (assumed models are pre-trained and saved in 'models' directory)
model_paths = {
'kmeans': 'models/kmeans_model.pkl',
'scaler': 'models/scaler.pkl',
'multi_target_classifier': 'models/multi_target_classifier_model.pkl',
'rf_regressor_personal_loan': 'models/rf_regressor_personal_loan_model.pkl',
'rf_regressor_home_loan': 'models/rf_regressor_home_loan_model.pkl',
'rf_regressor_credit_card': 'models/rf_regressor_credit_card_model.pkl'
}
kmeans = joblib.load(model_paths['kmeans'])
scaler = joblib.load(model_paths['scaler'])
multi_target_classifier = joblib.load(model_paths['multi_target_classifier'])
rf_regressor_personal_loan = joblib.load(model_paths['rf_regressor_personal_loan'])
rf_regressor_home_loan = joblib.load(model_paths['rf_regressor_home_loan'])
rf_regressor_credit_card = joblib.load(model_paths['rf_regressor_credit_card'])
print("Models loaded successfully!")
@lru_cache(maxsize=None)
def load_model():
model_name = "KingNish/Qwen2.5-0.5b-Test-ft"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)
return tokenizer, model
# Function to generate insights based on customer data
# Function to generate insights based on customer data
def generate_insights(customer_data):
tokenizer, model = load_model()
# Create a prompt from the customer data
prompt = f"""
Generate a personalized summarised insight about the following customer based on their data:
- Name: {customer_data['name']}
- Age: {customer_data['age']}
- Gender: {customer_data['gender']}
- Marital Status: {customer_data['marital_status']}
- Education: {customer_data['education']}
- Occupation: {customer_data['occupation']}
- Salary: ${customer_data['salary']:,.2f}
- Loan Amount: ${customer_data['loan_amount']:,.2f}
- Credit Limit: ${customer_data['credit_limit']:,.2f}
- Credit Utilization: {customer_data['credit_utilization']:.2%}
- EMI Paid: {customer_data['emi_paid']}
- Tenure Months: {round(float(customer_data['tenure_months']),2)}
- Max DPD: {customer_data['max_dpd']}
- Default Status: {int(customer_data['default_status'])}
- Account Balance: ${customer_data['account_balance']:,.2f}
- Credit Card: {customer_data['Credit Card']},
- Home Loan: {customer_data['Home Loan']},
- Personal Loan: {customer_data['Personal Loan']},
Here are the Summarised Insights about {customer_data['name']}:
"""
# Initialize the query pipeline with increased max_length
query_pipeline = transformers.pipeline(
"text-generation",
model=model,
tokenizer=tokenizer,
torch_dtype=torch.float16,
max_length=6000, # Increase max_length
max_new_tokens=500, # Control the number of new tokens generated
device_map="auto",
)
llm = HuggingFacePipeline(pipeline=query_pipeline)
insights = llm(prompt)
return insights
def process_customer_data(json_data, scaler):
# Convert JSON data to a DataFrame
customer_data = pd.DataFrame.from_dict(json_data, orient='index').T
# Drop columns not needed for clustering (based on your clustering model)
clustering_data = customer_data[['age', 'salary', 'loan_amount', 'credit_limit', 'credit_utilization',
'emi_paid', 'tenure_months', 'max_dpd', 'default_status',
'enquiry_amount', 'unique_products_enquired', 'total_enquiries',
'transaction_amount', 'account_balance', 'is_salary', 'Credit Card',
'Home Loan', 'Personal Loan']]
# Handle missing values by replacing with 0 (or any appropriate strategy)
clustering_data.fillna(0, inplace=True)
scaled_data = scaler.transform(clustering_data)
return customer_data, scaled_data
def predict_customer_segment(scaled_data, kmeans):
# Predict the customer segment using your pre-trained KMeans model
customer_segment = kmeans.predict(scaled_data)
return customer_segment[0]
def recommend_product_and_loan(json_data, kmeans, scaler, multi_target_classifier, rf_regressor_personal_loan, rf_regressor_home_loan, rf_regressor_credit_card):
# Convert JSON data to a DataFrame and scale the data
customer_data, scaled_data = process_customer_data(json_data, scaler)
# Step 1: Predict customer segment using KMeans
customer_segment = predict_customer_segment(scaled_data, kmeans)
# Add the predicted customer segment back to the customer_data DataFrame
customer_data['customer_segment'] = customer_segment
customer_data = customer_data[['age', 'salary', 'loan_amount', 'credit_limit', 'credit_utilization',
'emi_paid', 'tenure_months', 'max_dpd', 'default_status',
'enquiry_amount', 'unique_products_enquired', 'total_enquiries',
'transaction_amount', 'account_balance', 'is_salary', 'Credit Card',
'Home Loan', 'Personal Loan', 'customer_segment']]
# Prepare for product recommendation using Random Forest Classifier
X_classification_prod = customer_data.drop(columns=['Credit Card', 'Home Loan', 'Personal Loan'])
X_classification_amt = customer_data
print(X_classification_prod.columns)
# Step 2: Predict probabilities for each product using the multi-output classifier
prob_credit_card = [estimator.predict_proba(X_classification_prod)[:, 1] for estimator in multi_target_classifier.estimators_]
# Combine probabilities into a Series
product_probabilities = pd.Series({
'Credit Card': prob_credit_card[0][0], # Since it's for one customer, we get the first value
'Home Loan': prob_credit_card[1][0],
'Personal Loan': prob_credit_card[2][0]
})
print(product_probabilities)
# Identify the most probable product
recommended_product = product_probabilities.idxmax()
recommended_probability = product_probabilities.max()
recommendation = f"Recommended Product: {recommended_product} (Probability: {recommended_probability:.2f})"
# Step 3: Predict loan amounts or credit limits based on the recommended product
if recommended_product == 'Personal Loan':
predicted_loan_amount_personal = rf_regressor_personal_loan.predict(X_classification_amt.drop(columns=['loan_amount']))
recommendation += f"\nPredicted Loan Amount: {predicted_loan_amount_personal[0]:,.2f}"
elif recommended_product == 'Home Loan':
predicted_loan_amount_home = rf_regressor_home_loan.predict(X_classification_amt.drop(columns=['loan_amount']))
recommendation += f"\nPredicted Loan Amount: {predicted_loan_amount_home[0]:,.2f}"
elif recommended_product == 'Credit Card':
predicted_credit_limit = rf_regressor_credit_card.predict(X_classification_amt.drop(columns=['loan_amount']))
recommendation += f"\nPredicted Credit Limit: {predicted_credit_limit[0]:,.2f}"
if recommended_probability < 0.5:
recommendation = "No suitable product recommendations found for this customer."
return recommendation, customer_segment, product_probabilities
def clean_and_extract_insight(insights):
# Remove unwanted characters (non-alphanumeric characters except spaces)
cleaned_insight = re.sub(r'[^a-zA-Z0-9\s]', '', insights)
# Extract the portion after "Summarised Insight"
if "Here are the Summarised Insights about" in cleaned_insight:
extracted_insight = cleaned_insight.split("Here are the Summarised Insights about")[1].strip().split("\n\n")[0]
else:
extracted_insight = cleaned_insight.strip()
return extracted_insight
@app.route('/', methods=['GET', 'POST'])
def index():
recommendation = None
insights = None
customer_segment = None
product_probabilities = None
customer_data = {}
if request.method == 'POST':
json_data = request.form.get('customer_data')
customer_data = json.loads(json_data) if json_data else {}
if 'generate_insights' in request.form:
# Generate insights
insights = generate_insights(customer_data)
cleaned_insight = clean_and_extract_insight(insights)
cleaned_insight_raw = cleaned_insight.split("\n")[1:]
insights = list()
for line in cleaned_insight_raw:
insights.append(line.strip())
print(insights)
elif 'generate_recommendation' in request.form:
# Get product recommendations
recommendation, customer_segment, product_probabilities = recommend_product_and_loan(
customer_data, kmeans, scaler, multi_target_classifier,
rf_regressor_personal_loan, rf_regressor_home_loan, rf_regressor_credit_card
)
return render_template('index.html', recommendation=recommendation, insights=insights,
customer_segment=customer_segment, product_probabilities=product_probabilities,
customer_data=customer_data)
if __name__ == '__main__':
app.run(debug=True)