GenAICoder commited on
Commit
371dd6b
·
verified ·
1 Parent(s): 7c5367e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +251 -28
app.py CHANGED
@@ -3,21 +3,26 @@
3
  import pandas as pd
4
  import gradio as gr
5
 
6
- from helper.vintage_helpers import (
7
  create_booking_vintage
8
  )
9
 
10
- from helper.data_merger import (
11
  merge_acq_perf
12
  )
13
 
 
 
 
 
 
14
  from analytics.performance_analysis import (
15
  generate_metric_view
16
  )
17
 
18
- # -----------------------------------
19
  # LOAD DATA
20
- # -----------------------------------
21
 
22
  acq = pd.read_csv(
23
  "data/acquisition.csv"
@@ -27,26 +32,78 @@ perf = pd.read_csv(
27
  "data/performance.csv"
28
  )
29
 
30
- # -----------------------------------
31
  # CREATE VINTAGE
32
- # -----------------------------------
33
 
34
  acq = create_booking_vintage(acq)
35
 
36
- # -----------------------------------
37
  # MERGE DATA
38
- # -----------------------------------
39
 
40
  master_df = merge_acq_perf(
41
  acq,
42
  perf
43
  )
44
 
45
- # -----------------------------------
46
- # MAIN ANALYTICS ENGINE
47
- # -----------------------------------
48
 
49
- def run_analysis(
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
  metric_name,
51
  portfolio_view
52
  ):
@@ -78,32 +135,109 @@ def run_analysis(
78
  group_col=group_col
79
  )
80
 
81
- return result
 
 
82
 
83
- # -----------------------------------
84
- # GRADIO UI
85
- # -----------------------------------
 
86
 
87
- with gr.Blocks() as app:
88
 
89
- gr.Markdown(
90
- "# Risk Analytics Manager Agent"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91
  )
92
 
93
- with gr.Row():
 
94
 
95
- metric_name = gr.Dropdown(
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96
  choices=[
97
  "30+@3",
98
  "30+@6",
99
  "60+@6",
100
  "Yr1 NCL"
101
  ],
102
- value="30+@6",
103
- label="Metric"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
104
  )
105
 
106
- portfolio_view = gr.Dropdown(
 
 
107
  choices=[
108
  "Overall",
109
  "Channel",
@@ -111,10 +245,98 @@ with gr.Blocks() as app:
111
  "City Tier",
112
  "Occupation"
113
  ],
114
- value="Overall",
115
- label="Portfolio View"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
116
  )
117
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
118
  run_button = gr.Button(
119
  "Run Analysis"
120
  )
@@ -124,8 +346,9 @@ with gr.Blocks() as app:
124
  run_button.click(
125
  fn=run_analysis,
126
  inputs=[
127
- metric_name,
128
- portfolio_view
 
129
  ],
130
  outputs=output_table
131
  )
 
3
  import pandas as pd
4
  import gradio as gr
5
 
6
+ from helpers.vintage_helpers import (
7
  create_booking_vintage
8
  )
9
 
10
+ from helpers.data_merger import (
11
  merge_acq_perf
12
  )
13
 
14
+ from metrics.mix_metrics import (
15
+ calculate_vintage_mix,
16
+ calculate_limit_mix
17
+ )
18
+
19
  from analytics.performance_analysis import (
20
  generate_metric_view
21
  )
22
 
23
+ # ---------------------------------------------------
24
  # LOAD DATA
25
+ # ---------------------------------------------------
26
 
27
  acq = pd.read_csv(
28
  "data/acquisition.csv"
 
32
  "data/performance.csv"
33
  )
34
 
35
+ # ---------------------------------------------------
36
  # CREATE VINTAGE
37
+ # ---------------------------------------------------
38
 
39
  acq = create_booking_vintage(acq)
40
 
41
+ # ---------------------------------------------------
42
  # MERGE DATA
43
+ # ---------------------------------------------------
44
 
45
  master_df = merge_acq_perf(
46
  acq,
47
  perf
48
  )
49
 
50
+ # ---------------------------------------------------
51
+ # ACQUISITION ANALYSIS ENGINE
52
+ # ---------------------------------------------------
53
 
54
+ def run_acquisition_analysis(
55
+ analysis_type,
56
+ dimension
57
+ ):
58
+
59
+ # ----------------------------------------
60
+ # MIX ANALYSIS
61
+ # ----------------------------------------
62
+
63
+ if analysis_type == "Mix Analysis":
64
+
65
+ result = calculate_vintage_mix(
66
+ df=acq,
67
+ vintage_col="booking_vintage",
68
+ group_col=dimension
69
+ )
70
+
71
+ result = result.rename(
72
+ columns={
73
+ "accounts": "count"
74
+ }
75
+ )
76
+
77
+ return result
78
+
79
+ # ----------------------------------------
80
+ # LIMIT ANALYSIS
81
+ # ----------------------------------------
82
+
83
+ elif analysis_type == "Limit Analysis":
84
+
85
+ result = (
86
+ acq.groupby(
87
+ ["booking_vintage", dimension]
88
+ )
89
+ .agg(
90
+ count=("account_id", "nunique"),
91
+ balance=("credit_limit", "sum")
92
+ )
93
+ .reset_index()
94
+ )
95
+
96
+ return result
97
+
98
+ else:
99
+ return pd.DataFrame()
100
+
101
+
102
+ # ---------------------------------------------------
103
+ # PERFORMANCE ANALYSIS ENGINE
104
+ # ---------------------------------------------------
105
+
106
+ def run_performance_analysis(
107
  metric_name,
108
  portfolio_view
109
  ):
 
135
  group_col=group_col
136
  )
137
 
138
+ # ----------------------------------------
139
+ # STANDARDIZED OUTPUT FORMAT
140
+ # ----------------------------------------
141
 
142
+ metric_col = [
143
+ col for col in result.columns
144
+ if "rate" in col.lower()
145
+ ][0]
146
 
147
+ if group_col is not None:
148
 
149
+ final_result = result.rename(
150
+ columns={
151
+ group_col: "segment",
152
+ metric_col: "rate"
153
+ }
154
+ )
155
+
156
+ else:
157
+
158
+ final_result = result.rename(
159
+ columns={
160
+ metric_col: "rate"
161
+ }
162
+ )
163
+
164
+ final_result["segment"] = "Overall"
165
+
166
+ # Add dummy balance column for consistency
167
+ # Later replace with actual exposure logic
168
+
169
+ final_result["balance"] = None
170
+
171
+ final_result = final_result[
172
+ [
173
+ "booking_vintage",
174
+ "segment",
175
+ "total_accounts",
176
+ "balance",
177
+ "rate"
178
+ ]
179
+ ]
180
+
181
+ final_result = final_result.rename(
182
+ columns={
183
+ "booking_vintage": "vintage",
184
+ "total_accounts": "count"
185
+ }
186
  )
187
 
188
+ return final_result
189
+
190
 
191
+ # ---------------------------------------------------
192
+ # DYNAMIC DROPDOWN LOGIC
193
+ # ---------------------------------------------------
194
+
195
+ def update_analysis_dropdown(
196
+ dataset_type
197
+ ):
198
+
199
+ if dataset_type == "Acquisition":
200
+
201
+ return gr.update(
202
+ choices=[
203
+ "Mix Analysis",
204
+ "Limit Analysis"
205
+ ],
206
+ value="Mix Analysis"
207
+ )
208
+
209
+ elif dataset_type == "Performance":
210
+
211
+ return gr.update(
212
  choices=[
213
  "30+@3",
214
  "30+@6",
215
  "60+@6",
216
  "Yr1 NCL"
217
  ],
218
+ value="30+@6"
219
+ )
220
+
221
+
222
+ def update_dimension_dropdown(
223
+ dataset_type
224
+ ):
225
+
226
+ if dataset_type == "Acquisition":
227
+
228
+ return gr.update(
229
+ choices=[
230
+ "sourcing_channel",
231
+ "fico_band",
232
+ "city_tier",
233
+ "occupation_type"
234
+ ],
235
+ value="sourcing_channel"
236
  )
237
 
238
+ elif dataset_type == "Performance":
239
+
240
+ return gr.update(
241
  choices=[
242
  "Overall",
243
  "Channel",
 
245
  "City Tier",
246
  "Occupation"
247
  ],
248
+ value="Overall"
249
+ )
250
+
251
+
252
+ # ---------------------------------------------------
253
+ # MASTER ROUTER
254
+ # ---------------------------------------------------
255
+
256
+ def run_analysis(
257
+ dataset_type,
258
+ analysis_selection,
259
+ dimension_selection
260
+ ):
261
+
262
+ # ----------------------------------------
263
+ # ACQUISITION ANALYSIS
264
+ # ----------------------------------------
265
+
266
+ if dataset_type == "Acquisition":
267
+
268
+ return run_acquisition_analysis(
269
+ analysis_type=analysis_selection,
270
+ dimension=dimension_selection
271
+ )
272
+
273
+ # ----------------------------------------
274
+ # PERFORMANCE ANALYSIS
275
+ # ----------------------------------------
276
+
277
+ elif dataset_type == "Performance":
278
+
279
+ return run_performance_analysis(
280
+ metric_name=analysis_selection,
281
+ portfolio_view=dimension_selection
282
  )
283
 
284
+ else:
285
+
286
+ return pd.DataFrame()
287
+
288
+
289
+ # ---------------------------------------------------
290
+ # GRADIO UI
291
+ # ---------------------------------------------------
292
+
293
+ with gr.Blocks() as app:
294
+
295
+ gr.Markdown(
296
+ "# Risk Analytics Manager Agent"
297
+ )
298
+
299
+ with gr.Row():
300
+
301
+ dataset_type = gr.Dropdown(
302
+ choices=[
303
+ "Acquisition",
304
+ "Performance"
305
+ ],
306
+ value="Acquisition",
307
+ label="Dataset"
308
+ )
309
+
310
+ analysis_selection = gr.Dropdown(
311
+ choices=[],
312
+ label="Analysis"
313
+ )
314
+
315
+ dimension_selection = gr.Dropdown(
316
+ choices=[],
317
+ label="Dimension"
318
+ )
319
+
320
+ # ----------------------------------------
321
+ # DYNAMIC DROPDOWNS
322
+ # ----------------------------------------
323
+
324
+ dataset_type.change(
325
+ fn=update_analysis_dropdown,
326
+ inputs=dataset_type,
327
+ outputs=analysis_selection
328
+ )
329
+
330
+ dataset_type.change(
331
+ fn=update_dimension_dropdown,
332
+ inputs=dataset_type,
333
+ outputs=dimension_selection
334
+ )
335
+
336
+ # ----------------------------------------
337
+ # RUN BUTTON
338
+ # ----------------------------------------
339
+
340
  run_button = gr.Button(
341
  "Run Analysis"
342
  )
 
346
  run_button.click(
347
  fn=run_analysis,
348
  inputs=[
349
+ dataset_type,
350
+ analysis_selection,
351
+ dimension_selection
352
  ],
353
  outputs=output_table
354
  )