Translation_French_Wolof / pages /provide_sentences.py
=
adding new requirements
ddf8478
raw
history blame contribute delete
No virus
9.09 kB
import streamlit as st
import pandas as pd
st.markdown("Provide your own 🤗 sentences")
# recuperate the already saved sentences (for the moment french/wolof)
sentences_ = pd.read_csv('wolof_translate/data/sentences/wolof_french.csv')
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():
global 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:
if not sentences.empty > 0:
sentences = pd.concat((sentences, pd.DataFrame({sentences.columns.tolist()[0]: [sentence_1],
sentences.columns.tolist()[1]: [sentence_2]})))
else:
sentences = pd.DataFrame({sentences.columns.tolist()[0]: [sentence_1],
sentences.columns.tolist()[1]: [sentence_2]})
## save the result
sentences.to_csv('wolof_translate/data/sentences/wolof_french.csv', index=False)
# recuperate the already saved sentences (for the moment french/wolof)
sentences = pd.read_csv('wolof_translate/data/sentences/wolof_french.csv')
# add the last position to delete and modify
st.session_state.line1 = len(sentences) - 1
st.session_state.line2 = len(sentences) - 1
# let us create a callback which permit us to add sentences inside a DataFrame
def add_new_sentences2():
global 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:
if not sentences.empty > 0:
sentences = pd.concat((sentences, pd.DataFrame({sentences.columns.tolist()[0]: [sentence_1],
sentences.columns.tolist()[1]: [sentence_2]})))
else:
sentences = pd.DataFrame({sentences.columns.tolist()[0]: [sentence_1],
sentences.columns.tolist()[1]: [sentence_2]})
## save the result
sentences.to_csv('wolof_translate/data/sentences/wolof_french.csv', index=False)
# recuperate the already saved sentences (for the moment french/wolof)
sentences = pd.read_csv('wolof_translate/data/sentences/wolof_french.csv')
# 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 = len(sentences) - 1
st.session_state.line2 = len(sentences) - 1
def delete_line():
number = st.session_state.line1
if number > len(sentences) - 1:
st.warning("The line that you provided does not exist !")
else:
deleted = pd.read_csv('wolof_translate/data/sentences/deleted_lines.csv')
if not deleted.empty:
deleted = pd.concat((deleted, pd.DataFrame({sentences.columns.tolist()[0]: [sentences.loc[number, sentences.columns.tolist()[0]]],
sentences.columns.tolist()[1]: sentences.loc[number, sentences.columns.tolist()[1]]})))
else:
deleted = pd.DataFrame({sentences.columns.tolist()[0]: [sentences.loc[number, sentences.columns.tolist()[0]]],
sentences.columns.tolist()[1]: sentences.loc[number, sentences.columns.tolist()[1]]})
sentences.drop(index = number, inplace=True)
## save the result
sentences.to_csv('wolof_translate/data/sentences/wolof_french.csv', index=False)
deleted.to_csv('wolof_translate/data/sentences/deleted_lines.csv', index=False)
def modify_line():
number = st.session_state.line2
if number > len(sentences) - 1:
st.warning("The line that you provided does not exist !")
else:
modified = pd.read_csv('wolof_translate/data/sentences/modified_lines.csv')
if not modified.empty:
modified = pd.concat((modified, pd.DataFrame({sentences.columns.tolist()[0]: [sentences.loc[number, sentences.columns.tolist()[0]]],
sentences.columns.tolist()[1]: sentences.loc[number, sentences.columns.tolist()[1]]})))
else:
modified = pd.DataFrame({sentences.columns.tolist()[0]: [sentences.loc[number, sentences.columns.tolist()[0]]],
sentences.columns.tolist()[1]: sentences.loc[number, sentences.columns.tolist()[1]]})
st.session_state.left_sentence = sentences.loc[number, sentences.columns.tolist()[0]]
st.session_state.right_sentence = sentences.loc[number, sentences.columns.tolist()[1]]
sentences.drop(index = number, inplace=True)
## save the result
sentences.to_csv('wolof_translate/data/sentences/wolof_french.csv', index=False)
modified.to_csv('wolof_translate/data/sentences/deleted_lines.csv', index=False)
left.header("French")
left.text_area(sentences.columns.tolist()[0], key = "left_sentence")
right.header("Wolof")
right.text_area(sentences.columns.tolist()[1], 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)
st.button('Place', 'submit_button2', on_click=add_new_sentences2)
st.markdown("""---""")
# add delete button
number = st.number_input("Provide line to delete", key="line1", 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", min_value=0)
st.button("Modify", 'modify_button', on_click=modify_line)
# add data frame
st.dataframe(sentences, width=900)