Malek-AI commited on
Commit
6f75e95
·
verified ·
1 Parent(s): 04ee5d1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -4
app.py CHANGED
@@ -2,9 +2,25 @@ import gradio as gr
2
  from transformers import pipeline
3
 
4
  def mask(text):
5
- mask_model = pipeline("fill-mask", model="google-bert/bert-base-uncased")
6
- output = mask_model(text)
7
- return output[0]['sequence']
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
  # Gradio UI
10
  examples=[['Today I went to [MASK] after I got out of bed.']]
@@ -17,4 +33,4 @@ ui = gr.Interface(
17
  description="Enter some text with [MASK] to let BERT guess the missing word!",
18
  examples=examples
19
  )
20
- ui.launch(debug=True)
 
2
  from transformers import pipeline
3
 
4
  def mask(text):
5
+ mask_model = pipeline("fill-mask", model="google-bert/bert-base-uncased")
6
+
7
+ # Extract labels (classes) and scores from predictions
8
+ labels = [result["label"] for result in mask_model]
9
+ scores = [result["score"] for result in mask_model]
10
+
11
+ # Find the index of the best prediction (highest score)
12
+ best_prediction_idx = scores.index(max(scores))
13
+
14
+ # Create a dictionary with results
15
+ response_dict = {
16
+ "Original Text": text,
17
+ "All Predictions": labels,
18
+ "Best Prediction": f"**{labels[best_prediction_idx]}** (Score: {scores[best_prediction_idx]:.4f})"
19
+ }
20
+
21
+ return response_dict
22
+ # output = mask_model(text)
23
+ # return output[0]['sequence']
24
 
25
  # Gradio UI
26
  examples=[['Today I went to [MASK] after I got out of bed.']]
 
33
  description="Enter some text with [MASK] to let BERT guess the missing word!",
34
  examples=examples
35
  )
36
+ ui.launch(debug=True)