Spaces:
Sleeping
Sleeping
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,113 +0,0 @@
|
|
| 1 |
-
"""Gradio app wrapping the official `commonforms` package to convert PDFs
|
| 2 |
-
into fillable forms using jbarrow's FFDNet-L object detector (CPU ONNX).
|
| 3 |
-
|
| 4 |
-
- Paper: <https://arxiv.org/abs/2509.16506>
|
| 5 |
-
- Model: <https://huggingface.co/jbarrow/FFDNet-L-cpu>
|
| 6 |
-
- Package: <https://pypi.org/project/commonforms/>
|
| 7 |
-
|
| 8 |
-
Detecta 3 classes de campos: text boxes, checkboxes (choice buttons) e signatures.
|
| 9 |
-
"""
|
| 10 |
-
from __future__ import annotations
|
| 11 |
-
|
| 12 |
-
import os
|
| 13 |
-
|
| 14 |
-
# Força CPU antes de qualquer import que possa inicializar CUDA.
|
| 15 |
-
os.environ.setdefault("CUDA_VISIBLE_DEVICES", "")
|
| 16 |
-
os.environ.setdefault("NVIDIA_VISIBLE_DEVICES", "")
|
| 17 |
-
|
| 18 |
-
import inspect
|
| 19 |
-
import tempfile
|
| 20 |
-
from pathlib import Path
|
| 21 |
-
|
| 22 |
-
import gradio as gr
|
| 23 |
-
from commonforms import prepare_form
|
| 24 |
-
|
| 25 |
-
_PARAMS = inspect.signature(prepare_form).parameters
|
| 26 |
-
print(f"[commonforms] prepare_form signature: {list(_PARAMS.keys())}")
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
def detect_fields(
|
| 30 |
-
pdf_path: str | None,
|
| 31 |
-
image_size: int,
|
| 32 |
-
use_signature_fields: bool,
|
| 33 |
-
keep_existing_fields: bool,
|
| 34 |
-
) -> str:
|
| 35 |
-
if not pdf_path:
|
| 36 |
-
raise gr.Error("Envie um PDF.")
|
| 37 |
-
|
| 38 |
-
src = Path(pdf_path)
|
| 39 |
-
if not src.exists():
|
| 40 |
-
raise gr.Error(f"Arquivo não encontrado: {src}")
|
| 41 |
-
|
| 42 |
-
_, out_str = tempfile.mkstemp(suffix="_fillable.pdf")
|
| 43 |
-
out = Path(out_str)
|
| 44 |
-
|
| 45 |
-
optional = {
|
| 46 |
-
"image_size": int(image_size),
|
| 47 |
-
"use_signature_fields": bool(use_signature_fields),
|
| 48 |
-
"keep_existing_fields": bool(keep_existing_fields),
|
| 49 |
-
"device": "cpu",
|
| 50 |
-
"model_or_path": "jbarrow/FFDNet-L-cpu",
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
}
|
| 54 |
-
accepted = {k: v for k, v in optional.items() if k in _PARAMS}
|
| 55 |
-
print(f"[commonforms] calling prepare_form with kwargs: {accepted}")
|
| 56 |
-
|
| 57 |
-
try:
|
| 58 |
-
# input/output passados posicionalmente — robusto ao nome real do param
|
| 59 |
-
prepare_form(str(src), str(out), **accepted)
|
| 60 |
-
except Exception as exc:
|
| 61 |
-
raise gr.Error(f"Falha ao processar PDF: {exc}") from exc
|
| 62 |
-
|
| 63 |
-
return str(out)
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
with gr.Blocks(title="CommonForms — Form Field Detector") as demo:
|
| 67 |
-
gr.Markdown(
|
| 68 |
-
"# CommonForms — Form Field Detector\n"
|
| 69 |
-
"Converte um PDF em formulário preenchível usando **FFDNet-L** "
|
| 70 |
-
"(`jbarrow/FFDNet-L-cpu`, Object Detection ONNX em CPU). "
|
| 71 |
-
"Detecta *text boxes*, *checkboxes* e *signature fields*.\n\n"
|
| 72 |
-
"Paper: [arxiv 2509.16506](<https://arxiv.org/abs/2509.16506>) · "
|
| 73 |
-
"Modelo: [jbarrow/FFDNet-L-cpu](<https://huggingface.co/jbarrow/FFDNet-L-cpu>)"
|
| 74 |
-
)
|
| 75 |
-
with gr.Row():
|
| 76 |
-
with gr.Column():
|
| 77 |
-
pdf_in = gr.File(
|
| 78 |
-
label="PDF de entrada",
|
| 79 |
-
file_types=[".pdf"],
|
| 80 |
-
type="filepath",
|
| 81 |
-
)
|
| 82 |
-
image_size = gr.Slider(
|
| 83 |
-
minimum=512,
|
| 84 |
-
maximum=2048,
|
| 85 |
-
value=1600,
|
| 86 |
-
step=32,
|
| 87 |
-
label="Image size (px)",
|
| 88 |
-
info="Tamanho usado na inferência. Maior = mais preciso, mais lento.",
|
| 89 |
-
)
|
| 90 |
-
use_sig = gr.Checkbox(
|
| 91 |
-
value=False,
|
| 92 |
-
label="Incluir signature fields",
|
| 93 |
-
info="Detecta áreas de assinatura além de text/checkbox.",
|
| 94 |
-
)
|
| 95 |
-
keep = gr.Checkbox(
|
| 96 |
-
value=False,
|
| 97 |
-
label="Manter campos já existentes",
|
| 98 |
-
info="Preserva widgets AcroForm que já estavam no PDF.",
|
| 99 |
-
)
|
| 100 |
-
btn = gr.Button("Detectar campos", variant="primary")
|
| 101 |
-
with gr.Column():
|
| 102 |
-
pdf_out = gr.File(label="PDF preenchível")
|
| 103 |
-
|
| 104 |
-
btn.click(
|
| 105 |
-
fn=detect_fields,
|
| 106 |
-
inputs=[pdf_in, image_size, use_sig, keep],
|
| 107 |
-
outputs=pdf_out,
|
| 108 |
-
api_name="detect",
|
| 109 |
-
)
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
if __name__ == "__main__":
|
| 113 |
-
demo.queue(max_size=4).launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|