Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import requests
|
3 |
+
import json
|
4 |
+
import plotly
|
5 |
+
|
6 |
+
|
7 |
+
def predict_fraud(selected_model, step, transaction_type, amount, oldbalanceOrg):
|
8 |
+
# URL of the Flask API deployed on Heroku
|
9 |
+
url = "https://xai-fraud-sense-7f7f48d380fe.herokuapp.com/predict_and_explain"
|
10 |
+
|
11 |
+
# Prepare the data in the format expected by the Flask API
|
12 |
+
data = {
|
13 |
+
'selected_model': selected_model,
|
14 |
+
'step': step,
|
15 |
+
'transaction_type': transaction_type,
|
16 |
+
'amount': amount,
|
17 |
+
'oldbalanceOrg': oldbalanceOrg
|
18 |
+
}
|
19 |
+
|
20 |
+
# Send a POST request to the Flask API
|
21 |
+
response = requests.post(url, json=data)
|
22 |
+
if response.status_code == 200:
|
23 |
+
# Extract the response data
|
24 |
+
result = response.json()
|
25 |
+
prediction_text = result['prediction_text']
|
26 |
+
lime_explanation = result['lime_explanation']
|
27 |
+
|
28 |
+
# Parse the JSON strings back into Plotly figures
|
29 |
+
radial_plot_json = result['radial_plot']
|
30 |
+
bar_chart_json = result['bar_chart']
|
31 |
+
radial_plot = plotly.graph_objs.Figure(json.loads(radial_plot_json))
|
32 |
+
bar_chart = plotly.graph_objs.Figure(json.loads(bar_chart_json))
|
33 |
+
|
34 |
+
narrative = result['narrative']
|
35 |
+
|
36 |
+
# Return the results
|
37 |
+
return prediction_text, radial_plot, bar_chart, lime_explanation, narrative
|
38 |
+
else:
|
39 |
+
return "Error: " + response.text, None, None, None, None
|
40 |
+
|
41 |
+
|
42 |
+
# Organizing inputs and outputs with enhanced styling
|
43 |
+
with gr.Blocks() as iface:
|
44 |
+
gr.Markdown("<h2 style='text-align: center; font-weight: bold;'>FraudSenseXAI - Advanced Fraud Detection</h2>")
|
45 |
+
gr.Markdown("<p style='text-align: center;'>Predict and analyze fraudulent transactions.</p>", elem_id="description")
|
46 |
+
|
47 |
+
with gr.Row():
|
48 |
+
with gr.Column():
|
49 |
+
gr.Markdown("#### Input Parameters")
|
50 |
+
model_selection = gr.Dropdown(['Random Forest', 'Gradient Boost', 'Neural Network'], label="Model Selection")
|
51 |
+
step = gr.Number(value=1, label="Step")
|
52 |
+
transaction_type = gr.Dropdown(['Transfer', 'Payment', 'Cash Out', 'Cash In'], label="Transaction Type")
|
53 |
+
transaction_amount = gr.Number(label="Transaction Amount")
|
54 |
+
old_balance_org = gr.Number(label="Old Balance Org")
|
55 |
+
submit_button = gr.Button("Submit", variant="primary")
|
56 |
+
|
57 |
+
prediction_text = gr.Text(label="Prediction")
|
58 |
+
lime_explanation_text = gr.Text(label="LIME Explanation")
|
59 |
+
|
60 |
+
with gr.Column():
|
61 |
+
gr.Markdown("#### Visualization")
|
62 |
+
radial_plot = gr.Plot(label="Radial Plot")
|
63 |
+
bar_chart = gr.Plot(label="Bar Chart")
|
64 |
+
narrative_text = gr.Text(label="Narrative") # Placed in the same column
|
65 |
+
|
66 |
+
submit_button.click(
|
67 |
+
predict_fraud,
|
68 |
+
inputs=[model_selection, step, transaction_type, transaction_amount, old_balance_org],
|
69 |
+
outputs=[prediction_text, radial_plot, bar_chart, lime_explanation_text, narrative_text]
|
70 |
+
)
|
71 |
+
|
72 |
+
iface.launch(share=True)
|