Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions notebooks/modernbert-masked-language-modeling/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# ModernBERT Masked Language Modeling with OpenVINO

This notebook demonstrates how to convert and run the [ModernBERT](https://huggingface.co/blog/modernbert) model using OpenVINO. ModernBERT is an encoder-only model optimized for long-context understanding (up to 8k tokens) and efficiency.

## Notebook Contents

The notebook covers:
1. Loading **ModernBERT** from Hugging Face.
2. Converting the PyTorch model to **OpenVINO IR** format.
3. Running inference on CPUs and other Intel hardware.
4. Launch an interactive **Gradio** demo for masked language modeling.

## Installation Instructions

1. Clone the repository:
```bash
git clone https://github.com/openvinotoolkit/openvino_notebooks.git
cd openvino_notebooks
```

2. Install dependencies:
```bash
pip install -r .ci/dev-requirements.txt
```

3. Run the notebook:
```bash
jupyter lab notebooks/modernbert-masked-language-modeling/modernbert-masked-language-modeling.ipynb
```

<img referrerpolicy="no-referrer-when-downgrade" src="https://static.scarf.sh/a.png?x-pxid=5b5a4db0-7875-4bfb-bdbd-01698b5b1a77&file=notebooks/modernbert-masked-language-modeling/README.md" />
36 changes: 36 additions & 0 deletions notebooks/modernbert-masked-language-modeling/gradio_helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import gradio as gr


def make_demo(fn, title, description):
with gr.Blocks() as demo:
gr.Markdown(f"# {title}")
gr.Markdown(description)

with gr.Row():
with gr.Column():
input_text = gr.Textbox(
label="Input Text",
placeholder="Enter text with [MASK] to predict...",
lines=2,
info="Use the [MASK] token in your sentence to mask a word.",
)
submit_btn = gr.Button("Predict Mask", variant="primary")

examples = gr.Examples(
examples=[
["The capital of France is [MASK]."],
["The quick brown fox jumps over the lazy [MASK]."],
["I am going to the [MASK] to buy some milk."],
["ModernBERT is a state-of-the-art model for natural language [MASK]."],
["Please [MASK] the door when you leave."],
["The weather today is [MASK] and sunny."],
],
inputs=[input_text],
)

with gr.Column():
output_label = gr.Label(num_top_classes=5, label="Top 5 Predictions")

submit_btn.click(fn, inputs=[input_text], outputs=[output_label])

return demo
Loading