# -*- coding: utf-8 -*- """ 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() # function to show base page def show_base_page(): with c1: # show title and icon to the current page add_page_title() # show text text_css = """
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.
El resultado de la predicción (sentimiento) es un numero de extrellas, entre 1 y 5.
""" st.write(text_css, unsafe_allow_html=True) # show image img = Image.open("assets/flags.jpg") st.image(img, width=200) # placeholder for hide widget global placeholder, placeholder2 placeholder = st.empty() placeholder2 = st.empty() # file upload 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') # define action funtion for button def predecir(): if uploaded_file is not None: global data_df # progress bar 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: # formatear columnas if data_df.shape[1] < 2: data_df['review'] = 0 data_df.columns = ["review","predict"] result = batch_predict_sentiment_stars(data_df) # mostrar resulyados 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) # title ax.set_title("Histograma de Frecuencias", fontsize = 8) # axis ax.set_xlim([0, 6]) # x label ax.set_xlabel('Sentimiento (Número de estrellas)', fontsize = 6) ax.set_ylabel('Frecuencia', fontsize = 6) # Crear un histograma ax.hist(data['predict'], bins=20, color ="green") # Mostrar el gráfico en Streamlit st.pyplot(fig) #------------------------------------------ # main flow #------------------------------------------ show_base_page() if button_1: predecir()