ghostai1 commited on
Commit
493b784
Β·
verified Β·
1 Parent(s): 7ed3084

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -8
app.py CHANGED
@@ -1,10 +1,11 @@
1
  # πŸ” Masked Word Predictor | CPU-only HF Space
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):
@@ -16,16 +17,21 @@ def predict_mask(sentence: str, top_k: int):
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(
@@ -46,16 +52,18 @@ with gr.Blocks(title="πŸ” Masked Word Predictor") as demo:
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__":
 
1
  # πŸ” Masked Word Predictor | CPU-only HF Space
2
 
3
  import gradio as gr
4
+ import pandas as pd
5
  from transformers import pipeline
6
  from transformers.pipelines.base import PipelineException
7
 
8
+ # 1. Load the fill-mask pipeline once
9
  fill_mask = pipeline("fill-mask", model="distilroberta-base", device=-1)
10
 
11
  def predict_mask(sentence: str, top_k: int):
 
17
 
18
  # 4. Validate presence of mask
19
  if mask not in sentence:
20
+ return pd.DataFrame(
21
+ [["Error: please include `[MASK]` in your sentence.", 0.0]],
22
+ columns=["Sequence", "Score"]
23
+ )
24
 
25
  # 5. Run the pipeline safely
26
  try:
27
  preds = fill_mask(sentence, top_k=top_k)
28
  except PipelineException as e:
29
+ return pd.DataFrame([[f"Error: {str(e)}", 0.0]],
30
+ columns=["Sequence", "Score"])
31
 
32
+ # 6. Build a DataFrame from list-of-lists
33
+ rows = [[p["sequence"], round(p["score"], 3)] for p in preds]
34
+ return pd.DataFrame(rows, columns=["Sequence", "Score"])
35
 
36
  with gr.Blocks(title="πŸ” Masked Word Predictor") as demo:
37
  gr.Markdown(
 
52
 
53
  predict_btn = gr.Button("Predict πŸ”", variant="primary")
54
 
55
+ results_df = gr.Dataframe(
 
56
  headers=["Sequence", "Score"],
57
+ datatype=["str", "number"],
58
+ wrap=True,
59
+ interactive=False,
60
  label="Predictions"
61
  )
62
 
63
  predict_btn.click(
64
  fn=predict_mask,
65
  inputs=[sentence, top_k],
66
+ outputs=results_df
67
  )
68
 
69
  if __name__ == "__main__":