File size: 1,482 Bytes
311f28b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# ๐Ÿ” Masked Word Predictor | CPU-only HF Space

import gradio as gr
from transformers import pipeline

# Load the fill-mask pipeline once at startup
fill_mask = pipeline("fill-mask", model="distilroberta-base", device=-1)

def predict_mask(sentence: str, top_k: int):
    if "[MASK]" not in sentence:
        return [{"sequence": "Error: include [MASK] in your sentence.", "score": 0.0}]
    preds = fill_mask(sentence, top_k=top_k)
    return [
        {"sequence": p["sequence"], "score": round(p["score"], 3)}
        for p in preds
    ]

with gr.Blocks(title="๐Ÿ” Masked Word Predictor") as demo:
    gr.Markdown(
        "# ๐Ÿ” Masked Word Predictor\n"
        "Enter a sentence with one `[MASK]` token and see the modelโ€™s top predictions."
    )

    with gr.Row():
        sentence = gr.Textbox(
            lines=2,
            placeholder="The capital of France is [MASK].",
            label="Input Sentence"
        )
        top_k = gr.Slider(
            minimum=1, maximum=10, step=1, value=5,
            label="Top K Predictions"
        )
    predict_btn = gr.Button("Predict", variant="primary")

    results = gr.Dataframe(
        headers=["sequence", "score"],
        datatype=["str", "number"],
        wrap=True,
        interactive=False,
        label="Predictions"
    )

    predict_btn.click(
        predict_mask,
        inputs=[sentence, top_k],
        outputs=results
    )

if __name__ == "__main__":
    demo.launch(server_name="0.0.0.0")