Update app.py
Browse files
app.py
CHANGED
|
@@ -17,7 +17,9 @@ from datetime import datetime
|
|
| 17 |
from ratelimiter import RateLimiter
|
| 18 |
import difflib
|
| 19 |
from html import escape
|
| 20 |
-
from
|
|
|
|
|
|
|
| 21 |
|
| 22 |
load_dotenv()
|
| 23 |
|
|
@@ -451,7 +453,7 @@ def formatar_correção(texto):
|
|
| 451 |
if match:
|
| 452 |
nota = match.group(1)
|
| 453 |
texto_formatado = texto_formatado.replace(f"### ⭐ Nota Final\n\n{nota}", f"### ⭐ Nota Final: {nota}/10\n\n")
|
| 454 |
-
return texto_formatado
|
| 455 |
|
| 456 |
def extrair_peticao_corrigida(correcao_completa):
|
| 457 |
try:
|
|
@@ -513,22 +515,22 @@ def comparar_peticao_melhorada(caso, peticao_texto, correcao):
|
|
| 513 |
for linha in diff:
|
| 514 |
if linha.startswith(' '):
|
| 515 |
texto = linha[2:]
|
| 516 |
-
resultado_diff.append(f"
|
| 517 |
elif linha.startswith('- '):
|
| 518 |
texto = linha[2:]
|
| 519 |
if any(kw in texto.lower() for kw in ["invenção", "custas", "art. 319"]):
|
| 520 |
criticas.append(f"🔴 REMOVIDO: {texto} (inconsistente ou incorreto)")
|
| 521 |
-
resultado_diff.append(f"
|
| 522 |
elif linha.startswith('+ '):
|
| 523 |
texto = linha[2:]
|
| 524 |
if any(kw in texto.lower() for kw in ["custas", "art. 319"]):
|
| 525 |
criticas.append(f"🟢 ADICIONADO: {texto} (essencial para conformidade)")
|
| 526 |
-
resultado_diff.append(f"
|
| 527 |
elif linha.startswith('? '):
|
| 528 |
continue
|
| 529 |
|
| 530 |
-
total_removidas = len([linha for linha in resultado_diff if "
|
| 531 |
-
total_adicionadas = len([linha for linha in resultado_diff if "
|
| 532 |
|
| 533 |
resumo_estatistico = f"""
|
| 534 |
Linhas Removidas: {total_removidas}
|
|
@@ -537,49 +539,24 @@ Diferença Líquida: {total_adicionadas - total_removidas} linhas
|
|
| 537 |
"""
|
| 538 |
|
| 539 |
criticas_texto = "\n".join(criticas) if criticas else "Nenhuma mudança crítica detectada."
|
|
|
|
| 540 |
|
| 541 |
-
visualizacao_detalhada = f"""
|
| 542 |
-
<style>
|
| 543 |
-
.linha-igual {{ color: #000000; margin: 2px 0; }}
|
| 544 |
-
.linha-removida {{ color: #ffffff; background-color: #b30000; margin: 2px 0; padding: 2px; }}
|
| 545 |
-
.linha-adicionada {{ color: #ffffff; background-color: #006600; margin: 2px 0; padding: 2px; }}
|
| 546 |
-
</style>
|
| 547 |
-
{"".join(resultado_diff)}
|
| 548 |
-
"""
|
| 549 |
return peticao, peticao_corrigida, resumo_estatistico, criticas_texto, visualizacao_detalhada
|
| 550 |
|
| 551 |
def exportar_diferencas_html(caso, peticao_texto, correcao):
|
| 552 |
_, _, resumo_estatistico, criticas_texto, visualizacao_detalhada = comparar_peticao_melhorada(caso, peticao_texto, correcao)
|
| 553 |
-
|
| 554 |
-
|
| 555 |
-
<head>
|
| 556 |
-
<title>Diferenças na Petição</title>
|
| 557 |
-
<style>
|
| 558 |
-
body {{ font-family: Arial, sans-serif; margin: 20px; }}
|
| 559 |
-
.linha-igual {{ color: #000000; margin: 2px 0; }}
|
| 560 |
-
.linha-removida {{ color: #ffffff; background-color: #b30000; margin: 2px 0; padding: 2px; }}
|
| 561 |
-
.linha-adicionada {{ color: #ffffff; background-color: #006600; margin: 2px 0; padding: 2px; }}
|
| 562 |
-
h2 {{ color: #333; }}
|
| 563 |
-
pre {{ white-space: pre-wrap; }}
|
| 564 |
-
</style>
|
| 565 |
-
</head>
|
| 566 |
-
<body>
|
| 567 |
-
<h2>Estatísticas</h2>
|
| 568 |
-
<pre>{resumo_estatistico}</pre>
|
| 569 |
-
<h2>Mudanças Críticas</h2>
|
| 570 |
-
<pre>{criticas_texto}</pre>
|
| 571 |
-
<h2>Alterações Detalhadas</h2>
|
| 572 |
-
{visualizacao_detalhada}
|
| 573 |
-
</body>
|
| 574 |
-
</html>
|
| 575 |
-
"""
|
| 576 |
try:
|
| 577 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp:
|
| 578 |
temp_path = tmp.name
|
| 579 |
-
|
|
|
|
|
|
|
|
|
|
| 580 |
return temp_path
|
| 581 |
except Exception as e:
|
| 582 |
-
return f"❌ Erro ao gerar PDF: {str(e)}"
|
| 583 |
finally:
|
| 584 |
if 'temp_path' in locals() and os.path.exists(temp_path):
|
| 585 |
os.unlink(temp_path)
|
|
@@ -657,7 +634,7 @@ def interface_gradio():
|
|
| 657 |
resumo_alteracoes_output = gr.Markdown(label="📊 Resumo das Alterações")
|
| 658 |
criticas_output = gr.Markdown(label="🚨 Mudanças Críticas")
|
| 659 |
with gr.Column(scale=2):
|
| 660 |
-
visualizacao_detalhada_output = gr.
|
| 661 |
|
| 662 |
with gr.Row():
|
| 663 |
download_btn = gr.Button("📥 Baixar Diferenças em PDF", visible=False)
|
|
|
|
| 17 |
from ratelimiter import RateLimiter
|
| 18 |
import difflib
|
| 19 |
from html import escape
|
| 20 |
+
from reportlab.lib.pagesizes import letter
|
| 21 |
+
from reportlab.platypus import SimpleDocTemplate, Paragraph
|
| 22 |
+
from reportlab.lib.styles import getSampleStyleSheet
|
| 23 |
|
| 24 |
load_dotenv()
|
| 25 |
|
|
|
|
| 453 |
if match:
|
| 454 |
nota = match.group(1)
|
| 455 |
texto_formatado = texto_formatado.replace(f"### ⭐ Nota Final\n\n{nota}", f"### ⭐ Nota Final: {nota}/10\n\n")
|
| 456 |
+
return texto_formatado
|
| 457 |
|
| 458 |
def extrair_peticao_corrigida(correcao_completa):
|
| 459 |
try:
|
|
|
|
| 515 |
for linha in diff:
|
| 516 |
if linha.startswith(' '):
|
| 517 |
texto = linha[2:]
|
| 518 |
+
resultado_diff.append(f"- {escape(texto)}") # Simplificado para texto puro
|
| 519 |
elif linha.startswith('- '):
|
| 520 |
texto = linha[2:]
|
| 521 |
if any(kw in texto.lower() for kw in ["invenção", "custas", "art. 319"]):
|
| 522 |
criticas.append(f"🔴 REMOVIDO: {texto} (inconsistente ou incorreto)")
|
| 523 |
+
resultado_diff.append(f"- REMOVIDO: {escape(texto)}")
|
| 524 |
elif linha.startswith('+ '):
|
| 525 |
texto = linha[2:]
|
| 526 |
if any(kw in texto.lower() for kw in ["custas", "art. 319"]):
|
| 527 |
criticas.append(f"🟢 ADICIONADO: {texto} (essencial para conformidade)")
|
| 528 |
+
resultado_diff.append(f"- ADICIONADO: {escape(texto)}")
|
| 529 |
elif linha.startswith('? '):
|
| 530 |
continue
|
| 531 |
|
| 532 |
+
total_removidas = len([linha for linha in resultado_diff if "REMOVIDO" in linha])
|
| 533 |
+
total_adicionadas = len([linha for linha in resultado_diff if "ADICIONADO" in linha])
|
| 534 |
|
| 535 |
resumo_estatistico = f"""
|
| 536 |
Linhas Removidas: {total_removidas}
|
|
|
|
| 539 |
"""
|
| 540 |
|
| 541 |
criticas_texto = "\n".join(criticas) if criticas else "Nenhuma mudança crítica detectada."
|
| 542 |
+
visualizacao_detalhada = "\n".join(resultado_diff)
|
| 543 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 544 |
return peticao, peticao_corrigida, resumo_estatistico, criticas_texto, visualizacao_detalhada
|
| 545 |
|
| 546 |
def exportar_diferencas_html(caso, peticao_texto, correcao):
|
| 547 |
_, _, resumo_estatistico, criticas_texto, visualizacao_detalhada = comparar_peticao_melhorada(caso, peticao_texto, correcao)
|
| 548 |
+
content = f"Estatísticas:\n{resumo_estatistico}\n\nMudanças Críticas:\n{criticas_texto}\n\nAlterações Detalhadas:\n{visualizacao_detalhada}"
|
| 549 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 550 |
try:
|
| 551 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp:
|
| 552 |
temp_path = tmp.name
|
| 553 |
+
doc = SimpleDocTemplate(temp_path, pagesize=letter)
|
| 554 |
+
styles = getSampleStyleSheet()
|
| 555 |
+
story = [Paragraph(content.replace('\n', '<br/>'), styles['Normal'])]
|
| 556 |
+
doc.build(story)
|
| 557 |
return temp_path
|
| 558 |
except Exception as e:
|
| 559 |
+
return f"❌ Erro ao gerar PDF: {str(e)}"
|
| 560 |
finally:
|
| 561 |
if 'temp_path' in locals() and os.path.exists(temp_path):
|
| 562 |
os.unlink(temp_path)
|
|
|
|
| 634 |
resumo_alteracoes_output = gr.Markdown(label="📊 Resumo das Alterações")
|
| 635 |
criticas_output = gr.Markdown(label="🚨 Mudanças Críticas")
|
| 636 |
with gr.Column(scale=2):
|
| 637 |
+
visualizacao_detalhada_output = gr.Textbox(label="📝 Alterações Detalhadas", lines=10) # Mudado para Textbox
|
| 638 |
|
| 639 |
with gr.Row():
|
| 640 |
download_btn = gr.Button("📥 Baixar Diferenças em PDF", visible=False)
|