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() |