MUmairAB commited on
Commit
354770f
1 Parent(s): 17093fd

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -0
app.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #import necessary libraries
2
+ import gradio as gr
3
+ from transformers import pipeline
4
+ #Instantiate the model
5
+ model = pipeline(task="fill-mask",
6
+ model="MUmairAB/bert-based-MaskedLM")
7
+
8
+ #Typically, the model should be imported within a function. However, in this case, we are downloading it outside the function to avoid a significant delay that could annoy the user when downloading it inside the main function. By loading the model at this point, it will be downloaded when the app runs, and the user will overlook this initial loading time, as opposed to experiencing a delay after entering the input.
9
+
10
+
11
+ def fill_the_mask(text):
12
+
13
+ if "[MASK]" not in text:
14
+ return "You did not enter \"[MASK]\" in the text. Please write your text again!"
15
+ else:
16
+ #Apply the model
17
+ model_out = model(text)
18
+
19
+ #First sort the list of dictionaries according to the score
20
+ model_out = sorted(model_out, key=lambda x: x['score'],reverse=True)
21
+
22
+ #Create a list to store the model output
23
+ out_list = []
24
+
25
+ #Iterate over the list of dictionaries and get the required ouput
26
+ for sub_dict in model_out:
27
+ out_list.append(sub_dict["sequence"])
28
+
29
+ return out_list
30
+
31
+ #Create a Gradio user interface
32
+ my_interface = gr.Interface(title="Masked Language Model APP\n(by Umair Akram)",
33
+ description="This App uses a fine-tuned DistilBERT-Base-Uncased Masked Language Model to predict the missed word in a sentence.\nEnter your text and put \"[MASK]\" at the word which you want to predict, as shown in the following example: Can we [MASK] to Paris?",
34
+ fn=fill_the_mask,
35
+ inputs="text",
36
+ outputs="text")
37
+
38
+ #Define the main function
39
+ if __name__ == "__main__":
40
+ #Launch the Gradio interface
41
+ my_interface.launch()