Shangkhonil commited on
Commit
2f80053
·
verified ·
1 Parent(s): d7ccb8c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -0
app.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForMaskedLM
3
+ import torch
4
+
5
+ # Load tokenizer and model for masked language modeling
6
+ tokenizer = AutoTokenizer.from_pretrained("sentence-transformers/all-mpnet-base-v2")
7
+ model = AutoModelForMaskedLM.from_pretrained("sentence-transformers/all-mpnet-base-v2")
8
+
9
+ def fill_mask(text):
10
+ # Tokenize the input text
11
+ inputs = tokenizer(text, return_tensors="pt")
12
+
13
+ # Get model predictions for masked tokens
14
+ with torch.no_grad():
15
+ outputs = model(**inputs)
16
+
17
+ # Convert logits to token predictions
18
+ predictions = torch.topk(outputs.logits, k=1).indices.squeeze(0)
19
+
20
+ # Decode the predicted tokens to words
21
+ decoded_output = tokenizer.decode(predictions)
22
+
23
+ return decoded_output
24
+
25
+ # Gradio interface setup
26
+ title = "Masked Language Model (MPNet)"
27
+ description = "Provide input text with [MASK] and the model will predict the masked token."
28
+
29
+ # Gradio interface
30
+ interface = gr.Interface(
31
+ fn=fill_mask,
32
+ inputs=gr.Textbox(label="Input Text", placeholder="Type something with [MASK]..."),
33
+ outputs=gr.Textbox(label="Predicted Text"),
34
+ title=title,
35
+ description=description,
36
+ )
37
+
38
+ # Launch Gradio app
39
+ if __name__ == "__main__":
40
+ interface.launch()