Keyurjotaniya007 commited on
Commit
8913017
Β·
verified Β·
1 Parent(s): c90cb42

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -15
app.py CHANGED
@@ -1,56 +1,48 @@
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):
12
- # 2. Get the actual mask token (e.g. "<mask>")
13
  mask = fill_mask.tokenizer.mask_token
14
 
15
- # 3. Allow users to type [MASK]
16
  sentence = sentence.replace("[MASK]", mask)
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(
38
- "# πŸ” Masked Word Predictor\n"
39
  "Enter a sentence with one `[MASK]` token and see the top-K completions."
40
  )
41
 
42
  with gr.Row():
43
  sentence = gr.Textbox(
44
  lines=2,
45
- placeholder="e.g. The salon’s new color treatment is [MASK].",
46
  label="Input Sentence"
47
  )
48
  top_k = gr.Slider(
49
  minimum=1, maximum=10, step=1, value=5,
50
- label="Top K Predictions"
51
  )
52
 
53
- predict_btn = gr.Button("Predict πŸ”", variant="primary")
54
 
55
  results_df = gr.Dataframe(
56
  headers=["Sequence", "Score"],
@@ -67,4 +59,4 @@ with gr.Blocks(title="πŸ” Masked Word Predictor") as demo:
67
  )
68
 
69
  if __name__ == "__main__":
70
- demo.launch(server_name="0.0.0.0")
 
 
 
1
  import gradio as gr
2
  import pandas as pd
3
  from transformers import pipeline
4
  from transformers.pipelines.base import PipelineException
5
 
6
+ fill_mask = pipeline("fill-mask", model="Keyurjotaniya007/bert-large-cased-wikitext-mlm-3.0", device=-1)
 
7
 
8
  def predict_mask(sentence: str, top_k: int):
 
9
  mask = fill_mask.tokenizer.mask_token
10
 
 
11
  sentence = sentence.replace("[MASK]", mask)
12
 
 
13
  if mask not in sentence:
14
  return pd.DataFrame(
15
  [["Error: please include `[MASK]` in your sentence.", 0.0]],
16
  columns=["Sequence", "Score"]
17
  )
18
 
 
19
  try:
20
  preds = fill_mask(sentence, top_k=top_k)
21
  except PipelineException as e:
22
  return pd.DataFrame([[f"Error: {str(e)}", 0.0]],
23
  columns=["Sequence", "Score"])
24
 
 
25
  rows = [[p["sequence"], round(p["score"], 3)] for p in preds]
26
  return pd.DataFrame(rows, columns=["Sequence", "Score"])
27
 
28
+ with gr.Blocks(title="Masked Language Modeling") as demo:
29
  gr.Markdown(
30
+ "# Masked Language Modeling\n"
31
  "Enter a sentence with one `[MASK]` token and see the top-K completions."
32
  )
33
 
34
  with gr.Row():
35
  sentence = gr.Textbox(
36
  lines=2,
37
+ placeholder="e.g. The Great Wall of [MASK] is visible from space.",
38
  label="Input Sentence"
39
  )
40
  top_k = gr.Slider(
41
  minimum=1, maximum=10, step=1, value=5,
42
+ label="K Predictions[Min=1 & Max=10]"
43
  )
44
 
45
+ predict_btn = gr.Button("Evaluate [MASK] Words", variant="primary")
46
 
47
  results_df = gr.Dataframe(
48
  headers=["Sequence", "Score"],
 
59
  )
60
 
61
  if __name__ == "__main__":
62
+ demo.launch(server_name="0.0.0.0")