Update app.py
Browse files
app.py
CHANGED
|
@@ -481,15 +481,15 @@ def calcular_progresso():
|
|
| 481 |
|
| 482 |
def comparar_peticao_melhorada(caso, peticao_texto, correcao):
|
| 483 |
if not caso:
|
| 484 |
-
return "⚠️ Forneça um caso antes de comparar.", "", "", "", ""
|
| 485 |
peticao = peticao_texto if peticao_texto else (HISTORICO[-1]["peticao"] if HISTORICO else "")
|
| 486 |
if not peticao:
|
| 487 |
-
return "⚠️ Forneça uma petição para comparar ou corrija uma antes.", "", "", "", ""
|
| 488 |
if not correcao:
|
| 489 |
-
return "⚠️ Corrija a petição antes de comparar.", "", "", "", ""
|
| 490 |
ultima_correcao = HISTORICO[-1]["correcao_completa"] if HISTORICO else ""
|
| 491 |
if not ultima_correcao:
|
| 492 |
-
return "⚠️ Nenhuma correção disponível no histórico.", "", "", "", ""
|
| 493 |
peticao_corrigida = extrair_peticao_corrigida(ultima_correcao)
|
| 494 |
|
| 495 |
orig_lines = peticao.split('\n')
|
|
@@ -501,52 +501,23 @@ def comparar_peticao_melhorada(caso, peticao_texto, correcao):
|
|
| 501 |
corr_body = "\n".join(corr_lines[start_corr:]).strip()
|
| 502 |
|
| 503 |
diff = difflib.ndiff(orig_body.splitlines(), corr_body.splitlines())
|
| 504 |
-
secoes = {
|
| 505 |
-
"ENDEREÇAMENTO": [], "QUALIFICAÇÃO": [], "FATOS": [], "FUNDAMENTOS": [], "PEDIDOS": [], "FECHAMENTO": [], "OUTROS": []
|
| 506 |
-
}
|
| 507 |
-
|
| 508 |
-
def identificar_secao(linha):
|
| 509 |
-
linha_upper = linha.upper()
|
| 510 |
-
if "EXCELENTÍSSIMO" in linha_upper or "JUIZ" in linha_upper or "VARA" in linha_upper:
|
| 511 |
-
return "ENDEREÇAMENTO"
|
| 512 |
-
elif "QUALIFICAÇÃO" in linha_upper or "AUTOR" in linha_upper or "RÉU" in linha_upper:
|
| 513 |
-
return "QUALIFICAÇÃO"
|
| 514 |
-
elif "DOS FATOS" in linha_upper:
|
| 515 |
-
return "FATOS"
|
| 516 |
-
elif "DIREITO" in linha_upper or "FUNDAMENTO" in linha_upper:
|
| 517 |
-
return "FUNDAMENTOS"
|
| 518 |
-
elif "PEDIDO" in linha_upper or "REQUER" in linha_upper:
|
| 519 |
-
return "PEDIDOS"
|
| 520 |
-
elif "TERMOS" in linha_upper and "DEFERIMENTO" in linha_upper:
|
| 521 |
-
return "FECHAMENTO"
|
| 522 |
-
else:
|
| 523 |
-
return "OUTROS"
|
| 524 |
|
| 525 |
-
secao_atual = "OUTROS"
|
| 526 |
resultado_diff = []
|
| 527 |
criticas = []
|
| 528 |
for linha in diff:
|
| 529 |
if linha.startswith(' '):
|
| 530 |
texto = linha[2:]
|
| 531 |
-
nova_secao = identificar_secao(texto)
|
| 532 |
-
if nova_secao != "OUTROS":
|
| 533 |
-
secao_atual = nova_secao
|
| 534 |
resultado_diff.append(f"<div class='linha-igual'>{escape(texto)}</div>")
|
| 535 |
elif linha.startswith('- '):
|
| 536 |
texto = linha[2:]
|
| 537 |
if any(kw in texto.lower() for kw in ["invenção", "custas", "art. 319"]):
|
| 538 |
criticas.append(f"🔴 REMOVIDO: {texto}")
|
| 539 |
resultado_diff.append(f"<div class='linha-removida'><del>{escape(texto)}</del></div>")
|
| 540 |
-
secoes[secao_atual].append(f"- REMOVIDO: {texto}")
|
| 541 |
elif linha.startswith('+ '):
|
| 542 |
texto = linha[2:]
|
| 543 |
-
nova_secao = identificar_secao(texto)
|
| 544 |
-
if nova_secao != "OUTROS":
|
| 545 |
-
secao_atual = nova_secao
|
| 546 |
if any(kw in texto.lower() for kw in ["custas", "art. 319"]):
|
| 547 |
criticas.append(f"🟢 ADICIONADO: {texto}")
|
| 548 |
resultado_diff.append(f"<div class='linha-adicionada'><ins>{escape(texto)}</ins></div>")
|
| 549 |
-
secoes[secao_atual].append(f"+ ADICIONADO: {texto}")
|
| 550 |
elif linha.startswith('? '):
|
| 551 |
continue
|
| 552 |
|
|
@@ -560,23 +531,6 @@ Diferença Líquida: {total_adicionadas - total_removidas} linhas
|
|
| 560 |
"""
|
| 561 |
|
| 562 |
criticas_texto = "\n".join(criticas) if criticas else "Nenhuma mudança crítica detectada."
|
| 563 |
-
alteracoes_texto = ""
|
| 564 |
-
for secao, alteracoes in secoes.items():
|
| 565 |
-
if alteracoes:
|
| 566 |
-
adicionadas = sum(1 for alt in alteracoes if alt.startswith("+"))
|
| 567 |
-
removidas = sum(1 for alt in alteracoes if alt.startswith("-"))
|
| 568 |
-
alteracoes_texto += f"""
|
| 569 |
-
### {secao}
|
| 570 |
-
| Tipo | Quantidade |
|
| 571 |
-
|--------------|------------|
|
| 572 |
-
| Adicionadas | {adicionadas} |
|
| 573 |
-
| Removidas | {removidas} |
|
| 574 |
-
"""
|
| 575 |
-
for alt in alteracoes[:5]:
|
| 576 |
-
texto = alt[:60] + "..." if len(alt) > 60 else alt
|
| 577 |
-
alteracoes_texto += f" {texto}\n"
|
| 578 |
-
if len(alteracoes) > 5:
|
| 579 |
-
alteracoes_texto += f" - ... e mais {len(alteracoes) - 5} alterações\n"
|
| 580 |
|
| 581 |
visualizacao_detalhada = f"""
|
| 582 |
<style>
|
|
@@ -586,18 +540,21 @@ Diferença Líquida: {total_adicionadas - total_removidas} linhas
|
|
| 586 |
</style>
|
| 587 |
{"".join(resultado_diff)}
|
| 588 |
"""
|
| 589 |
-
return peticao, peticao_corrigida, resumo_estatistico, criticas_texto,
|
| 590 |
|
| 591 |
def exportar_diferencas_html(caso, peticao_texto, correcao):
|
| 592 |
-
_, _, resumo_estatistico, criticas_texto,
|
| 593 |
html_content = f"""
|
| 594 |
<html>
|
| 595 |
<head>
|
| 596 |
<title>Diferenças na Petição</title>
|
| 597 |
<style>
|
|
|
|
| 598 |
.linha-igual {{ color: #000000; margin: 2px 0; }}
|
| 599 |
.linha-removida {{ color: #ffffff; background-color: #b30000; margin: 2px 0; padding: 2px; }}
|
| 600 |
.linha-adicionada {{ color: #ffffff; background-color: #006600; margin: 2px 0; padding: 2px; }}
|
|
|
|
|
|
|
| 601 |
</style>
|
| 602 |
</head>
|
| 603 |
<body>
|
|
@@ -610,7 +567,7 @@ def exportar_diferencas_html(caso, peticao_texto, correcao):
|
|
| 610 |
</body>
|
| 611 |
</html>
|
| 612 |
"""
|
| 613 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp:
|
| 614 |
HTML(string=html_content).write_pdf(tmp.name)
|
| 615 |
return tmp.name
|
| 616 |
|
|
@@ -684,7 +641,7 @@ def interface_gradio():
|
|
| 684 |
|
| 685 |
with gr.Row():
|
| 686 |
with gr.Column(scale=1):
|
| 687 |
-
alteracoes_texto_output = gr.Markdown(label="🔍 Alterações por Seção")
|
| 688 |
with gr.Column(scale=2):
|
| 689 |
resumo_alteracoes_output = gr.Markdown(label="📊 Resumo das Alterações")
|
| 690 |
criticas_output = gr.Markdown(label="🚨 Mudanças Críticas")
|
|
@@ -724,7 +681,7 @@ def interface_gradio():
|
|
| 724 |
fn=comparar_peticao_melhorada,
|
| 725 |
inputs=[caso_input, peticao_texto, resultado_output],
|
| 726 |
outputs=[peticao_aluno_output, peticao_corrigida_output, resumo_alteracoes_output, criticas_output,
|
| 727 |
-
|
| 728 |
).then(
|
| 729 |
fn=lambda: gr.update(visible=True),
|
| 730 |
outputs=[download_btn]
|
|
@@ -733,6 +690,9 @@ def interface_gradio():
|
|
| 733 |
fn=exportar_diferencas_html,
|
| 734 |
inputs=[caso_input, peticao_texto, resultado_output],
|
| 735 |
outputs=[download_file]
|
|
|
|
|
|
|
|
|
|
| 736 |
)
|
| 737 |
|
| 738 |
with gr.Accordion("📘 Critérios Detalhados", open=False):
|
|
|
|
| 481 |
|
| 482 |
def comparar_peticao_melhorada(caso, peticao_texto, correcao):
|
| 483 |
if not caso:
|
| 484 |
+
return "⚠️ Forneça um caso antes de comparar.", "", "", "", ""
|
| 485 |
peticao = peticao_texto if peticao_texto else (HISTORICO[-1]["peticao"] if HISTORICO else "")
|
| 486 |
if not peticao:
|
| 487 |
+
return "⚠️ Forneça uma petição para comparar ou corrija uma antes.", "", "", "", ""
|
| 488 |
if not correcao:
|
| 489 |
+
return "⚠️ Corrija a petição antes de comparar.", "", "", "", ""
|
| 490 |
ultima_correcao = HISTORICO[-1]["correcao_completa"] if HISTORICO else ""
|
| 491 |
if not ultima_correcao:
|
| 492 |
+
return "⚠️ Nenhuma correção disponível no histórico.", "", "", "", ""
|
| 493 |
peticao_corrigida = extrair_peticao_corrigida(ultima_correcao)
|
| 494 |
|
| 495 |
orig_lines = peticao.split('\n')
|
|
|
|
| 501 |
corr_body = "\n".join(corr_lines[start_corr:]).strip()
|
| 502 |
|
| 503 |
diff = difflib.ndiff(orig_body.splitlines(), corr_body.splitlines())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 504 |
|
|
|
|
| 505 |
resultado_diff = []
|
| 506 |
criticas = []
|
| 507 |
for linha in diff:
|
| 508 |
if linha.startswith(' '):
|
| 509 |
texto = linha[2:]
|
|
|
|
|
|
|
|
|
|
| 510 |
resultado_diff.append(f"<div class='linha-igual'>{escape(texto)}</div>")
|
| 511 |
elif linha.startswith('- '):
|
| 512 |
texto = linha[2:]
|
| 513 |
if any(kw in texto.lower() for kw in ["invenção", "custas", "art. 319"]):
|
| 514 |
criticas.append(f"🔴 REMOVIDO: {texto}")
|
| 515 |
resultado_diff.append(f"<div class='linha-removida'><del>{escape(texto)}</del></div>")
|
|
|
|
| 516 |
elif linha.startswith('+ '):
|
| 517 |
texto = linha[2:]
|
|
|
|
|
|
|
|
|
|
| 518 |
if any(kw in texto.lower() for kw in ["custas", "art. 319"]):
|
| 519 |
criticas.append(f"🟢 ADICIONADO: {texto}")
|
| 520 |
resultado_diff.append(f"<div class='linha-adicionada'><ins>{escape(texto)}</ins></div>")
|
|
|
|
| 521 |
elif linha.startswith('? '):
|
| 522 |
continue
|
| 523 |
|
|
|
|
| 531 |
"""
|
| 532 |
|
| 533 |
criticas_texto = "\n".join(criticas) if criticas else "Nenhuma mudança crítica detectada."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 534 |
|
| 535 |
visualizacao_detalhada = f"""
|
| 536 |
<style>
|
|
|
|
| 540 |
</style>
|
| 541 |
{"".join(resultado_diff)}
|
| 542 |
"""
|
| 543 |
+
return peticao, peticao_corrigida, resumo_estatistico, criticas_texto, visualizacao_detalhada
|
| 544 |
|
| 545 |
def exportar_diferencas_html(caso, peticao_texto, correcao):
|
| 546 |
+
_, _, resumo_estatistico, criticas_texto, visualizacao_detalhada = comparar_peticao_melhorada(caso, peticao_texto, correcao)
|
| 547 |
html_content = f"""
|
| 548 |
<html>
|
| 549 |
<head>
|
| 550 |
<title>Diferenças na Petição</title>
|
| 551 |
<style>
|
| 552 |
+
body {{ font-family: Arial, sans-serif; margin: 20px; }}
|
| 553 |
.linha-igual {{ color: #000000; margin: 2px 0; }}
|
| 554 |
.linha-removida {{ color: #ffffff; background-color: #b30000; margin: 2px 0; padding: 2px; }}
|
| 555 |
.linha-adicionada {{ color: #ffffff; background-color: #006600; margin: 2px 0; padding: 2px; }}
|
| 556 |
+
h2 {{ color: #333; }}
|
| 557 |
+
pre {{ white-space: pre-wrap; }}
|
| 558 |
</style>
|
| 559 |
</head>
|
| 560 |
<body>
|
|
|
|
| 567 |
</body>
|
| 568 |
</html>
|
| 569 |
"""
|
| 570 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf", mode='w') as tmp:
|
| 571 |
HTML(string=html_content).write_pdf(tmp.name)
|
| 572 |
return tmp.name
|
| 573 |
|
|
|
|
| 641 |
|
| 642 |
with gr.Row():
|
| 643 |
with gr.Column(scale=1):
|
| 644 |
+
alteracoes_texto_output = gr.Markdown(label="🔍 Alterações por Seção", visible=False)
|
| 645 |
with gr.Column(scale=2):
|
| 646 |
resumo_alteracoes_output = gr.Markdown(label="📊 Resumo das Alterações")
|
| 647 |
criticas_output = gr.Markdown(label="🚨 Mudanças Críticas")
|
|
|
|
| 681 |
fn=comparar_peticao_melhorada,
|
| 682 |
inputs=[caso_input, peticao_texto, resultado_output],
|
| 683 |
outputs=[peticao_aluno_output, peticao_corrigida_output, resumo_alteracoes_output, criticas_output,
|
| 684 |
+
visualizacao_detalhada_output]
|
| 685 |
).then(
|
| 686 |
fn=lambda: gr.update(visible=True),
|
| 687 |
outputs=[download_btn]
|
|
|
|
| 690 |
fn=exportar_diferencas_html,
|
| 691 |
inputs=[caso_input, peticao_texto, resultado_output],
|
| 692 |
outputs=[download_file]
|
| 693 |
+
).then(
|
| 694 |
+
fn=lambda x: [gr.update(visible=True, value=x), gr.update(visible=True)],
|
| 695 |
+
outputs=[download_file, download_btn]
|
| 696 |
)
|
| 697 |
|
| 698 |
with gr.Accordion("📘 Critérios Detalhados", open=False):
|