cancel
Showing results forย 
Search instead forย 
Did you mean:ย 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

Widgets Not Displaying

Rasputin312
New Contributor II

I am trying to run this attention visualization in my Databricks notebook.   This is my code and this is the error I get:

```

from IPython.display import display, Javascript
import ipywidgets as widgets
from ipywidgets import interact
from transformers import AutoTokenizer, AutoModel, utils
from bertviz import model_view
utils.logging.set_verbosity_error()  # Suppress standard warnings

model_name = "microsoft/xtremedistil-l12-h384-uncased"  # Find popular HuggingFace models here: https://huggingface.co/models
input_text = "The cat sat on the mat"
model = AutoModel.from_pretrained(model_name, output_attentions=True)  # Configure model to return attention values
tokenizer = AutoTokenizer.from_pretrained(model_name)
inputs = tokenizer.encode(input_text, return_tensors='pt')  # Tokenize input text
outputs = model(inputs)  # Run model
attention = outputs[-1]  # Retrieve attention from model outputs
tokens = tokenizer.convert_ids_to_tokens(inputs[0])  # Convert input ids to token string
model_view(attention, tokens)  # Display model view
```

/databricks/python/lib/python3.12/site-packages/transformers/tokenization_utils_base.py:1601: FutureWarning: `clean_up_tokenization_spaces` was not set. It will be set to `True` by default. This behavior will be depracted in transformers v4.45, and will be then set to `False` by default. For more details check this issue: https://github.com/huggingface/transformers/issues/31884 warnings.warn(

  <IPython.core.display.Javascript object>

Does anyone have thoughts on how to actually see the display?  

1 ACCEPTED SOLUTION

Accepted Solutions

koji_kawamura
Databricks Employee
Databricks Employee

Hi @Rasputin312 !

I was able to render the visualization with bertviz library.

The default moview_view html_action is view that does not work with Databricks notebook. Instead, using the returned HTML, we can visualize the model.

display(model_view(attention, tokens, html_action="return")) 

From model_view doc:

html_action: Specifies the action to be performed with the generated HTML object - 'view' (default): Displays the generated HTML representation as a notebook cell output - 'return' : Returns an HTML object containing the generated view for further processing or custom visualization

The entire notebook code is available here. https://gist.github.com/koji-kawamura-db/49c52d5d96b25b936e038fc21d3bbcec

koji_kawamura_0-1741827713282.png

 

View solution in original post

1 REPLY 1

koji_kawamura
Databricks Employee
Databricks Employee

Hi @Rasputin312 !

I was able to render the visualization with bertviz library.

The default moview_view html_action is view that does not work with Databricks notebook. Instead, using the returned HTML, we can visualize the model.

display(model_view(attention, tokens, html_action="return")) 

From model_view doc:

html_action: Specifies the action to be performed with the generated HTML object - 'view' (default): Displays the generated HTML representation as a notebook cell output - 'return' : Returns an HTML object containing the generated view for further processing or custom visualization

The entire notebook code is available here. https://gist.github.com/koji-kawamura-db/49c52d5d96b25b936e038fc21d3bbcec

koji_kawamura_0-1741827713282.png

 

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you wonโ€™t want to miss the chance to attend and share knowledge.

If there isnโ€™t a group near you, start one and help create a community that brings people together.

Request a New Group