UnarineLeo commited on
Commit
8091567
1 Parent(s): e2a34c5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -43
app.py CHANGED
@@ -31,15 +31,19 @@ if 'text_input' not in st.session_state:
31
  if 'warnings' not in st.session_state:
32
  st.session_state['warnings'] = []
33
 
 
 
 
34
  language_options = ['Choose language', 'Zulu', 'Tshivenda', 'Sepedi', 'Tswana', 'Tsonga']
35
 
 
 
36
  with col1:
37
  with st.container():
38
  st.markdown("Input :clipboard:")
39
 
40
  input1, input2 = st.columns(2)
41
 
42
- input_sentences = {}
43
  for i in range(5):
44
  with input1:
45
  language = st.selectbox(f"Select language for sentence {i+1}:", language_options, key=f'language_{i}')
@@ -50,22 +54,19 @@ with col1:
50
  input_sentences[f'{language.lower()}_{i+1}'] = (language.lower(), sentence)
51
 
52
  button1, button2, _ = st.columns([2, 2, 4])
53
- with button1:
54
- if st.button("Test Example"):
55
- sample_sentences = {
56
- 'zulu_1': ('zulu', "Le ndoda ithi izo <mask> ukudla."),
57
- 'tshivenda_2': ('tshivenda', "Vhana vhane vha kha ḓi bva u bebwa vha kha khombo ya u <mask> nga Listeriosis."),
58
- 'tshivenda_3': ('tshivenda', "Rabulasi wa <mask> u khou bvelela nga u lima"),
59
- 'tswana_4': ('tswana', "Monna o <mask> tsamaya."),
60
- 'tsonga_5': ('tsonga', "N'wana wa xisati u <mask> ku tsaka.")
61
- }
62
- input_sentences = sample_sentences
63
- result, warnings = fill_mask(input_sentences)
64
-
65
- with button2:
66
- if st.button("Submit"):
67
- result, warnings = fill_mask(input_sentences)
68
- st.session_state['warnings'] = warnings
69
 
70
  if st.session_state['warnings']:
71
  for warning in st.session_state['warnings']:
@@ -83,32 +84,24 @@ with col1:
83
  with col2:
84
  with st.container():
85
  st.markdown("Output :bar_chart:")
86
- if input_sentences:
87
- for key, (language, sentence) in input_sentences.items():
88
- masked_sentence = sentence.replace('<mask>', unmasker.tokenizer.mask_token)
89
- predictions = unmasker(masked_sentence)
90
-
91
- if predictions:
92
- top_prediction = predictions[0]
93
- predicted_word = top_prediction['token_str']
94
- score = top_prediction['score'] * 100
95
-
96
- st.markdown(f"""
97
- <div class="bar">
98
- <div class="bar-fill" style="width: {score}%;"></div>
99
- </div>
100
- <div class="container">
101
- <div style="align-items: left;">{predicted_word} ({language})</div>
102
- <div style="align-items: right;">{score:.2f}%</div>
103
- </div>
104
- """, unsafe_allow_html=True)
105
-
106
- if 'predictions' in locals():
107
- if result:
108
- for key, (language, language_predictions) in result.items():
109
- original_sentence = input_sentences[key][1]
110
- predicted_sentence = replace_mask(original_sentence, language_predictions[0]['token_str'])
111
- st.write(f"{language}: {predicted_sentence}\n")
112
 
113
  css = """
114
  <style>
 
31
  if 'warnings' not in st.session_state:
32
  st.session_state['warnings'] = []
33
 
34
+ if 'result' not in st.session_state:
35
+ st.session_state['result'] = {}
36
+
37
  language_options = ['Choose language', 'Zulu', 'Tshivenda', 'Sepedi', 'Tswana', 'Tsonga']
38
 
39
+ input_sentences = {}
40
+
41
  with col1:
42
  with st.container():
43
  st.markdown("Input :clipboard:")
44
 
45
  input1, input2 = st.columns(2)
46
 
 
47
  for i in range(5):
48
  with input1:
49
  language = st.selectbox(f"Select language for sentence {i+1}:", language_options, key=f'language_{i}')
 
54
  input_sentences[f'{language.lower()}_{i+1}'] = (language.lower(), sentence)
55
 
56
  button1, button2, _ = st.columns([2, 2, 4])
57
+
58
+ if st.button("Test Example"):
59
+ sample_sentences = {
60
+ 'zulu_1': ('zulu', "Le ndoda ithi izo <mask> ukudla."),
61
+ 'tshivenda_2': ('tshivenda', "Vhana vhane vha kha ḓi bva u bebwa vha kha khombo ya u <mask> nga Listeriosis."),
62
+ 'tshivenda_3': ('tshivenda', "Rabulasi wa <mask> u khou bvelela nga u lima"),
63
+ 'tswana_4': ('tswana', "Monna o <mask> tsamaya."),
64
+ 'tsonga_5': ('tsonga', "N'wana wa xisati u <mask> ku tsaka.")
65
+ }
66
+ st.session_state['result'], st.session_state['warnings'] = fill_mask(sample_sentences)
67
+
68
+ if st.button("Submit"):
69
+ st.session_state['result'], st.session_state['warnings'] = fill_mask(input_sentences)
 
 
 
70
 
71
  if st.session_state['warnings']:
72
  for warning in st.session_state['warnings']:
 
84
  with col2:
85
  with st.container():
86
  st.markdown("Output :bar_chart:")
87
+ if st.session_state['result']:
88
+ for key, (language, predictions) in st.session_state['result'].items():
89
+ original_sentence = input_sentences[key][1]
90
+ predicted_word = predictions[0]['token_str']
91
+ score = predictions[0]['score'] * 100
92
+
93
+ st.markdown(f"""
94
+ <div class="bar">
95
+ <div class="bar-fill" style="width: {score}%;"></div>
96
+ </div>
97
+ <div class="container">
98
+ <div style="align-items: left;">{predicted_word} ({language})</div>
99
+ <div style="align-items: right;">{score:.2f}%</div>
100
+ </div>
101
+ """, unsafe_allow_html=True)
102
+
103
+ predicted_sentence = replace_mask(original_sentence, predicted_word)
104
+ st.write(f"{language}: {predicted_sentence}\n")
 
 
 
 
 
 
 
 
105
 
106
  css = """
107
  <style>