import streamlit as st
import pandas as pd
import streamlit.components.v1 as components
import textwrap as tw

#st.set_page_config(  initial_sidebar_state="expanded", margin_top = 20, margin_left = 20, margin_right = 10, margin_bottom=50, footer_text = "Creative Commons ... " )
st.set_page_config(page_title='Portparser', layout="wide")
##9bc2d1,#9bc2d1,#2f76a3
page_bg_img = f"""
<style>
[data-testid="stAppViewContainer"] > .main {{
background: linear-gradient(180deg, #88bbcf,#f1f1f1,#f1f1f1,#f1f1f1); /**#ccebff 10%, #f1f1f1 90%  #0088be);
padding-left:4rem;
padding-right:4rem;
background-image: url("img/nilc.png");
background-repeat: repeat;
background-position: center center;
background-attachment: local;
/**#f1f1f1/** #008fb3/**#accad2;/**#b3b3ff;**/
/**background-image: url("https://i.postimg.cc/4xgNnkfX/Untitled-design.png");
background-position: center center;
background-repeat: no-repeat;
background-attachment: local;**/
}}
[data-testid="stForm"] {{
background-color: #9bc2d1;/**#7ebac9;/**#0086b3;**/
}}
.appview-container .main .block-container {{
    padding-top: 1rem;
    padding-bottom: 3rem;
    }}
h1 {{
    color:#003d66;/**#143350**/;
    padding-left:1rem;
    padding-right:1rem;
}}
[class="css-1n543e5 e1ewe7hr5"] {{
    background-color: #ffffff /**#000066; /**#9bc2d1;/**#7ebac9;/**#0086b3;**/

}}
[class="css-1n543e5 e1ewe7hr5"]:hover {{
    background-color: #8080ff; /**#9bc2d1;/**#7ebac9;/**#0086b3;**/
    color:white;
    border: solid 1px  #000066;
}}
a:link{{
    color:#0088be;
}}
a:hover {{
    color: #7733ff/**#8080ff**/;
}}
button{{
    padding-left:1rem;
    padding-right:1rem;
    border-radius: 15%;
}}
button:hover {{
    color:#7733ff;
    border:solid 1px #7733ff;
   
}}
</style>
"""
# head style
head_css = """
<style>
[class="css-ocqkz7 esravye3"] {
    /**background-color: #9bc2d1;**/
}
[class="css-ocqkz7 esravye3"]{
    /**row1**/
    margin:0px 0px 0px 0px;
    padding:0;
}
.stApp {
    background-image: url("portparser_brasil1.jpg");
    background-repeat: repeat;
    background-position: center;
}
</style>
"""

#class="css-o7kwkx esravye0"]

a = """
<style>
div.css-10r1649 esravye0 {
    background-color: red;
}
</style>
"""
custom_html = """
<div class="banner" style="background-color:#0088be; color:white">
<h1>PortParser</h1>
    <!--<img src="https://img.freepik.com/premium-photo/wide-banner-with-many-random-square-hexagons-charcoal-dark-black-color_105589-1820.jpg" alt="Banner Image">-->
</div>
<style>
    .banner {
        width: 160%;
        height: 200px;
        overflow: hidden;
    }
    .banner img {
        width: 100%;
        object-fit: cover;
    }
</style>
"""
#<div width="449" data-testid="stVerticalBlock" class="css-10r1649 esravye0">
#components.html(custom_html)
st.markdown(page_bg_img, unsafe_allow_html=True)
st.markdown(head_css, unsafe_allow_html=True)

row2 = st.columns([6,2,3])

with row2[0]:
    st.markdown("<p style='padding-bottom:25px; padding-top:50px'><b style='font-size:calc(40px + 2vw); color:#003d66;line-height: 40px'><i>Portparser</i></b><br><b style='font-size:18px;color:#266087;line-height:4px'>\
    A parsing model for Brazilian Portuguese</b></p>",unsafe_allow_html=True)
    st.write('This is Portparser, a parsing model for Brazilian Portuguese that follows the Universal Dependencies (UD) framework.\
    We built our model by using a recently released manually annotated corpus, the Porttinari-base, \
    and we explored different parsing methods and parameters for training. We also test multiple embedding models and parsing methods. \
    Portparse is the result of the best combination achieved in our experiments.')
    st.write('Our model is explained in the paper https://aclanthology.org/2024.propor-1.41.pdf, and all datasets and full instructions to reproduce our experiments \
    freely available at https://github.com/LuceleneL/Portparser. More details about this work may also be found at \
    the POeTiSA project webpage at https://sites.google.com/icmc.usp.br/poetisa/.')
    with st.expander('How to cite?', expanded=False):
        st.code("""
        @inproceedings{lopes2024towards,
            title={Towards Portparser-a highly accurate parsing system for Brazilian Portuguese following the Universal Dependencies framework},
            author={Lopes, Lucelene and Pardo, Thiago},
            booktitle={Proceedings of the 16th International Conference on Computational Processing of Portuguese},
            pages={401--410},
            year={2024}
        }""")
    
with row2[2]:
    st.image('img/wordcloud_brasil5.png')
#wordcloud_vertical1.png


   

#st.markdown('##### Write a sentence and run to parse:')

#with st.sidebar:
#    st.header("About Portparser")
#    with st.expander('How was Portparser developed?'):
#        st.write('We build our model by using a recently released manually annotated corpus, the Porttinari-base, \
#            and explored different parsing methods and parameters for training. We also test multiple embedding models and parsing methods. \
#            Portparse is the result of the best combination achieved in our experiments.' )

print('---------------------------')

st.markdown("""
<script language="JavaScript" type="text/javascript" src="arborator-draft.js"></script>
<script language="JavaScript" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.10.0/d3.js"></script>
<script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
<link rel="stylesheet" href="arborator-draft.css" type="text/css" />
<script src="d3.js"></script>
<script src="jquery-3.2.1.min.js"></script>
<script>
new ArboratorDraft();
</script>"""
,unsafe_allow_html=True)


def make_conllu(path_text, path_input):
    try:
        os.system(f'python portTokenizer/portTok.py -o {path_input} -m -t -s S0000 {path_text}')
        return 'Converti o texto para conllu.'
        #st.text(open(path_input,'r',encoding='utf-8').read())        
    except Exception as e:
        return str(e)
    

def make_embedding(path_input, path_embedding, model_selected):
    try:
        os.system(f'python ./wembedding_service/compute_wembeddings.py {path_input} {path_embedding} --model {model_selected}')
        return 'Fiz as embeddings.'
    except Exception as e:
        return str(e)


def make_predictions(path_input, path_prediction):
    try:
        os.system(f'python ./udpipe2/udpipe2.py Portparser_model --predict --predict_input {path_input} --predict_output {path_prediction}')
        return f'Fiz a predição.'# {path_input}, {path_prediction}'
    except Exception as e:
        return str(e)


def get_predictions(path_prediction):
    try:
        with open(path_prediction, 'r') as f:
            st.text(f.read())
    except Exception as e:
        st.text('Resposta: '+e)

st.write('Write a sentence and run to parse:')
with st.form("parser"):
    text = st.text_input('Text: ')
    model = st.selectbox('Pick a model (Pick a embedding model:):', ['bert-base-portuguese-cased','bert-base-multilingual-uncased','robeczech-base','xlm-roberta-base'])
    model_selected = model+'-last4'
    submit = st.form_submit_button('Run')

tab1, tab2, tab3, tab4 = st.tabs(["Running status" ,"Table", "Raw", "Tree"])

if submit:
    import sys, os
    print(type(text))

    tab1.text('input: '+text)

    files = 'temp'
    input_text = 'text_input.txt'
    input_conllu = 'input.conllu' #'h2104_0_test.conllu'
    embedding_conllu = 'input.conllu.npz' #'h2104_0_test.conllu.npz'
    prediction_conllu = 'input_prediction.conllu'
    model = 'Portparser_model' 

    path_text = os.path.join(files, input_text)
    path_input = os.path.join(files, input_conllu)
    path_prediction = os.path.join(files, prediction_conllu)
    path_embedding = os.path.join(files,embedding_conllu)

    with open(path_text,'w',encoding='utf-8') as f:
        f.write(text)
    import time
    with st.spinner('Transforming text into .conllu...'): #st.progress(0,text="Transformando texto para o formato .conllu"):
        time.sleep(3)
        tab1.text(make_conllu(path_text, path_input))
    with st.spinner('Processing embeddings...'): #st.progress(0,text="Processando embeddings"):
        time.sleep(6)
        tab1.text(make_embedding(path_input, path_embedding, model_selected))
    with st.spinner('Making predictions...'): #st.progress(0,text="Realizando a predição"):
        time.sleep(6)
        tab1.text(make_predictions(path_input, path_prediction))

    try:
        with open(path_prediction, 'r', encoding='utf-8') as f:
            content = f.read()
            tab3.text(content)
            #tab4.markdown(f'<conll>{content[4:]}</conll>',unsafe_allow_html=True)
            content = content.split('\n')
            #tab2.text(content[:4])
            table = pd.DataFrame([line.split('\t') for line in content[4:]])
            table.columns = ['ID','FORM','LEMMA','UPOS','XPOS','FEATS','HEAD','DEPREL','DEPS','MISC']
            tab2.dataframe(table, use_container_width=True)
    except Exception as e:
            st.text('Não deu certo a predição.'+str(e)+repr(e))



row1 = st.columns([18,3,4,4])
with row1[1]:      
    st.image('img/nilc-removebg.png')
with row1[2]:
    st.image('img/poetisa2.png')
with row1[3]:      
    st.image('img/icmc.png')



st.markdown("""
<script language="JavaScript" type="text/javascript" src="arborator-draft.js"></script>
<script language="JavaScript" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.10.0/d3.js"></script>
<script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
<link rel="stylesheet" href="arborator-draft.css" type="text/css" />
<script src="d3.js"></script>
<script src="jquery-3.2.1.min.js"></script>
<script>
new ArboratorDraft();
</script>"""
,unsafe_allow_html=True)