File size: 1,090 Bytes
cc6e48e |
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 |
import gradio as gr
from transformers import pipeline
fill_mask = pipeline("fill-mask", model="bert-base-uncased")
def complete_sentence(text):
if "[MASK]" not in text:
return "Please include [MASK] token in your input text"
results = fill_mask(text)
output = []
for result in results:
completed = f"Token: {result['token_str']} (Score: {result['score']:.3f})"
output.append(completed)
return "\n".join(output)
iface = gr.Interface(
fn=complete_sentence,
inputs=gr.Textbox(
lines=3,
placeholder="Enter text with [MASK] token...",
label="Input Text"
),
outputs=gr.Textbox(
lines=5,
label="Completed Sentences"
),
title="Sentence Completion",
description="Enter a sentence with [MASK] token and get predictions for the masked word",
examples=[
["The [MASK] is bright today."],
["I love to [MASK] in my free time."],
["She [MASK] to the store yesterday."]
],
theme=gr.themes.Soft()
)
iface.launch()
if __name__ == '__main__':
demo.launch() |