UnarineLeo commited on
Commit
73fb5bb
1 Parent(s): f3c64a3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -32
app.py CHANGED
@@ -3,53 +3,83 @@ from transformers import pipeline
3
 
4
  unmasker = pipeline('fill-mask', model='dsfsi/zabantu-nso-ven-170m')
5
 
 
 
6
  def fill_mask(sentences):
7
  results = {}
 
8
  for sentence in sentences:
9
- unmasked = unmasker(sentence)
10
- results[sentence] = unmasked
11
- return results
 
 
 
12
 
13
  def replace_mask(sentence, predicted_word):
14
  return sentence.replace("<mask>", f"**{predicted_word}**")
15
 
16
- st.title("Fill Mask | Zabantu-nso-ven-170m")
17
  st.write(f"")
18
 
19
  col1, col2 = st.columns(2)
20
 
21
- with col1:
22
- sample_sentences = [
23
- "Le nna ke gona, ke a <mask>"
24
- ]
25
 
26
- text_input = st.text_area(
27
- "Enter sentences with <mask> token (one per line):",
28
- "\n".join(sample_sentences)
29
- )
 
 
 
 
 
 
 
 
 
 
30
 
31
- input_sentences = text_input.split("\n")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
 
33
- if st.button("Submit"):
34
- result = fill_mask(input_sentences)
35
 
36
  with col2:
37
- if 'result' in locals():
38
- if result:
39
- for sentence, predictions in result.items():
40
- for prediction in predictions:
41
- predicted_word = prediction['token_str']
42
- score = prediction['score'] * 100
43
-
44
- st.markdown(f"""
45
- <div class="bar">
46
- <div class="bar-fill" style="width: {score}%;"></div>
47
- </div>
48
- <div class="container">
49
- <div style="align-items: left;">{predicted_word}</div>
50
- <div style="align-items: right;">{score:.2f}%</div>
51
- </div>
52
- """, unsafe_allow_html=True)
 
 
53
 
54
  if 'result' in locals():
55
  if result:
@@ -123,6 +153,7 @@ footer {display:none !important;}
123
  --tw-text-opacity: 1 !important;
124
  color:rgb(37 56 133 / var(--tw-text-opacity)) !important;
125
  }
 
126
  .container {
127
  display: flex;
128
  justify-content: space-between;
@@ -131,7 +162,7 @@ footer {display:none !important;}
131
  width: 100%;
132
  }
133
  .bar {
134
- width: 70%;
135
  background-color: #e6e6e6;
136
  border-radius: 12px;
137
  overflow: hidden;
@@ -143,6 +174,7 @@ footer {display:none !important;}
143
  height: 100%;
144
  border-radius: 12px;
145
  }
 
146
  </style>
147
  """
148
 
 
3
 
4
  unmasker = pipeline('fill-mask', model='dsfsi/zabantu-nso-ven-170m')
5
 
6
+ st.set_page_config(layout="wide")
7
+
8
  def fill_mask(sentences):
9
  results = {}
10
+ warnings = []
11
  for sentence in sentences:
12
+ if "<mask>" in sentence:
13
+ unmasked = unmasker(sentence)
14
+ results[sentence] = unmasked
15
+ else:
16
+ warnings.append(f"Warning: No <mask> token found in sentence: {sentence}")
17
+ return results, warnings
18
 
19
  def replace_mask(sentence, predicted_word):
20
  return sentence.replace("<mask>", f"**{predicted_word}**")
21
 
22
+ st.title("Fill Mask | Zabantu-sot-ven-170m")
23
  st.write(f"")
24
 
25
  col1, col2 = st.columns(2)
26
 
27
+ if 'text_input' not in st.session_state:
28
+ st.session_state['text_input'] = ""
 
 
29
 
30
+ if 'warnings' not in st.session_state:
31
+ st.session_state['warnings'] = []
32
+
33
+ with col1:
34
+ with st.container(border=True):
35
+ st.markdown("Input :clipboard:")
36
+ sample_sentence = "Vhana vhane vha kha ḓi bva u bebwa vha kha khombo ya u <mask> nga Listeriosis."
37
+
38
+ text_input = st.text_area(
39
+ "Enter sentences with <mask> token:",
40
+ value=st.session_state['text_input']
41
+ )
42
+
43
+ input_sentences = text_input.split("\n")
44
 
45
+ button1, button2, _ = st.columns([1.25, 1, 5.7])
46
+ with button1:
47
+ if st.button("Test Example"):
48
+ # st.rerun()
49
+ result, warnings = fill_mask(sample_sentence.split("\n"))
50
+ # st.session_state['text_input'] = sample_sentence
51
+
52
+ with button2:
53
+ if st.button("Submit"):
54
+ result, warnings = fill_mask(input_sentences)
55
+ st.session_state['warnings'] = warnings
56
+
57
+ if st.session_state['warnings']:
58
+ for warning in st.session_state['warnings']:
59
+ st.warning(warning)
60
 
61
+ st.markdown("Example")
62
+ st.code(sample_sentence, wrap_lines=True)
63
 
64
  with col2:
65
+ with st.container(border=True):
66
+ st.markdown("Output :bar_chart:")
67
+ if 'result' in locals() and result:
68
+ if result:
69
+ for sentence, predictions in result.items():
70
+ for prediction in predictions:
71
+ predicted_word = prediction['token_str']
72
+ score = prediction['score'] * 100
73
+
74
+ st.markdown(f"""
75
+ <div class="bar">
76
+ <div class="bar-fill" style="width: {score}%;"></div>
77
+ </div>
78
+ <div class="container">
79
+ <div style="align-items: left;">{predicted_word}</div>
80
+ <div style="align-items: center;">{score:.2f}%</div>
81
+ </div>
82
+ """, unsafe_allow_html=True)
83
 
84
  if 'result' in locals():
85
  if result:
 
153
  --tw-text-opacity: 1 !important;
154
  color:rgb(37 56 133 / var(--tw-text-opacity)) !important;
155
  }
156
+
157
  .container {
158
  display: flex;
159
  justify-content: space-between;
 
162
  width: 100%;
163
  }
164
  .bar {
165
+ # width: 70%;
166
  background-color: #e6e6e6;
167
  border-radius: 12px;
168
  overflow: hidden;
 
174
  height: 100%;
175
  border-radius: 12px;
176
  }
177
+
178
  </style>
179
  """
180