MarcelRobeer / explabox

Explore/examine/explain/expose your model with the explabox!
https://explabox.readthedocs.io
GNU Lesser General Public License v3.0
14 stars 0 forks source link

explain_prediction method should also work in native python without jupyter notebook #1

Closed blindeboom closed 2 years ago

blindeboom commented 2 years ago

Feature suggestion

I would like to generate the HTML code of the digestible in native python as well. This allows me to send the explanation over to a frontend via an API.

Purpose/Goal

Show a user the explanation of a prediction in an interactive frontend environment via an API. It should also allow the programmer to show the output within applications like Streamlit.

Usage example

expl = expl_model.explain.explain_prediction("An example text")
html = expl.html
MarcelRobeer commented 2 years ago

Think there are two things going on here:

  1. In the future, we intend to support API usage through the use of configs (accessible in native Python through expl.to_config() or as JSON (expl.to_json()) or YAML (expl.to_yaml()). Internally, we use expl.to_config() as the sole dependency for further rendering (e.g. Jupyter Notebook, HTML, ...).
  2. Rendering of the Jupyter Notebook UI is already done in HTML, using genbase.ui.notebook.Render.as_html() and subclasses thereof. This may be of use for the solution design.

Suggested solution

For a website interface or streamlit interface, we might need to rethink the rendering pipeline to more clearly separate the main return of the digestible (e.g. explanation, sensitivity test results), its overview of the provided inputs (e.g. the second tab in sensitivity testing) and the Config tab (which may or may not be equally relevant).

Current approach: Jupyter Notebook UI takes a config object (dictionary {'META': ..., 'CONTENT': ...}), and returns (1) styling with CSS, (2) HTML contents, (3) Config as JSON & YAML, (4) optional JavaScript code [e.g. copy/pasting], (5) footer. Inclusion of plotly is done centrally.

Suggested approach: Separate HTML rendering from CSS. Make Jupyter Notebook UI rendering an extension of HTML rendering, which takes a config object and returns (1) styling with CSS, (2) HTML contents, (5) footer. Make Jupyter Notebook UI rendering take 1, 2 & 5, extend it with 3 & 4, and put them nicely in tabs. If transformed into a single HTML page, the HTML rendering (but not the Jupyter Notebook UI) should additionally include a reference to plotly, and perhaps some descriptive webpage name and/or favicons.

MarcelRobeer commented 2 years ago

Fixed in genbase==0.2.11. Use the .raw_html property of your digestible to obtain the raw HTML output.

Example usage for streamlit:

import streamlit as st
import streamlit.components.v1 as components

with st.spinner('Environment setup'):
    from explabox_demo_drugreview import model, dataset_file
    from explabox import import_data

    data = import_data(dataset_file, data_cols='review', label_cols='rating')

    from explabox import Explabox

    box = Explabox(data=data,
                   model=model,
                   splits={'train': 'drugsComTrain.tsv', 'test': 'drugsComTest.tsv'})

components.html(box.explain.explain_prediction('This is an example instance!')[0].raw_html, width=840, height=800, scrolling=True)