Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# 🔍 Masked Word Predictor | CPU-only HF Space
|
2 |
+
|
3 |
+
import gradio as gr
|
4 |
+
from transformers import pipeline
|
5 |
+
|
6 |
+
# Load the fill-mask pipeline once at startup
|
7 |
+
fill_mask = pipeline("fill-mask", model="distilroberta-base", device=-1)
|
8 |
+
|
9 |
+
def predict_mask(sentence: str, top_k: int):
|
10 |
+
if "[MASK]" not in sentence:
|
11 |
+
return [{"sequence": "Error: include [MASK] in your sentence.", "score": 0.0}]
|
12 |
+
preds = fill_mask(sentence, top_k=top_k)
|
13 |
+
return [
|
14 |
+
{"sequence": p["sequence"], "score": round(p["score"], 3)}
|
15 |
+
for p in preds
|
16 |
+
]
|
17 |
+
|
18 |
+
with gr.Blocks(title="🔍 Masked Word Predictor") as demo:
|
19 |
+
gr.Markdown(
|
20 |
+
"# 🔍 Masked Word Predictor\n"
|
21 |
+
"Enter a sentence with one `[MASK]` token and see the model’s top predictions."
|
22 |
+
)
|
23 |
+
|
24 |
+
with gr.Row():
|
25 |
+
sentence = gr.Textbox(
|
26 |
+
lines=2,
|
27 |
+
placeholder="The capital of France is [MASK].",
|
28 |
+
label="Input Sentence"
|
29 |
+
)
|
30 |
+
top_k = gr.Slider(
|
31 |
+
minimum=1, maximum=10, step=1, value=5,
|
32 |
+
label="Top K Predictions"
|
33 |
+
)
|
34 |
+
predict_btn = gr.Button("Predict", variant="primary")
|
35 |
+
|
36 |
+
results = gr.Dataframe(
|
37 |
+
headers=["sequence", "score"],
|
38 |
+
datatype=["str", "number"],
|
39 |
+
wrap=True,
|
40 |
+
interactive=False,
|
41 |
+
label="Predictions"
|
42 |
+
)
|
43 |
+
|
44 |
+
predict_btn.click(
|
45 |
+
predict_mask,
|
46 |
+
inputs=[sentence, top_k],
|
47 |
+
outputs=results
|
48 |
+
)
|
49 |
+
|
50 |
+
if __name__ == "__main__":
|
51 |
+
demo.launch(server_name="0.0.0.0")
|