ga11en commited on
Commit
cae2a70
·
verified ·
1 Parent(s): 7b06682

Public demo with Nuitka-compiled wheel

Browse files
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip 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
 
 
33
  *.zip 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
+ polygen-0.1.0b1-cp313-cp313-linux_x86_64.whl filter=lfs diff=lfs merge=lfs -text
Dockerfile ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Docker Space for the polygen tokenization demo (public).
2
+ #
3
+ # A gradio-SDK Space cannot install the wheel (HF mounts requirements.txt and
4
+ # runs pip before copying repo files), and polygen requires Python 3.13. A
5
+ # Docker base solves both. The wheel is Nuitka-compiled (a binary .so, no
6
+ # readable source), so a public Space does not expose the SDK source;
7
+ # mlx[cpu] resolves at install on Linux x86.
8
+ FROM python:3.13-slim
9
+
10
+ WORKDIR /app
11
+
12
+ # git: required by HF Spaces' build wrapper (git config / dev-mode steps),
13
+ # which the slim base lacks. curl is handy for healthchecks.
14
+ RUN apt-get update && apt-get install -y --no-install-recommends git curl \
15
+ && rm -rf /var/lib/apt/lists/*
16
+
17
+ # Deps first (wheel + requirements) so this layer caches across code edits.
18
+ COPY requirements.txt polygen-0.1.0b1-cp313-cp313-linux_x86_64.whl ./
19
+ RUN pip install --no-cache-dir -r requirements.txt
20
+
21
+ # App code.
22
+ COPY . .
23
+
24
+ # gradio binds all interfaces on the HF app port; analytics off.
25
+ ENV GRADIO_SERVER_NAME=0.0.0.0 \
26
+ GRADIO_SERVER_PORT=7860 \
27
+ GRADIO_ANALYTICS_ENABLED=False \
28
+ GRADIO_SSR_MODE=False \
29
+ GRADIO_TEMP_DIR=/tmp/gradio
30
+
31
+ EXPOSE 7860
32
+ CMD ["python", "app.py"]
README.md CHANGED
@@ -1,10 +1,57 @@
1
  ---
2
- title: Polygen Demo
3
- emoji: 🌍
4
- colorFrom: red
5
- colorTo: yellow
6
  sdk: docker
 
7
  pinned: false
8
  ---
9
 
10
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
+ title: Datasent's Polygen Tokenization Demo
3
+ emoji: 📈
4
+ colorFrom: indigo
5
+ colorTo: blue
6
  sdk: docker
7
+ app_port: 7860
8
  pinned: false
9
  ---
10
 
11
+ # Datasent's Polygen Tokenization Demo
12
+
13
+ An interactive demo of Datasent's Polygen SDK on numeric signals. Pick a sample (or
14
+ upload a CSV), and it tokenizes the signal through the real SDK and reports:
15
+
16
+ - **Lossless round-trip** -- EXACT decode is bit-faithful to the 1/scale grid.
17
+ - **COARSE analytics archive** -- coefficients only, tens to hundreds of times
18
+ smaller than raw, and directly queryable (the coefficients are a feature
19
+ vector).
20
+ - **Per-segment anomaly signal** (sigma_R) -- a free quality marker; segments
21
+ the model fits poorly stand out.
22
+ - **EXACT lossless size** shown honestly against gzip (it sits at parity --
23
+ polygen is a tokenizer, not a byte-compression replacement).
24
+
25
+ Everything goes through the supported SDK surface: `DataMatrix.from_float` ->
26
+ `PolygenTokenizer.tokenize(..., parallel=False)` with `BinaryEncoder` for a
27
+ real token, and `tokenizer.decode` for EXACT / COARSE. The in-process
28
+ `parallel=False` path means no multiprocessing pool, which keeps the Space
29
+ light.
30
+
31
+ ## Running locally
32
+
33
+ ```bash
34
+ pip install -r requirements.txt
35
+ # plus polygen on the path (editable dev install or the stable wheel)
36
+ POLYGEN_DEMO_LOCAL_MOCK=1 python app.py
37
+ ```
38
+
39
+ `POLYGEN_DEMO_LOCAL_MOCK=1` generates a self-consistent throwaway license for
40
+ local dev (the same pattern `tests/conftest.py` uses). Do not set it in
41
+ production.
42
+
43
+ ## Deploying to a HuggingFace Space
44
+
45
+ 1. Install `polygen` via `requirements.txt` (point at the stable wheel URL).
46
+ 2. Set the `POLYGEN_LICENSE` secret to a real JWT issued for that wheel.
47
+ - Note: this depends on the license keypair being aligned (audit finding
48
+ F8). The bundled public key must match the key that signed the JWT.
49
+ 3. Leave `POLYGEN_DEMO_LOCAL_MOCK` unset so the SDK validates the real license.
50
+
51
+ ## Notes
52
+
53
+ - This is a prototype. CPU-only; no GPU / RunPod needed (polygen tokenization
54
+ is CPU-light). RunPod is only warranted for GPU demos such as the VLM Space.
55
+ - Decide stable-vs-dev SDK before a customer-facing deploy. This prototype
56
+ uses dev-only surface (`from_float`, `sigma_r`, `DecodeMode`, the
57
+ `parallel=False` path). The frozen stable wheel currently lacks some of it.
app.py ADDED
@@ -0,0 +1,155 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Polygen tokenization demo -- Gradio Space.
2
+
3
+ Tokenizes a numeric signal through the polygen SDK and shows what the
4
+ tokenizer buys you beyond byte compression: a lossless round-trip, a compact
5
+ queryable analytics archive (COARSE), and a per-segment anomaly signal
6
+ (sigma_R). EXACT lossless is shown honestly -- it sits at gzip parity; the
7
+ wins are the analytics archive, the coefficients-as-features, and the anomaly
8
+ signal, none of which a byte codec can provide.
9
+
10
+ Local: ``POLYGEN_DEMO_LOCAL_MOCK=1 python app.py``.
11
+ Deploy: set the ``POLYGEN_LICENSE`` secret on the Space.
12
+ """
13
+
14
+ import license_bootstrap
15
+
16
+ license_bootstrap.ensure_license() # must run before polygen is imported
17
+
18
+ import gradio as gr # noqa: E402
19
+ import numpy as np # noqa: E402
20
+
21
+ import demo_core # noqa: E402
22
+ import plots # noqa: E402
23
+
24
+ # Datasent 2026 brand: Minsk purple primary, bright blue accent, lavender
25
+ # light shades, white page. Mirrors docs/polygen_theme/custom.css and the
26
+ # qwen2vl_polygen Space, so the two demos share a house style.
27
+ THEME = gr.themes.Soft(
28
+ primary_hue="indigo",
29
+ neutral_hue="slate",
30
+ text_size=gr.themes.sizes.text_md,
31
+ radius_size=gr.themes.sizes.radius_lg,
32
+ ).set(
33
+ block_border_width="1px",
34
+ block_shadow="*shadow_drop_lg",
35
+ button_primary_text_color="white",
36
+ )
37
+
38
+ BRAND_CSS = """
39
+ :root {
40
+ --ds-minsk:#3c3475; --ds-blue:#4caaff; --ds-dark-3:#6464a0;
41
+ --ds-light-2:#ced8f8; --ds-light-4:#e6ebfc;
42
+ }
43
+ #ds-hero { text-align:center; padding:14px 0 4px 0; }
44
+ #ds-hero img { height:34px; display:block; margin:0 auto 10px; }
45
+ #ds-hero .eyebrow { font-size:0.8em; letter-spacing:0.18em; text-transform:uppercase;
46
+ color:var(--ds-dark-3); margin-bottom:6px; }
47
+ #ds-hero h1 { margin:0 0 8px 0; font-size:2.1em; line-height:1.1; color:var(--ds-minsk); }
48
+ #ds-hero .tagline { color:#555; font-size:1.05em; max-width:680px; margin:0 auto; }
49
+ #ds-summary h3 { color:var(--ds-minsk); }
50
+ #ds-summary strong { color:var(--ds-minsk); }
51
+ #ds-summary a { color:var(--ds-blue); }
52
+ #ds-summary table th { background:var(--ds-light-4); color:var(--ds-minsk); }
53
+ #ds-summary table tr:nth-child(even) td { background:var(--ds-light-4); }
54
+ """
55
+
56
+ HERO_HTML = """
57
+ <div id="ds-hero">
58
+ <img src="https://datasent-demo.com/images/datasent-logo.svg" alt="Datasent"
59
+ onerror="this.style.display='none'"/>
60
+ <div class="eyebrow">Datasent &middot; Polygen</div>
61
+ <h1>Tokenization Demo</h1>
62
+ <div class="tagline">Fit a compact model to each window of a numeric signal and
63
+ store it as a token. Lossless when you want it, a tiny queryable archive when
64
+ you don't, and a built-in anomaly signal either way.</div>
65
+ </div>
66
+ """
67
+
68
+
69
+ def _load_signal(sample_name: str, csv_file) -> np.ndarray:
70
+ if csv_file is not None:
71
+ # gradio 5 returns a filepath string; 6 may return an object with .name.
72
+ path = getattr(csv_file, "name", csv_file)
73
+ raw = np.genfromtxt(path, delimiter=",", skip_header=0)
74
+ arr = np.asarray(raw, dtype=np.float32)
75
+ if arr.ndim == 1:
76
+ arr = arr.reshape(-1, 1)
77
+ # Drop all-NaN columns (header rows etc.) and NaN rows.
78
+ arr = arr[:, ~np.all(np.isnan(arr), axis=0)]
79
+ arr = arr[~np.any(np.isnan(arr), axis=1)]
80
+ if arr.size == 0:
81
+ raise gr.Error("Could not parse numeric columns from the CSV.")
82
+ return arr
83
+ return demo_core.SAMPLES[sample_name]()
84
+
85
+
86
+ def _summary(r: dict) -> str:
87
+ anom = int(np.sum(r["sigma_r"] > np.mean(r["sigma_r"]) + 2 * np.std(r["sigma_r"]))) if r["segments"] > 1 else 0
88
+ bases = ", ".join(f"{k} x{v}" for k, v in r["bases_used"].items())
89
+ picker = "MDL chose" if r["basis_mode"] == "auto" else "fixed"
90
+ return f"""
91
+ ### Results -- {r["n"]:,} rows x {r["d"]} channel(s), {r["segments"]} segments
92
+
93
+ | What | Result |
94
+ |---|---|
95
+ | **Bases ({picker})** | {bases} |
96
+ | **Lossless round-trip** | max error **{r["max_err"]:.1e}** (exact to the 1/{r["scale"]} grid) |
97
+ | **COARSE analytics archive** | **{r["coarse_ratio_vs_raw"]:.0f}x** smaller than raw -- coefficients only, queryable directly |
98
+ | **EXACT lossless archive** | {r["token_size"] / 1024:.1f} KB vs gzip {r["gzip_size"] / 1024:.1f} KB (**{r["ratio_vs_gzip"]:.2f}x**) |
99
+ | **Anomaly segments flagged** | **{anom}** of {r["segments"]} (sigma_R > mean + 2 sigma) |
100
+
101
+ Polygen is a tokenizer, not a gzip replacement. With **MDL multi-basis** the
102
+ SDK picks the best basis family per segment. EXACT lossless sits near gzip; the
103
+ value a byte codec cannot match is the COARSE archive (the signal's shape at a
104
+ fraction of the size), the coefficients as a ready-to-use feature vector, and a
105
+ per-segment anomaly score for free.
106
+ """.strip()
107
+
108
+
109
+ def run(sample_name: str, csv_file, basis_mode: str, degree: int, segment_length: int):
110
+ signal = _load_signal(sample_name, csv_file)
111
+ r = demo_core.run_demo(
112
+ signal, basis_mode=basis_mode, degree=int(degree), segment_length=int(segment_length),
113
+ )
114
+ return _summary(r), plots.fig_reconstruction(r), plots.fig_sigma(r), plots.fig_sizes(r)
115
+
116
+
117
+ def build_ui() -> gr.Blocks:
118
+ with gr.Blocks(title="Datasent's Polygen Tokenization Demo", theme=THEME, css=BRAND_CSS) as ui:
119
+ gr.HTML(HERO_HTML)
120
+ with gr.Row():
121
+ with gr.Column(scale=1):
122
+ sample = gr.Dropdown(
123
+ choices=list(demo_core.SAMPLES), value=list(demo_core.SAMPLES)[0],
124
+ label="Sample signal",
125
+ )
126
+ csv = gr.File(label="...or upload a numeric CSV", file_types=[".csv"])
127
+ basis_mode = gr.Radio(
128
+ choices=[("MDL multi-basis (auto)", "auto"), ("Chebyshev only", "chebyshev")],
129
+ value="auto",
130
+ label="Basis selection",
131
+ )
132
+ degree = gr.Slider(1, 10, value=5, step=1, label="Polynomial degree")
133
+ seg = gr.Slider(100, 2000, value=500, step=100, label="Segment length")
134
+ go = gr.Button("Tokenize", variant="primary")
135
+ with gr.Column(scale=2):
136
+ summary = gr.Markdown(elem_id="ds-summary")
137
+ recon = gr.Plot(label="Reconstruction")
138
+ with gr.Row():
139
+ sigma = gr.Plot(label="Anomaly signal")
140
+ sizes = gr.Plot(label="Stored size")
141
+ inputs = [sample, csv, basis_mode, degree, seg]
142
+ outputs = [summary, recon, sigma, sizes]
143
+ go.click(run, inputs, outputs)
144
+ ui.load(run, inputs, outputs)
145
+ return ui
146
+
147
+
148
+ # HuggingFace Spaces auto-launches a module-level ``demo``. Building it at
149
+ # import requires a valid license: the POLYGEN_LICENSE secret on the Space,
150
+ # or POLYGEN_DEMO_LOCAL_MOCK=1 locally (handled by ensure_license above).
151
+ demo = build_ui()
152
+
153
+ if __name__ == "__main__":
154
+ # ssr_mode=False: gradio 5 SSR renders blank behind HF Spaces' proxy.
155
+ demo.launch(ssr_mode=False)
demo_core.py ADDED
@@ -0,0 +1,221 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Core polygen demo logic: tokenize a numeric signal through the SDK and
2
+ measure real compression, lossless round-trip, and the per-segment anomaly
3
+ signal.
4
+
5
+ Pure compute -- no Gradio, no license handling. Import only after polygen is
6
+ importable (see ``license_bootstrap`` for local runs). Everything here goes
7
+ through the supported SDK surface: ``DataMatrix.from_float`` ->
8
+ ``PolygenTokenizer.tokenize(..., parallel=False)`` -> ``BinaryEncoder`` for a
9
+ real token, and ``tokenizer.decode`` for EXACT / COARSE reconstruction. The
10
+ in-process ``parallel=False`` path keeps the demo free of a multiprocessing
11
+ pool, which matters in a hosted Space.
12
+ """
13
+
14
+ import gzip
15
+ import io
16
+ from collections import Counter
17
+
18
+ import numpy as np
19
+ import zstandard as zstd
20
+
21
+ from polygen.data import DataMatrix
22
+ from polygen.encoder import BinaryEncoder
23
+ from polygen.tokenizer import DecodeMode, PolygenTokenizer
24
+ from polygen.tokenizer.basis import BASIS_REGISTRY, ChebyshevBasis
25
+
26
+ SCALE = 1000
27
+
28
+
29
+ def _basis_name(basis_id: int) -> str:
30
+ """Human label for a segment's chosen basis family."""
31
+ if basis_id == 0:
32
+ return "Chebyshev" # explicit (non-MDL) path stores basis_id 0
33
+ cls = BASIS_REGISTRY.get(basis_id)
34
+ return cls.__name__.replace("Basis", "") if cls is not None else f"id{basis_id}"
35
+
36
+
37
+ def _as_2d_float32(signal: np.ndarray) -> np.ndarray:
38
+ """Coerce input to a 2D float32 array with shape ``(N, D)``."""
39
+ arr = np.asarray(signal, dtype=np.float32)
40
+ if arr.ndim == 1:
41
+ arr = arr.reshape(-1, 1)
42
+ if arr.ndim != 2:
43
+ raise ValueError(f"Expected 1D or 2D numeric data, got shape {arr.shape}.")
44
+ return arr
45
+
46
+
47
+ def run_demo(
48
+ signal: np.ndarray,
49
+ *,
50
+ basis_mode: str = "auto",
51
+ degree: int = 5,
52
+ segment_length: int = 200,
53
+ ) -> dict:
54
+ """Tokenize ``signal`` with the polygen SDK and return metrics + arrays.
55
+
56
+ Args:
57
+ signal: 1D or 2D numeric array. Columns are channels.
58
+ basis_mode: ``"auto"`` runs the per-segment MDL selector across all
59
+ basis families (multi-basis); ``"chebyshev"`` applies a single
60
+ fixed Chebyshev basis to every segment.
61
+ degree: polynomial degree (coefficient budget is degree + 1); for
62
+ ``"auto"`` it is the MDL base degree.
63
+ segment_length: rows per fitted window.
64
+
65
+ Returns:
66
+ A dict of measured sizes, ratios, the max lossless-decode error, the
67
+ chosen-basis breakdown, and the arrays needed to plot.
68
+ """
69
+ data = _as_2d_float32(signal)
70
+ n, d = data.shape
71
+ segment_length = int(max(16, min(segment_length, n)))
72
+
73
+ matrix = DataMatrix.from_float(data, scale=SCALE)
74
+
75
+ if basis_mode == "auto":
76
+ # Per-segment MDL selection across the basis registry (multi-basis).
77
+ tokenizer = PolygenTokenizer(
78
+ basis="auto",
79
+ degree=degree,
80
+ encoder=BinaryEncoder(),
81
+ preprocess=True,
82
+ fast_profile=True,
83
+ )
84
+ else:
85
+ tokenizer = PolygenTokenizer(
86
+ basis=ChebyshevBasis,
87
+ degree=degree,
88
+ encoder=BinaryEncoder(),
89
+ preprocess=True,
90
+ )
91
+
92
+ # Real binary token -> a true compressed-size number, not an estimate.
93
+ buf = io.BytesIO()
94
+ output = tokenizer.tokenize(
95
+ matrix,
96
+ segment_length=segment_length,
97
+ encode=True,
98
+ encoder_options={"output": buf},
99
+ parallel=False,
100
+ )
101
+ token_size = len(buf.getvalue())
102
+
103
+ metas = [c[0] for c in output.components]
104
+ coeffs = [c[1] for c in output.components]
105
+ resids = [c[2] for c in output.components]
106
+
107
+ # EXACT = lossless (prediction + residual). COARSE = prediction only.
108
+ exact = np.asarray(
109
+ tokenizer.decode((n, d), metas, coeffs, resids, mode=DecodeMode.EXACT),
110
+ )
111
+ coarse = np.asarray(
112
+ tokenizer.decode((n, d), metas, coeffs, resids, mode=DecodeMode.COARSE),
113
+ )
114
+ exact_float = exact / SCALE
115
+ coarse_float = coarse / SCALE
116
+ max_err = float(np.max(np.abs(exact_float - data)))
117
+
118
+ # Baselines measured on the same raw float32 bytes.
119
+ raw_bytes = data.astype(np.float32).tobytes()
120
+ raw_size = len(raw_bytes)
121
+ gzip_size = len(gzip.compress(raw_bytes, 6))
122
+
123
+ # Coefficients-only ("analytics" / COARSE delivery) size: the same Zstd
124
+ # codec the encoder uses, applied to just the coefficient block.
125
+ coeff_blob = (
126
+ np.concatenate([np.asarray(c).ravel() for c in coeffs])
127
+ .astype(np.float32)
128
+ .tobytes()
129
+ )
130
+ coeff_size = len(zstd.ZstdCompressor(level=3).compress(coeff_blob))
131
+
132
+ sigma_r = np.array([float(m.sigma_r) for m in metas], dtype=float)
133
+ seg_starts = np.array([int(m.start) for m in metas], dtype=int)
134
+ bases_used = dict(
135
+ sorted(Counter(_basis_name(m.basis_id) for m in metas).items(), key=lambda kv: -kv[1]),
136
+ )
137
+
138
+ return {
139
+ "n": n,
140
+ "d": d,
141
+ "segments": len(metas),
142
+ "segment_length": segment_length,
143
+ "degree": degree,
144
+ "scale": SCALE,
145
+ "basis_mode": basis_mode,
146
+ "bases_used": bases_used,
147
+ "raw_size": raw_size,
148
+ "gzip_size": gzip_size,
149
+ "token_size": token_size,
150
+ "coeff_size": coeff_size,
151
+ "ratio_vs_raw": raw_size / token_size if token_size else 0.0,
152
+ "ratio_vs_gzip": gzip_size / token_size if token_size else 0.0,
153
+ "coarse_ratio_vs_raw": raw_size / coeff_size if coeff_size else 0.0,
154
+ "max_err": max_err,
155
+ "original": data,
156
+ "exact": exact_float,
157
+ "coarse": coarse_float,
158
+ "sigma_r": sigma_r,
159
+ "seg_starts": seg_starts,
160
+ }
161
+
162
+
163
+ # ---------------------------------------------------------------------------
164
+ # Synthetic sample signals (deterministic; no external data dependency).
165
+ # ---------------------------------------------------------------------------
166
+
167
+ def _mixed_character(n: int = 16000) -> np.ndarray:
168
+ # Smooth polynomial first half, high-frequency oscillation second half.
169
+ # MDL should pick a polynomial family for the smooth segments and an
170
+ # oscillatory family (Fourier / Wavelet) for the rest -- a real mix.
171
+ rng = np.random.default_rng(4)
172
+ t = np.linspace(0, 1, n)
173
+ half = n // 2
174
+ x = np.empty(n)
175
+ x[:half] = 3.0 * t[:half] ** 2 - 1.5 * t[:half] + 0.5
176
+ x[half:] = 0.8 * np.sin(2 * np.pi * 60 * t[half:]) + 0.4 * np.sin(2 * np.pi * 140 * t[half:])
177
+ return (x + rng.normal(0, 0.02, n)).astype(np.float32)
178
+
179
+
180
+ def _temperature(n: int = 16000) -> np.ndarray:
181
+ rng = np.random.default_rng(0)
182
+ t = np.linspace(0, 8, n)
183
+ trend = 18.0 + 0.4 * t
184
+ daily = 6.0 * np.sin(2 * np.pi * t)
185
+ return (trend + daily + rng.normal(0, 0.3, n)).astype(np.float32)
186
+
187
+
188
+ def _ecg_like(n: int = 16000) -> np.ndarray:
189
+ rng = np.random.default_rng(1)
190
+ x = np.zeros(n, dtype=np.float64)
191
+ period = 80
192
+ for c in range(0, n, period):
193
+ peak = min(c + 6, n)
194
+ x[c:peak] += np.hanning(2 * (peak - c))[: peak - c] * 3.0
195
+ x += 0.2 * np.sin(np.linspace(0, 40 * np.pi, n)) + rng.normal(0, 0.05, n)
196
+ return x.astype(np.float32)
197
+
198
+
199
+ def _vibration_with_anomaly(n: int = 16000) -> np.ndarray:
200
+ rng = np.random.default_rng(2)
201
+ t = np.linspace(0, 1, n)
202
+ base = np.sin(2 * np.pi * 12 * t) + 0.5 * np.sin(2 * np.pi * 30 * t)
203
+ # Injected transient burst in one region -> should light up sigma_R there.
204
+ burst = np.zeros(n)
205
+ s = int(n * 0.62)
206
+ burst[s : s + 120] = 4.0 * np.sin(2 * np.pi * 90 * t[s : s + 120])
207
+ return (base + burst + rng.normal(0, 0.05, n)).astype(np.float32)
208
+
209
+
210
+ def _random_walk(n: int = 16000) -> np.ndarray:
211
+ rng = np.random.default_rng(3)
212
+ return np.cumsum(rng.normal(0, 1, n)).astype(np.float32)
213
+
214
+
215
+ SAMPLES = {
216
+ "Mixed character (smooth then oscillatory)": _mixed_character,
217
+ "Temperature sensor (smooth + daily cycle)": _temperature,
218
+ "ECG-like (periodic spikes)": _ecg_like,
219
+ "Vibration with injected anomaly": _vibration_with_anomaly,
220
+ "Random walk (hard to compress -- honest baseline)": _random_walk,
221
+ }
license_bootstrap.py ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """License bootstrap for the polygen demo.
2
+
3
+ The polygen SDK validates a license at import time. Two modes:
4
+
5
+ - **Deploy (HuggingFace Space):** set the ``POLYGEN_LICENSE`` secret to a real
6
+ JWT issued for the deployed wheel. Leave ``POLYGEN_DEMO_LOCAL_MOCK`` unset;
7
+ ``ensure_license`` is then a no-op and the SDK validates normally.
8
+ - **Local dev:** set ``POLYGEN_DEMO_LOCAL_MOCK=1``. This generates a
9
+ self-consistent RSA keypair, signs a short-lived JWT with it, and patches
10
+ the SDK's public-key lookup to the matching key -- the same pattern
11
+ ``tests/conftest.py`` uses for ad-hoc scripts. No real license needed.
12
+
13
+ ``ensure_license`` MUST run before ``import polygen``.
14
+ """
15
+
16
+ import os
17
+
18
+
19
+ def apply_local_mock() -> None:
20
+ """Generate a self-consistent license and patch the SDK key lookup."""
21
+ import datetime
22
+ from pathlib import Path
23
+ from unittest import mock
24
+
25
+ import jwt
26
+ from cryptography.hazmat.primitives import serialization
27
+ from cryptography.hazmat.primitives.asymmetric import rsa
28
+
29
+ private_key = rsa.generate_private_key(public_exponent=65537, key_size=4096)
30
+ private_pem = private_key.private_bytes(
31
+ encoding=serialization.Encoding.PEM,
32
+ format=serialization.PrivateFormat.PKCS8,
33
+ encryption_algorithm=serialization.NoEncryption(),
34
+ )
35
+ public_pem = private_key.public_key().public_bytes(
36
+ encoding=serialization.Encoding.PEM,
37
+ format=serialization.PublicFormat.SubjectPublicKeyInfo,
38
+ )
39
+
40
+ now = datetime.datetime.now(datetime.timezone.utc)
41
+ os.environ["POLYGEN_LICENSE"] = jwt.encode(
42
+ {
43
+ "iss": "https://license.datasent.com",
44
+ "sub": "polygen demo (local mock)",
45
+ "aud": "polygen-sdk",
46
+ "email": "demo@datasent.com",
47
+ "iat": now,
48
+ "exp": now + datetime.timedelta(hours=12),
49
+ },
50
+ key=private_pem,
51
+ algorithm="PS512",
52
+ )
53
+
54
+ mock_path = mock.Mock(spec=Path)
55
+ mock_path.joinpath.return_value = mock_path
56
+ mock_path.__truediv__ = mock.Mock(return_value=mock_path)
57
+ mock_path.read_text.return_value = public_pem
58
+
59
+ # Scope the patch to polygen's license lookup ONLY. A global patch of
60
+ # importlib.resources.files (as in tests/conftest.py) breaks any other
61
+ # library that locates package resources -- e.g. gradio/starlette loading
62
+ # Jinja templates. Delegate every other anchor to the real ``files``.
63
+ import importlib.resources as _ir
64
+
65
+ real_files = _ir.files
66
+
67
+ def fake_files(*args, **kwargs):
68
+ if args and args[0] == "polygen._license":
69
+ return mock_path
70
+ return real_files(*args, **kwargs)
71
+
72
+ mock.patch("importlib.resources.files", side_effect=fake_files).start()
73
+
74
+
75
+ def ensure_license() -> None:
76
+ """Apply the local mock when requested; otherwise rely on POLYGEN_LICENSE."""
77
+ if os.environ.get("POLYGEN_DEMO_LOCAL_MOCK") == "1":
78
+ apply_local_mock()
plots.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Matplotlib figures for the polygen demo. No Gradio dependency, so the
2
+ plots can be rendered and tested independently of the UI."""
3
+
4
+ import matplotlib
5
+
6
+ matplotlib.use("Agg")
7
+ import matplotlib.pyplot as plt
8
+ import numpy as np
9
+
10
+ ACCENT, EXACT_C, COARSE_C, ANOM_C = "#2b2bff", "#1a1a1a", "#d08400", "#c0392b"
11
+
12
+
13
+ def fig_reconstruction(r: dict):
14
+ """Original vs EXACT (lossless) vs COARSE (smooth fit only), channel 0."""
15
+ fig, ax = plt.subplots(figsize=(8, 3.2))
16
+ ch = r["original"][:, 0]
17
+ view = slice(0, min(len(ch), 2000)) # readable window
18
+ x = np.arange(len(ch))[view]
19
+ ax.plot(x, ch[view], color="#bbbbbb", lw=2.4, label="original")
20
+ ax.plot(x, r["exact"][:, 0][view], color=EXACT_C, lw=0.9, label="EXACT decode (lossless)")
21
+ ax.plot(x, r["coarse"][:, 0][view], color=COARSE_C, lw=1.2, ls="--", label="COARSE (smooth fit only)")
22
+ ax.set_title("Reconstruction: EXACT is bit-faithful; COARSE is the smooth fit")
23
+ ax.set_xlabel("position")
24
+ ax.legend(loc="upper right", fontsize=8, frameon=False)
25
+ ax.spines[["top", "right"]].set_visible(False)
26
+ fig.tight_layout()
27
+ return fig
28
+
29
+
30
+ def fig_sigma(r: dict):
31
+ """Per-segment sigma_R, with anomalies (mean + 2 sigma) highlighted."""
32
+ fig, ax = plt.subplots(figsize=(8, 2.8))
33
+ s = r["sigma_r"]
34
+ idx = np.arange(len(s))
35
+ thresh = float(np.mean(s) + 2 * np.std(s)) if len(s) > 1 else float("inf")
36
+ colors = [ANOM_C if v > thresh else ACCENT for v in s]
37
+ ax.bar(idx, s, color=colors)
38
+ if np.isfinite(thresh):
39
+ ax.axhline(thresh, color=ANOM_C, ls="--", lw=1, label="mean + 2 sigma")
40
+ ax.legend(loc="upper right", fontsize=8, frameon=False)
41
+ ax.set_title("Per-segment anomaly signal (sigma_R) -- free, no extra model")
42
+ ax.set_xlabel("segment")
43
+ ax.set_ylabel("sigma_R")
44
+ ax.spines[["top", "right"]].set_visible(False)
45
+ fig.tight_layout()
46
+ return fig
47
+
48
+
49
+ def fig_sizes(r: dict):
50
+ """Stored size: raw / gzip / polygen EXACT / polygen COARSE (log scale)."""
51
+ fig, ax = plt.subplots(figsize=(8, 2.8))
52
+ labels = ["raw\nfloat32", "gzip", "polygen\nEXACT", "polygen\nCOARSE\n(coeffs)"]
53
+ vals = [r["raw_size"] / 1024, r["gzip_size"] / 1024, r["token_size"] / 1024, r["coeff_size"] / 1024]
54
+ ax.bar(labels, vals, color=["#bbbbbb", "#888888", EXACT_C, COARSE_C])
55
+ ax.set_yscale("log")
56
+ ax.set_ylabel("KB (log scale)")
57
+ ax.set_title("Stored size: EXACT ~ gzip parity; COARSE is the analytics archive")
58
+ for i, v in enumerate(vals):
59
+ ax.text(i, v, f"{v:.1f}", ha="center", va="bottom", fontsize=8)
60
+ ax.spines[["top", "right"]].set_visible(False)
61
+ fig.tight_layout()
62
+ return fig
polygen-0.1.0b1-cp313-cp313-linux_x86_64.whl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:25b627c7938e1a3b6bac70b05658b22eedd5c72781c0558f62da12f3669d7544
3
+ size 1032097
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ # Demo dependencies. polygen -- and its mlx[cpu] backend on Linux -- comes
2
+ # from the vendored Nuitka-compiled wheel (a binary .so, no readable source;
3
+ # the matching demo JWT is the POLYGEN_LICENSE Space secret).
4
+ gradio==5.34.0
5
+ requests
6
+ matplotlib
7
+ ./polygen-0.1.0b1-cp313-cp313-linux_x86_64.whl