forked from sachin9536/Bankruptcy-prediction
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GradioApp.py
463 lines (388 loc) · 15.2 KB
/
GradioApp.py
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
import gradio as gr
import pandas as pd
import numpy as np
import pickle
import shap
from reportlab.lib.pagesizes import letter
import re
import requests
from reportlab.lib.styles import ParagraphStyle, getSampleStyleSheet
from reportlab.platypus import Paragraph, SimpleDocTemplate, Spacer
from reportlab.lib.colors import HexColor
api_Key = "Enter Your Gemini API Key Here"
def create_custom_styles():
# Previous styles code remains the same
styles = getSampleStyleSheet()
# Title style
title_style = ParagraphStyle(
"CustomTitle",
parent=styles["Heading1"],
fontSize=24,
spaceAfter=30,
textColor=HexColor("#1a237e"),
)
# Heading style
heading_style = ParagraphStyle(
"CustomHeading",
parent=styles["Heading2"],
fontSize=16,
spaceAfter=12,
spaceBefore=16,
textColor=HexColor("#303f9f"),
)
# Normal text style
normal_style = ParagraphStyle(
"CustomNormal",
parent=styles["Normal"],
fontSize=12,
spaceBefore=6,
spaceAfter=6,
leading=14,
)
# Status style
status_style = ParagraphStyle(
"StatusStyle",
parent=styles["Normal"],
fontSize=14,
spaceBefore=12,
spaceAfter=12,
textColor=HexColor("#1b5e20"),
borderWidth=1,
borderColor=HexColor("#1b5e20"),
borderPadding=8,
borderRadius=8,
)
# Bullet point style with bold option
bullet_style = ParagraphStyle(
"BulletStyle", parent=normal_style, leftIndent=20, firstLineIndent=0
)
return {
"title": title_style,
"heading": heading_style,
"normal": normal_style,
"status": status_style,
"bullet": bullet_style,
}
def process_markdown_text(text):
"""
Process markdown-style formatting in text
"""
# Convert markdown to ReportLab's paragraph markup
# Handle bold text with double asterisks
text = re.sub(r"\*\*(.*?)\*\*", r"<b>\1</b>", text)
# Handle single asterisk for italic
text = re.sub(r"\*(.*?)\*", r"<i>\1</i>", text)
# Handle bullet points with asterisk or hyphen
text = re.sub(r"^\s*[\*\-]\s+", "• ", text)
return text
def generate_pdf_report(
company,
year,
predicted_status,
shap_summary_df,
filename="Output/BankruptcyReport.pdf",
top_n=5,
):
"""
Generate a professionally formatted PDF report for bankruptcy prediction with proper markdown support.
"""
# Create the document template
doc = SimpleDocTemplate(
filename,
pagesize=letter,
rightMargin=72,
leftMargin=72,
topMargin=72,
bottomMargin=72,
title="FinGuard",
author="", # Replace this with a relevant name
)
# Get custom styles
styles = create_custom_styles()
# Prepare the story (content) for the PDF
story = []
# Add title
story.append(Paragraph("Bankruptcy Prediction Report", styles["title"]))
story.append(Spacer(1, 20))
# Add prediction status with special formatting
status_text = f"Predicted Status: {predicted_status}"
story.append(Paragraph(status_text, styles["status"]))
story.append(Spacer(1, 20))
# Prepare top features for Gemini API
top_features = (
shap_summary_df[["Feature", "SHAP Value"]].head(top_n).values.tolist()
)
# Get generated content from Gemini
generated_text = call_gemini_api(company, year, predicted_status, top_features)
# Split text into sections while preserving empty lines
sections = generated_text.splitlines()
# Process the generated text and add it to the story
in_bullet_list = False
for section in sections:
# Skip empty lines but add a small space
if not section.strip():
story.append(Spacer(1, 6))
continue
# Process the section text for markdown
processed_text = process_markdown_text(section.strip())
# Handle different section types
if "###" in section or "##" in section:
# Section heading - remove the ### or ## markers and any extra spaces
heading_text = re.sub(r"^#{2,3}\s*", "", section).strip()
story.append(Spacer(1, 12)) # Add space before heading
story.append(Paragraph(heading_text, styles["heading"]))
story.append(Spacer(1, 6)) # Add space after heading
in_bullet_list = False
elif section.strip().startswith(("*", "-")) or "**X" in section:
# Bullet points or financial ratios
if not in_bullet_list:
story.append(Spacer(1, 6))
in_bullet_list = True
story.append(Paragraph(processed_text, styles["bullet"]))
else:
# Normal paragraph text
in_bullet_list = False
if section.startswith("**") and section.endswith("**"):
# Metadata lines (company, date) or bold text
story.append(Paragraph(processed_text, styles["normal"]))
else:
story.append(Paragraph(processed_text, styles["normal"]))
story.append(Spacer(1, 6))
# Build the PDF
doc.build(story)
# Load models and training data (keeping existing code)
with open("Output/voting_classifier_model.pkl", "rb") as f:
voting_clf = pickle.load(f)
with open("Output/logistic_regression_model.pkl", "rb") as f:
lr_model = pickle.load(f)
with open("Output/xgboost_model.pkl", "rb") as f:
xgb_model = pickle.load(f)
with open("Output/random_forest_model.pkl", "rb") as f:
rf_model = pickle.load(f)
X_train = np.load("Output/X_train_data.npy")
feature_names = [
"X1: Current Assets / Current Liabilities",
"X2: (Current Assets - Inventories) / Current Liabilities",
"X3: Cash and Cash Equivalents / Current Liabilities",
"X4: Total Liabilities / Total Equity",
"X5: Current Liabilities / Total Liabilities",
"X6: Equity Share Capital / Fixed Assets",
"X7: Net Sales / Average Total Assets",
"X8: Net Sales / Average Current Assets",
"X9: Gross Profit / Net Sales",
"X10: Operating Profit / Net Sales",
"X11: Net Profit / Net Sales",
"X12: Net Profit / Total Assets",
"X13: Total Debt / Total Assets",
"X14: Working Capital / Total Assets",
"X15: Sales / Total Assets",
"X16: (Total Assets - Total Assets Previous Year) / Total Assets Previous Year",
"X17: Net Profit / Net Sales",
"X18: Cash & Short Term Investment / Total Assets",
"X19: Cash & Short Term Investment / (Equity Share Capital + Total Liability)",
"X20: Cash / Total Assets",
"X21: Cash / Current Liabilities",
"X22: (Inventory - Inventory Previous Year) / Inventory Previous Year",
"X23: Inventory / Sales",
"X24: (Current Liabilities - Cash) / Total Asset",
"X25: Current Liabilities / Sales",
"X26: Total Liabilities / Total Assets",
"X27: Total Liabilities / (Equity Share Capital + Total Liabilities)",
"X28: Net Income / (Equity Share Capital + Total Liabilities)",
"X29: Operating Income / Total Assets",
"X30: Operating Income / Sales",
"X31: Quick Assets / Current Liabilities",
"X32: Dividends / Net Income",
"X33: EBIT / Overall Capital Employed",
"X34: Net Cash Flow / Revenue",
"X35: Cash Flow from Operations / Total Debt",
"X36: EBT / Current Liabilities",
"X37: EBT / Total Equity",
"X38: Equity / Total Liabilities",
"X39: (Gross Profit + Depreciation) / Sales",
"X40: Quick Assets / Total Assets",
"X41: Gross Profit / Total Assets",
"X42: Operating Expenses / Total Liabilities",
"X43: (Current Assets - Inventory) / Short term Liabilities",
"X44: Current Assets / Total Liabilities",
"X45: Short term Liabilities / Total Assets",
"X46: (Current Assets - Inventory - Short term Liabilities) / (Sales - Gross Profit - Depreciation)",
"X47: (Net Profit + Depreciation) / Total Liabilities",
"X48: Working Capital / Fixed Assets",
"X49: (Total Liabilities - Cash) / Sales",
"X50: Long term Liability / Equity Capital",
"X51: Current Assets / Total Assets",
"X52: Current Liabilities / Assets",
"X53: Inventory / Working Capital",
"X54: Inventory / Current Liability",
"X55: Current Liabilities / Total Liability",
"X56: Working Capital / Equity Capital",
"X57: Current Liabilities / Equity Share Capital",
"X58: Long term Liability / Current Assets",
"X59: Total Income / Total Expense",
"X60: Total Expense / Assets",
"X61: Net Sales / Quick Assets",
"X62: Sales / Working Capital",
"X63: Inflation Rate",
"X64: Unemployment Rate",
"X65: Real Interest Rate",
"X66: GDP",
]
# Keep all your original functions exactly the same
def predict_company_status(company, year, *features):
# Your original prediction function remains unchanged
user_data = pd.DataFrame([features], columns=feature_names)
user_input_np = user_data.to_numpy()
prediction = voting_clf.predict(user_input_np)
# SHAP explanation (keeping your original code)
explainer_lr = shap.Explainer(lr_model, X_train)
explainer_xgb = shap.Explainer(xgb_model, X_train)
explainer_rf = shap.Explainer(rf_model, X_train)
shap_values_lr = explainer_lr(user_input_np).values
shap_values_xgb = explainer_xgb(user_input_np).values
shap_values_rf = explainer_rf(user_input_np).values
shap_values_ensemble = (shap_values_lr + shap_values_xgb + shap_values_rf) / 3
status_map = {
0: "Bankrupt",
1: "Financial Distress",
2: "Healthy",
3: "Probable Bankrupt",
}
predicted_status = status_map[prediction[0]]
shap_summary_df = pd.DataFrame(
{
"Feature": feature_names,
"SHAP Value": shap_values_ensemble[0, :, prediction[0]],
}
).sort_values(by="SHAP Value", key=abs, ascending=False)
# Generate and save the PDF report
generate_pdf_report(company, year, predicted_status, shap_summary_df)
return predicted_status
# Keep all other original functions unchanged
def call_gemini_api(company, year, predicted_status, top_features):
global api_Key
api_url = (
"https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash-latest:generateContent?key="
+ api_Key
)
headers = {"Content-Type": "application/json"}
input_text = f"""
Generate a bankruptcy report in the following structured format:
## Bankruptcy Prediction Report
**Company:** {company}
**Financial Year:** {year}
### Predicted Status
{predicted_status}
### Analysis
Provide an analysis based on the following top contributing financial ratios and SHAP values. Each ratio should include a brief explanation in full sentences and no more than two sentences per ratio.
Top Contributing Financial Ratios:
{', '.join([f"{feature} ({value:.2f})" for feature, value in top_features])}
### Recommendations
Based on the predicted status, provide detailed recommendations. Recommendations should be presented as clear, actionable steps, each in a separate bullet point.
### Disclaimer
End with a standard disclaimer stating that the report is based on model predictions and should be used as a tool for analysis, not as financial advice.
"""
data = {"contents": [{"parts": [{"text": input_text}]}]}
response = requests.post(api_url, headers=headers, json=data)
if response.status_code != 200:
print(f"Error: Received status code {response.status_code}")
print("Response text:", response.text)
return "Error in response from Gemini API."
response_json = response.json()
try:
generated_text = response_json["candidates"][0]["content"]["parts"][0]["text"]
except KeyError:
print("Error: Unexpected response format from Gemini API.")
generated_text = "The Gemini API failed to generate a response."
return generated_text
with gr.Blocks(
theme=gr.themes.Base(
primary_hue="blue", secondary_hue="gray", font=gr.themes.GoogleFont("Inter")
)
) as app:
gr.Markdown(
"""
# Financial Health Prediction System
This advanced system analyzes company financial health using 66 key financial ratios and machine learning models.
Enter your financial metrics below to receive a comprehensive analysis with a downloadable report.
"""
)
# Add input fields for company name and financial year
company_input = gr.Textbox(label="Company Name")
year_input = gr.Textbox(label="Financial Year")
with gr.Tabs():
with gr.TabItem("Liquidity & Working Capital (1-13)"):
inputs_1_13 = [
gr.Slider(
0.0, 10.0, value=0.5, label=feature_names[i], info="Enter value"
)
for i in range(13)
]
with gr.TabItem("Profitability & Asset Management (14-26)"):
inputs_14_26 = [
gr.Slider(
0.0, 10.0, value=0.5, label=feature_names[i], info="Enter value"
)
for i in range(13, 26)
]
with gr.TabItem("Leverage & Capital Structure (27-39)"):
inputs_27_39 = [
gr.Slider(
0.0, 10.0, value=0.5, label=feature_names[i], info="Enter value"
)
for i in range(26, 39)
]
with gr.TabItem("Operational Efficiency (40-52)"):
inputs_40_52 = [
gr.Slider(
0.0, 10.0, value=0.5, label=feature_names[i], info="Enter value"
)
for i in range(39, 52)
]
with gr.TabItem("Market & Economic Indicators (53-66)"):
inputs_53_66 = [
gr.Slider(
0.0, 10.0, value=0.5, label=feature_names[i], info="Enter value"
)
for i in range(52, 66)
]
with gr.Row():
with gr.Column():
predict_btn = gr.Button("Generate Prediction", variant="primary", scale=2)
with gr.Row():
output_status = gr.Textbox(label="Predicted Company Status", interactive=False)
output_file = gr.File(label="Download Detailed Report", interactive=False)
# Combine company name, year input, and all feature sliders in the correct order
all_inputs = (
[company_input, year_input]
+ inputs_1_13
+ inputs_14_26
+ inputs_27_39
+ inputs_40_52
+ inputs_53_66
)
# Keep your original prediction function call
predict_btn.click(
fn=lambda company, year, *features: (
predict_company_status(company, year, *features),
"Output/BankruptcyReport.pdf",
),
inputs=all_inputs,
outputs=[output_status, output_file],
)
gr.Markdown(
"""
### About the System
This prediction system uses an ensemble of machine learning models including:
- Random Forest
- XGBoost
- Logistic Regression
The downloadable report includes:
- Detailed financial health analysis
- Key contributing factors
- Recommendations based on the analysis
"""
)
# Launch the app with your original settings
if __name__ == "__main__":
app.launch()