jcmachicao
commited on
Commit
•
ed06be6
1
Parent(s):
9f312ed
Upload app.py
Browse files
app.py
CHANGED
@@ -12,6 +12,7 @@ from torch.utils.data import DataLoader, TensorDataset
|
|
12 |
import plotly.express as px
|
13 |
import streamlit as st
|
14 |
from es_class_nn import SimplePlusNN2
|
|
|
15 |
version_name = 'CON_44'
|
16 |
|
17 |
c1, c2 = st.columns([6,6])
|
@@ -23,52 +24,134 @@ st.write("Cargue el archivo PKL para visualizar el análisis de su contenido.")
|
|
23 |
|
24 |
st.write('Cargue el archivo con datos nuevos aqui. Este archivo deberá seguir las pautas del diccionario de categorías y deberá estar en formato XLSX')
|
25 |
st.write('En caso no conozca el diccionario descarguelo aquí.')
|
26 |
-
st.link_button('https://huggingface.co/spaces/gestiodinamica/continental_predictivo/resolve/main/auxiliares/diccionario_variables_pkl_train.txt?download=true')
|
27 |
|
28 |
uploaded_file = st.file_uploader("Cargar archivo: ", type='xlsx')
|
29 |
|
30 |
-
|
31 |
-
df_categ = pd.read_excel('lista_categorias.xlsx')
|
32 |
|
33 |
if uploaded_file is not None:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
-
|
36 |
-
|
37 |
-
verif = df_test.columns == df_muestra.columns
|
38 |
-
st.write(verif.sum())
|
39 |
-
df_scaled = pd.concat([df_muestra, df_test], axis=0, ignore_index=True)
|
40 |
-
df_scaled = df_scaled.fillna(0)
|
41 |
-
df = df_scaled.tail(len(df_test)).reset_index(drop=True)
|
42 |
-
st.write(df)
|
43 |
-
X = df.values
|
44 |
-
X_test_tensor = torch.tensor(X.astype(np.float32), dtype=torch.float32)
|
45 |
|
46 |
-
|
47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
num_classes = 2
|
|
|
49 |
model = SimplePlusNN2(input_size, num_classes)
|
50 |
-
data_path = ''
|
51 |
dict_name = f'edusights_20240702_state_dict_{version_name}.pth'
|
|
|
52 |
model.load_state_dict(torch.load(data_path+dict_name))
|
53 |
model.eval()
|
54 |
-
|
55 |
-
|
56 |
-
inputs = X_test_tensor
|
57 |
-
outputs = model(inputs)
|
58 |
outputs_show = outputs.detach().numpy().flatten()
|
59 |
-
|
60 |
-
outputs_show[outputs_show
|
|
|
61 |
filtered_arr = outputs_show[(outputs_show == 0.0) | (outputs_show == 1.0)]
|
62 |
-
|
63 |
-
st.write(
|
64 |
-
|
65 |
-
|
|
|
|
|
|
|
66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
st.download_button(
|
68 |
-
label="Descargar
|
69 |
-
|
70 |
-
file_name='predicciones_carga.csv',
|
71 |
-
mime='text/csv'
|
72 |
)
|
73 |
|
74 |
c3, c4 = st.columns([6,6])
|
|
|
12 |
import plotly.express as px
|
13 |
import streamlit as st
|
14 |
from es_class_nn import SimplePlusNN2
|
15 |
+
from prediction_analyzer import PredictionAnalyzer
|
16 |
version_name = 'CON_44'
|
17 |
|
18 |
c1, c2 = st.columns([6,6])
|
|
|
24 |
|
25 |
st.write('Cargue el archivo con datos nuevos aqui. Este archivo deberá seguir las pautas del diccionario de categorías y deberá estar en formato XLSX')
|
26 |
st.write('En caso no conozca el diccionario descarguelo aquí.')
|
27 |
+
st.link_button('Diccionario', 'https://huggingface.co/spaces/gestiodinamica/continental_predictivo/resolve/main/auxiliares/diccionario_variables_pkl_train.txt?download=true')
|
28 |
|
29 |
uploaded_file = st.file_uploader("Cargar archivo: ", type='xlsx')
|
30 |
|
31 |
+
df_categ = pd.read_excel('auxiliares/lista_categorias_rev.xlsx')
|
|
|
32 |
|
33 |
if uploaded_file is not None:
|
34 |
+
|
35 |
+
df_2 = pd.read_excel(uploaded_file)
|
36 |
+
|
37 |
+
# Procesamiento de archivo listo para predicción
|
38 |
+
df_2 = df_2.dropna(axis=0)
|
39 |
+
df_2.index = df_2.DNI
|
40 |
+
df_2.drop(columns=['DNI'], inplace=True)
|
41 |
+
st.write('Dimensiones de archivo ingresado: ', df_2.shape)
|
42 |
+
|
43 |
+
cat_list = []
|
44 |
+
num_list = []
|
45 |
+
for col in df_2.columns:
|
46 |
+
if df_2[col].dtype == 'object':
|
47 |
+
if len(df_2[col].unique()) < 35:
|
48 |
+
cat_list.append(col)
|
49 |
+
else:
|
50 |
+
num_list.append(col)
|
51 |
+
|
52 |
+
df_a = df_2[cat_list]
|
53 |
+
df_b = pd.get_dummies(df_a)
|
54 |
+
df_b = df_b.set_index(df_2.index)
|
55 |
+
#df_b.index
|
56 |
+
scaler = MinMaxScaler()
|
57 |
+
X_sc = scaler.fit_transform(df_b)
|
58 |
+
df_sc = pd.DataFrame(X_sc)
|
59 |
+
df_sc.columns = df_b.columns
|
60 |
+
df_n = df_2[num_list]
|
61 |
+
df_n = df_n.set_index(df_2.index)
|
62 |
+
#df_n.index
|
63 |
+
df_sc.index = df_b.index
|
64 |
+
if (df_sc.index == df_n.index).sum()==len(df_2):
|
65 |
+
st.write('Indices Consistentes Verificados.')
|
66 |
+
df_r = pd.concat([df_sc, df_n], axis=1)
|
67 |
+
st.write('Dimensiones del archivo procesado: ', df_r.shape)
|
68 |
+
|
69 |
|
70 |
+
list_df_modelo = pd.read_excel('auxiliares/lista_categorias_CON_44_v0.xlsx')
|
71 |
+
lista_categorias = list_df_modelo[0].to_list()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
|
73 |
+
cats_ad = []
|
74 |
+
cats_res = []
|
75 |
+
for c in df_r.columns:
|
76 |
+
if c not in lista_categorias:
|
77 |
+
cats_ad.append(c)
|
78 |
+
else:
|
79 |
+
cats_res.append(c)
|
80 |
+
|
81 |
+
cats_dis = []
|
82 |
+
cats_res2 = []
|
83 |
+
for c in lista_categorias:
|
84 |
+
if c not in df_r.columns:
|
85 |
+
cats_dis.append(c)
|
86 |
+
else:
|
87 |
+
cats_res2.append(c)
|
88 |
+
|
89 |
+
df_ad = pd.DataFrame(0, index=range(140), columns=cats_dis)
|
90 |
+
df_ad.index = df_r.index
|
91 |
+
df_base = df_r[cats_res]
|
92 |
+
df_base.index = df_r.index
|
93 |
+
df_tot = pd.concat([df_base, df_ad], axis=1)
|
94 |
+
|
95 |
+
cats_test = []
|
96 |
+
for c in df_tot.columns:
|
97 |
+
if c in lista_categorias:
|
98 |
+
cats_test.append(c)
|
99 |
+
|
100 |
+
cats_final = []
|
101 |
+
for c in lista_categorias:
|
102 |
+
if c[0:6]=='ESTADO':
|
103 |
+
print(c)
|
104 |
+
else:
|
105 |
+
cats_final.append(c)
|
106 |
+
|
107 |
+
df_ready = df_tot[cats_final]
|
108 |
+
#df_ready.shape
|
109 |
+
#st.write(df_ready.index[0:10])
|
110 |
+
st.write('Dimensiones del archivo para predicción: ', df_ready.shape)
|
111 |
+
|
112 |
+
DROPOUTX = 0.10
|
113 |
+
version_name = 'CON_44'
|
114 |
+
X_train = df_ready.values
|
115 |
+
X_train_tensor = torch.tensor(X_train, dtype=torch.float32)
|
116 |
+
st.write('Dimensiones del tensor: ', X_train_tensor.shape)
|
117 |
+
input_size = X_train_tensor.shape[1]
|
118 |
num_classes = 2
|
119 |
+
|
120 |
model = SimplePlusNN2(input_size, num_classes)
|
121 |
+
data_path = 'models/'
|
122 |
dict_name = f'edusights_20240702_state_dict_{version_name}.pth'
|
123 |
+
st.write(dict_name)
|
124 |
model.load_state_dict(torch.load(data_path+dict_name))
|
125 |
model.eval()
|
126 |
+
|
127 |
+
outputs = model(X_train_tensor)
|
|
|
|
|
128 |
outputs_show = outputs.detach().numpy().flatten()
|
129 |
+
st.write('Salida: ', outputs_show.shape)
|
130 |
+
outputs_show[outputs_show > 0.51] = 1.0
|
131 |
+
outputs_show[outputs_show < 0.49] = 0.0
|
132 |
filtered_arr = outputs_show[(outputs_show == 0.0) | (outputs_show == 1.0)]
|
133 |
+
unique, counts = np.unique(filtered_arr, return_counts=True)
|
134 |
+
st.write(unique, counts)
|
135 |
+
pred_df = pd.DataFrame(filtered_arr)
|
136 |
+
st.write('Dimensión de predicciones totales: ', pred_df.shape)
|
137 |
+
df_ready_2 = df_ready.copy()
|
138 |
+
df_ready_2['Predicción'] = pred_df
|
139 |
+
st.write(df_ready_2.head())
|
140 |
|
141 |
+
analyzer = PredictionAnalyzer(model, df_ready)
|
142 |
+
results_df = analyzer.predictions_loop()
|
143 |
+
st.write(results_df)
|
144 |
+
|
145 |
+
csv_out = df_ready_2.to_csv(encoding='iso-8859-1')
|
146 |
+
st.download_button(
|
147 |
+
label="Descargar Predicciones Totales", data=csv_out,
|
148 |
+
file_name='predicciones_carga.csv', mime='text/csv'
|
149 |
+
)
|
150 |
+
|
151 |
+
csv_out = results_df.to_csv(encoding='iso-8859-1')
|
152 |
st.download_button(
|
153 |
+
label="Descargar Predicciones por Categoría", data=csv_out,
|
154 |
+
file_name='predicciones_carga.csv', mime='text/csv'
|
|
|
|
|
155 |
)
|
156 |
|
157 |
c3, c4 = st.columns([6,6])
|