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
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
#!/usr/bin/env python3
"""Nacrith GPU -- Neural Arithmetic Compression -- Hugging Face Space Demo.

Supports both text (NC05) and binary (NC06) compression.
"""

import gzip
import time
import base64
import spaces
import gradio as gr

from model_wrapper import ModelWrapper
from compressor import NeuralCompressor, MAGIC, MAGIC_BIN, HEADER_SIZE
from utils import format_size

MAX_TEXT_TOKENS = 1500
MAX_BINARY_UPLOAD = 1 * 1024 * 1024  # 1 MB
MAX_NC_UPLOAD = 1 * 1024 * 1024      # 1 MB

model = None
compressor = None


def get_compressor():
    global model, compressor
    if compressor is None:
        model = ModelWrapper(verbose=False)
        compressor = NeuralCompressor(model=model, verbose=False, use_lzp=False)
    return compressor


# ---------------------------------------------------------------------------
# Tab 1: Compress Text (NC05)
# ---------------------------------------------------------------------------

@spaces.GPU(duration=120)
def compress_text(text):
    if not text or not text.strip():
        return "Please enter some text to compress.", "", ""

    text = text.strip()
    comp = get_compressor()

    original_bytes = len(text.encode("utf-8"))
    token_ids = comp.model.tokenizer.encode(text)
    num_tokens = len(token_ids)

    if num_tokens > MAX_TEXT_TOKENS:
        return (
            f"**Input too long.** Please use up to {MAX_TEXT_TOKENS} tokens (~4000 characters) "
            "for the demo. Larger files work locally -- see "
            "[GitHub](https://github.com/st4ck/Nacrith-GPU).",
            "", "",
        )

    t0 = time.time()
    compressed = comp.compress(text)
    elapsed = time.time() - t0
    compressed_size = len(compressed)
    ratio = compressed_size / original_bytes * 100
    tokens_per_sec = num_tokens / elapsed if elapsed > 0 else 0

    decompressed = comp.decompress(compressed)
    lossless = decompressed == text

    gzip_data = gzip.compress(text.encode("utf-8"), compresslevel=9)
    gzip_size = len(gzip_data)
    gzip_ratio = gzip_size / original_bytes * 100
    improvement = gzip_size / compressed_size if compressed_size > 0 else 0

    b64 = base64.b64encode(compressed).decode("ascii")
    verify = "Yes" if lossless else "FAILED"

    md = f"""## Compression Results

| | Size | Ratio |
|---|---|---|
| **Original** | {format_size(original_bytes)} | 100% |
| **gzip -9** | {format_size(gzip_size)} | {gzip_ratio:.1f}% |
| **Nacrith GPU** | {format_size(compressed_size)} | {ratio:.1f}% |

**Nacrith GPU is {improvement:.1f}x smaller than gzip**

Tokens: {num_tokens} | Time: {elapsed:.1f}s ({tokens_per_sec:.0f} tok/s) | Lossless: {verify} | Space saved: {100 - ratio:.1f}%
"""

    download_html = (
        f'<a download="compressed.nc" '
        f'href="data:application/octet-stream;base64,{b64}" '
        f'style="display:inline-block;padding:10px 24px;background:#ef4444;color:white;'
        f'border-radius:8px;text-decoration:none;font-weight:bold;font-size:14px;'
        f'margin-top:8px;cursor:pointer;">'
        f'Download compressed.nc ({format_size(compressed_size)})</a>'
    )

    return md, download_html, b64


# ---------------------------------------------------------------------------
# Tab 2: Upload Text/Binary — auto-detects format
# ---------------------------------------------------------------------------

def _is_text_file(data: bytes) -> bool:
    """Check if data looks like a valid UTF-8 text file."""
    try:
        data.decode("utf-8")
        return True
    except UnicodeDecodeError:
        return False


@spaces.GPU(duration=300)
def compress_file_b64(b64_data, filename):
    if not b64_data or not b64_data.strip():
        return "Please upload a file using the button above.", ""

    try:
        data = base64.b64decode(b64_data.strip())
    except Exception:
        return "**Failed to read uploaded file.**", ""

    if len(data) > MAX_BINARY_UPLOAD:
        return (
            f"**File too large** ({format_size(len(data))}). "
            f"Max upload size is {format_size(MAX_BINARY_UPLOAD)}.",
            "",
        )

    if len(data) == 0:
        return "**Empty file.** Please upload a file with content.", ""

    comp = get_compressor()
    original_size = len(data)
    is_text = _is_text_file(data)

    gzip_data = gzip.compress(data, compresslevel=9)
    gzip_size = len(gzip_data)
    gzip_ratio = gzip_size / original_size * 100

    t0 = time.time()
    if is_text:
        text = data.decode("utf-8")
        compressed = comp.compress(text)
    else:
        compressed = comp.compress_bytes(data)
    elapsed = time.time() - t0
    compressed_size = len(compressed)
    ratio = compressed_size / original_size * 100
    improvement = gzip_size / compressed_size if compressed_size > 0 else 0

    # Verify lossless round-trip
    decompressed = comp.decompress(compressed)
    if is_text:
        lossless = decompressed == text
    else:
        lossless = decompressed == data
    verify = "Yes" if lossless else "FAILED"

    b64_out = base64.b64encode(compressed).decode("ascii")

    # Derive output filename
    out_name = (filename.strip() + ".nc") if filename and filename.strip() else "compressed.nc"
    mode_label = "Text (NC05)" if is_text else "Binary (NC06)"

    md = f"""## Compression Results

| | Size | Ratio |
|---|---|---|
| **Original** | {format_size(original_size)} | 100% |
| **gzip -9** | {format_size(gzip_size)} | {gzip_ratio:.1f}% |
| **Nacrith GPU** | {format_size(compressed_size)} | {ratio:.1f}% |

**Nacrith GPU is {improvement:.1f}x smaller than gzip**

Mode: {mode_label} | Time: {elapsed:.1f}s | Lossless: {verify} | Space saved: {100 - ratio:.1f}%
"""

    download_html = (
        f'<a download="{out_name}" '
        f'href="data:application/octet-stream;base64,{b64_out}" '
        f'style="display:inline-block;padding:10px 24px;background:#ef4444;color:white;'
        f'border-radius:8px;text-decoration:none;font-weight:bold;font-size:14px;'
        f'margin-top:8px;cursor:pointer;">'
        f'Download {out_name} ({format_size(compressed_size)})</a>'
    )

    return md, download_html


# ---------------------------------------------------------------------------
# Tab 3: Decompress (NC05 text or NC06 binary)
# ---------------------------------------------------------------------------

def _decompress_data(data):
    """Shared decompression logic. Returns (md, download_html, text)."""
    if len(data) < HEADER_SIZE:
        return "**Invalid data.** Too short to be a Nacrith GPU compressed file.", "", ""

    magic = data[:4]
    if magic not in (MAGIC, MAGIC_BIN):
        return "**Invalid data.** Not a Nacrith GPU compressed file (wrong magic bytes).", "", ""

    comp = get_compressor()
    is_binary = magic == MAGIC_BIN

    try:
        t0 = time.time()
        result = comp.decompress(data)
        elapsed = time.time() - t0
    except Exception as e:
        return f"**Decompression failed:** {e}", "", ""

    if is_binary:
        original_size = len(result)
        b64_out = base64.b64encode(result).decode("ascii")

        md = f"""## Decompression Results (Binary)

- Compressed: {format_size(len(data))}
- Decompressed: {format_size(original_size)}
- Time: {elapsed:.1f}s
- Format: NC06 (hybrid binary)
- **Lossless reconstruction successful**
"""
        download_html = (
            f'<a download="decompressed.bin" '
            f'href="data:application/octet-stream;base64,{b64_out}" '
            f'style="display:inline-block;padding:10px 24px;background:#22c55e;color:white;'
            f'border-radius:8px;text-decoration:none;font-weight:bold;font-size:14px;'
            f'margin-top:8px;cursor:pointer;">'
            f'Download decompressed.bin ({format_size(original_size)})</a>'
        )
        return md, download_html, ""
    else:
        original_bytes = len(result.encode("utf-8"))
        md = f"""## Decompression Results (Text)

- Compressed: {format_size(len(data))}
- Decompressed: {format_size(original_bytes)}
- Time: {elapsed:.1f}s
- Format: NC05 (text)
- **Lossless reconstruction successful**
"""
        return md, "", result


@spaces.GPU(duration=300)
def decompress_file_b64(b64_data):
    """Decompress from file uploaded via JS (passed as base64)."""
    if not b64_data or not b64_data.strip():
        return "**No file.** Please upload a .nc file.", "", ""

    try:
        data = base64.b64decode(b64_data.strip())
    except Exception:
        return "**Failed to read uploaded file.**", "", ""

    if len(data) > MAX_NC_UPLOAD:
        return (
            f"**File too large** ({format_size(len(data))}). "
            f"Max size is {format_size(MAX_NC_UPLOAD)}.",
            "", "",
        )

    return _decompress_data(data)


@spaces.GPU(duration=300)
def decompress_b64(b64_text):
    """Decompress from pasted base64 data."""
    if not b64_text or not b64_text.strip():
        return "**No data.** Paste base64 data from the Compress tab, or upload a .nc file above.", "", ""

    try:
        data = base64.b64decode(b64_text.strip())
    except Exception:
        return "**Invalid base64 data.** Please paste the exact output from the Compress tab.", "", ""

    if len(data) > MAX_NC_UPLOAD:
        return (
            f"**Data too large** ({format_size(len(data))}). "
            f"Max size is {format_size(MAX_NC_UPLOAD)}.",
            "", "",
        )

    return _decompress_data(data)


# ---------------------------------------------------------------------------
# UI
# ---------------------------------------------------------------------------

HEADER_HTML = """
<div style="text-align: center; margin-bottom: 0.5em;">
    <img src="https://raw.githubusercontent.com/st4ck/Nacrith-GPU/main/assets/banner_gpu.png" alt="Nacrith GPU" style="max-width:420px;width:100%;margin:0 auto;">
    <p style="font-size: 1.1em; color: #aaa; margin-top: 8px;">Neural Arithmetic Compression -- Advanced Lossless Compression</p>
    <p style="font-size: 0.9em;">
        <a href="https://nacrith.com">Website</a> |
        <a href="https://github.com/st4ck/Nacrith-GPU">GitHub</a> |
        SmolLM2-135M + Arithmetic Coding | Supports text &amp; binary files
    </p>
    <p style="font-size: 1.1em; color: #aaa; margin-top: 8px;"><i>Information is Already There</i></p>
</div>
"""

# JS to read binary file upload as base64 into the hidden textbox
BINARY_UPLOAD_JS = """
function setupBinaryUpload() {
    const input = document.getElementById('binary-file-input');
    if (!input) return;
    input.addEventListener('change', function(e) {
        const file = e.target.files[0];
        if (!file) return;
        // Update file name display
        const nameSpan = document.getElementById('binary-file-name');
        if (nameSpan) nameSpan.textContent = file.name + ' (' + (file.size / 1024).toFixed(1) + ' KB)';
        // Store filename
        const fnBox = document.querySelectorAll('#binary-filename textarea');
        if (fnBox.length > 0) {
            fnBox[0].value = file.name;
            fnBox[0].dispatchEvent(new Event('input', {bubbles: true}));
        }
        // Read as base64
        const reader = new FileReader();
        reader.onload = function(ev) {
            const b64 = ev.target.result.split(',')[1];
            const textareas = document.querySelectorAll('#binary-b64-data textarea');
            if (textareas.length > 0) {
                textareas[0].value = b64;
                textareas[0].dispatchEvent(new Event('input', {bubbles: true}));
            }
        };
        reader.readAsDataURL(file);
    });
}
setTimeout(setupBinaryUpload, 1000);
"""

# JS to read .nc file upload as base64 into the hidden textbox
NC_UPLOAD_JS = """
function setupNcUpload() {
    const input = document.getElementById('nc-file-input');
    if (!input) return;
    input.addEventListener('change', function(e) {
        const file = e.target.files[0];
        if (!file) return;
        const nameSpan = document.getElementById('nc-file-name');
        if (nameSpan) nameSpan.textContent = file.name + ' (' + (file.size / 1024).toFixed(1) + ' KB)';
        const reader = new FileReader();
        reader.onload = function(ev) {
            const b64 = ev.target.result.split(',')[1];
            const textareas = document.querySelectorAll('#nc-b64-data textarea');
            if (textareas.length > 0) {
                textareas[0].value = b64;
                textareas[0].dispatchEvent(new Event('input', {bubbles: true}));
            }
        };
        reader.readAsDataURL(file);
    });
}
setTimeout(setupNcUpload, 1200);
"""

with gr.Blocks(title="Nacrith GPU") as demo:
    gr.HTML(HEADER_HTML)

    # ---- Tab 1: Compress Text ----
    with gr.Tab("Compress Text"):
        gr.Markdown("Compress text using neural arithmetic coding (NC05 format).")
        text_input = gr.Textbox(
            label="Input Text",
            placeholder="Paste or type text here (up to ~4000 characters)...",
            lines=8,
        )
        compress_text_btn = gr.Button("Compress Text", variant="primary", size="lg")
        compress_text_results = gr.Markdown()
        compress_text_download = gr.HTML()
        compress_text_b64 = gr.Textbox(
            label="Compressed data (base64) -- copy to Decompress tab to verify",
            lines=3,
            show_copy_button=True,
        )

        compress_text_btn.click(
            fn=compress_text,
            inputs=[text_input],
            outputs=[compress_text_results, compress_text_download, compress_text_b64],
        )

        gr.Examples(
            examples=[
                ["The quick brown fox jumps over the lazy dog. This is a simple test of the neural compression system."],
                ["In the beginning, the universe was created. This has made a lot of people very angry and has been widely regarded as a bad move. The story so far: In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."],
                ["Machine learning is a subset of artificial intelligence that focuses on building systems that learn from data. Unlike traditional programming where rules are explicitly coded, machine learning algorithms identify patterns in data and make decisions with minimal human intervention. Deep learning, a further subset, uses neural networks with many layers to model complex patterns in large amounts of data."],
            ],
            inputs=[text_input],
            label="Try these examples",
        )

    # ---- Tab 2: Upload Text/Binary ----
    with gr.Tab("Upload Text/Binary"):
        gr.Markdown(
            "Upload any file (up to 1 MB) to compress.\n\n"
            "The format is auto-detected: text files use neural compression (NC05), "
            "binary files use hybrid compression (NC06) where text-like regions are "
            "neural-compressed and binary regions are gzip/lzma-compressed."
        )
        gr.HTML(
            '<div style="margin:8px 0;">'
            '<label style="display:inline-block;padding:10px 24px;background:#6366f1;color:white;'
            'border-radius:8px;cursor:pointer;font-weight:bold;font-size:14px;">'
            'Choose file to compress'
            '<input id="binary-file-input" type="file" style="display:none;">'
            '</label>'
            '<span id="binary-file-name" style="margin-left:10px;color:#aaa;"></span>'
            '</div>'
        )
        # Hidden textboxes to receive data from JS
        binary_b64_data = gr.Textbox(visible=False, elem_id="binary-b64-data")
        binary_filename = gr.Textbox(visible=False, elem_id="binary-filename")

        compress_file_btn = gr.Button("Compress", variant="primary", size="lg")
        compress_file_results = gr.Markdown()
        compress_file_download = gr.HTML()

        compress_file_btn.click(
            fn=compress_file_b64,
            inputs=[binary_b64_data, binary_filename],
            outputs=[compress_file_results, compress_file_download],
        )

    # ---- Tab 3: Decompress ----
    with gr.Tab("Decompress"):
        gr.Markdown(
            "Upload a `.nc` file to decompress, or paste base64 data from the Compress Text tab.\n\n"
            "Supports both NC05 (text) and NC06 (binary) formats."
        )

        gr.HTML(
            '<div style="margin:8px 0;">'
            '<label style="display:inline-block;padding:10px 24px;background:#6366f1;color:white;'
            'border-radius:8px;cursor:pointer;font-weight:bold;font-size:14px;">'
            'Upload .nc file'
            '<input id="nc-file-input" type="file" accept=".nc" style="display:none;">'
            '</label>'
            '<span id="nc-file-name" style="margin-left:10px;color:#aaa;"></span>'
            '</div>'
        )
        nc_b64_data = gr.Textbox(visible=False, elem_id="nc-b64-data")
        decompress_file_btn = gr.Button("Decompress File", variant="primary", size="lg")

        gr.Markdown("**Or** paste base64 data:")
        decompress_b64_input = gr.Textbox(
            label="Compressed data (base64)",
            placeholder="Paste base64 data here...",
            lines=3,
        )
        decompress_b64_btn = gr.Button("Decompress Base64", variant="primary", size="lg")

        decompress_results = gr.Markdown()
        decompress_download = gr.HTML()
        decompress_text_output = gr.Textbox(
            label="Decompressed Text",
            lines=10,
            interactive=False,
        )

        decompress_file_btn.click(
            fn=decompress_file_b64,
            inputs=[nc_b64_data],
            outputs=[decompress_results, decompress_download, decompress_text_output],
        )
        decompress_b64_btn.click(
            fn=decompress_b64,
            inputs=[decompress_b64_input],
            outputs=[decompress_results, decompress_download, decompress_text_output],
        )

    gr.Markdown("""
---
**How it works:** A 135M-parameter language model predicts the next token at each step.
Those predictions feed an arithmetic coder -- high-confidence predictions cost nearly zero bits.
The same model runs on both sides, guaranteeing perfect lossless reconstruction.

**Text (NC05):** Text is tokenized and neural-compressed directly. Achieves ~15% ratio on English text (2.5x better than gzip).

**Binary (NC06):** Files are segmented into text-like and binary regions. Text regions are neural-compressed;
binary regions are compressed with gzip or lzma. The hybrid approach beats gzip on files with significant text content.
<br>
<img src="https://raw.githubusercontent.com/st4ck/Nacrith-GPU/main/assets/compression_ratio.png" alt="Compression Ratio Bar Charts">
<br>
Apache 2.0 | Made by [Roberto Tacconelli](https://github.com/st4ck) | [arxiv.org/abs/2602.19626](https://arxiv.org/abs/2602.19626) | [tacconelli.rob@gmail.com](tacconelli.rob@gmail.com) | [roberto@elizetaplus.com](roberto@elizetaplus.com)
""")

    # Inject upload JS
    gr.HTML(f"<script>{BINARY_UPLOAD_JS}</script>")
    gr.HTML(f"<script>{NC_UPLOAD_JS}</script>")

demo.queue()
demo.launch()