openfree commited on
Commit
cc6e48e
·
verified ·
1 Parent(s): 19874ab

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +36 -0
app.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+ fill_mask = pipeline("fill-mask", model="bert-base-uncased")
4
+ def complete_sentence(text):
5
+ if "[MASK]" not in text:
6
+ return "Please include [MASK] token in your input text"
7
+ results = fill_mask(text)
8
+ output = []
9
+ for result in results:
10
+ completed = f"Token: {result['token_str']} (Score: {result['score']:.3f})"
11
+ output.append(completed)
12
+ return "\n".join(output)
13
+ iface = gr.Interface(
14
+ fn=complete_sentence,
15
+ inputs=gr.Textbox(
16
+ lines=3,
17
+ placeholder="Enter text with [MASK] token...",
18
+ label="Input Text"
19
+ ),
20
+ outputs=gr.Textbox(
21
+ lines=5,
22
+ label="Completed Sentences"
23
+ ),
24
+ title="Sentence Completion",
25
+ description="Enter a sentence with [MASK] token and get predictions for the masked word",
26
+ examples=[
27
+ ["The [MASK] is bright today."],
28
+ ["I love to [MASK] in my free time."],
29
+ ["She [MASK] to the store yesterday."]
30
+ ],
31
+ theme=gr.themes.Soft()
32
+ )
33
+ iface.launch()
34
+
35
+ if __name__ == '__main__':
36
+ demo.launch()