Upload folder using huggingface_hub
Browse files- .gitattributes +1 -0
- README.md +16 -4
- docs/gui.png +3 -0
- src/fiberpose/web/__init__.py +9 -1
- src/fiberpose/web/usage.py +43 -0
.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`,
|
| 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 "
|
| 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
|
| 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
|
| 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 |
+

|
| 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
|
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}")
|