Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
# Load a pretrained fill-mask model
|
| 5 |
+
unmasker = pipeline("fill-mask", model="bert-base-uncased")
|
| 6 |
+
|
| 7 |
+
def fill_mask_fn(text, top_k):
|
| 8 |
+
mask_token = unmasker.tokenizer.mask_token
|
| 9 |
+
if mask_token not in text:
|
| 10 |
+
return f"Please include the mask token '{mask_token}' in your input."
|
| 11 |
+
results = unmasker(text, top_k=top_k)
|
| 12 |
+
output_lines = []
|
| 13 |
+
for r in results:
|
| 14 |
+
token = r["token_str"]
|
| 15 |
+
score = r["score"]
|
| 16 |
+
sequence = r["sequence"]
|
| 17 |
+
output_lines.append(f"{sequence}\n→ Prediction: {token} (score: {score:.4f})\n")
|
| 18 |
+
return "\n".join(output_lines)
|
| 19 |
+
|
| 20 |
+
with gr.Blocks() as demo:
|
| 21 |
+
gr.Markdown("""
|
| 22 |
+
# 🧩 Fill-Mask Demo
|
| 23 |
+
Type a sentence with a **[MASK]** token to see model predictions.
|
| 24 |
+
|
| 25 |
+
Example:
|
| 26 |
+
`Paris is the [MASK] of France.`
|
| 27 |
+
""")
|
| 28 |
+
|
| 29 |
+
text_input = gr.Textbox(label="Input Text", value="The quick brown [MASK] jumps over the lazy dog.")
|
| 30 |
+
top_k_slider = gr.Slider(label="Top-K Predictions", minimum=1, maximum=10, step=1, value=5)
|
| 31 |
+
output = gr.Textbox(label="Model Output", lines=12)
|
| 32 |
+
btn = gr.Button("Generate Predictions")
|
| 33 |
+
|
| 34 |
+
btn.click(fill_mask_fn, inputs=[text_input, top_k_slider], outputs=output)
|
| 35 |
+
|
| 36 |
+
demo.launch()
|