Spaces:
Paused
Paused
Update routers/inference_createposter.py
Browse files
routers/inference_createposter.py
CHANGED
|
@@ -52,9 +52,33 @@ def fix_citation_quotes(citation_text: str) -> str:
|
|
| 52 |
# Adicionar as aspas corretas
|
| 53 |
return f"“{text.strip()}”"
|
| 54 |
|
| 55 |
-
def
|
| 56 |
"""
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
Mantém apenas o conteúdo textual limpo.
|
| 59 |
"""
|
| 60 |
if not text:
|
|
@@ -63,7 +87,6 @@ def clean_text_content_improved(text: str) -> str:
|
|
| 63 |
import re
|
| 64 |
|
| 65 |
# Remove TODAS as tags HTML usando regex mais ampla
|
| 66 |
-
# Esta regex captura qualquer tag: <qualquer_coisa>
|
| 67 |
text = re.sub(r'<[^>]*>', '', text)
|
| 68 |
|
| 69 |
# Remove possíveis entidades HTML comuns
|
|
@@ -72,26 +95,34 @@ def clean_text_content_improved(text: str) -> str:
|
|
| 72 |
|
| 73 |
return text.strip()
|
| 74 |
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
"""
|
| 77 |
-
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
| 79 |
"""
|
| 80 |
try:
|
| 81 |
# Parse da URL
|
| 82 |
parsed_url = urlparse(url)
|
| 83 |
query_params = parse_qs(parsed_url.query)
|
| 84 |
|
| 85 |
-
#
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
-
# Processar
|
| 89 |
-
for param in
|
| 90 |
if param in query_params and query_params[param]:
|
| 91 |
original_text = query_params[param][0]
|
| 92 |
-
|
| 93 |
-
# Primeiro, limpar todas as tags HTML
|
| 94 |
-
cleaned_text = clean_text_content_improved(original_text)
|
| 95 |
|
| 96 |
# Se for citation, aplicar correção específica das aspas
|
| 97 |
if param == 'citation':
|
|
@@ -99,6 +130,13 @@ def fix_url_citation(url: str) -> str:
|
|
| 99 |
|
| 100 |
query_params[param] = [cleaned_text]
|
| 101 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
# Reconstruir a query string
|
| 103 |
new_query = urlencode(
|
| 104 |
{k: v[0] if isinstance(v, list) and len(v) == 1 else v for k, v in query_params.items()},
|
|
|
|
| 52 |
# Adicionar as aspas corretas
|
| 53 |
return f"“{text.strip()}”"
|
| 54 |
|
| 55 |
+
def clean_text_content_for_text_param(text: str) -> str:
|
| 56 |
"""
|
| 57 |
+
Limpa o conteúdo do parâmetro 'text':
|
| 58 |
+
- Remove apenas tags <wiki>
|
| 59 |
+
- Mantém <strong> e <em>
|
| 60 |
+
- Se tiver tags aninhadas (ex: <strong><em>), prioriza a segunda (mais interna)
|
| 61 |
+
"""
|
| 62 |
+
if not text:
|
| 63 |
+
return text
|
| 64 |
+
|
| 65 |
+
import re
|
| 66 |
+
|
| 67 |
+
# Primeiro, resolver conflitos de tags aninhadas - priorizar a segunda (mais interna)
|
| 68 |
+
# <strong><em>conteúdo</em></strong> -> <em>conteúdo</em>
|
| 69 |
+
text = re.sub(r'<strong>\s*<em>(.*?)</em>\s*</strong>', r'<em>\1</em>', text)
|
| 70 |
+
|
| 71 |
+
# <em><strong>conteúdo</strong></em> -> <strong>conteúdo</strong>
|
| 72 |
+
text = re.sub(r'<em>\s*<strong>(.*?)</strong>\s*</em>', r'<strong>\1</strong>', text)
|
| 73 |
+
|
| 74 |
+
# Remover apenas tags <wiki>
|
| 75 |
+
text = re.sub(r'</?wiki[^>]*>', '', text)
|
| 76 |
+
|
| 77 |
+
return text.strip()
|
| 78 |
+
|
| 79 |
+
def clean_text_content_remove_all_tags(text: str) -> str:
|
| 80 |
+
"""
|
| 81 |
+
Remove TODAS as tags HTML do texto (para headline, title, citation).
|
| 82 |
Mantém apenas o conteúdo textual limpo.
|
| 83 |
"""
|
| 84 |
if not text:
|
|
|
|
| 87 |
import re
|
| 88 |
|
| 89 |
# Remove TODAS as tags HTML usando regex mais ampla
|
|
|
|
| 90 |
text = re.sub(r'<[^>]*>', '', text)
|
| 91 |
|
| 92 |
# Remove possíveis entidades HTML comuns
|
|
|
|
| 95 |
|
| 96 |
return text.strip()
|
| 97 |
|
| 98 |
+
# Função atualizada para manter compatibilidade com o código existente
|
| 99 |
+
def clean_text_content(text: str) -> str:
|
| 100 |
+
"""
|
| 101 |
+
Função mantida para compatibilidade.
|
| 102 |
+
Usa a nova lógica para parâmetro 'text'.
|
| 103 |
"""
|
| 104 |
+
return clean_text_content_for_text_param(text)
|
| 105 |
+
"""
|
| 106 |
+
Analisa uma URL e trata os parâmetros de texto de forma específica:
|
| 107 |
+
- Para 'text': mantém <strong> e <em>, remove <wiki>, resolve conflitos de tags aninhadas
|
| 108 |
+
- Para 'headline', 'title', 'citation': remove TODAS as tags HTML
|
| 109 |
"""
|
| 110 |
try:
|
| 111 |
# Parse da URL
|
| 112 |
parsed_url = urlparse(url)
|
| 113 |
query_params = parse_qs(parsed_url.query)
|
| 114 |
|
| 115 |
+
# Parâmetros que devem ter TODAS as tags removidas
|
| 116 |
+
clean_all_params = ['headline', 'title', 'citation']
|
| 117 |
+
|
| 118 |
+
# Parâmetros que têm tratamento especial (apenas text)
|
| 119 |
+
special_text_params = ['text']
|
| 120 |
|
| 121 |
+
# Processar parâmetros que devem ser completamente limpos
|
| 122 |
+
for param in clean_all_params:
|
| 123 |
if param in query_params and query_params[param]:
|
| 124 |
original_text = query_params[param][0]
|
| 125 |
+
cleaned_text = clean_text_content_remove_all_tags(original_text)
|
|
|
|
|
|
|
| 126 |
|
| 127 |
# Se for citation, aplicar correção específica das aspas
|
| 128 |
if param == 'citation':
|
|
|
|
| 130 |
|
| 131 |
query_params[param] = [cleaned_text]
|
| 132 |
|
| 133 |
+
# Processar parâmetro 'text' com tratamento especial
|
| 134 |
+
for param in special_text_params:
|
| 135 |
+
if param in query_params and query_params[param]:
|
| 136 |
+
original_text = query_params[param][0]
|
| 137 |
+
cleaned_text = clean_text_content_for_text_param(original_text)
|
| 138 |
+
query_params[param] = [cleaned_text]
|
| 139 |
+
|
| 140 |
# Reconstruir a query string
|
| 141 |
new_query = urlencode(
|
| 142 |
{k: v[0] if isinstance(v, list) and len(v) == 1 else v for k, v in query_params.items()},
|