import os
import markdown
import plotly.express as px
import streamlit as st
from groq import Groq
from html2docx import html2docx
from mistralai import Mistral
from streamlit_extras.add_vertical_space import add_vertical_space
from streamlit_extras.stylable_container import stylable_container
from streamlit_option_menu import option_menu
from data_cleaning import processar_arquivo
st.title(" 📊 - Análise de microárea")
ch1, ch2 = st.columns([0.6, 0.4])
with ch1:
st.markdown("""
#### 📑 Instruções:
##### 1. Site :orange[PEC SUS], relatórios, :blue[*RELATÓRIO DE CADASTRO INDIVIDUAL*]
##### 2. Clique em :orange[baixar arquivo .csv]. Faça o upload do arquivo .csv, no menu ao lado. 👈
##### 3. Solicite o relatório ao 🤖 Zé Flamengo, ou tire suas dúvidas.
###### ⏳ :red[_ATENÇÃO!_], Caso solicite o relatório, clique :blue[**uma vez**] em "Gerar Relatório e aguarde. Demora cerca de 1 minuto para ficar pronto!
""")
st.write("-----")
st.markdown("""
#### Dados de saúde Relatório de cadastro individual.""")
with ch2, stylable_container(
key="banner",
css_styles="""
img {
width: 360px;
height: 360px;
overflow: hidden;
position: relative;
object-fit: cover;
border-radius: 28px; /* Adiciona bordas arredondadas */
mask-image: linear-gradient(to bottom, rgba(0, 0, 0, 1), rgba(0, 0, 0, 0));
-webkit-mask-image: linear-gradient(to bottom, rgba(0, 0, 0, 1), rgba(0, 0, 0, 0)); /* For Safari */
}
""",
): st.image("icon/family.png")
def clear_chat_history():
"""
Clears the chat history and resets the initial analysis in the session state.
This function clears the chat history and resets the initial analysis in the session state.
It sets the value of "groq_chat_history" in the session state to an empty list and the value of "initial_analysis" to an empty string.
"""
st.session_state["groq_chat_history"] = []
st.session_state["initial_analysis"] = ""
#################################
############ DATA ###############
#################################
my_secret = os.environ['GROQ_API_KEY']
DF_DATA = None
DF_HEAD = None
DF_IDADE = None
DF_GENERO = None
DF_COR = None
DF_DEFICIENCIA = None
DF_DOENCAS = None
DF_ESCOLA = None
DF_TRANSGEN = None
uploaded_file = st.sidebar.file_uploader("Escolha um arquivo CSV", type="csv")
if uploaded_file is not None:
dataframes = processar_arquivo(uploaded_file)
DF_DATA = dataframes.get("Data")
DF_HEAD = dataframes.get("Head")
DF_IDADE = dataframes.get("Idade")
DF_GENERO = dataframes.get("genero")
DF_COR = dataframes.get("cor")
DF_DEFICIENCIA = dataframes.get("deficiencia")
DF_DOENCAS = dataframes.get("doencas")
DF_ESCOLA = dataframes.get("Escola")
DF_TRANSGEN = dataframes.get("transgen")
for df in [
DF_IDADE,
DF_GENERO,
DF_COR,
DF_DEFICIENCIA,
DF_DOENCAS,
DF_ESCOLA,
DF_TRANSGEN,
]:
if df is not None:
df["Descrição"] = df["Descrição"].str.capitalize()
else:
st.sidebar.info("Adicione um arquivo .csv.")
if st.sidebar.button("Limpar", type="primary"):
clear_chat_history()
def gerar_resumo_df():
"""
Generates a summary of the dataframes.
This function creates a summary of the dataframes by concatenating the
string representation of each dataframe.
The summary includes the name of the dataframe and its content.
Returns:
str: The summary of the dataframes.
"""
resumo = ""
if DF_DATA is not None:
resumo += "Resumo DF_DATA:\n"
resumo += DF_DATA.to_string(index=False) + "\n\n"
if DF_HEAD is not None:
resumo += "Resumo DF_HEAD:\n"
resumo += DF_HEAD.to_string(index=False) + "\n\n"
if DF_IDADE is not None:
resumo += "Resumo DF_IDADE:\n"
resumo += DF_IDADE.to_string(index=False) + "\n\n"
if DF_GENERO is not None:
resumo += f"Resumo DF_GENERO:\n{DF_GENERO.to_string(index=False)}\n\n"
if DF_COR is not None:
resumo += f"Resumo DF_COR:\n{DF_COR.to_string(index=False)}\n\n"
if DF_DEFICIENCIA is not None:
resumo += f"Resumo DF_DEFICIENCIA:\n{DF_DEFICIENCIA.to_string(index=False)}\n\n"
if DF_DOENCAS is not None:
resumo += f"Resumo DF_DOENCAS:\n{DF_DOENCAS.to_string(index=False)}\n\n"
if DF_ESCOLA is not None:
resumo += f"Resumo DF_ESCOLA:\n{DF_ESCOLA.to_string(index=False)}\n\n"
if DF_TRANSGEN is not None:
resumo += f"Resumo DF_TRANSGEN:\n{DF_TRANSGEN.to_string(index=False)}\n\n"
return resumo
###########################################
############### EXPANDERCOM GRÁFICOS ######
###########################################
def criar_grafico_personalizado(df, x_col, y_col, titulo):
"""
A function to create a customized bar chart based on the input data, x
and y columns, and title.
Parameters:
df (DataFrame): The input DataFrame containing the data.
x_col (str): The column name for the x-axis data.
y_col (str): The column name for the y-axis data.
titulo (str): The title of the chart.
Returns:
fig: The customized bar chart figure.
"""
fig = px.bar(
df,
x=x_col,
y=y_col,
title=titulo,
text=y_col,
color=x_col,
color_discrete_sequence=px.colors.qualitative.Bold,
)
fig.update_traces(texttemplate="%{text:.2s}", textposition="outside")
fig.update_layout(
uniformtext_minsize=8,
uniformtext_mode="hide",
xaxis_title=x_col,
yaxis_title=y_col,
title_font_size=24,
title_font_family="Arial",
title_font_color="white",
title_x=0.2,
paper_bgcolor="rgba(0,0,0,0)",
plot_bgcolor="rgba(0,0,0,0)",
xaxis_tickangle=-45,
)
return fig
if uploaded_file is not None:
c1, c2 = st.columns(2)
with c1:
if DF_DATA is not None:
st.dataframe(DF_DATA, hide_index=True)
else:
st.write("Data não disponível")
with c2:
if DF_HEAD is not None:
st.dataframe(DF_HEAD, hide_index=True)
else:
st.write("Informações não disponíveis")
if uploaded_file is not None:
with st.expander("Visualização", expanded=True):
selected_tab = option_menu(
menu_title=None,
options=[
"Faixa Etária",
"Gênero",
"Cor",
"Deficiência",
"Doenças",
"Escolaridade",
"Identidade de Gênero",
],
icons=[
"person",
"gender-female",
"person-plus",
"person-wheelchair",
"capsule-pill",
"stars",
"gender-trans",
],
menu_icon="cast",
default_index=0,
orientation="horizontal",
styles={
"container": {
"padding": "0!important",
"background-color": "#262730",
},
"icon": {
"color": "#4FCBFC",
"font-size": "18px"
},
"nav-link": {
"font-size": "14px",
"text-align": "center",
"margin": "0px",
"padding": "10px",
"--hover-color": "#363940",
"color": "#FFFFFF",
},
"nav-link-selected": {
"background-color": "#0083B8"
},
"separator": {
"border-color": "#4B4B4B"
},
},
)
if selected_tab == "Faixa Etária" and DF_IDADE is not None:
st.subheader("Distribuição por Faixa Etária")
col1, col2 = st.columns(2)
with col1:
st.dataframe(DF_IDADE, hide_index=True)
with col2:
DF_IDADE["Masculino"] = DF_IDADE["Masculino"].astype(int) * -1
DF_IDADE["Feminino"] = DF_IDADE["Feminino"].astype(int)
fig_idade = px.bar(
DF_IDADE,
x=["Masculino", "Feminino"],
y="Descrição",
orientation="h",
title="Pirâmide Etária",
labels={
"value": "População",
"Descrição": "Faixa Etária"
},
color="Descrição",
color_discrete_sequence=px.colors.qualitative.Set3,
)
fig_idade.update_layout(
barmode="relative",
xaxis_title="População",
yaxis_title="Faixa Etária",
)
st.plotly_chart(fig_idade)
DF_IDADE["Masculino"] = DF_IDADE["Masculino"].abs()
elif selected_tab == "Gênero" and DF_GENERO is not None:
st.subheader("Distribuição por Gênero")
col1, col2 = st.columns(2)
with col1:
st.dataframe(DF_GENERO, hide_index=True)
with col2:
fig_genero = px.pie(
DF_GENERO,
names="Descrição",
values="Valor",
title="Distribuição por Gênero",
color_discrete_sequence=px.colors.qualitative.Pastel,
)
st.plotly_chart(fig_genero)
elif selected_tab == "Cor" and DF_COR is not None:
st.subheader("Distribuição por Cor")
col1, col2 = st.columns(2)
with col1:
st.dataframe(DF_COR, hide_index=True)
with col2:
fig_cor = px.pie(
DF_COR,
names="Descrição",
values="Valor",
title="Distribuição por Cor",
color_discrete_sequence=px.colors.qualitative.Vivid,
)
st.plotly_chart(fig_cor)
elif selected_tab == "Deficiência" and DF_DEFICIENCIA is not None:
st.subheader("Distribuição por Deficiência")
col1, col2 = st.columns(2)
with col1:
st.dataframe(DF_DEFICIENCIA, hide_index=True)
with col2:
fig_deficiencia = criar_grafico_personalizado(
DF_DEFICIENCIA,
x_col="Descrição",
y_col="Valor",
titulo="Distribuição por Deficiência",
)
st.plotly_chart(fig_deficiencia)
elif selected_tab == "Doenças" and DF_DOENCAS is not None:
st.subheader("Distribuição por Doenças")
col1, col2 = st.columns(2)
with col1:
st.dataframe(DF_DOENCAS, hide_index=True)
with col2:
fig_doencas = criar_grafico_personalizado(
DF_DOENCAS,
x_col="Descrição",
y_col="Valor",
titulo="Distribuição por Doenças",
)
st.plotly_chart(fig_doencas)
elif selected_tab == "Escolaridade" and DF_ESCOLA is not None:
st.subheader("Distribuição por Escolaridade")
col1, col2 = st.columns(2)
with col1:
st.dataframe(DF_ESCOLA, hide_index=True)
with col2:
fig_escola = criar_grafico_personalizado(
DF_ESCOLA,
x_col="Descrição",
y_col="Valor",
titulo="Distribuição por Escolaridade",
)
st.plotly_chart(fig_escola)
elif selected_tab == "Identidade de Gênero" and DF_TRANSGEN is not None:
st.subheader("Distribuição por Identidade de Gênero")
col1, col2 = st.columns(2)
with col1:
st.dataframe(DF_TRANSGEN, hide_index=True)
with col2:
fig_transgen = criar_grafico_personalizado(
DF_TRANSGEN,
x_col="Descrição",
y_col="Valor",
titulo="Distribuição por Identidade de Gênero",
)
st.plotly_chart(fig_transgen)
add_vertical_space(2)
###########################################
############### FOTO DO BOT ###############
###########################################
with stylable_container(
key="bot",
css_styles="""
img {
width: 200px;
height: 260px;
overflow: hidden;
position: relative;
object-fit: cover;
border-radius: 14px; /* Adiciona bordas arredondadas */
}
""",
):
st.image("src/Images/bot.png")
###########################################
############### CHATBOT RCI ###############
###########################################
# Configuração inicial do Groq client
client = Groq(api_key=my_secret)
my_secret_mi = os.environ['MISTRAL_API_KEY']
client_mi = Mistral(api_key=my_secret_mi)
def create_word_doc(markdown_content):
# Converter o Markdown em HTML
html = markdown.markdown(markdown_content)
# Converter o HTML em um documento Word
# html2docx() retorna um objeto BytesIO
docx_file = html2docx(html, title="Análise Inicial")
# Retornar o objeto BytesIO contendo o documento Word
return docx_file
# Função para gerar a análise inicial
def generate_initial_analysis(resumo_rci):
"""
Generates an initial analysis report based on the provided resumo_rci.
Parameters:
resumo_rci (str): The resumo_rci to be analyzed.
"""
initial_analysis = client_mi.agents.complete(
agent_id="ag:493f2384:20240924:agent-rci:23eed30a",
messages=[
{
"role": "user",
"content": f"""
Dados da microárea: