Rename app_4.py to app.py
Browse files- app_4.py → app.py +134 -114
app_4.py → app.py
RENAMED
|
@@ -110,107 +110,6 @@ def gerar_sugestoes(segmentos, numero):
|
|
| 110 |
return ", ".join(map(str, sugestoes))
|
| 111 |
|
| 112 |
|
| 113 |
-
def interpolar(df, coluna_cdlog, coluna_num):
|
| 114 |
-
|
| 115 |
-
df = df.copy()
|
| 116 |
-
df[coluna_num] = pd.to_numeric(df[coluna_num], errors="coerce").fillna(0).astype(int)
|
| 117 |
-
|
| 118 |
-
lons = []
|
| 119 |
-
lats = []
|
| 120 |
-
falhas = []
|
| 121 |
-
|
| 122 |
-
for _, row in df.iterrows():
|
| 123 |
-
|
| 124 |
-
idx = row["_idx"]
|
| 125 |
-
cdlog = row[coluna_cdlog]
|
| 126 |
-
numero = row[coluna_num]
|
| 127 |
-
|
| 128 |
-
segmentos = eixos_por_cdlog.get(cdlog)
|
| 129 |
-
|
| 130 |
-
if segmentos is None:
|
| 131 |
-
lons.append(None)
|
| 132 |
-
lats.append(None)
|
| 133 |
-
|
| 134 |
-
falhas.append({
|
| 135 |
-
"_idx": idx,
|
| 136 |
-
"CDLOG": cdlog,
|
| 137 |
-
"Numero Atual": numero,
|
| 138 |
-
"Motivo": "CDLOG não encontrado",
|
| 139 |
-
"Sugestões": "",
|
| 140 |
-
"Novo Número": None
|
| 141 |
-
})
|
| 142 |
-
continue
|
| 143 |
-
|
| 144 |
-
if numero % 2 == 0:
|
| 145 |
-
cond = (segmentos["NRPARINI"] <= numero) & (segmentos["NRPARFIN"] >= numero)
|
| 146 |
-
else:
|
| 147 |
-
cond = (segmentos["NRIMPINI"] <= numero) & (segmentos["NRIMPFIN"] >= numero)
|
| 148 |
-
|
| 149 |
-
seg_validos = segmentos[cond]
|
| 150 |
-
|
| 151 |
-
if seg_validos.empty:
|
| 152 |
-
|
| 153 |
-
sugestoes = gerar_sugestoes(segmentos, numero)
|
| 154 |
-
|
| 155 |
-
lons.append(None)
|
| 156 |
-
lats.append(None)
|
| 157 |
-
|
| 158 |
-
falhas.append({
|
| 159 |
-
"_idx": idx,
|
| 160 |
-
"CDLOG": cdlog,
|
| 161 |
-
"Numero Atual": numero,
|
| 162 |
-
"Motivo": "Fora do intervalo",
|
| 163 |
-
"Sugestões": sugestoes,
|
| 164 |
-
"Novo Número": None
|
| 165 |
-
})
|
| 166 |
-
continue
|
| 167 |
-
|
| 168 |
-
linha = seg_validos.iloc[0]
|
| 169 |
-
geom = linha.geometry
|
| 170 |
-
|
| 171 |
-
if numero % 2 == 0:
|
| 172 |
-
ini = linha["NRPARINI"]
|
| 173 |
-
fim = linha["NRPARFIN"]
|
| 174 |
-
else:
|
| 175 |
-
ini = linha["NRIMPINI"]
|
| 176 |
-
fim = linha["NRIMPFIN"]
|
| 177 |
-
|
| 178 |
-
frac = (numero - ini) / (fim - ini)
|
| 179 |
-
frac = max(0, min(1, frac))
|
| 180 |
-
ponto = geom.interpolate(geom.length * frac)
|
| 181 |
-
|
| 182 |
-
lons.append(ponto.x)
|
| 183 |
-
lats.append(ponto.y)
|
| 184 |
-
|
| 185 |
-
df["lon"] = lons
|
| 186 |
-
df["lat"] = lats
|
| 187 |
-
|
| 188 |
-
output_path = "dados_interpolados.xlsx"
|
| 189 |
-
df.to_excel(output_path, index=False)
|
| 190 |
-
|
| 191 |
-
# 🔴 GARANTIR ESTRUTURA FIXA
|
| 192 |
-
if len(falhas) == 0:
|
| 193 |
-
df_falhas = pd.DataFrame(columns=[
|
| 194 |
-
"_idx",
|
| 195 |
-
"CDLOG",
|
| 196 |
-
"Numero Atual",
|
| 197 |
-
"Motivo",
|
| 198 |
-
"Sugestões",
|
| 199 |
-
"Novo Número"
|
| 200 |
-
])
|
| 201 |
-
else:
|
| 202 |
-
df_falhas = pd.DataFrame(falhas)[[
|
| 203 |
-
"_idx",
|
| 204 |
-
"CDLOG",
|
| 205 |
-
"Numero Atual",
|
| 206 |
-
"Motivo",
|
| 207 |
-
"Sugestões",
|
| 208 |
-
"Novo Número"
|
| 209 |
-
]]
|
| 210 |
-
|
| 211 |
-
return formatar_float_visualizacao(df), output_path, df_falhas
|
| 212 |
-
|
| 213 |
-
|
| 214 |
# --------------------------------------------------
|
| 215 |
# Motor de Interpolação
|
| 216 |
# --------------------------------------------------
|
|
@@ -263,12 +162,56 @@ def interpolar(df, coluna_cdlog, coluna_num):
|
|
| 263 |
|
| 264 |
seg_validos = segmentos[cond]
|
| 265 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 266 |
# --------------------------------------------------
|
| 267 |
# Número fora do intervalo
|
| 268 |
# --------------------------------------------------
|
| 269 |
if seg_validos.empty:
|
| 270 |
|
| 271 |
-
|
|
|
|
|
|
|
| 272 |
|
| 273 |
if not segmentos.empty:
|
| 274 |
|
|
@@ -289,7 +232,12 @@ def interpolar(df, coluna_cdlog, coluna_num):
|
|
| 289 |
fim = linha_proxima[fim_col]
|
| 290 |
|
| 291 |
if pd.notna(ini) and pd.notna(fim):
|
| 292 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 293 |
|
| 294 |
lons.append(None)
|
| 295 |
lats.append(None)
|
|
@@ -299,8 +247,9 @@ def interpolar(df, coluna_cdlog, coluna_num):
|
|
| 299 |
"CDLOG": cdlog,
|
| 300 |
"Numero Atual": numero,
|
| 301 |
"Motivo": "Numeração fora do intervalo",
|
| 302 |
-
"
|
| 303 |
-
"
|
|
|
|
| 304 |
})
|
| 305 |
|
| 306 |
continue
|
|
@@ -338,13 +287,23 @@ def interpolar(df, coluna_cdlog, coluna_num):
|
|
| 338 |
# --------------------------------------------------
|
| 339 |
# Garantir estrutura fixa das falhas
|
| 340 |
# --------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 341 |
colunas_fixas = [
|
| 342 |
"_idx",
|
| 343 |
"CDLOG",
|
| 344 |
"Numero Atual",
|
| 345 |
"Motivo",
|
| 346 |
-
"
|
| 347 |
-
"
|
|
|
|
| 348 |
]
|
| 349 |
|
| 350 |
if len(falhas) == 0:
|
|
@@ -355,6 +314,40 @@ def interpolar(df, coluna_cdlog, coluna_num):
|
|
| 355 |
|
| 356 |
return formatar_float_visualizacao(df), output_path, df_falhas
|
| 357 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 358 |
|
| 359 |
# --------------------------------------------------
|
| 360 |
# Aplicar Correções
|
|
@@ -374,6 +367,7 @@ def aplicar_correcoes(df_original, df_falhas, coluna_num):
|
|
| 374 |
return df_original
|
| 375 |
|
| 376 |
|
|
|
|
| 377 |
# --------------------------------------------------
|
| 378 |
# Interface
|
| 379 |
# --------------------------------------------------
|
|
@@ -397,10 +391,23 @@ with gr.Blocks() as app:
|
|
| 397 |
|
| 398 |
falhas = gr.Dataframe(label="Falhas (Editar Novo Número)", interactive=True)
|
| 399 |
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 404 |
|
| 405 |
dropdown_abas.change(
|
| 406 |
fn=exibir_tabela,
|
|
@@ -418,9 +425,20 @@ with gr.Blocks() as app:
|
|
| 418 |
outputs=[tabela_resultado, arquivo_saida, falhas]
|
| 419 |
)
|
| 420 |
|
| 421 |
-
|
| 422 |
-
fn=
|
| 423 |
-
inputs=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 424 |
outputs=tabela
|
| 425 |
).then(
|
| 426 |
fn=interpolar,
|
|
@@ -428,4 +446,6 @@ with gr.Blocks() as app:
|
|
| 428 |
outputs=[tabela_resultado, arquivo_saida, falhas]
|
| 429 |
)
|
| 430 |
|
|
|
|
|
|
|
| 431 |
app.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 110 |
return ", ".join(map(str, sugestoes))
|
| 111 |
|
| 112 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
# --------------------------------------------------
|
| 114 |
# Motor de Interpolação
|
| 115 |
# --------------------------------------------------
|
|
|
|
| 162 |
|
| 163 |
seg_validos = segmentos[cond]
|
| 164 |
|
| 165 |
+
# # --------------------------------------------------
|
| 166 |
+
# # Número fora do intervalo
|
| 167 |
+
# # --------------------------------------------------
|
| 168 |
+
# if seg_validos.empty:
|
| 169 |
+
|
| 170 |
+
# sugestao_formatada = ""
|
| 171 |
+
|
| 172 |
+
# if not segmentos.empty:
|
| 173 |
+
|
| 174 |
+
# if numero % 2 == 0:
|
| 175 |
+
# diffs = (segmentos["NRPARINI"] - numero).abs()
|
| 176 |
+
# ini_col = "NRPARINI"
|
| 177 |
+
# fim_col = "NRPARFIN"
|
| 178 |
+
# else:
|
| 179 |
+
# diffs = (segmentos["NRIMPINI"] - numero).abs()
|
| 180 |
+
# ini_col = "NRIMPINI"
|
| 181 |
+
# fim_col = "NRIMPFIN"
|
| 182 |
+
|
| 183 |
+
# if not diffs.empty:
|
| 184 |
+
# min_index = diffs.idxmin()
|
| 185 |
+
# linha_proxima = segmentos.loc[min_index]
|
| 186 |
+
|
| 187 |
+
# ini = linha_proxima[ini_col]
|
| 188 |
+
# fim = linha_proxima[fim_col]
|
| 189 |
+
|
| 190 |
+
# if pd.notna(ini) and pd.notna(fim):
|
| 191 |
+
# sugestao_formatada = f"Intervalo válido: {int(ini)} até {int(fim)}"
|
| 192 |
+
|
| 193 |
+
# lons.append(None)
|
| 194 |
+
# lats.append(None)
|
| 195 |
+
|
| 196 |
+
# falhas.append({
|
| 197 |
+
# "_idx": idx,
|
| 198 |
+
# "CDLOG": cdlog,
|
| 199 |
+
# "Numero Atual": numero,
|
| 200 |
+
# "Motivo": "Numeração fora do intervalo",
|
| 201 |
+
# "Sugestões": sugestao_formatada,
|
| 202 |
+
# "Novo Número": None
|
| 203 |
+
# })
|
| 204 |
+
|
| 205 |
+
# continue
|
| 206 |
+
|
| 207 |
# --------------------------------------------------
|
| 208 |
# Número fora do intervalo
|
| 209 |
# --------------------------------------------------
|
| 210 |
if seg_validos.empty:
|
| 211 |
|
| 212 |
+
sugestoes_lista = []
|
| 213 |
+
lado = "Par" if numero % 2 == 0 else "Ímpar"
|
| 214 |
+
intervalo_texto = ""
|
| 215 |
|
| 216 |
if not segmentos.empty:
|
| 217 |
|
|
|
|
| 232 |
fim = linha_proxima[fim_col]
|
| 233 |
|
| 234 |
if pd.notna(ini) and pd.notna(fim):
|
| 235 |
+
|
| 236 |
+
intervalo_texto = f"{int(ini)} até {int(fim)}"
|
| 237 |
+
|
| 238 |
+
numeros_validos = list(range(int(ini), int(fim) + 1, 2))
|
| 239 |
+
numeros_validos.sort(key=lambda x: abs(x - numero))
|
| 240 |
+
sugestoes_lista = numeros_validos[:10]
|
| 241 |
|
| 242 |
lons.append(None)
|
| 243 |
lats.append(None)
|
|
|
|
| 247 |
"CDLOG": cdlog,
|
| 248 |
"Numero Atual": numero,
|
| 249 |
"Motivo": "Numeração fora do intervalo",
|
| 250 |
+
"Lado": lado,
|
| 251 |
+
"Intervalo": intervalo_texto,
|
| 252 |
+
"Sugestões": sugestoes_lista,
|
| 253 |
})
|
| 254 |
|
| 255 |
continue
|
|
|
|
| 287 |
# --------------------------------------------------
|
| 288 |
# Garantir estrutura fixa das falhas
|
| 289 |
# --------------------------------------------------
|
| 290 |
+
# colunas_fixas = [
|
| 291 |
+
# "_idx",
|
| 292 |
+
# "CDLOG",
|
| 293 |
+
# "Numero Atual",
|
| 294 |
+
# "Motivo",
|
| 295 |
+
# "Sugestões",
|
| 296 |
+
# "Novo Número"
|
| 297 |
+
# ]
|
| 298 |
+
|
| 299 |
colunas_fixas = [
|
| 300 |
"_idx",
|
| 301 |
"CDLOG",
|
| 302 |
"Numero Atual",
|
| 303 |
"Motivo",
|
| 304 |
+
"Lado",
|
| 305 |
+
"Intervalo",
|
| 306 |
+
"Sugestões"
|
| 307 |
]
|
| 308 |
|
| 309 |
if len(falhas) == 0:
|
|
|
|
| 314 |
|
| 315 |
return formatar_float_visualizacao(df), output_path, df_falhas
|
| 316 |
|
| 317 |
+
# --------------------------------------------------
|
| 318 |
+
# Preparar sugestões
|
| 319 |
+
# --------------------------------------------------
|
| 320 |
+
|
| 321 |
+
def preparar_sugestoes(df_falhas, evt: gr.SelectData):
|
| 322 |
+
|
| 323 |
+
linha = df_falhas.iloc[evt.index[0]]
|
| 324 |
+
|
| 325 |
+
idx = linha["_idx"]
|
| 326 |
+
lado = linha["Lado"]
|
| 327 |
+
intervalo = linha["Intervalo"]
|
| 328 |
+
sugestoes = linha["Sugestões"]
|
| 329 |
+
|
| 330 |
+
return (
|
| 331 |
+
idx,
|
| 332 |
+
lado,
|
| 333 |
+
intervalo,
|
| 334 |
+
gr.update(choices=sugestoes, value=None)
|
| 335 |
+
)
|
| 336 |
+
|
| 337 |
+
|
| 338 |
+
# --------------------------------------------------
|
| 339 |
+
# Aplicar Sugestões
|
| 340 |
+
# --------------------------------------------------
|
| 341 |
+
def aplicar_sugestao(df_original, idx, numero_escolhido, coluna_num):
|
| 342 |
+
|
| 343 |
+
if numero_escolhido is None:
|
| 344 |
+
return df_original
|
| 345 |
+
|
| 346 |
+
df_original = df_original.copy()
|
| 347 |
+
|
| 348 |
+
df_original.loc[df_original["_idx"] == idx, coluna_num] = int(numero_escolhido)
|
| 349 |
+
|
| 350 |
+
return df_original
|
| 351 |
|
| 352 |
# --------------------------------------------------
|
| 353 |
# Aplicar Correções
|
|
|
|
| 367 |
return df_original
|
| 368 |
|
| 369 |
|
| 370 |
+
|
| 371 |
# --------------------------------------------------
|
| 372 |
# Interface
|
| 373 |
# --------------------------------------------------
|
|
|
|
| 391 |
|
| 392 |
falhas = gr.Dataframe(label="Falhas (Editar Novo Número)", interactive=True)
|
| 393 |
|
| 394 |
+
gr.Markdown("### Correção Assistida")
|
| 395 |
+
idx_selecionado = gr.Number(visible=False)
|
| 396 |
+
info_lado = gr.Textbox(label="Lado", interactive=False)
|
| 397 |
+
info_intervalo = gr.Textbox(label="Intervalo Válido", interactive=False)
|
| 398 |
+
radio_sugestoes = gr.Radio(label="Escolha o Número Correto")
|
| 399 |
+
btn_aplicar_sugestao = gr.Button("Aplicar Sugestão")
|
| 400 |
+
|
| 401 |
+
|
| 402 |
+
# =============================
|
| 403 |
+
# EVENTOS
|
| 404 |
+
# =============================
|
| 405 |
+
|
| 406 |
+
arquivo.change(
|
| 407 |
+
fn=carregar_abas,
|
| 408 |
+
inputs=arquivo,
|
| 409 |
+
outputs=[dropdown_abas, dropdown_abas]
|
| 410 |
+
)
|
| 411 |
|
| 412 |
dropdown_abas.change(
|
| 413 |
fn=exibir_tabela,
|
|
|
|
| 425 |
outputs=[tabela_resultado, arquivo_saida, falhas]
|
| 426 |
)
|
| 427 |
|
| 428 |
+
falhas.select(
|
| 429 |
+
fn=preparar_sugestoes,
|
| 430 |
+
inputs=falhas,
|
| 431 |
+
outputs=[
|
| 432 |
+
idx_selecionado,
|
| 433 |
+
info_lado,
|
| 434 |
+
info_intervalo,
|
| 435 |
+
radio_sugestoes
|
| 436 |
+
]
|
| 437 |
+
)
|
| 438 |
+
|
| 439 |
+
btn_aplicar_sugestao.click(
|
| 440 |
+
fn=aplicar_sugestao,
|
| 441 |
+
inputs=[tabela, idx_selecionado, radio_sugestoes, dropdown_num],
|
| 442 |
outputs=tabela
|
| 443 |
).then(
|
| 444 |
fn=interpolar,
|
|
|
|
| 446 |
outputs=[tabela_resultado, arquivo_saida, falhas]
|
| 447 |
)
|
| 448 |
|
| 449 |
+
|
| 450 |
+
|
| 451 |
app.launch(server_name="0.0.0.0", server_port=7860)
|