pdrMottaS's picture
Update app.py
4dc4445 verified
import streamlit as st
import pandas as pd
import requests
import os
## Label Metrics Size
st.markdown("""<style>[data-testid="stMetricValue"] {font-size: 20px;}</style>""", unsafe_allow_html=True,)
## Horizontal Radio Button
st.write('<style>div.row-widget.stRadio > div{flex-direction:row;justify-content: left;} </style>', unsafe_allow_html=True)
#st.set_page_config(page_title="The Ramsey Highlights", layout="wide")
st.markdown("""<style>[data-testid="stSidebar"][aria-expanded="true"] > div:first-child{width: 370px;}
[data-testid="stSidebar"][aria-expanded="false"] > div:first-child{width: 370px;margin-left: -370px;}""",
unsafe_allow_html=True,
)
url = "https://pdrmottas-afa-bd-sql-service.hf.space"
prof_sql = """
SELECT
prof.prof_id as prof_id,
prof.prof_identificador as prof_identificador,
CONCAT(fun.fun_primeiro_nome,' ',fun.fun_segundo_nome) as prof_nome,
prof.uni_esc_codigo_inep as codigo_inep,
prof.credenciado_para_alfabetizacao as professor_credenciado,
fun.fun_email as prof_email
FROM professor prof
LEFT JOIN funcionario fun
ON prof.fun_id = fun.fun_id
LIMIT 12
"""
sch_sql = """
SELECT
uni_esc.uni_esc_id as unidade_escolar_id,
uni_esc.uni_esc_codigo_inep as codigo_inep,
uni_esc.uni_esc_nome as unidade_escolar_nome,
uni_esc.uni_esc_tel as unidade_escolar_telefone,
tip_esc.tip_esc_categoria_administrativa as unidade_escolar_categoria_administrativa,
tip_esc.tip_esc_dependencia_administrativa as unidade_escolar_dependencia_administrativa,
tip_esc.tip_esc_porte as unidade_escolar_porte,
loc_esc.loc_esc_tip_localizacao as unidade_escolar_localizacao,
mun.mun_nome as unidade_escolar_municipio,
uf_loc.uf_nome as unidade_escolar_estado
FROM unidade_escolar uni_esc
LEFT JOIN tipo_escola tip_esc
ON uni_esc.tip_esc_id = tip_esc.tip_esc_id
LEFT JOIN localizacao_escola loc_esc
ON uni_esc.loc_esc_id = loc_esc.loc_esc_id
INNER JOIN municipio mun
ON loc_esc.mun_id = mun.mun_id
INNER JOIN uf uf_loc
ON uf_loc.uf_id = loc_esc.uf_id
LIMIT 12
"""
std_sql = """
SELECT
alu.alu_id as aluno_id,
CONCAT(alu.alu_primeiro_nome,' ',alu.alu_segundo_nome) as aluno_nome,
alu.alu_data_nascimento as aluno_data_nascimento,
alu.alu_cpf as aluno_cpf,
alu.uni_esc_codigo_inep as codigo_inep,
tip_alu.tip_alu_desc as aluno_tipo
FROM aluno alu
LEFT JOIN tipo_aluno tip_alu
ON alu.tip_alu_id = tip_alu.tip_alu_id
LIMIT 12
"""
sql_example = """SELECT alu_id, alu_nome, alu_uf, alu_cep, progresso_alfabetizacao FROM tb_students WHERE progresso_alfabetizacao >= 6 LIMIT 100"""
txt_example = """Qual um dos estudantes com maior nota no estado de SP?"""
@st.cache_resource
def refresh_data_professors():
res = requests.post(f"{url}/sql",json={"query":prof_sql})
prof_data = res.json()
df_pro = pd.DataFrame(prof_data)
return df_pro, len(prof_data)
@st.cache_resource
def refresh_data_schools():
res = requests.post(f"{url}/sql",json={"query":sch_sql})
sch_data = res.json()
sch_pro = pd.DataFrame(sch_data)
return sch_pro, len(sch_pro)
@st.cache_resource
def refresh_data_students():
res = requests.post(f"{url}/sql",json={"query":std_sql})
std_data = res.json()
df_std = pd.DataFrame(std_data)
return df_std, len(std_data)
def execute_sql(sql):
data = None
res = requests.post(f"{url}/sql",json={"query":sql})
if res.status_code == 200:
data = pd.DataFrame(res.json())
else:
st.error(res.text)
return data
def execute_llm(prompt):
data = None
res = requests.post(f"{url}/llm",json={"prompt":prompt})
if res.status_code == 200:
data = res.json()['response']
else:
st.error(res.text)
return data
st.info('Amostras de Dados:')
e1 = st.expander('Professores:', expanded=False)
with e1:
df, n = refresh_data_professors()
st.dataframe(df)
st.warning('A tabela professores possui {0} registros'.format(n))
e2 = st.expander('Escolas:', expanded=False)
with e2:
df, n = refresh_data_schools()
st.dataframe(df)
st.warning('A tabela escolas possui {0} registros'.format(n))
e3 = st.expander('Estudantes:', expanded=False)
with e3:
df, n = refresh_data_students()
st.dataframe(df)
st.warning('A tabela estudantes possui {0} registros'.format(n))
st.info('Consultas:')
e4 = st.expander('Linguagem SQL:', expanded=False)
with e4:
sql = st.text_input("Digite uma instrução SQL válida:", sql_example)
if st.button("Processar instrução"):
df = execute_sql(sql)
if df is not None:
st.dataframe(df)
st.warning('A consulta retornou {0} registros'.format(df.shape[0]))
del df
e5 = st.expander('Linguagem Natural:', expanded=False)
with e5:
txt = st.text_input("Digite um texto de consulta válido:", txt_example)
if st.button("Processar texto"):
text = execute_llm(txt)
if text is not None:
st.text(text)
del df