Spaces:
Sleeping
Sleeping
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://xaifraudsense-04ba19097287.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) | |