File size: 959 Bytes
2f80053
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from transformers import AutoTokenizer, AutoModelForMaskedLM
import torch

tokenizer = AutoTokenizer.from_pretrained("sentence-transformers/all-mpnet-base-v2")
model = AutoModelForMaskedLM.from_pretrained("sentence-transformers/all-mpnet-base-v2")

def fill_mask(text):
    inputs = tokenizer(text, return_tensors="pt")
    with torch.no_grad():
        outputs = model(**inputs)
    predictions = torch.topk(outputs.logits, k=1).indices.squeeze(0)
    decoded_output = tokenizer.decode(predictions)
    return decoded_output

title = "Masked Language Model (MPNet)"
description = "Provide input text with [MASK] and the model will predict the masked token."

interface = gr.Interface(
    fn=fill_mask,
    inputs=gr.Textbox(label="Input Text", placeholder="Type something with [MASK]..."),
    outputs=gr.Textbox(label="Predicted Text"),
    title=title,
    description=description,
)

if __name__ == "__main__":
    interface.launch()