luisabwk commited on
Commit
97214f8
·
verified ·
1 Parent(s): 1b9b5e8

Delete app.py

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