Spaces:
Runtime error
Runtime error
Gabriel Ramos
commited on
Commit
·
6c2e797
1
Parent(s):
03be1b2
refactor: Melhorias de robustez
Browse files- app.py +29 -6
- processors/docling_processor.py +3 -1
- utils/validators.py +5 -1
app.py
CHANGED
|
@@ -69,12 +69,35 @@ def check_rate_limit(request: gr.Request) -> bool:
|
|
| 69 |
if request is None:
|
| 70 |
return True
|
| 71 |
|
| 72 |
-
# Obtém IP do cliente
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
now = datetime.now()
|
| 80 |
window_start = now - timedelta(hours=config.RATE_LIMIT_WINDOW_HOURS)
|
|
|
|
| 69 |
if request is None:
|
| 70 |
return True
|
| 71 |
|
| 72 |
+
# Obtém IP do cliente de múltiplas fontes
|
| 73 |
+
ip = None
|
| 74 |
+
|
| 75 |
+
# Tenta headers primeiro (X-Forwarded-For para proxies/containers)
|
| 76 |
+
if hasattr(request, "headers"):
|
| 77 |
+
headers = request.headers or {}
|
| 78 |
+
ip = headers.get("x-forwarded-for", "").split(",")[0].strip()
|
| 79 |
+
if not ip:
|
| 80 |
+
ip = headers.get("x-real-ip", "").strip()
|
| 81 |
+
|
| 82 |
+
# Fallback para client info
|
| 83 |
+
if not ip:
|
| 84 |
+
client_info = getattr(request, "client", None)
|
| 85 |
+
if client_info:
|
| 86 |
+
if isinstance(client_info, dict):
|
| 87 |
+
ip = client_info.get("host", "")
|
| 88 |
+
elif hasattr(client_info, "host"):
|
| 89 |
+
ip = getattr(client_info, "host", "")
|
| 90 |
+
else:
|
| 91 |
+
ip = str(client_info)
|
| 92 |
+
|
| 93 |
+
# Se ainda não tem IP, usa session_hash como identificador alternativo
|
| 94 |
+
if not ip or ip == "unknown":
|
| 95 |
+
session_hash = getattr(request, "session_hash", None)
|
| 96 |
+
if session_hash:
|
| 97 |
+
ip = f"session_{session_hash[:16]}"
|
| 98 |
+
else:
|
| 99 |
+
# Último fallback: permite a requisição mas não rastreia
|
| 100 |
+
return True
|
| 101 |
|
| 102 |
now = datetime.now()
|
| 103 |
window_start = now - timedelta(hours=config.RATE_LIMIT_WINDOW_HOURS)
|
processors/docling_processor.py
CHANGED
|
@@ -43,7 +43,9 @@ class DoclingProcessor:
|
|
| 43 |
Args:
|
| 44 |
enable_ocr: Se deve habilitar OCR para imagens.
|
| 45 |
enable_table_detection: Se deve detectar tabelas.
|
| 46 |
-
use_gpu:
|
|
|
|
|
|
|
| 47 |
"""
|
| 48 |
self.enable_ocr = enable_ocr
|
| 49 |
self.enable_table_detection = enable_table_detection
|
|
|
|
| 43 |
Args:
|
| 44 |
enable_ocr: Se deve habilitar OCR para imagens.
|
| 45 |
enable_table_detection: Se deve detectar tabelas.
|
| 46 |
+
use_gpu: Flag indicando se GPU está disponível.
|
| 47 |
+
Nota: A aceleração GPU real é obtida via `@spaces.GPU`
|
| 48 |
+
no app.py. Esta flag serve para logging/debug.
|
| 49 |
"""
|
| 50 |
self.enable_ocr = enable_ocr
|
| 51 |
self.enable_table_detection = enable_table_detection
|
utils/validators.py
CHANGED
|
@@ -167,7 +167,11 @@ def get_mime_type(file_path: str | Path) -> str:
|
|
| 167 |
String com o MIME type detectado.
|
| 168 |
"""
|
| 169 |
if HAS_MAGIC:
|
| 170 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
return _get_mime_type_fallback(file_path)
|
| 172 |
|
| 173 |
|
|
|
|
| 167 |
String com o MIME type detectado.
|
| 168 |
"""
|
| 169 |
if HAS_MAGIC:
|
| 170 |
+
try:
|
| 171 |
+
return _get_mime_type_magic(file_path)
|
| 172 |
+
except Exception:
|
| 173 |
+
# Fallback se libmagic falhar em runtime (OSError, etc)
|
| 174 |
+
pass
|
| 175 |
return _get_mime_type_fallback(file_path)
|
| 176 |
|
| 177 |
|