Translation_French_Wolof / pages /provide_sentences_mongo.py
=
modify sentences
af4ae78
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:Jacksparrow360@woloftranslationcluster.u0gk7.mongodb.net/?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)