MUmairAB commited on
Commit
18cb05d
1 Parent(s): 57cad47

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -0
app.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # import the module
2
+ import streamlit as st
3
+ from transformers import pipeline
4
+
5
+
6
+ #This function accepts the masked text like: "How are [MASK]"
7
+ # and feeds this text to the model and prints the output in which [MASK] is filled with the appropriate word.
8
+ def print_the_mask(text):
9
+
10
+ #Import the model
11
+ model = pipeline(task="fill-mask",
12
+ model="MUmairAB/bert-based-MaskedLM")
13
+
14
+ #Apply the model
15
+ model_out = model("I want to [MASK]")
16
+
17
+ #First sort the list of dictionaries according to the score
18
+ model_out = sorted(model_out, key=lambda x: x['score'],reverse=True)
19
+ for sub_dict in model_out:
20
+ print(sub_dict["sequence"])
21
+
22
+
23
+ #The main function that will be executed when this file is executed
24
+ def main():
25
+ # Set the title
26
+ st.title("Masked Language Model App")
27
+ st.write("Created by: [Umair Akram](https://www.linkedin.com/in/m-umair01/)")
28
+
29
+ h1 = "This App uses a fine-tuned DistilBERT-Base-Uncased Masked Language Model to predict the missed word in a sentence."
30
+ st.subheader(h1)
31
+
32
+ st.write("Its code and other interesting projects are available on my [website](https://mumairab.github.io/)")
33
+ h2 = "Enter your text and put \"[MASK]\" at the word which you want to predict, as shown in the following example: How are [MASK]"
34
+ st.write(h2)
35
+
36
+ text = st.text_input(label="Enter your text here:",
37
+ value="Type here ...")
38
+
39
+ if(st.button('Submit')):
40
+ # Perform the input validation
41
+ if "[MASK]" not in text:
42
+ st.write("You did not enter \"[MASK]\" in the text. Please write your text again!")
43
+ else:
44
+ print_the_mask(text)