UnarineLeo commited on
Commit
d104ff1
1 Parent(s): f5f8f9a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -14
app.py CHANGED
@@ -1,45 +1,67 @@
1
  import streamlit as st
2
  from transformers import pipeline
3
 
 
4
  unmasker = pipeline('fill-mask', model='dsfsi/zabantu-bantu-250m')
5
 
 
6
  sample_sentences = {
7
- 'Zulu': "Le ndoda ithi izo____ ukudla.",
8
- 'Tshivenda': "Mufana uyo____ vhukuma.",
9
- 'Sepedi': "Mosadi o ____ pheka.",
10
- 'Tswana': "Monna o ____ tsamaya.",
11
- 'Tsonga': "N'wana wa xisati u ____ ku tsaka."
12
  }
13
 
 
14
  def fill_mask_for_languages(sentences):
15
  results = {}
16
  for language, sentence in sentences.items():
 
17
  masked_sentence = sentence.replace('____', unmasker.tokenizer.mask_token)
18
 
 
19
  unmasked = unmasker(masked_sentence)
20
 
 
21
  results[language] = unmasked
22
  return results
23
 
 
24
  st.title("Fill Mask for Multiple Languages | Zabantu-Bantu-250m")
25
  st.write("This app predicts the missing word for sentences in Zulu, Tshivenda, Sepedi, Tswana, and Tsonga using a Zabantu BERT model.")
26
 
 
27
  st.write("### Sample sentences:")
28
  for language, sentence in sample_sentences.items():
29
  st.write(f"**{language}**: {sentence}")
30
 
 
 
 
 
31
  if st.button("Submit"):
32
- result = fill_mask_for_languages(sample_sentences)
 
 
 
 
33
 
34
- if result:
35
- st.write("### Predictions:")
36
- for language, predictions in result.items():
37
- original_sentence = sample_sentences[language]
38
- predicted_sentence = predictions[0]['sequence']
39
- st.write(f"Original sentence ({language}): {original_sentence}")
40
- st.write(f"Top prediction for the masked token: {predicted_sentence}\n")
41
- st.write("=" * 80)
 
 
 
 
 
42
 
 
43
  css = """
44
  <style>
45
  footer {display:none !important}
 
1
  import streamlit as st
2
  from transformers import pipeline
3
 
4
+ # Initialize the pipeline for the fill-mask task
5
  unmasker = pipeline('fill-mask', model='dsfsi/zabantu-bantu-250m')
6
 
7
+ # Sample sentences for different languages with placeholders for the masked word
8
  sample_sentences = {
9
+ 'Zulu': "Le ndoda ithi izo____ ukudla.", # Masked word for Zulu
10
+ 'Tshivenda': "Mufana uyo____ vhukuma.", # Masked word for Tshivenda
11
+ 'Sepedi': "Mosadi o ____ pheka.", # Masked word for Sepedi
12
+ 'Tswana': "Monna o ____ tsamaya.", # Masked word for Tswana
13
+ 'Tsonga': "N'wana wa xisati u ____ ku tsaka." # Masked word for Tsonga
14
  }
15
 
16
+ # Function to perform fill-mask on sentences with the token '__' replaced
17
  def fill_mask_for_languages(sentences):
18
  results = {}
19
  for language, sentence in sentences.items():
20
+ # Replace the '____' placeholder with the model's mask token
21
  masked_sentence = sentence.replace('____', unmasker.tokenizer.mask_token)
22
 
23
+ # Get predictions for the masked sentence
24
  unmasked = unmasker(masked_sentence)
25
 
26
+ # Store the result for each language
27
  results[language] = unmasked
28
  return results
29
 
30
+ # Streamlit interface
31
  st.title("Fill Mask for Multiple Languages | Zabantu-Bantu-250m")
32
  st.write("This app predicts the missing word for sentences in Zulu, Tshivenda, Sepedi, Tswana, and Tsonga using a Zabantu BERT model.")
33
 
34
+ # Display the original sample sentences
35
  st.write("### Sample sentences:")
36
  for language, sentence in sample_sentences.items():
37
  st.write(f"**{language}**: {sentence}")
38
 
39
+ # User input for custom sentences
40
+ user_sentence = st.text_input("Enter your own sentence with a masked word (use '____'):", "Enter sentence here...")
41
+
42
+ # Add a submit button
43
  if st.button("Submit"):
44
+ # Prepare user input for prediction
45
+ user_masked_sentence = user_sentence.replace('____', unmasker.tokenizer.mask_token)
46
+
47
+ # Get predictions for the user input sentence
48
+ user_predictions = unmasker(user_masked_sentence)
49
 
50
+ # Display results for user input
51
+ st.write("### Your Input:")
52
+ st.write(f"Original sentence: {user_sentence}")
53
+ st.write(f"Top prediction for the masked token: {user_predictions[0]['sequence']}")
54
+
55
+ # Display results for sample sentences
56
+ st.write("### Predictions for Sample Sentences:")
57
+ for language, predictions in fill_mask_for_languages(sample_sentences).items():
58
+ original_sentence = sample_sentences[language]
59
+ predicted_sentence = predictions[0]['sequence']
60
+ st.write(f"Original sentence ({language}): {original_sentence}")
61
+ st.write(f"Top prediction for the masked token: {predicted_sentence}\n")
62
+ st.write("=" * 80)
63
 
64
+ # Custom CSS styling for Streamlit elements
65
  css = """
66
  <style>
67
  footer {display:none !important}