Forums

FLASK API + SHAP viz

In the code below, this part doesnt work ... any ideas ?

# Create a bar plot of SHAP values
    summary_plot = shap.summary_plot(shap_values, df_normalized, plot_type='bar', show=False)

    # Save the SHAP plot as an image
    plot_file = '/home/patriciadubray/api/shap_plot.png'
    shap.save_html(plot_file, summary_plot)

The whole code is here :

import joblib
import pandas as pd
import shap
import os

from flask import Flask, jsonify, request

os.environ["MKL_NUM_THREADS"] = "1"
os.environ["NUMEXPR_NUM_THREADS"] = "1"
os.environ["OMP_NUM_THREADS"] = "1"

app = Flask(__name__)

# Load the model
model = joblib.load('/home/patriciadubray/api/model.pkl')

# Load the MinMaxScaler from mms.pkl
mms = joblib.load('/home/patriciadubray/api/mms.pkl')

# Load the explainer SHAP from shap.pkl
explainer = joblib.load('/home/patriciadubray/api/shap.pkl')


# Define the route for making predictions
@app.route('/predict', methods=['POST'])
def predict():
    # Retrieve the data sent as JSON
    data = request.get_json()

    # Create a DataFrame from the data
    df = pd.DataFrame([data], index=[0])

    # Normalize the input data using MinMaxScaler
    df_normalized = pd.DataFrame(mms.transform(df), columns=df.columns)

    # Make predictions
    proba = model.predict_proba(df_normalized)
    class_0_proba = proba[0][0]
    class_1_proba = proba[0][1]

    class_0_percentage = round(class_0_proba * 100, 1)
    class_1_percentage = round(class_1_proba * 100, 1)

    class_0 = f"Classe 0 : {class_0_percentage}% de probabilité que le remboursement soit ok"
    class_1 = f"Classe 1 : {class_1_percentage}% de probabilité d'avoir des problèmes de remboursement"

    # Calculate SHAP values for the observation
    shap_values = explainer.shap_values(df_normalized)

    # Create a bar plot of SHAP values
    summary_plot = shap.summary_plot(shap_values, df_normalized, plot_type='bar', show=False)

    # Save the SHAP plot as an image
    plot_file = '/home/patriciadubray/api/shap_plot.png'
    shap.save_html(plot_file, summary_plot)

    result = {
        "class_0": class_0,
        "class_1": class_1,
        "shap_plot_path": plot_file
    }

    return jsonify(result)

# Route for the home page
@app.route('/')
def home():
    return "API dashboard prêt à dépenser"

if __name__ == '__main__':
    # Display a message when the URL is launched
    print("API dashboard prêt à dépenser")

thanks for yout help ;)

What do you mean by "doesn't work"?

That was a good question ! It runs and runs for a long time ( +/- 5 minutes) and in the end there is no 'error log' ... On postman I get this error message :

<html>

<head>
    <title>Something went wrong :-(</title>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"
        integrity="sha384-pdapHxIh7EYuwy6K7iE41uXVxGCXY0sAjBzaElYGJUrzwodck3Lx6IE2lA0rFREo" crossorigin="anonymous">
    <style>
        body {
            font-family: Helvetica, Arial, sans-serif;
            width: 500px;
            margin-left: auto;
            margin-right: auto;
            margin-top: 20px;
        }

        .for-site-owner {
            font-size: smaller;
        }
    </style>
</head>

<body>
    <div class="main">
        <div class="header">
            <h1>Something went wrong :-(</h1>

            <p>
                Something went wrong while trying to load this site;
                please try again later.
            </p>

            <div class="for-site-owner">
                <h2>Debugging tips</h2>
                <p>
                    If this is your site, and you just reloaded
                    it, then the problem might simply be that it hasn't loaded up yet.
                    Try refreshing this page and see if this message disappears.
                </p>

                <p>
                    If you keep getting this message, you should check your site's
                    server and error logs for any messages.
                </p>
            </div>

            <p>
                Error code: 504-loadbalancer
            </p>
        </div>
    </div>
</body>

</html>

But the API works fine without this vizualisation ! This part seams to "block" it :

# Create a bar plot of SHAP values
    summary_plot = shap.summary_plot(shap_values, df_normalized, plot_type='bar', show=False)

Maybe pythonAnywhere cannot hold generating this bar plot ? Maybe there is another way to do it ?

OOOps

There is something on 'error log'

  File "/home/patriciadubray/api/main.py", line 55, in predict
    fig, ax = shap.summary_plot(shap_values, df_normalized, plot_type='bar')
NameError: name 'shap' is not defined

But I installed shap, pip install shap and it seams to be ok :

17:20 ~/api $ pip show shap
Name: shap
Version: 0.41.0
Summary: A unified approach to explain the output of any machine learning model.
Home-page: http://github.com/slundberg/shap
Author: Scott Lundberg
Author-email: slund1@cs.washington.edu
License: MIT
Location: /home/patriciadubray/.local/lib/python3.10/site-packages
Requires: cloudpickle, numba, numpy, packaging, pandas, scikit-learn, scipy, slicer, tqdm
Required-by: 
17:20 ~/api $

I'm sorry if I sound note very polite. hard to express myself in english ! i will get better !

The line in the error message doesn't seem to be in your code. Are you sure you are running the correct file?

My bad ! I'm running the file i sent at first, the error log was an acient one. I have no error log actually... just the error message from Postman that I posted before...

it works now ! thanks !

Excellent, glad to hear it!

Hello!

I don't know if this is right forum to put forward this question.. but, anyone knows how to connect PythonAnyWhere web app api ( flask ) to tableau via "Analytics Extension API" feature of tableau ?

Hello, You're right, it was my first question in this type of forum, and clearly, I don't know how to do it (how to ask questions!). I will better prepare my questions next time! Thank you for your help !

You asked better than me, at least! however, I just used your forum to ask a question of my interest!

I am getting difficulty to connect Pythonanywhere web app to tableau

@mosaic03 , see my response to your other thread.