|
|
|
""" |
|
Created on Sat Dec 23 13:22:00 2023 |
|
@author: Essio Rubin C. |
|
""" |
|
from PIL import Image |
|
import streamlit as st |
|
from st_pages import add_page_title |
|
import time |
|
import pandas as pd |
|
import matplotlib.pyplot as plt |
|
from as_bert_df import batch_predict_sentiment_stars |
|
|
|
c1 = st.container() |
|
c2 = st.container() |
|
|
|
|
|
def show_base_page(): |
|
with c1: |
|
|
|
add_page_title() |
|
|
|
|
|
text_css = """ |
|
<p style="font-family: Verdana; font-size: 15px; font-weight: 400; ; color: #5b5f62;"> |
|
El archivo de reseñas debe ser un archivo de texto que contenga una reseña por cada línea. |
|
La reseña puede estar escrita en idioma español, holandés, italiano, alemán, francés o inglés. |
|
</p> |
|
<p style="font-family: Verdana; font-size: 15px; font-weight: 400; ; color: #5b5f62;"> |
|
El resultado de la predicción (sentimiento) es un numero de extrellas, entre 1 y 5. |
|
<ul style="font-family: Verdana; font-size: 15px; font-weight: 400; ; color: #5b5f62;"> |
|
<li>1 estrellas: DEPLORABLE</li> |
|
<li>2 estrellas: MALO</li> |
|
<li>3 estrellas: NEUTRAL</li> |
|
<li>4 estrellas: BUENO</li> |
|
<li>5 estrellas: EXCELENTE</li> |
|
<ul> |
|
</p> |
|
<p> </p> |
|
""" |
|
st.write(text_css, unsafe_allow_html=True) |
|
|
|
|
|
img = Image.open("assets/flags.jpg") |
|
st.image(img, width=200) |
|
|
|
|
|
global placeholder, placeholder2 |
|
placeholder = st.empty() |
|
placeholder2 = st.empty() |
|
|
|
|
|
global uploaded_file |
|
uploaded_file = placeholder.file_uploader("Cargar archivo de reseñas ...", type=['txt', 'csv'], key='uploader') |
|
|
|
global button_1 |
|
button_1 = placeholder2.button(" :gear: Predecir", type="primary", key='but_1') |
|
|
|
|
|
|
|
|
|
def predecir(): |
|
if uploaded_file is not None: |
|
global data_df |
|
|
|
progress_text = "Procesando. Espere." |
|
my_bar = st.progress(0, text=progress_text) |
|
|
|
for percent_complete in range(100): |
|
time.sleep(0.01) |
|
my_bar.progress(percent_complete + 1, text=progress_text) |
|
time.sleep(1) |
|
my_bar.empty() |
|
|
|
data_df = pd.read_csv(uploaded_file, sep=";") |
|
if data_df.shape[0] > 0: |
|
|
|
if data_df.shape[1] < 2: |
|
data_df['review'] = 0 |
|
data_df.columns = ["review","predict"] |
|
|
|
result = batch_predict_sentiment_stars(data_df) |
|
|
|
|
|
show_data_editor(result) |
|
show_chart(result) |
|
placeholder.empty() |
|
placeholder2.empty() |
|
else: |
|
st.error("Archivo se encuentra vacío.") |
|
else: |
|
st.error("Debe subir un archivo de texto") |
|
|
|
def show_data_editor(data_df): |
|
with c2: |
|
st.data_editor( |
|
data_df, |
|
column_config={ |
|
"review": st.column_config.TextColumn( |
|
"review", |
|
help="Ingrese la reseña.", |
|
width="large", |
|
required=True, |
|
max_chars=500, |
|
validate="[a-zA-Z]+$" |
|
), |
|
"predict": st.column_config.NumberColumn( |
|
"predict", |
|
help="Sentimiento expresado en cantidad de estrellas.", |
|
width="small", |
|
required=False, |
|
default=0, |
|
min_value=0, |
|
max_value=5, |
|
format="%d ⭐", |
|
) |
|
}, |
|
hide_index=True, |
|
num_rows="dynamic", |
|
height = 260, |
|
width = 900, |
|
) |
|
|
|
|
|
|
|
def show_chart(data): |
|
fig, ax = plt.subplots() |
|
fig.set_figwidth(5) |
|
fig.set_figheight(3) |
|
|
|
ax.set_title("Histograma de Frecuencias", fontsize = 8) |
|
|
|
|
|
ax.set_xlim([0, 6]) |
|
|
|
|
|
ax.set_xlabel('Sentimiento (Número de estrellas)', fontsize = 6) |
|
|
|
ax.set_ylabel('Frecuencia', fontsize = 6) |
|
|
|
|
|
ax.hist(data['predict'], bins=20, color ="green") |
|
|
|
|
|
st.pyplot(fig) |
|
|
|
|
|
|
|
|
|
show_base_page() |
|
|
|
if button_1: |
|
predecir() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|