Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -17,11 +17,11 @@ import openai
|
|
| 17 |
# Path to the CSV file in the environment
|
| 18 |
CSV_PATH = 'FI_Transactions.csv'
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
# Read the CSV file from the environment
|
| 26 |
df = pd.read_csv(CSV_PATH)
|
| 27 |
|
|
@@ -109,6 +109,9 @@ def detect_anomalies(api_key, nu_value, n_clusters):
|
|
| 109 |
def get_ai_insights(df, anomalies_df):
|
| 110 |
"""Get insights about the anomalies using OpenAI API"""
|
| 111 |
try:
|
|
|
|
|
|
|
|
|
|
| 112 |
# Prepare information about the dataset and anomalies
|
| 113 |
df_info = df.describe().to_string()
|
| 114 |
anomaly_info = anomalies_df.head(5).to_string() if not anomalies_df.empty else "No anomalies detected"
|
|
@@ -145,7 +148,7 @@ def get_ai_insights(df, anomalies_df):
|
|
| 145 |
return response.choices[0].message.content
|
| 146 |
|
| 147 |
except Exception as e:
|
| 148 |
-
return f"Could not generate AI insights.
|
| 149 |
|
| 150 |
def plt_to_img():
|
| 151 |
buf = io.BytesIO()
|
|
@@ -161,7 +164,6 @@ with gr.Blocks(title="Financial Transaction Anomaly Detection") as demo:
|
|
| 161 |
|
| 162 |
with gr.Row():
|
| 163 |
with gr.Column():
|
| 164 |
-
api_key = gr.Textbox(type="password", label="OpenAI API Key", placeholder="Enter your OpenAI API key")
|
| 165 |
nu_slider = gr.Slider(0.01, 0.2, value=0.05, step=0.01, label="SVM nu parameter (controls anomaly threshold)")
|
| 166 |
cluster_slider = gr.Slider(2, 10, value=2, step=1, label="Number of KMeans clusters")
|
| 167 |
detect_button = gr.Button("Detect Anomalies")
|
|
@@ -181,16 +183,15 @@ with gr.Blocks(title="Financial Transaction Anomaly Detection") as demo:
|
|
| 181 |
|
| 182 |
detect_button.click(
|
| 183 |
detect_anomalies,
|
| 184 |
-
inputs=[
|
| 185 |
outputs=[pie_output, kmeans_output, svm_output, summary_output, anomalies_output]
|
| 186 |
)
|
| 187 |
|
| 188 |
gr.Markdown("""
|
| 189 |
## How to Use
|
| 190 |
-
1.
|
| 191 |
-
2.
|
| 192 |
-
3.
|
| 193 |
-
4. Click 'Detect Anomalies' to analyze the data
|
| 194 |
|
| 195 |
## Interpretation
|
| 196 |
- The pie chart shows the proportion of normal vs anomalous transactions
|
|
|
|
| 17 |
# Path to the CSV file in the environment
|
| 18 |
CSV_PATH = 'FI_Transactions.csv'
|
| 19 |
|
| 20 |
+
# Get OpenAI API key from environment variables
|
| 21 |
+
OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")
|
| 22 |
+
openai.api_key = OPENAI_API_KEY
|
| 23 |
+
|
| 24 |
+
def detect_anomalies(nu_value, n_clusters):
|
| 25 |
# Read the CSV file from the environment
|
| 26 |
df = pd.read_csv(CSV_PATH)
|
| 27 |
|
|
|
|
| 109 |
def get_ai_insights(df, anomalies_df):
|
| 110 |
"""Get insights about the anomalies using OpenAI API"""
|
| 111 |
try:
|
| 112 |
+
if not OPENAI_API_KEY:
|
| 113 |
+
return "OpenAI API key not found in environment variables. AI insights are unavailable."
|
| 114 |
+
|
| 115 |
# Prepare information about the dataset and anomalies
|
| 116 |
df_info = df.describe().to_string()
|
| 117 |
anomaly_info = anomalies_df.head(5).to_string() if not anomalies_df.empty else "No anomalies detected"
|
|
|
|
| 148 |
return response.choices[0].message.content
|
| 149 |
|
| 150 |
except Exception as e:
|
| 151 |
+
return f"Could not generate AI insights. Error: {str(e)}"
|
| 152 |
|
| 153 |
def plt_to_img():
|
| 154 |
buf = io.BytesIO()
|
|
|
|
| 164 |
|
| 165 |
with gr.Row():
|
| 166 |
with gr.Column():
|
|
|
|
| 167 |
nu_slider = gr.Slider(0.01, 0.2, value=0.05, step=0.01, label="SVM nu parameter (controls anomaly threshold)")
|
| 168 |
cluster_slider = gr.Slider(2, 10, value=2, step=1, label="Number of KMeans clusters")
|
| 169 |
detect_button = gr.Button("Detect Anomalies")
|
|
|
|
| 183 |
|
| 184 |
detect_button.click(
|
| 185 |
detect_anomalies,
|
| 186 |
+
inputs=[nu_slider, cluster_slider],
|
| 187 |
outputs=[pie_output, kmeans_output, svm_output, summary_output, anomalies_output]
|
| 188 |
)
|
| 189 |
|
| 190 |
gr.Markdown("""
|
| 191 |
## How to Use
|
| 192 |
+
1. Adjust the SVM nu parameter (controls anomaly detection sensitivity)
|
| 193 |
+
2. Choose the number of clusters for KMeans
|
| 194 |
+
3. Click 'Detect Anomalies' to analyze the data
|
|
|
|
| 195 |
|
| 196 |
## Interpretation
|
| 197 |
- The pie chart shows the proportion of normal vs anomalous transactions
|