luisabwk commited on
Commit
6a186d2
·
verified ·
1 Parent(s): 6c56d01

Hello world

Browse files
Files changed (5) hide show
  1. README 2.md +111 -0
  2. app.py +109 -65
  3. ffdnet_model.py +50 -0
  4. gitignore.txt +6 -0
  5. requirements.txt +4 -0
README 2.md ADDED
@@ -0,0 +1,111 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: pdfFormDetector
3
+ emoji: 🖼️
4
+ colorFrom: blue
5
+ colorTo: indigo
6
+ sdk: gradio
7
+ sdk_version: 4.44.0
8
+ app_file: app.py
9
+ pinned: false
10
+ license: mit
11
+ ---
12
+
13
+ # FFDNet Space (pdfFormDetector)
14
+
15
+ Gradio UI + API HTTP para o denoiser FFDNet (Zhang et al., 2018), rodando
16
+ em CPU no HF Spaces grátis.
17
+
18
+ ## Arquivos
19
+
20
+ | Arquivo | Propósito |
21
+ |-------------------|-------------------------------------------------------------------|
22
+ | `app.py` | UI Gradio + endpoint `/denoise` |
23
+ | `ffdnet_model.py` | FFDNet self-contained (compatível com pesos oficiais do KAIR) |
24
+ | `requirements.txt`| Dependências (torch, gradio, numpy, pillow) |
25
+ | `.gitignore` | Ignora `weights/` (baixado em runtime no primeiro start) |
26
+
27
+ Os pesos são baixados no primeiro start do Space a partir do release v1.0 de
28
+ https://github.com/cszn/KAIR (`ffdnet_color.pth` ~4 MB, `ffdnet_gray.pth` ~1 MB).
29
+
30
+ ## Subindo para o HF Space
31
+
32
+ O Space é https://huggingface.co/spaces/luisabwk/pdfFormDetector. Opções:
33
+
34
+ ### Opção 1 — git clone + copy (mais direto)
35
+
36
+ ```bash
37
+ # 1. clone o Space em outra pasta
38
+ git clone https://huggingface.co/spaces/luisabwk/pdfFormDetector /tmp/pdfFormDetector
39
+ cd /tmp/pdfFormDetector
40
+
41
+ # 2. copie os arquivos do Cortex
42
+ cp /caminho/para/cortex-mvp/services/ffdnet-space/*.py .
43
+ cp /caminho/para/cortex-mvp/services/ffdnet-space/requirements.txt .
44
+ cp /caminho/para/cortex-mvp/services/ffdnet-space/.gitignore .
45
+ cp /caminho/para/cortex-mvp/services/ffdnet-space/README.md .
46
+
47
+ # 3. commit + push (precisa de HF token com write — gere em https://huggingface.co/settings/tokens)
48
+ git add -A
49
+ git commit -m "feat: FFDNet denoiser via Gradio"
50
+ git push
51
+ ```
52
+
53
+ ### Opção 2 — huggingface_hub CLI
54
+
55
+ ```bash
56
+ pip install -U huggingface_hub
57
+ huggingface-cli login # cola o token write
58
+
59
+ huggingface-cli upload luisabwk/pdfFormDetector \
60
+ /caminho/para/cortex-mvp/services/ffdnet-space \
61
+ . \
62
+ --repo-type=space \
63
+ --commit-message "feat: FFDNet denoiser via Gradio"
64
+ ```
65
+
66
+ ### Opção 3 — git subtree (mantém sync com o Cortex)
67
+
68
+ ```bash
69
+ # dentro do repo cortex-mvp, na raiz
70
+ git remote add ffdnet-hf https://huggingface.co/spaces/luisabwk/pdfFormDetector
71
+ git subtree push --prefix=services/ffdnet-space ffdnet-hf main
72
+ ```
73
+
74
+ ## Uso da API
75
+
76
+ Depois do Space buildar, a API fica disponível em:
77
+
78
+ ```bash
79
+ curl -X POST https://luisabwk-pdfformdetector.hf.space/api/denoise \
80
+ -H "Content-Type: application/json" \
81
+ -d '{"data": ["<base64 data URL ou URL pública>", 15, "color"]}'
82
+ ```
83
+
84
+ Ou via `gradio_client`:
85
+
86
+ ```python
87
+ from gradio_client import Client, handle_file
88
+
89
+ client = Client("luisabwk/pdfFormDetector")
90
+ result = client.predict(
91
+ image=handle_file("/caminho/para/imagem.png"),
92
+ noise_sigma=15,
93
+ mode="color",
94
+ api_name="/denoise",
95
+ )
96
+ print(result) # path para o PNG denoised
97
+ ```
98
+
99
+ ## Parâmetros
100
+
101
+ - **σ (sigma)**: 0–75. Valores típicos: 10–15 (ruído baixo), 20–30 (scan médio),
102
+ 40+ (foto de celular com pouca luz). Valor alto em imagem limpa **borra**.
103
+ - **mode**: `color` (RGB) ou `gray` (grayscale; melhor pra documento texto).
104
+
105
+ ## Limitações do CPU grátis
106
+
107
+ - ~2–6s para uma A4 300dpi.
108
+ - Cold start: o container hiberna após ~48h ocioso (1ª chamada após dormir
109
+ demora 20–60s).
110
+ - Memória: 16 GB; imagens muito grandes (>8 MP) podem estourar — pré-resize
111
+ no cliente se necessário.
app.py CHANGED
@@ -1,69 +1,113 @@
 
 
 
 
 
 
 
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
3
-
4
-
5
- def respond(
6
- message,
7
- history: list[dict[str, str]],
8
- system_message,
9
- max_tokens,
10
- temperature,
11
- top_p,
12
- hf_token: gr.OAuthToken,
13
- ):
14
- """
15
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
16
- """
17
- client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
18
-
19
- messages = [{"role": "system", "content": system_message}]
20
-
21
- messages.extend(history)
22
-
23
- messages.append({"role": "user", "content": message})
24
-
25
- response = ""
26
-
27
- for message in client.chat_completion(
28
- messages,
29
- max_tokens=max_tokens,
30
- stream=True,
31
- temperature=temperature,
32
- top_p=top_p,
33
- ):
34
- choices = message.choices
35
- token = ""
36
- if len(choices) and choices[0].delta.content:
37
- token = choices[0].delta.content
38
-
39
- response += token
40
- yield response
41
-
42
-
43
- """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
- chatbot = gr.ChatInterface(
47
- respond,
48
- additional_inputs=[
49
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
51
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
52
- gr.Slider(
53
- minimum=0.1,
54
- maximum=1.0,
55
- value=0.95,
56
- step=0.05,
57
- label="Top-p (nucleus sampling)",
58
- ),
59
- ],
60
- )
61
-
62
- with gr.Blocks() as demo:
63
- with gr.Sidebar():
64
- gr.LoginButton()
65
- chatbot.render()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
 
67
 
68
  if __name__ == "__main__":
69
- demo.launch()
 
1
+ """Gradio app for the FFDNet denoiser — roda em CPU no HF Spaces grátis."""
2
+ from __future__ import annotations
3
+
4
+ import os
5
+ import urllib.request
6
+ from pathlib import Path
7
+
8
  import gradio as gr
9
+ import numpy as np
10
+ import torch
11
+ from PIL import Image
12
+
13
+ from ffdnet_model import FFDNet
14
+
15
+ WEIGHTS_DIR = Path(os.environ.get("FFDNET_WEIGHTS_DIR", "weights"))
16
+
17
+ WEIGHTS = {
18
+ "color": {
19
+ "url": "https://github.com/cszn/KAIR/releases/download/v1.0/ffdnet_color.pth",
20
+ "file": WEIGHTS_DIR / "ffdnet_color.pth",
21
+ "in_nc": 3,
22
+ "out_nc": 3,
23
+ "nc": 96,
24
+ "nb": 12,
25
+ },
26
+ "gray": {
27
+ "url": "https://github.com/cszn/KAIR/releases/download/v1.0/ffdnet_gray.pth",
28
+ "file": WEIGHTS_DIR / "ffdnet_gray.pth",
29
+ "in_nc": 1,
30
+ "out_nc": 1,
31
+ "nc": 64,
32
+ "nb": 15,
33
+ },
34
+ }
35
+
36
+ DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
37
+ _MODELS: dict[str, FFDNet] = {}
38
+
39
+
40
+ def _download(url: str, target: Path) -> None:
41
+ if target.exists():
42
+ return
43
+ target.parent.mkdir(parents=True, exist_ok=True)
44
+ print(f"[ffdnet] downloading {url} -> {target}")
45
+ urllib.request.urlretrieve(url, target)
46
+
47
+
48
+ def _load(mode: str) -> FFDNet:
49
+ if mode in _MODELS:
50
+ return _MODELS[mode]
51
+ cfg = WEIGHTS[mode]
52
+ _download(cfg["url"], cfg["file"])
53
+ model = FFDNet(in_nc=cfg["in_nc"], out_nc=cfg["out_nc"], nc=cfg["nc"], nb=cfg["nb"])
54
+ state = torch.load(cfg["file"], map_location=DEVICE, weights_only=True)
55
+ # Strip DataParallel prefix se existir
56
+ state = {k.replace("module.", "", 1): v for k, v in state.items()}
57
+ model.load_state_dict(state, strict=True)
58
+ model.eval().to(DEVICE)
59
+ _MODELS[mode] = model
60
+ return model
61
+
62
+
63
+ def denoise(image: Image.Image, noise_sigma: float, mode: str) -> Image.Image:
64
+ if image is None:
65
+ raise gr.Error("Envie uma imagem.")
66
+ if mode not in WEIGHTS:
67
+ raise gr.Error(f"Modo inválido: {mode}")
68
+
69
+ model = _load(mode)
70
+
71
+ pil = image.convert("RGB" if mode == "color" else "L")
72
+ arr = np.array(pil)
73
+ if arr.ndim == 2:
74
+ arr = arr[..., None]
75
+
76
+ tensor = (
77
+ torch.from_numpy(arr.astype(np.float32) / 255.0)
78
+ .permute(2, 0, 1)
79
+ .unsqueeze(0)
80
+ .to(DEVICE)
81
+ )
82
+ sigma = torch.tensor([float(noise_sigma) / 255.0], dtype=torch.float32, device=DEVICE)
83
+
84
+ with torch.no_grad():
85
+ out = model(tensor, sigma).clamp(0.0, 1.0)
86
+
87
+ out_np = (out.squeeze(0).permute(1, 2, 0).cpu().numpy() * 255.0).round().astype(np.uint8)
88
+ if mode == "gray":
89
+ out_np = out_np[..., 0]
90
+ return Image.fromarray(out_np)
91
+
92
+
93
+ with gr.Blocks(title="FFDNet — Denoiser") as demo:
94
+ gr.Markdown(
95
+ "# FFDNet — Denoiser\n"
96
+ "CNN denoiser com nível de ruído (σ) ajustável. "
97
+ "Útil para limpar scans/fotos antes de OCR. "
98
+ "Primeira chamada baixa os pesos (~4MB/10MB) e pode demorar alguns segundos."
99
+ )
100
+ with gr.Row():
101
+ with gr.Column():
102
+ inp = gr.Image(type="pil", label="Imagem de entrada")
103
+ sigma = gr.Slider(0, 75, value=15, step=1, label="σ — nível de ruído estimado")
104
+ mode = gr.Radio(choices=["color", "gray"], value="color", label="Modo")
105
+ btn = gr.Button("Denoise", variant="primary")
106
+ with gr.Column():
107
+ out_img = gr.Image(type="pil", label="Resultado")
108
+
109
+ btn.click(fn=denoise, inputs=[inp, sigma, mode], outputs=out_img, api_name="denoise")
110
 
111
 
112
  if __name__ == "__main__":
113
+ demo.queue(max_size=8).launch()
ffdnet_model.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """FFDNet denoiser (Zhang et al., 2018 — https://arxiv.org/abs/1710.04026).
2
+
3
+ A arquitetura espelha exatamente a implementação oficial do KAIR
4
+ (https://github.com/cszn/KAIR, arquivos `models/network_ffdnet.py` +
5
+ `models/basicblock.py`) para que os pesos pré-treinados do release v1.0
6
+ carreguem com `strict=True`, sem rename de chaves.
7
+
8
+ Convs são empilhadas num único `nn.Sequential` (sem blocos aninhados),
9
+ reproduzindo o comportamento do `basicblock.sequential(...)` do KAIR que
10
+ achata filhos de `nn.Sequential`.
11
+
12
+ Configurações oficiais:
13
+ - color: in_nc=3, out_nc=3, nc=96, nb=12 (ffdnet_color.pth)
14
+ - gray: in_nc=1, out_nc=1, nc=64, nb=15 (ffdnet_gray.pth)
15
+ """
16
+ from __future__ import annotations
17
+
18
+ import math
19
+
20
+ import torch
21
+ import torch.nn as nn
22
+ import torch.nn.functional as F
23
+
24
+
25
+ class FFDNet(nn.Module):
26
+ def __init__(self, in_nc: int = 3, out_nc: int = 3, nc: int = 96, nb: int = 12):
27
+ super().__init__()
28
+ self.sf = 2
29
+
30
+ layers: list[nn.Module] = []
31
+ layers.append(nn.Conv2d(in_nc * self.sf * self.sf + 1, nc, 3, 1, 1, bias=True))
32
+ layers.append(nn.ReLU(inplace=True))
33
+ for _ in range(nb - 2):
34
+ layers.append(nn.Conv2d(nc, nc, 3, 1, 1, bias=True))
35
+ layers.append(nn.ReLU(inplace=True))
36
+ layers.append(nn.Conv2d(nc, out_nc * self.sf * self.sf, 3, 1, 1, bias=True))
37
+ self.model = nn.Sequential(*layers)
38
+
39
+ def forward(self, x: torch.Tensor, sigma: torch.Tensor) -> torch.Tensor:
40
+ h, w = x.shape[-2], x.shape[-1]
41
+ pad_b = int(math.ceil(h / 2) * 2 - h)
42
+ pad_r = int(math.ceil(w / 2) * 2 - w)
43
+ x = F.pad(x, (0, pad_r, 0, pad_b), mode="replicate")
44
+
45
+ x = F.pixel_unshuffle(x, self.sf)
46
+ noise_map = sigma.view(-1, 1, 1, 1).expand(x.shape[0], 1, x.shape[-2], x.shape[-1])
47
+ x = torch.cat([x, noise_map], dim=1)
48
+ x = self.model(x)
49
+ x = F.pixel_shuffle(x, self.sf)
50
+ return x[..., :h, :w]
gitignore.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ weights/
2
+ __pycache__/
3
+ *.pyc
4
+ .DS_Store
5
+ .gradio/
6
+ flagged/
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ gradio==4.44.0
2
+ torch==2.4.1
3
+ numpy<2
4
+ pillow