Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
import joblib
|
| 5 |
+
import xgboost as xgb
|
| 6 |
+
from tensorflow.keras.models import load_model
|
| 7 |
+
import pickle
|
| 8 |
+
import matplotlib.pyplot as plt
|
| 9 |
+
import seaborn as sns
|
| 10 |
+
|
| 11 |
+
# Load models & scalers
|
| 12 |
+
xgb_clf = xgb.XGBClassifier(); xgb_clf.load_model("xgb_model.json")
|
| 13 |
+
xgb_reg = joblib.load("xgb_pipeline_model.pkl")
|
| 14 |
+
scaler_X = pickle.load(open("scaler_X.pkl", "rb"))
|
| 15 |
+
scaler_y = pickle.load(open("scaler_y.pkl", "rb"))
|
| 16 |
+
lstm_model = load_model("lstm_revenue_model.keras")
|
| 17 |
+
|
| 18 |
+
# Prediction + Plot functions
|
| 19 |
+
def classify_fn(df):
|
| 20 |
+
# df: pandas DataFrame of features
|
| 21 |
+
preds = xgb_clf.predict(df)
|
| 22 |
+
probs = xgb_clf.predict_proba(df)
|
| 23 |
+
# Bar chart for first prediction probabilities
|
| 24 |
+
fig, ax = plt.subplots()
|
| 25 |
+
ax.bar(['No Bankruptcy','Bankruptcy'], probs[0], color=['#4CAF50','#F44336'])
|
| 26 |
+
ax.set_ylim(0,1)
|
| 27 |
+
ax.set_title('Bankruptcy Probability')
|
| 28 |
+
ax.set_ylabel('Probability')
|
| 29 |
+
plt.tight_layout()
|
| 30 |
+
return {"Predicted Label": int(preds[0])}, fig
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def regress_fn(df):
|
| 34 |
+
preds = xgb_reg.predict(df)
|
| 35 |
+
# Histogram of predictions
|
| 36 |
+
fig, ax = plt.subplots()
|
| 37 |
+
sns.histplot(preds, bins=20, kde=True, ax=ax)
|
| 38 |
+
ax.set_title('Anomaly Score Distribution')
|
| 39 |
+
ax.set_xlabel('Predicted Anomaly Score')
|
| 40 |
+
plt.tight_layout()
|
| 41 |
+
return preds.tolist(), fig
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def lstm_fn(seq_str):
|
| 45 |
+
# seq_str: comma-separated Q0-Q9 revenues
|
| 46 |
+
vals = np.array(list(map(float, seq_str.split(',')))).reshape(1, -1)
|
| 47 |
+
vals_s = scaler_X.transform(vals).reshape((1, vals.shape[1], 1))
|
| 48 |
+
pred_s = lstm_model.predict(vals_s)
|
| 49 |
+
pred = scaler_y.inverse_transform(pred_s)[0,0]
|
| 50 |
+
# Plot input series + predicted point
|
| 51 |
+
fig, ax = plt.subplots()
|
| 52 |
+
ax.plot(range(10), vals.flatten(), marker='o', label='Input Revenue')
|
| 53 |
+
ax.plot(10, pred, marker='X', markersize=10, color='red', label='Predicted Q10')
|
| 54 |
+
ax.set_xlabel('Quarter Index (0-10)')
|
| 55 |
+
ax.set_ylabel('Revenue')
|
| 56 |
+
ax.set_title('Revenue Forecast')
|
| 57 |
+
ax.legend()
|
| 58 |
+
plt.tight_layout()
|
| 59 |
+
return float(pred), fig
|
| 60 |
+
|
| 61 |
+
# Build UI
|
| 62 |
+
grid_css = """
|
| 63 |
+
body {background-color: #f7f7f7;}
|
| 64 |
+
.gradio-container {max-width: 800px; margin: auto; padding: 20px;}
|
| 65 |
+
h1, h2 {color: #333;}
|
| 66 |
+
"""
|
| 67 |
+
|
| 68 |
+
demo = gr.Blocks(css=grid_css)
|
| 69 |
+
with demo:
|
| 70 |
+
gr.Markdown("# 🚀 FinSight 360™ Dashboard")
|
| 71 |
+
gr.Markdown("Comprehensive financial AI: Bankruptcy Classification, Anomaly Scoring, and Revenue Forecasting.")
|
| 72 |
+
with gr.Tab("🏦 Bankruptcy Classifier"):
|
| 73 |
+
gr.Markdown("Upload a single row of company features (CSV or paste) to predict bankruptcy:")
|
| 74 |
+
inp1 = gr.Dataframe(headers="...", datatype=["number"]*20 + ["text"]*4, label="Features DataFrame")
|
| 75 |
+
lbl1 = gr.Label(label="Predicted Label")
|
| 76 |
+
plt1 = gr.Plot()
|
| 77 |
+
inp1.submit(classify_fn, inp1, [lbl1, plt1])
|
| 78 |
+
with gr.Tab("📈 Anomaly Regression"):
|
| 79 |
+
gr.Markdown("Upload company features to predict anomaly score:")
|
| 80 |
+
inp2 = gr.Dataframe(headers="...", datatype=["number"]*100, label="Features DataFrame")
|
| 81 |
+
out2 = gr.Textbox(label="Predicted Scores List")
|
| 82 |
+
plt2 = gr.Plot()
|
| 83 |
+
inp2.submit(regress_fn, inp2, [out2, plt2])
|
| 84 |
+
with gr.Tab("📊 LSTM Revenue Forecast"):
|
| 85 |
+
gr.Markdown("Enter last 10 quarterly revenues (comma-separated) to forecast Q10 revenue:")
|
| 86 |
+
inp3 = gr.Textbox(placeholder="e.g. 1000,1200,1100,...", label="Q0–Q9 Revenues")
|
| 87 |
+
out3 = gr.Number(label="Predicted Q10 Revenue")
|
| 88 |
+
plt3 = gr.Plot()
|
| 89 |
+
inp3.submit(lstm_fn, inp3, [out3, plt3])
|
| 90 |
+
gr.Markdown("---\n*Built with ❤️ by FinSight AI Team*")
|
| 91 |
+
|
| 92 |
+
demo.launch()
|