File size: 5,185 Bytes
5693fc3
b0f862d
136504a
b46b626
b0f862d
58e48ed
 
 
 
 
 
 
 
 
 
 
 
136504a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5693fc3
72a1ca8
daac352
 
 
58e48ed
1d951a6
136504a
 
 
 
b0f862d
58e48ed
1d951a6
136504a
 
 
 
8346666
58e48ed
1d951a6
136504a
 
 
 
b0f862d
72a1ca8
 
136504a
 
 
 
 
 
 
 
 
 
 
 
 
 
72a1ca8
 
daac352
58e48ed
 
daac352
 
 
58e48ed
 
 
daac352
 
 
b0f862d
58e48ed
 
daac352
 
 
 
 
 
 
 
3b8eb2f
72a1ca8
 
 
297bc74
 
daac352
 
 
 
3b8eb2f
136504a
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
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