Spaces:
Sleeping
Sleeping
Commit
·
0e7ecc2
1
Parent(s):
a2074bf
refactor: alterar exibição de texto extraído para texto simples
Browse filesCo-authored-by: aider (anthropic/claude-sonnet-4-20250514) <aider@aider.chat>
app.py
CHANGED
|
@@ -220,82 +220,24 @@ def extract_references_with_regex(text):
|
|
| 220 |
except Exception as e:
|
| 221 |
return [{"error": f"Erro na extração por regex: {str(e)}"}]
|
| 222 |
|
| 223 |
-
def
|
| 224 |
-
"""
|
| 225 |
try:
|
| 226 |
-
|
| 227 |
-
lines = text.split('\n')
|
| 228 |
-
highlighted_lines = []
|
| 229 |
-
|
| 230 |
-
colors = ['#ff5722', '#ffeb3b', '#4caf50', '#2196f3', '#ff9800', '#9c27b0', '#e91e63', '#795548']
|
| 231 |
-
|
| 232 |
-
# Processar cada linha
|
| 233 |
-
for line in lines:
|
| 234 |
-
original_line = line
|
| 235 |
-
line_stripped = line.strip()
|
| 236 |
-
|
| 237 |
-
# Verificar se a linha corresponde a algum padrão
|
| 238 |
-
matched = False
|
| 239 |
-
for i, pattern in enumerate(REFERENCE_PATTERNS):
|
| 240 |
-
if re.match(pattern, line_stripped, re.MULTILINE | re.IGNORECASE):
|
| 241 |
-
if len(line_stripped) >= 20 and line_stripped[0].isupper():
|
| 242 |
-
color = colors[i % len(colors)]
|
| 243 |
-
highlighted_line = f'<span style="background-color: {color}; padding: 2px; border-radius: 3px; display: block; margin: 1px 0;" title="Padrão {i+1}">{original_line}</span>'
|
| 244 |
-
highlighted_lines.append(highlighted_line)
|
| 245 |
-
matched = True
|
| 246 |
-
break
|
| 247 |
-
|
| 248 |
-
if not matched:
|
| 249 |
-
highlighted_lines.append(original_line)
|
| 250 |
-
|
| 251 |
-
# Criar HTML final
|
| 252 |
-
html_content = '<br>'.join(highlighted_lines)
|
| 253 |
-
|
| 254 |
-
styled_html = f"""
|
| 255 |
-
<div style="
|
| 256 |
-
font-family: 'Courier New', monospace;
|
| 257 |
-
font-size: 12px;
|
| 258 |
-
line-height: 1.4;
|
| 259 |
-
max-height: 400px;
|
| 260 |
-
overflow-y: auto;
|
| 261 |
-
padding: 15px;
|
| 262 |
-
border: 1px solid #ddd;
|
| 263 |
-
border-radius: 5px;
|
| 264 |
-
background-color: #fafafa;
|
| 265 |
-
white-space: pre-wrap;
|
| 266 |
-
">
|
| 267 |
-
<div style="margin-bottom: 10px; font-weight: bold; color: #333;">
|
| 268 |
-
📄 Texto Extraído com Destaques das Referências
|
| 269 |
-
</div>
|
| 270 |
-
<div style="margin-bottom: 15px; font-size: 11px; color: #666;">
|
| 271 |
-
<span style="background-color: #ff5722; padding: 2px;">■</span> Padrão 0
|
| 272 |
-
<span style="background-color: #ffeb3b; padding: 2px;">■</span> Padrão 1
|
| 273 |
-
<span style="background-color: #4caf50; padding: 2px;">■</span> Padrão 2
|
| 274 |
-
<span style="background-color: #2196f3; padding: 2px;">■</span> Padrão 3
|
| 275 |
-
<span style="background-color: #ff9800; padding: 2px;">■</span> Padrão 4
|
| 276 |
-
<span style="background-color: #9c27b0; padding: 2px;">■</span> Padrão 5
|
| 277 |
-
<span style="background-color: #e91e63; padding: 2px;">■</span> Padrão 6
|
| 278 |
-
<span style="background-color: #795548; padding: 2px;">■</span> Padrão 7
|
| 279 |
-
</div>
|
| 280 |
-
{html_content}
|
| 281 |
-
</div>
|
| 282 |
-
"""
|
| 283 |
-
|
| 284 |
-
return styled_html
|
| 285 |
|
| 286 |
except Exception as e:
|
| 287 |
-
return f"
|
| 288 |
|
| 289 |
def process_pdf(pdf_file, model_name):
|
| 290 |
"""Função principal que processa o PDF e retorna resultados"""
|
| 291 |
if pdf_file is None:
|
| 292 |
-
return {"error": "Nenhum arquivo enviado"}, pd.DataFrame(), pd.DataFrame(), "❌ Nenhum arquivo enviado", "
|
| 293 |
|
| 294 |
# Extrair texto do PDF
|
| 295 |
text, metadata = extract_pdf_text(pdf_file)
|
| 296 |
|
| 297 |
if text is None:
|
| 298 |
-
return metadata, pd.DataFrame(), pd.DataFrame(), "❌ Erro ao processar PDF", "
|
| 299 |
|
| 300 |
# Adicionar modelo selecionado aos metadados
|
| 301 |
metadata["modelo_usado"] = model_name
|
|
@@ -308,8 +250,8 @@ def process_pdf(pdf_file, model_name):
|
|
| 308 |
# Extrair referências com Regex
|
| 309 |
regex_references = extract_references_with_regex(text)
|
| 310 |
|
| 311 |
-
# Criar
|
| 312 |
-
|
| 313 |
|
| 314 |
# Converter para DataFrames
|
| 315 |
if llm_references and not any("error" in ref for ref in llm_references):
|
|
@@ -328,7 +270,7 @@ def process_pdf(pdf_file, model_name):
|
|
| 328 |
|
| 329 |
status = f"📊 **Resultados da Extração:**\n- LLM ({model_name}): {llm_count} referências\n- Regex: {regex_count} referências"
|
| 330 |
|
| 331 |
-
return metadata, llm_df, regex_df, status,
|
| 332 |
|
| 333 |
def create_interface():
|
| 334 |
"""Cria a interface Gradio"""
|
|
@@ -364,8 +306,12 @@ def create_interface():
|
|
| 364 |
with gr.Column():
|
| 365 |
metadata_output = gr.JSON(label="📋 Metadados do Artigo")
|
| 366 |
with gr.Column():
|
| 367 |
-
extracted_text_output = gr.
|
| 368 |
-
label="📄 Texto Extraído
|
|
|
|
|
|
|
|
|
|
|
|
|
| 369 |
)
|
| 370 |
|
| 371 |
with gr.Row():
|
|
|
|
| 220 |
except Exception as e:
|
| 221 |
return [{"error": f"Erro na extração por regex: {str(e)}"}]
|
| 222 |
|
| 223 |
+
def create_plain_text(text, regex_references):
|
| 224 |
+
"""Retorna o texto extraído como texto simples"""
|
| 225 |
try:
|
| 226 |
+
return text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 227 |
|
| 228 |
except Exception as e:
|
| 229 |
+
return f"Erro ao processar texto: {str(e)}"
|
| 230 |
|
| 231 |
def process_pdf(pdf_file, model_name):
|
| 232 |
"""Função principal que processa o PDF e retorna resultados"""
|
| 233 |
if pdf_file is None:
|
| 234 |
+
return {"error": "Nenhum arquivo enviado"}, pd.DataFrame(), pd.DataFrame(), "❌ Nenhum arquivo enviado", "Nenhum texto para exibir"
|
| 235 |
|
| 236 |
# Extrair texto do PDF
|
| 237 |
text, metadata = extract_pdf_text(pdf_file)
|
| 238 |
|
| 239 |
if text is None:
|
| 240 |
+
return metadata, pd.DataFrame(), pd.DataFrame(), "❌ Erro ao processar PDF", "Erro ao extrair texto"
|
| 241 |
|
| 242 |
# Adicionar modelo selecionado aos metadados
|
| 243 |
metadata["modelo_usado"] = model_name
|
|
|
|
| 250 |
# Extrair referências com Regex
|
| 251 |
regex_references = extract_references_with_regex(text)
|
| 252 |
|
| 253 |
+
# Criar texto simples
|
| 254 |
+
plain_text = create_plain_text(text, regex_references)
|
| 255 |
|
| 256 |
# Converter para DataFrames
|
| 257 |
if llm_references and not any("error" in ref for ref in llm_references):
|
|
|
|
| 270 |
|
| 271 |
status = f"📊 **Resultados da Extração:**\n- LLM ({model_name}): {llm_count} referências\n- Regex: {regex_count} referências"
|
| 272 |
|
| 273 |
+
return metadata, llm_df, regex_df, status, plain_text
|
| 274 |
|
| 275 |
def create_interface():
|
| 276 |
"""Cria a interface Gradio"""
|
|
|
|
| 306 |
with gr.Column():
|
| 307 |
metadata_output = gr.JSON(label="📋 Metadados do Artigo")
|
| 308 |
with gr.Column():
|
| 309 |
+
extracted_text_output = gr.Textbox(
|
| 310 |
+
label="📄 Texto Extraído",
|
| 311 |
+
lines=20,
|
| 312 |
+
max_lines=20,
|
| 313 |
+
show_copy_button=True,
|
| 314 |
+
interactive=False
|
| 315 |
)
|
| 316 |
|
| 317 |
with gr.Row():
|