Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,39 +2,35 @@
|
|
| 2 |
|
| 3 |
import gradio as gr
|
| 4 |
from transformers import pipeline
|
| 5 |
-
from transformers.pipelines.base import PipelineException
|
| 6 |
|
| 7 |
-
# Load
|
| 8 |
fill_mask = pipeline("fill-mask", model="distilroberta-base", device=-1)
|
| 9 |
|
| 10 |
def predict_mask(sentence: str, top_k: int):
|
| 11 |
-
# Get the
|
| 12 |
mask = fill_mask.tokenizer.mask_token
|
| 13 |
|
| 14 |
-
# Allow users to type [MASK]
|
| 15 |
-
|
| 16 |
-
sentence = sentence.replace("[MASK]", mask)
|
| 17 |
|
| 18 |
-
#
|
| 19 |
if mask not in sentence:
|
| 20 |
-
return [
|
| 21 |
|
| 22 |
-
#
|
| 23 |
try:
|
| 24 |
preds = fill_mask(sentence, top_k=top_k)
|
| 25 |
except PipelineException as e:
|
| 26 |
-
return [
|
| 27 |
|
| 28 |
-
#
|
| 29 |
-
return [
|
| 30 |
-
{"sequence": p["sequence"], "score": round(p["score"], 3)}
|
| 31 |
-
for p in preds
|
| 32 |
-
]
|
| 33 |
|
| 34 |
with gr.Blocks(title="π Masked Word Predictor") as demo:
|
| 35 |
gr.Markdown(
|
| 36 |
"# π Masked Word Predictor\n"
|
| 37 |
-
"Enter a sentence with one `[MASK]` token and see the top-K
|
| 38 |
)
|
| 39 |
|
| 40 |
with gr.Row():
|
|
@@ -44,24 +40,22 @@ with gr.Blocks(title="π Masked Word Predictor") as demo:
|
|
| 44 |
label="Input Sentence"
|
| 45 |
)
|
| 46 |
top_k = gr.Slider(
|
| 47 |
-
minimum=1, maximum=10,
|
| 48 |
label="Top K Predictions"
|
| 49 |
)
|
| 50 |
|
| 51 |
predict_btn = gr.Button("Predict π", variant="primary")
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
wrap=True,
|
| 57 |
-
interactive=False,
|
| 58 |
label="Predictions"
|
| 59 |
)
|
| 60 |
|
| 61 |
predict_btn.click(
|
| 62 |
-
predict_mask,
|
| 63 |
inputs=[sentence, top_k],
|
| 64 |
-
outputs=
|
| 65 |
)
|
| 66 |
|
| 67 |
if __name__ == "__main__":
|
|
|
|
| 2 |
|
| 3 |
import gradio as gr
|
| 4 |
from transformers import pipeline
|
| 5 |
+
from transformers.pipelines.base import PipelineException
|
| 6 |
|
| 7 |
+
# 1. Load fill-mask pipeline once
|
| 8 |
fill_mask = pipeline("fill-mask", model="distilroberta-base", device=-1)
|
| 9 |
|
| 10 |
def predict_mask(sentence: str, top_k: int):
|
| 11 |
+
# 2. Get the actual mask token (e.g. "<mask>")
|
| 12 |
mask = fill_mask.tokenizer.mask_token
|
| 13 |
|
| 14 |
+
# 3. Allow users to type [MASK]
|
| 15 |
+
sentence = sentence.replace("[MASK]", mask)
|
|
|
|
| 16 |
|
| 17 |
+
# 4. Validate presence of mask
|
| 18 |
if mask not in sentence:
|
| 19 |
+
return [["Error: please include `[MASK]` in your sentence.", 0.0]]
|
| 20 |
|
| 21 |
+
# 5. Run the pipeline safely
|
| 22 |
try:
|
| 23 |
preds = fill_mask(sentence, top_k=top_k)
|
| 24 |
except PipelineException as e:
|
| 25 |
+
return [[f"Error: {str(e)}", 0.0]]
|
| 26 |
|
| 27 |
+
# 6. Return list-of-lists for the Table
|
| 28 |
+
return [[p["sequence"], round(p["score"], 3)] for p in preds]
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
with gr.Blocks(title="π Masked Word Predictor") as demo:
|
| 31 |
gr.Markdown(
|
| 32 |
"# π Masked Word Predictor\n"
|
| 33 |
+
"Enter a sentence with one `[MASK]` token and see the top-K completions."
|
| 34 |
)
|
| 35 |
|
| 36 |
with gr.Row():
|
|
|
|
| 40 |
label="Input Sentence"
|
| 41 |
)
|
| 42 |
top_k = gr.Slider(
|
| 43 |
+
minimum=1, maximum=10, step=1, value=5,
|
| 44 |
label="Top K Predictions"
|
| 45 |
)
|
| 46 |
|
| 47 |
predict_btn = gr.Button("Predict π", variant="primary")
|
| 48 |
|
| 49 |
+
# β Use Table here (list-of-lists) instead of Dataframe
|
| 50 |
+
results_table = gr.Table(
|
| 51 |
+
headers=["Sequence", "Score"],
|
|
|
|
|
|
|
| 52 |
label="Predictions"
|
| 53 |
)
|
| 54 |
|
| 55 |
predict_btn.click(
|
| 56 |
+
fn=predict_mask,
|
| 57 |
inputs=[sentence, top_k],
|
| 58 |
+
outputs=results_table
|
| 59 |
)
|
| 60 |
|
| 61 |
if __name__ == "__main__":
|