Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import requests
|
3 |
+
import json
|
4 |
+
import plotly
|
5 |
+
|
6 |
+
def predict_fraud(selected_model, selected_interpretability_method, step, transaction_type, amount, oldbalanceOrg):
|
7 |
+
url = "https://sea-lion-app-5kxay.ondigitalocean.app"
|
8 |
+
data = {
|
9 |
+
'selected_model': selected_model,
|
10 |
+
'selected_interpretability_method': selected_interpretability_method,
|
11 |
+
'step': step,
|
12 |
+
'transaction_type': transaction_type,
|
13 |
+
'amount': amount,
|
14 |
+
'oldbalanceOrg': oldbalanceOrg
|
15 |
+
}
|
16 |
+
|
17 |
+
response = requests.post(url, json=data)
|
18 |
+
if response.status_code == 200:
|
19 |
+
result = response.json()
|
20 |
+
|
21 |
+
# Directly use the base64-encoded image string for the network graph
|
22 |
+
network_graph = result['network_graph']
|
23 |
+
|
24 |
+
# Ensure other data is handled correctly
|
25 |
+
prediction_text = result['prediction_text']
|
26 |
+
model_explanation = result['model_explanation']
|
27 |
+
|
28 |
+
|
29 |
+
|
30 |
+
mod_plot_json = result['mod_plot']
|
31 |
+
# Parse the JSON strings back into Plotly figures
|
32 |
+
mod_plot = plotly.graph_objs.Figure(json.loads(mod_plot_json))
|
33 |
+
features_influence = result['features_influence']
|
34 |
+
|
35 |
+
network_graph_json = result['network_graph'] #graph_objects
|
36 |
+
# Parse the JSON strings back into Plotly figures
|
37 |
+
network_graph = plotly.graph_objs.Figure(json.loads(network_graph_json))
|
38 |
+
network_explainer = result['network_explainer']
|
39 |
+
top_main_effect = result['top_main_effect']
|
40 |
+
top_interaction = result['top_interaction']
|
41 |
+
|
42 |
+
# Parse the JSON strings back into Plotly figures
|
43 |
+
radial_plot_json = result['radial_plot']
|
44 |
+
bar_chart_json = result['bar_chart']
|
45 |
+
radial_plot = plotly.graph_objs.Figure(json.loads(radial_plot_json))
|
46 |
+
bar_chart = plotly.graph_objs.Figure(json.loads(bar_chart_json))
|
47 |
+
|
48 |
+
narrative = result.get('narrative', "")
|
49 |
+
|
50 |
+
# Return the results
|
51 |
+
return prediction_text, model_explanation, mod_plot, features_influence, network_graph, network_explainer, top_main_effect, top_interaction,radial_plot, bar_chart, narrative
|
52 |
+
else:
|
53 |
+
# Handle error scenario by returning placeholders for each expected output
|
54 |
+
return "Error: " + response.text, None, None, None, None,None, None, None, None, None, None
|
55 |
+
|
56 |
+
|
57 |
+
# Define your Gradio interface here
|
58 |
+
with gr.Blocks() as app:
|
59 |
+
gr.Markdown("<h2 style='text-align: center; font-weight: bold;'>FraudSenseXAI - Advanced Fraud Detection</h2>")
|
60 |
+
gr.Markdown("<p style='text-align: center;'>Predict and analyze fraudulent transactions.</p>", elem_id="description")
|
61 |
+
|
62 |
+
|
63 |
+
with gr.Row():
|
64 |
+
with gr.Column():
|
65 |
+
gr.Markdown("#### INPUT PARAMTERS")
|
66 |
+
model_selection = gr.Dropdown(choices=['Random Forest', 'Gradient Boost'], label="Model Selection")
|
67 |
+
interpretability_selection = gr.Dropdown(choices=['LIME', 'SHAP'], label="Interpretability Technique")
|
68 |
+
step = gr.Number(label="Step")
|
69 |
+
transaction_type = gr.Dropdown(choices=['Transfer', 'Payment', 'Cash Out', 'Cash In'], label="Transaction Type")
|
70 |
+
transaction_amount = gr.Number(label="Transaction Amount")
|
71 |
+
old_balance_org = gr.Number(label="Old Balance Org")
|
72 |
+
submit_btn = gr.Button("Analyze")
|
73 |
+
|
74 |
+
# Define outputs
|
75 |
+
gr.Markdown("#### PREDICTION RESULT")
|
76 |
+
prediction_text = gr.Textbox(label="Prediction", lines=7)
|
77 |
+
|
78 |
+
with gr.Column():
|
79 |
+
gr.Markdown("#### MODEL INTERPRETATIONS")
|
80 |
+
model_explanation = gr.Textbox(label="Model Explanation", lines=7)
|
81 |
+
|
82 |
+
mod_plot = gr.Plot(label="Model Plot")
|
83 |
+
features_influence = gr.Textbox(label="Features Influence", lines=7)
|
84 |
+
|
85 |
+
|
86 |
+
with gr.Row():
|
87 |
+
with gr.Column():
|
88 |
+
gr.Markdown("#### FEATURE INTERACTIONS: Note that this function only supports SHAP at the moment")
|
89 |
+
network_graph = gr.Plot(label="Network Graph")
|
90 |
+
network_explainer = gr.Text(label="Network Graph Explanation")
|
91 |
+
top_main_effect = gr.Text(label="Top Main Effect", lines=7)
|
92 |
+
top_interaction = gr.Text(label="Top Interaction", lines=7)
|
93 |
+
|
94 |
+
with gr.Column():
|
95 |
+
gr.Markdown("#### COUNTERFACTUAL EXPLANATIONS")
|
96 |
+
radial_plot = gr.Plot(label="Radial Plot")
|
97 |
+
bar_chart = gr.Plot(label="Bar Chart")
|
98 |
+
narrative = gr.Textbox(label="Narrative")
|
99 |
+
|
100 |
+
submit_btn.click(
|
101 |
+
predict_fraud,
|
102 |
+
inputs=[model_selection, interpretability_selection, step, transaction_type, transaction_amount, old_balance_org],
|
103 |
+
outputs=[prediction_text, model_explanation, mod_plot, features_influence, network_graph, network_explainer, top_main_effect, top_interaction,radial_plot, bar_chart, narrative]
|
104 |
+
)
|
105 |
+
|
106 |
+
app.launch(share=True)
|