File size: 7,040 Bytes
9b14109
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ddf8478
9b14109
ddf8478
9b14109
 
ddf8478
9b14109
 
ddf8478
9b14109
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2cb4802
 
 
 
 
9b14109
 
7a9f5f6
9b14109
 
96c8541
9b14109
96c8541
9b14109
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96c8541
9b14109
96c8541
9b14109
 
 
 
 
abb2a6b
9b14109
 
 
 
 
 
 
 
 
7a9f5f6
9b14109
 
 
 
 
abb2a6b
9b14109
 
 
 
8bda43d
9b53c1b
9b14109
9b53c1b
395e237
6ad7167
27139f7
8bda43d
 
9b14109
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7dc7c6c
 
9b14109
 
 
 
b144740
9b14109
 
 
 
b144740
9b14109
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
from wolof_translate.utils.database_manager import TranslationMongoDBManager
import streamlit as st
import pandas as pd

st.markdown("Provide your own 🤗 sentences")

# let us initialize the database manage
def get_cluster():
    
    db_manager = TranslationMongoDBManager('mongodb+srv://oumar199:[email protected]/?retryWrites=true&w=majority', 'WolofTranslation')

    return db_manager

db_manager = get_cluster()

# recuperate the already saved sentences (for the moment french/wolof sentences)
sentences_, deleted = db_manager.load_data_frames()

sentences = sentences_.copy()

# get french and wolof sentences
french_examples = pd.read_csv('wolof_translate/data/sentences/french.csv')

wolof_examples = pd.read_csv('wolof_translate/data/sentences/wolof_2.csv')

# add special characters from Wolof
sp_wolof_chars = pd.read_csv('wolof_translate/data/wolof_writing/wolof_special_chars.csv')

# add definitions
sp_wolof_words = pd.read_csv('wolof_translate/data/wolof_writing/definitions.csv')

sp_wolof_words.sort_values(by = ['french', 'wolof'], inplace = True)

# initialize the input texts
st.title("Provide sentences below ⤵️")

st.markdown("""---""")

# create three columns
left, right = st.columns(2)

# let us add a callback functions to change the input text
def add_symbol_to_french():
    
    st.session_state.left_sentence += st.session_state.fr_symbol
    
def add_symbol_to_wolof():
    
    st.session_state.right_sentence += st.session_state.wf_symbol

def add_special_token_french():
    
    st.session_state.left_sentence += '<mask>'
    
def add_special_token_wolof():
    
    st.session_state.right_sentence += '<mask>'

def add_word_to_text():
    
    word = st.session_state.word.split('/')[0].strip()
    
    st.session_state.right_sentence += word

def add_french_sentence_to_text():
    
    sentence = st.session_state.french_sentence.strip()
    
    st.session_state.left_sentence = sentence
    
def add_wolof_sentence_to_text():
    
    sentence = st.session_state.wolof_sentence.strip()
    
    st.session_state.right_sentence = sentence

# let us create a callback which permit us to add sentences inside a DataFrame
def add_new_sentences():
    
    sentence_1 = st.session_state.left_sentence.strip()
    
    sentence_2 = st.session_state.right_sentence.strip()
    
    if sentence_1 == '' or sentence_2 == '':
        
        st.warning("You didn't provide a sentence ! Please provide before submitting.", icon= "🚨")
    
    else:
        
        # insert the new sentences
        db_manager.insert_document({
            'french': sentence_1,
            'wolof': sentence_2
        })
        
        # recuperate the already saved sentences (for the moment french/wolof)
        sentences, deleted = db_manager.load_data_frames()
        
        # add the last position to delete and modify
        st.session_state.line1 = db_manager.get_max_id()
        
        st.session_state.line2 = db_manager.get_max_id()
        
# let us create a callback which permit us to add sentences inside a DataFrame
def add_new_sentences2():
    
    sentence_1 = st.session_state.left_sentence.strip()
    
    sentence_2 = st.session_state.right_sentence.strip()
    
    if sentence_1 == '' or sentence_2 == '':
        
        st.warning("You didn't provide a sentence ! Please provide before submitting.", icon= "🚨")
    
    else:
        
        # insert the new sentences
        db_manager.insert_document({
            'french': sentence_1,
            'wolof': sentence_2
        })
        
        # clean the inputs' contents
        st.session_state.left_sentence = ''
        st.session_state.right_sentence = ''
        
        # add the last position to delete and modify
        st.session_state.line1 = db_manager.get_max_id()
        
        st.session_state.line2 = db_manager.get_max_id()
        
def delete_line():
    
    number = st.session_state.line1
    
    if not number in set(sentences['_id']):
        
        st.warning("The line that you provided does not exist !")
    
    else:
        
        # delete a document
        db_manager.delete_document(number)
        
        # recuperate the already saved sentences (for the moment french/wolof)
        sentences, deleted = db_manager.load_data_frames()

def modify_line():
    
    number = st.session_state.line2
    
    if not number in set(sentences['_id']):
        
        st.warning("The line that you provided does not exist !")
    
    else:
        
        st.session_state['left_sentence'] = sentences[sentences['_id'] == number][sentences.columns.tolist()[1]].to_list()[0]
        
        st.session_state['right_sentence'] = sentences[sentences['_id'] == number][sentences.columns.tolist()[2]].to_list()[0]
        
        sentences.drop(index = sentences[sentences['_id'] == number].index, inplace=True)
        
        # add the document to update to the updated documents
        db_manager.update_document(number)

left.header("French")
left.text_area(sentences.columns.tolist()[1], key = "left_sentence")

right.header("Wolof")
right.text_area(sentences.columns.tolist()[2], key = "right_sentence")

fr_symbol = left.selectbox("French characters", key="fr_symbol", options = sp_wolof_chars['wolof_special_chars'])

wf_symbol = right.selectbox("Wolof characters", key="wf_symbol", options = sp_wolof_chars['wolof_special_chars'])

word = right.selectbox("Wolof words/Definitions", key="word", options = [sp_wolof_words.loc[i, 'wolof']+" / "+sp_wolof_words.loc[i, 'french'] for i in range(sp_wolof_words.shape[0])], on_change=add_word_to_text)

left.button("Add symbol", "add fr symbol", on_click=add_symbol_to_french)

left.button("Add mask", "french mask", on_click=add_special_token_french)

right.button("Add symbol", "add wf symbol", on_click=add_symbol_to_wolof)

right.button("Add mask", "wolof mask", on_click=add_special_token_wolof)

# add sentences at sidebar
st.sidebar.selectbox("French sentences", key="french_sentence", options = french_examples['sentences'], on_change=add_french_sentence_to_text)

st.sidebar.selectbox("Wolof sentences", key="wolof_sentence", options = wolof_examples['sentences'], on_change=add_wolof_sentence_to_text)

# add a submit button to add new sentences
st.button('Submit', 'submit_button', on_click=add_new_sentences, help="Place the new sentences without deleting them.")
st.button('Place', 'submit_button2', on_click=add_new_sentences2, help="Remove sentences when placing them inside the data set.")

st.markdown("""---""")

# add delete button
number = st.number_input("Provide line to delete", key="line1", value=db_manager.get_max_id(), min_value=0)

st.button("Delete", 'delete_button', on_click=delete_line)

# add modify button
number = st.number_input("Provide line to modify", key="line2", value=db_manager.get_max_id(), min_value=0)

st.button("Modify", 'modify_button', on_click=modify_line)

# add data frame
st.dataframe(sentences.set_index('_id'), width=900)