ghostai1 commited on
Commit
7ed3084
Β·
verified Β·
1 Parent(s): 67be689

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -24
app.py CHANGED
@@ -2,39 +2,35 @@
2
 
3
  import gradio as gr
4
  from transformers import pipeline
5
- from transformers.pipelines.base import PipelineException # correct import
6
 
7
- # Load the fill-mask pipeline once at startup
8
  fill_mask = pipeline("fill-mask", model="distilroberta-base", device=-1)
9
 
10
  def predict_mask(sentence: str, top_k: int):
11
- # Get the model’s actual mask token (e.g. "<mask>")
12
  mask = fill_mask.tokenizer.mask_token
13
 
14
- # Allow users to type [MASK]; convert it under the hood
15
- if "[MASK]" in sentence:
16
- sentence = sentence.replace("[MASK]", mask)
17
 
18
- # If no mask token present, show error
19
  if mask not in sentence:
20
- return [{"sequence": "Error: please include `[MASK]` in your sentence.", "score": 0.0}]
21
 
22
- # Call the pipeline and catch any pipeline-specific exceptions
23
  try:
24
  preds = fill_mask(sentence, top_k=top_k)
25
  except PipelineException as e:
26
- return [{"sequence": f"Error: {str(e)}", "score": 0.0}]
27
 
28
- # Format into list-of-dicts for Gradio Dataframe
29
- return [
30
- {"sequence": p["sequence"], "score": round(p["score"], 3)}
31
- for p in preds
32
- ]
33
 
34
  with gr.Blocks(title="πŸ” Masked Word Predictor") as demo:
35
  gr.Markdown(
36
  "# πŸ” Masked Word Predictor\n"
37
- "Enter a sentence with one `[MASK]` token and see the top-K model predictions."
38
  )
39
 
40
  with gr.Row():
@@ -44,24 +40,22 @@ with gr.Blocks(title="πŸ” Masked Word Predictor") as demo:
44
  label="Input Sentence"
45
  )
46
  top_k = gr.Slider(
47
- minimum=1, maximum=10, value=5, step=1,
48
  label="Top K Predictions"
49
  )
50
 
51
  predict_btn = gr.Button("Predict πŸ”", variant="primary")
52
 
53
- results = gr.Dataframe(
54
- headers=["sequence", "score"],
55
- datatype=["str", "number"],
56
- wrap=True,
57
- interactive=False,
58
  label="Predictions"
59
  )
60
 
61
  predict_btn.click(
62
- predict_mask,
63
  inputs=[sentence, top_k],
64
- outputs=results
65
  )
66
 
67
  if __name__ == "__main__":
 
2
 
3
  import gradio as gr
4
  from transformers import pipeline
5
+ from transformers.pipelines.base import PipelineException
6
 
7
+ # 1. Load fill-mask pipeline once
8
  fill_mask = pipeline("fill-mask", model="distilroberta-base", device=-1)
9
 
10
  def predict_mask(sentence: str, top_k: int):
11
+ # 2. Get the actual mask token (e.g. "<mask>")
12
  mask = fill_mask.tokenizer.mask_token
13
 
14
+ # 3. Allow users to type [MASK]
15
+ sentence = sentence.replace("[MASK]", mask)
 
16
 
17
+ # 4. Validate presence of mask
18
  if mask not in sentence:
19
+ return [["Error: please include `[MASK]` in your sentence.", 0.0]]
20
 
21
+ # 5. Run the pipeline safely
22
  try:
23
  preds = fill_mask(sentence, top_k=top_k)
24
  except PipelineException as e:
25
+ return [[f"Error: {str(e)}", 0.0]]
26
 
27
+ # 6. Return list-of-lists for the Table
28
+ return [[p["sequence"], round(p["score"], 3)] for p in preds]
 
 
 
29
 
30
  with gr.Blocks(title="πŸ” Masked Word Predictor") as demo:
31
  gr.Markdown(
32
  "# πŸ” Masked Word Predictor\n"
33
+ "Enter a sentence with one `[MASK]` token and see the top-K completions."
34
  )
35
 
36
  with gr.Row():
 
40
  label="Input Sentence"
41
  )
42
  top_k = gr.Slider(
43
+ minimum=1, maximum=10, step=1, value=5,
44
  label="Top K Predictions"
45
  )
46
 
47
  predict_btn = gr.Button("Predict πŸ”", variant="primary")
48
 
49
+ # ← Use Table here (list-of-lists) instead of Dataframe
50
+ results_table = gr.Table(
51
+ headers=["Sequence", "Score"],
 
 
52
  label="Predictions"
53
  )
54
 
55
  predict_btn.click(
56
+ fn=predict_mask,
57
  inputs=[sentence, top_k],
58
+ outputs=results_table
59
  )
60
 
61
  if __name__ == "__main__":