Spaces:
Sleeping
Sleeping
Upload 2 files
Browse filesadded app.py and req file
- app.py +43 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
# Load the ModernBERT-large fill-mask pipeline
|
| 5 |
+
try:
|
| 6 |
+
mask_filler = pipeline("fill-mask", model="answerdotai/ModernBERT-large")
|
| 7 |
+
except Exception as e:
|
| 8 |
+
# Fallback if local loading has env limitations in Hugging Face Space setup
|
| 9 |
+
mask_filler = None
|
| 10 |
+
print(f"Error loading model: {e}")
|
| 11 |
+
|
| 12 |
+
def fill_mask(text):
|
| 13 |
+
if mask_filler is None:
|
| 14 |
+
return "Model failed to load. Please check logs."
|
| 15 |
+
|
| 16 |
+
# ModernBERT uses [MASK] or standard BERT mask tokens depending on configuration.
|
| 17 |
+
# We ensure we replace standard blank cues with [MASK] if needed.
|
| 18 |
+
if "[MASK]" not in text:
|
| 19 |
+
return "Please include '[MASK]' in your input sentence."
|
| 20 |
+
|
| 21 |
+
results = mask_filler(text)
|
| 22 |
+
# Format results nicely for Gradio
|
| 23 |
+
output = []
|
| 24 |
+
for res in results:
|
| 25 |
+
output.append(f"Token: {res['token_str']} (Score: {res['score']:.4f}) -> {res['sequence']}")
|
| 26 |
+
return "
|
| 27 |
+
".join(output)
|
| 28 |
+
|
| 29 |
+
# Build the Gradio interface
|
| 30 |
+
demo = gr.Interface(
|
| 31 |
+
fn=fill_mask,
|
| 32 |
+
inputs=gr.Textbox(lines=2, placeholder="The capital of France is [MASK].", label="Input Sentence with [MASK]"),
|
| 33 |
+
outputs=gr.Textbox(label="Predicted Fill-Mask Results"),
|
| 34 |
+
title="ModernBERT-large Demo",
|
| 35 |
+
description="A simple Fill-Mask demo using answerdotai/ModernBERT-large.",
|
| 36 |
+
examples=[
|
| 37 |
+
["ModernBERT is a [MASK] language model."],
|
| 38 |
+
["The quick brown [MASK] jumps over the lazy dog."]
|
| 39 |
+
]
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
if __name__ == '__main__':
|
| 43 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
transformers
|
| 3 |
+
torch
|
| 4 |
+
sentencepiece
|