vikba commited on
Commit
be2fe7e
Β·
verified Β·
1 Parent(s): 405af39

Upload folder using huggingface_hub

Browse files
.gitattributes CHANGED
@@ -34,3 +34,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
  src/fiberpose/resources/test.png filter=lfs diff=lfs merge=lfs -text
 
 
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
  src/fiberpose/resources/test.png filter=lfs diff=lfs merge=lfs -text
37
+ docs/gui.png filter=lfs diff=lfs merge=lfs -text
README.md CHANGED
@@ -16,6 +16,10 @@ license: gpl-3.0
16
 
17
  It is designed for fibrous materials where conventional thresholding becomes fragile: dense networks, weak contrast, uneven illumination, touching fibers, and overlapping structures.
18
 
 
 
 
 
19
  > Status: research-preview software under active development. Validate outputs on your own data before using them for publication or production decisions.
20
 
21
  ---
@@ -81,7 +85,7 @@ Running `pip install -e .` again reinstalls the CPU build, so repeat these two c
81
 
82
  ## Pretrained model
83
 
84
- The default model, `R108_dt_fg.onnx`, is downloaded automatically on first use and cached locally. Fiberpose looks for models, by file name, in this order:
85
 
86
  1. the folder set in the `FIBERPOSE_MODELS_DIR` environment variable;
87
  2. `checkpoints/` in the repository, when running from a source checkout;
@@ -173,13 +177,15 @@ fiberpose --cli --input /path/to/image_or_folder_or_stack --measurements --save-
173
 
174
  ### Web interface
175
 
 
 
176
  ```bash
177
- pip install "fiberpose[web]"
178
  fiberpose --web # http://localhost:7860
179
  ```
180
 
181
  The same page runs in Docker (`docker build -t fiberpose . && docker run -p 7860:7860 fiberpose`)
182
- and as a Hugging Face Docker Space; `.github/workflows/deploy.yml` publishes both on a version tag.
183
 
184
  ### Python API
185
 
@@ -262,6 +268,11 @@ Fiberpose/
262
  β”œβ”€β”€ README.md
263
  β”œβ”€β”€ PIPELINE.md # Runtime pipeline details
264
  β”œβ”€β”€ LICENSE
 
 
 
 
 
265
  └── src/fiberpose/
266
  β”œβ”€β”€ cli.py # GUI/CLI/web launcher (`fiberpose` command)
267
  β”œβ”€β”€ api.py # Public Python API: analyze()
@@ -273,6 +284,7 @@ Fiberpose/
273
  β”œβ”€β”€ pipeline.py # Pipeline orchestration
274
  β”œβ”€β”€ analysis.py # Stateless prediction, segmentation, and measurement functions
275
  β”œβ”€β”€ render.py # Display/export images of a frame (overlays, views)
 
276
  β”œβ”€β”€ ridge_detector.py # Ridge/centerline detector
277
  β”œβ”€β”€ ml.py # ONNX Runtime inference wrapper
278
  β”œβ”€β”€ domain.py # Data models and configuration
@@ -290,7 +302,7 @@ Fiberpose/
290
 
291
  ### Model file not found
292
 
293
- The default model is downloaded on first use, which needs an internet connection. On an offline machine, copy `R108_dt_fg.onnx` into a folder and point `FIBERPOSE_MODELS_DIR` at it. For a custom model, check that `model_path` in your configuration is a file name in the models folder or an absolute path.
294
 
295
  ### GPU is not used
296
 
 
16
 
17
  It is designed for fibrous materials where conventional thresholding becomes fragile: dense networks, weak contrast, uneven illumination, touching fibers, and overlapping structures.
18
 
19
+ ![Fiberpose desktop GUI: instance segmentation with diameter measurements, diameter and orientation histograms](docs/gui.png)
20
+
21
+ **Try it in the browser:** [Fiberpose on Hugging Face Spaces](https://huggingface.co/spaces/vikba/fiberpose) β€” no installation needed.
22
+
23
  > Status: research-preview software under active development. Validate outputs on your own data before using them for publication or production decisions.
24
 
25
  ---
 
85
 
86
  ## Pretrained model
87
 
88
+ The default model, `R108_dt_fg.onnx`, ships with the repository in `checkpoints/`, so a source checkout works offline. Fiberpose looks for models, by file name, in this order:
89
 
90
  1. the folder set in the `FIBERPOSE_MODELS_DIR` environment variable;
91
  2. `checkpoints/` in the repository, when running from a source checkout;
 
177
 
178
  ### Web interface
179
 
180
+ A hosted version runs at <https://huggingface.co/spaces/vikba/fiberpose>. To run it locally, from the repository folder:
181
+
182
  ```bash
183
+ pip install -e ".[web]"
184
  fiberpose --web # http://localhost:7860
185
  ```
186
 
187
  The same page runs in Docker (`docker build -t fiberpose . && docker run -p 7860:7860 fiberpose`)
188
+ and as a Hugging Face Gradio Space; `.github/workflows/deploy.yml` publishes both on a version tag.
189
 
190
  ### Python API
191
 
 
268
  β”œβ”€β”€ README.md
269
  β”œβ”€β”€ PIPELINE.md # Runtime pipeline details
270
  β”œβ”€β”€ LICENSE
271
+ β”œβ”€β”€ app.py # Entry point of the Hugging Face Space
272
+ β”œβ”€β”€ requirements.txt # Dependencies of the Hugging Face Space
273
+ β”œβ”€β”€ Dockerfile # Web interface as a container
274
+ β”œβ”€β”€ checkpoints/ # Pretrained model (R108_dt_fg.onnx)
275
+ β”œβ”€β”€ docs/ # README images
276
  └── src/fiberpose/
277
  β”œβ”€β”€ cli.py # GUI/CLI/web launcher (`fiberpose` command)
278
  β”œβ”€β”€ api.py # Public Python API: analyze()
 
284
  β”œβ”€β”€ pipeline.py # Pipeline orchestration
285
  β”œβ”€β”€ analysis.py # Stateless prediction, segmentation, and measurement functions
286
  β”œβ”€β”€ render.py # Display/export images of a frame (overlays, views)
287
+ β”œβ”€β”€ raster.py # Vectorized disk rasterization helpers
288
  β”œβ”€β”€ ridge_detector.py # Ridge/centerline detector
289
  β”œβ”€β”€ ml.py # ONNX Runtime inference wrapper
290
  β”œβ”€β”€ domain.py # Data models and configuration
 
302
 
303
  ### Model file not found
304
 
305
+ The default model is expected in `checkpoints/` of the source checkout. If Fiberpose runs from elsewhere, copy `R108_dt_fg.onnx` into a folder and point `FIBERPOSE_MODELS_DIR` at it. For a custom model, check that `model_path` in your configuration is a file name in the models folder or an absolute path.
306
 
307
  ### GPU is not used
308
 
docs/gui.png ADDED

Git LFS Details

  • SHA256: d1dead2498fe8b186ffdd567a55172212e7a1ac7e10e1698c40aaf9682225a04
  • Pointer size: 132 Bytes
  • Size of remote file: 1.48 MB
src/fiberpose/web/__init__.py CHANGED
@@ -4,12 +4,14 @@
4
  Analysis settings come from the same config file as the GUI and CLI.
5
  Host, port and proxy path come from Gradio's own environment variables
6
  (``GRADIO_SERVER_NAME``, ``GRADIO_SERVER_PORT``, ``GRADIO_ROOT_PATH``);
7
- ``FIBERPOSE_MAX_UPLOAD`` caps the upload size (default ``50mb``).
 
8
  """
9
 
10
  from __future__ import annotations
11
 
12
  import os
 
13
  from dataclasses import replace
14
 
15
  import gradio as gr
@@ -18,6 +20,7 @@ from ..api import FiberposeError
18
  from ..domain import AppConfig
19
  from ..paths import EXAMPLE_IMAGE_PATH
20
  from . import callbacks as cb
 
21
 
22
  __all__ = ["build_demo", "launch"]
23
 
@@ -34,10 +37,15 @@ def build_demo(config: AppConfig | None = None) -> gr.Blocks:
34
  if not path:
35
  raise gr.Error("Upload an image first.")
36
  settings = replace(config, watershed_grad_threshold=edge_threshold)
 
 
37
  try:
38
  session = cb.analyze_file(path, settings, pixel_size, unit, invert, previous)
39
  except FiberposeError as exc:
 
40
  raise gr.Error(str(exc)) from exc
 
 
41
  if session.result.message:
42
  gr.Info(session.result.message)
43
  figure, svg = cb.charts(session)
 
4
  Analysis settings come from the same config file as the GUI and CLI.
5
  Host, port and proxy path come from Gradio's own environment variables
6
  (``GRADIO_SERVER_NAME``, ``GRADIO_SERVER_PORT``, ``GRADIO_ROOT_PATH``);
7
+ ``FIBERPOSE_MAX_UPLOAD`` caps the upload size (default ``50mb``);
8
+ ``FIBERPOSE_USAGE_REPO`` turns on the usage log (see ``usage.py``).
9
  """
10
 
11
  from __future__ import annotations
12
 
13
  import os
14
+ import time
15
  from dataclasses import replace
16
 
17
  import gradio as gr
 
20
  from ..domain import AppConfig
21
  from ..paths import EXAMPLE_IMAGE_PATH
22
  from . import callbacks as cb
23
+ from . import usage
24
 
25
  __all__ = ["build_demo", "launch"]
26
 
 
37
  if not path:
38
  raise gr.Error("Upload an image first.")
39
  settings = replace(config, watershed_grad_threshold=edge_threshold)
40
+ example = os.path.basename(path) == EXAMPLE_IMAGE_PATH.name
41
+ start = time.perf_counter()
42
  try:
43
  session = cb.analyze_file(path, settings, pixel_size, unit, invert, previous)
44
  except FiberposeError as exc:
45
+ usage.log(ok=False, seconds=round(time.perf_counter() - start, 2), example=example)
46
  raise gr.Error(str(exc)) from exc
47
+ height, width = session.result.frame.orig_img.shape[:2]
48
+ usage.log(ok=True, seconds=round(time.perf_counter() - start, 2), width=width, height=height, example=example)
49
  if session.result.message:
50
  gr.Info(session.result.message)
51
  figure, svg = cb.charts(session)
src/fiberpose/web/usage.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Usage log of the web interface: one JSON line per analysis, kept in a private Hugging Face dataset.
2
+
3
+ Off unless ``FIBERPOSE_USAGE_REPO`` names the dataset; uploading needs a write token in ``HF_TOKEN``.
4
+ """
5
+
6
+ from __future__ import annotations
7
+
8
+ import json
9
+ import os
10
+ import tempfile
11
+ import threading
12
+ import uuid
13
+ from datetime import datetime, timezone
14
+ from pathlib import Path
15
+
16
+ _lock = threading.Lock()
17
+ _scheduler = None # uploads the folder of `_file` every few minutes
18
+ _file: Path | None = None
19
+ _failed = False
20
+
21
+
22
+ def log(**fields) -> None:
23
+ """Append one event; never raises, and stops trying after the first failure."""
24
+ global _scheduler, _file, _failed
25
+ repo = os.environ.get("FIBERPOSE_USAGE_REPO")
26
+ if not repo or _failed:
27
+ return
28
+ try:
29
+ with _lock:
30
+ if _scheduler is None:
31
+ from huggingface_hub import CommitScheduler
32
+
33
+ folder = Path(tempfile.mkdtemp(prefix="fiberpose_usage_"))
34
+ # A Space loses its disk on restart, so every process logs to a file of its own:
35
+ # a fixed name would replace the history already in the dataset.
36
+ _file = folder / f"events-{uuid.uuid4().hex}.jsonl"
37
+ _scheduler = CommitScheduler(repo_id=repo, repo_type="dataset", folder_path=folder, private=True, every=5)
38
+ event = {"ts": datetime.now(timezone.utc).isoformat(timespec="seconds"), **fields}
39
+ with _scheduler.lock, _file.open("a") as lines:
40
+ lines.write(json.dumps(event) + "\n")
41
+ except Exception as exc: # logging must never break an analysis
42
+ _failed = True
43
+ print(f"Usage logging disabled: {exc}")