File size: 2,957 Bytes
c1689bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import gradio as gr
import requests
import json
import plotly


def predict_fraud(selected_model, step, transaction_type, amount, oldbalanceOrg):
     # URL of the Flask API deployed on Heroku
    url = "https://xai-fraud-sense-7f7f48d380fe.herokuapp.com/predict_and_explain" 

    # Prepare the data in the format expected by the Flask API
    data = {
        'selected_model': selected_model,
        'step': step,
        'transaction_type': transaction_type,
        'amount': amount,
        'oldbalanceOrg': oldbalanceOrg
    }

    # Send a POST request to the Flask API
    response = requests.post(url, json=data)
    if response.status_code == 200:
        # Extract the response data
        result = response.json()
        prediction_text = result['prediction_text']
        lime_explanation = result['lime_explanation']

        # Parse the JSON strings back into Plotly figures
        radial_plot_json = result['radial_plot']
        bar_chart_json = result['bar_chart']
        radial_plot = plotly.graph_objs.Figure(json.loads(radial_plot_json))
        bar_chart = plotly.graph_objs.Figure(json.loads(bar_chart_json))

        narrative = result['narrative']

        # Return the results
        return prediction_text, radial_plot, bar_chart, lime_explanation, narrative
    else:
        return "Error: " + response.text, None, None, None, None


# Organizing inputs and outputs with enhanced styling
with gr.Blocks() as iface:
    gr.Markdown("<h2 style='text-align: center; font-weight: bold;'>FraudSenseXAI - Advanced Fraud Detection</h2>")
    gr.Markdown("<p style='text-align: center;'>Predict and analyze fraudulent transactions.</p>", elem_id="description")

    with gr.Row():
        with gr.Column():
            gr.Markdown("#### Input Parameters")
            model_selection = gr.Dropdown(['Random Forest', 'Gradient Boost', 'Neural Network'], label="Model Selection")
            step = gr.Number(value=1, label="Step")
            transaction_type = gr.Dropdown(['Transfer', 'Payment', 'Cash Out', 'Cash In'], label="Transaction Type")
            transaction_amount = gr.Number(label="Transaction Amount")
            old_balance_org = gr.Number(label="Old Balance Org")
            submit_button = gr.Button("Submit", variant="primary")
            
            prediction_text = gr.Text(label="Prediction")
            lime_explanation_text = gr.Text(label="LIME Explanation")

        with gr.Column():
            gr.Markdown("#### Visualization")
            radial_plot = gr.Plot(label="Radial Plot")
            bar_chart = gr.Plot(label="Bar Chart")
            narrative_text = gr.Text(label="Narrative")  # Placed in the same column

    submit_button.click(
        predict_fraud, 
        inputs=[model_selection, step, transaction_type, transaction_amount, old_balance_org],
        outputs=[prediction_text, radial_plot, bar_chart, lime_explanation_text, narrative_text]
    )

iface.launch(share=True)