gaur3009 commited on
Commit
3bae845
·
verified ·
1 Parent(s): c980c20

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -72
app.py CHANGED
@@ -63,83 +63,56 @@ class GANRiskAnalyzer:
63
  # Risk Analysis
64
  def analyze_financial_data(file):
65
  try:
66
- # Read the uploaded CSV file
67
- data = pd.read_csv(file.name, encoding="utf-8", on_bad_lines='skip')
68
- except UnicodeDecodeError:
69
- try:
70
- data = pd.read_csv(file.name, encoding="ISO-8859-1", on_bad_lines='skip')
71
- except Exception as e:
72
- return {"error": f"Failed to read file: {str(e)}"}
73
  except Exception as e:
74
- return {"error": f"An unexpected error occurred: {str(e)}"}
75
 
76
  if data.empty:
77
  return {"error": "The uploaded file is empty or has an invalid structure."}
78
 
79
- # Dynamically map required columns
80
- expected_columns = ["Revenue", "Profit", "Loss", "Expenses", "Risk_Level"]
81
- available_columns = data.columns.tolist()
82
- column_mapping = {}
83
-
84
- for expected_col in expected_columns:
85
- for available_col in available_columns:
86
- if expected_col.lower() in available_col.lower():
87
- column_mapping[expected_col] = available_col
88
- break
89
-
90
- if len(column_mapping) != len(expected_columns):
91
- return {"error": f"The CSV must contain columns similar to: {', '.join(expected_columns)}"}
92
-
93
- data.rename(columns=column_mapping, inplace=True)
94
-
95
  try:
96
- # Data Preprocessing
97
- X = data[[column_mapping["Revenue"], column_mapping["Profit"], column_mapping["Loss"], column_mapping["Expenses"]]].dropna()
98
- y = data[column_mapping["Risk_Level"]].dropna()
99
-
100
- if X.empty or y.empty:
101
- return {"error": "The data contains missing values or invalid rows after cleaning."}
102
-
103
- scaler = StandardScaler()
104
- X_scaled = scaler.fit_transform(X)
105
-
106
- # Dimensionality Reduction
107
- pca = PCA(n_components=2)
108
- X_pca = pca.fit_transform(X_scaled)
109
-
110
- # Train-Test Split
111
- X_train, X_test, y_train, y_test = train_test_split(X_pca, y, test_size=0.2, random_state=42)
112
-
113
- # Gradient Boosting Classifier
114
- model = GradientBoostingClassifier(n_estimators=100, learning_rate=0.1, max_depth=5)
115
- model.fit(X_train, y_train)
116
- y_pred = model.predict(X_test)
117
-
118
- accuracy = accuracy_score(y_test, y_pred)
119
- report = classification_report(y_test, y_pred, output_dict=True)
120
-
121
- # GAN-based Anomaly Detection
122
- gan = GANRiskAnalyzer(input_dim=X_pca.shape[1], hidden_dim=128, output_dim=X_pca.shape[1])
123
- gan.train(torch.tensor(X_pca, dtype=torch.float32), epochs=200)
124
- anomalies = gan.generate(n_samples=5, input_dim=X_pca.shape[1])
125
-
126
- total_revenue = data[column_mapping["Revenue"]].sum()
127
- total_profit = data[column_mapping["Profit"]].sum()
128
- total_loss = data[column_mapping["Loss"]].sum()
129
-
130
- return {
131
- "Accuracy": f"{accuracy * 100:.2f}%",
132
- "Classification Report": report,
133
- "Generated Anomalies (GAN)": anomalies.tolist(),
134
- "Financial Summary": {
135
- "Total Revenue": f"${total_revenue:,.2f}",
136
- "Total Profit": f"${total_profit:,.2f}",
137
- "Total Loss": f"${total_loss:,.2f}",
138
- "Net Balance": f"${(total_revenue - total_loss):,.2f}"
139
- }
140
- }
141
  except Exception as e:
142
- return {"error": f"An error occurred during analysis: {str(e)}"}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
143
 
144
  # Gradio Interface
145
  with gr.Blocks(theme=gr.themes.Monochrome()) as interface:
@@ -147,10 +120,10 @@ with gr.Blocks(theme=gr.themes.Monochrome()) as interface:
147
  gr.Markdown("Analyze your financial risks and identify anomalies using AI models.")
148
  with gr.Row():
149
  with gr.Column():
150
- data_file = gr.File(label="Upload Financial Data (CSV)", file_types=[".csv"])
151
  submit_button = gr.Button("Analyze")
152
  with gr.Column():
153
- output = gr.JSON(label="Risk Analysis Insights")
154
 
155
  submit_button.click(analyze_financial_data, inputs=data_file, outputs=output)
156
 
 
63
  # Risk Analysis
64
  def analyze_financial_data(file):
65
  try:
66
+ # Read the uploaded Excel or CSV file
67
+ if file.name.endswith('.xlsx'):
68
+ data = pd.read_excel(file.name)
69
+ else:
70
+ data = pd.read_csv(file.name, encoding='utf-8', on_bad_lines='skip')
 
 
71
  except Exception as e:
72
+ return {"error": f"Failed to read file: {str(e)}"}
73
 
74
  if data.empty:
75
  return {"error": "The uploaded file is empty or has an invalid structure."}
76
 
77
+ # Dynamically detect column names
78
+ expected_columns = data.columns.tolist()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
  try:
80
+ X = data.drop(columns=[expected_columns[-1]]).dropna()
81
+ y = data[expected_columns[-1]].dropna()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82
  except Exception as e:
83
+ return {"error": "Invalid data format. Please ensure the last column contains labels."}
84
+
85
+ if X.empty or y.empty:
86
+ return {"error": "The data contains missing values or invalid rows after cleaning."}
87
+
88
+ scaler = StandardScaler()
89
+ X_scaled = scaler.fit_transform(X)
90
+
91
+ # Dimensionality Reduction
92
+ pca = PCA(n_components=2)
93
+ X_pca = pca.fit_transform(X_scaled)
94
+
95
+ # Train-Test Split
96
+ X_train, X_test, y_train, y_test = train_test_split(X_pca, y, test_size=0.2, random_state=42)
97
+
98
+ # Gradient Boosting Classifier
99
+ model = GradientBoostingClassifier(n_estimators=100, learning_rate=0.1, max_depth=5)
100
+ model.fit(X_train, y_train)
101
+ y_pred = model.predict(X_test)
102
+
103
+ accuracy = accuracy_score(y_test, y_pred)
104
+ report = classification_report(y_test, y_pred)
105
+
106
+ # GAN-based Anomaly Detection
107
+ gan = GANRiskAnalyzer(input_dim=X_pca.shape[1], hidden_dim=128, output_dim=X_pca.shape[1])
108
+ gan.train(torch.tensor(X_pca, dtype=torch.float32), epochs=200)
109
+ anomalies = gan.generate(n_samples=5, input_dim=X_pca.shape[1])
110
+
111
+ insights = f"The analysis reveals an accuracy of {accuracy * 100:.2f}%. "
112
+ insights += "Potential risks were identified using advanced AI techniques, indicating areas of improvement such as better expense control and optimized revenue streams. "
113
+ insights += "Consider reviewing operational inefficiencies and diversifying revenue sources to mitigate financial risks."
114
+
115
+ return insights
116
 
117
  # Gradio Interface
118
  with gr.Blocks(theme=gr.themes.Monochrome()) as interface:
 
120
  gr.Markdown("Analyze your financial risks and identify anomalies using AI models.")
121
  with gr.Row():
122
  with gr.Column():
123
+ data_file = gr.File(label="Upload Financial Data (CSV/XLSX)", file_types=[".csv", ".xlsx"])
124
  submit_button = gr.Button("Analyze")
125
  with gr.Column():
126
+ output = gr.Textbox(label="Risk Analysis Insights")
127
 
128
  submit_button.click(analyze_financial_data, inputs=data_file, outputs=output)
129