robzchhangte commited on
Commit
2d17b2b
·
verified ·
1 Parent(s): 76cabf8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -19
app.py CHANGED
@@ -1,21 +1,14 @@
 
1
  from transformers import pipeline
2
 
3
- # Initialize the fill-mask pipeline with your chosen model
4
- unmasker = pipeline("fill-mask", model="bert-base-uncased")
5
-
6
- def predict(text):
7
- """
8
- Function to take user input, process it, and return predictions.
9
- """
10
- # Add any preprocessing steps here if needed (e.g., tokenization)
11
- masked_text = f"The sentence is {text}." # Example with a mask
12
-
13
- # Use the pipeline to get predictions for the masked word
14
- predictions = unmasker(masked_text, top_k=3) # Top 3 most likely words
15
- return predictions
16
-
17
- # Example usage (replace with your app logic for handling user input)
18
- user_text = input("Enter a word to fill the mask: ")
19
- results = predict(user_text)
20
- for result in results:
21
- print(f"Predicted word: {result['token_str']}, Score: {result['score']}")
 
1
+ import gradio as gr
2
  from transformers import pipeline
3
 
4
+ def fill_mask(text, mask_token="[MASK]"):
5
+ fill_mask_pipeline = pipeline("fill-mask")
6
+ return fill_mask_pipeline(text, mask_token=mask_token)["generated_texts"][0]
7
+
8
+ interface = gr.Interface(
9
+ fn=fill_mask,
10
+ inputs=gr.Textbox(label="Enter text with a mask ([MASK])"),
11
+ outputs=gr.Textbox(label="Masked text filled"),
12
+ title="Fill Mask with Transformers",
13
+ description="Fill the masked word in the sentence using a pre-trained model."
14
+ )