You can apply SHAP or LIME to generative AI models by analyzing the influence of input features on the model’s outputs to enhance interpretability.
Here is the code snippet below:

In the above code we are using the following key points:
-
Loaded a pre-trained generative model using Hugging Face’s pipeline.
-
Wrapped the model with a prediction function suitable for SHAP analysis.
-
Used SHAP's text masker to apply the explainer on token-level variations.
-
Visualized the SHAP values to interpret influential input tokens.
Hence, this approach enables interpretability by highlighting which parts of the input text contribute most to the generated output.