Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,133 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pandas as pd
|
3 |
+
import numpy as np
|
4 |
+
from sklearn.ensemble import GradientBoostingClassifier
|
5 |
+
from sklearn.decomposition import PCA
|
6 |
+
from sklearn.preprocessing import StandardScaler
|
7 |
+
from sklearn.model_selection import train_test_split
|
8 |
+
from sklearn.metrics import classification_report, accuracy_score
|
9 |
+
import torch
|
10 |
+
from torch import nn
|
11 |
+
from torch.autograd import Variable
|
12 |
+
|
13 |
+
# GAN-based anomaly detection for financial analysis
|
14 |
+
class GANRiskAnalyzer:
|
15 |
+
def __init__(self, input_dim, hidden_dim, output_dim):
|
16 |
+
self.generator = nn.Sequential(
|
17 |
+
nn.Linear(input_dim, hidden_dim),
|
18 |
+
nn.ReLU(),
|
19 |
+
nn.Linear(hidden_dim, output_dim),
|
20 |
+
nn.Tanh()
|
21 |
+
)
|
22 |
+
self.discriminator = nn.Sequential(
|
23 |
+
nn.Linear(output_dim, hidden_dim),
|
24 |
+
nn.LeakyReLU(0.2),
|
25 |
+
nn.Linear(hidden_dim, 1),
|
26 |
+
nn.Sigmoid()
|
27 |
+
)
|
28 |
+
self.loss = nn.BCELoss()
|
29 |
+
self.generator_optimizer = torch.optim.Adam(self.generator.parameters(), lr=0.0002)
|
30 |
+
self.discriminator_optimizer = torch.optim.Adam(self.discriminator.parameters(), lr=0.0002)
|
31 |
+
|
32 |
+
def train(self, data, epochs=100):
|
33 |
+
real_labels = Variable(torch.ones(data.size(0), 1))
|
34 |
+
fake_labels = Variable(torch.zeros(data.size(0), 1))
|
35 |
+
for epoch in range(epochs):
|
36 |
+
# Train Discriminator
|
37 |
+
self.discriminator_optimizer.zero_grad()
|
38 |
+
real_data = Variable(data)
|
39 |
+
real_output = self.discriminator(real_data)
|
40 |
+
real_loss = self.loss(real_output, real_labels)
|
41 |
+
|
42 |
+
z = Variable(torch.randn(data.size(0), data.size(1)))
|
43 |
+
fake_data = self.generator(z)
|
44 |
+
fake_output = self.discriminator(fake_data.detach())
|
45 |
+
fake_loss = self.loss(fake_output, fake_labels)
|
46 |
+
|
47 |
+
d_loss = real_loss + fake_loss
|
48 |
+
d_loss.backward()
|
49 |
+
self.discriminator_optimizer.step()
|
50 |
+
|
51 |
+
# Train Generator
|
52 |
+
self.generator_optimizer.zero_grad()
|
53 |
+
fake_output = self.discriminator(fake_data)
|
54 |
+
g_loss = self.loss(fake_output, real_labels)
|
55 |
+
g_loss.backward()
|
56 |
+
self.generator_optimizer.step()
|
57 |
+
|
58 |
+
def generate(self, n_samples, input_dim):
|
59 |
+
z = Variable(torch.randn(n_samples, input_dim))
|
60 |
+
generated_data = self.generator(z)
|
61 |
+
return generated_data.detach().numpy()
|
62 |
+
|
63 |
+
# Risk Analysis
|
64 |
+
def analyze_financial_data(file):
|
65 |
+
# Load CSV data
|
66 |
+
data = pd.read_csv(file)
|
67 |
+
|
68 |
+
# Check required columns
|
69 |
+
required_columns = ["Revenue", "Profit", "Loss", "Expenses", "Risk_Level"]
|
70 |
+
if not all(column in data.columns for column in required_columns):
|
71 |
+
return "The uploaded CSV must include these columns: Revenue, Profit, Loss, Expenses, Risk_Level."
|
72 |
+
|
73 |
+
# Data Preprocessing
|
74 |
+
X = data[["Revenue", "Profit", "Loss", "Expenses"]]
|
75 |
+
y = data["Risk_Level"]
|
76 |
+
|
77 |
+
scaler = StandardScaler()
|
78 |
+
X_scaled = scaler.fit_transform(X)
|
79 |
+
|
80 |
+
# Dimensionality Reduction
|
81 |
+
pca = PCA(n_components=2)
|
82 |
+
X_pca = pca.fit_transform(X_scaled)
|
83 |
+
|
84 |
+
# Train-Test Split
|
85 |
+
X_train, X_test, y_train, y_test = train_test_split(X_pca, y, test_size=0.2, random_state=42)
|
86 |
+
|
87 |
+
# Gradient Boosting Classifier
|
88 |
+
model = GradientBoostingClassifier(n_estimators=100, learning_rate=0.1, max_depth=5)
|
89 |
+
model.fit(X_train, y_train)
|
90 |
+
y_pred = model.predict(X_test)
|
91 |
+
|
92 |
+
accuracy = accuracy_score(y_test, y_pred)
|
93 |
+
report = classification_report(y_test, y_pred, output_dict=True)
|
94 |
+
|
95 |
+
# GAN-based Anomaly Detection
|
96 |
+
gan = GANRiskAnalyzer(input_dim=X_pca.shape[1], hidden_dim=128, output_dim=X_pca.shape[1])
|
97 |
+
gan.train(torch.tensor(X_pca, dtype=torch.float32), epochs=200)
|
98 |
+
anomalies = gan.generate(n_samples=5, input_dim=X_pca.shape[1])
|
99 |
+
|
100 |
+
# Analysis Insights
|
101 |
+
total_revenue = data["Revenue"].sum()
|
102 |
+
total_profit = data["Profit"].sum()
|
103 |
+
total_loss = data["Loss"].sum()
|
104 |
+
|
105 |
+
insights = {
|
106 |
+
"Accuracy": f"{accuracy * 100:.2f}%",
|
107 |
+
"Classification Report": report,
|
108 |
+
"Generated Anomalies (GAN)": anomalies.tolist(),
|
109 |
+
"Financial Summary": {
|
110 |
+
"Total Revenue": f"${total_revenue:,.2f}",
|
111 |
+
"Total Profit": f"${total_profit:,.2f}",
|
112 |
+
"Total Loss": f"${total_loss:,.2f}",
|
113 |
+
"Net Balance": f"${(total_revenue - total_loss):,.2f}"
|
114 |
+
}
|
115 |
+
}
|
116 |
+
return insights
|
117 |
+
|
118 |
+
with gr.Blocks(theme=gr.themes.Monochrome()) as interface:
|
119 |
+
gr.Markdown("# **AI Risk Analyst Agent**")
|
120 |
+
gr.Markdown(
|
121 |
+
"Analyze your financial risks and identify anomalies using advanced AI models. Upload financial data in CSV format to get started."
|
122 |
+
)
|
123 |
+
|
124 |
+
with gr.Row():
|
125 |
+
with gr.Column():
|
126 |
+
data_file = gr.File(label="Upload Financial Data (CSV)")
|
127 |
+
submit_button = gr.Button("Analyze")
|
128 |
+
with gr.Column():
|
129 |
+
output = gr.JSON(label="Risk Analysis Insights")
|
130 |
+
|
131 |
+
submit_button.click(analyze_financial_data, inputs=[data_file], outputs=output)
|
132 |
+
|
133 |
+
interface.launch()
|