Spaces:
Running
Running
File size: 17,878 Bytes
1346a5c a5d3f72 3f05a44 a5d3f72 3f05a44 a5d3f72 3f05a44 5180038 a5d3f72 3f05a44 a5d3f72 3f05a44 5180038 3f05a44 5180038 3f05a44 5180038 3f05a44 a5d3f72 3f05a44 a5d3f72 3f05a44 a5d3f72 3f05a44 a5d3f72 3f05a44 a5d3f72 3f05a44 a5d3f72 3f05a44 a5d3f72 3f05a44 5180038 ac76143 5180038 3f05a44 a5d3f72 3f05a44 1346a5c 3f05a44 bb1a206 3d052c5 a5d3f72 3f05a44 e7042cd 3f05a44 e7042cd 3f05a44 a5d3f72 3f05a44 a5d3f72 3f05a44 a5d3f72 3f05a44 a5d3f72 3f05a44 a5d3f72 3f05a44 a5d3f72 | 1 2 3 4 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 | #!/usr/bin/env python3
"""Midicoth β Micro-Diffusion Compression β HuggingFace Space Demo."""
import os, subprocess, tempfile, base64
from flask import Flask, request, send_file, render_template_string
app = Flask(__name__)
BINARY = os.path.join(os.path.dirname(os.path.abspath(__file__)), "mdc")
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
def fmt(n):
if n < 1024: return f"{n} B"
if n < 1024**2: return f"{n/1024:.1f} KB"
return f"{n/1024**2:.2f} MB"
def is_utf8(data):
try:
data.decode("utf-8"); return True
except UnicodeDecodeError:
return False
def dl_link(b64, name, label, color="#22c55e"):
return (f'<a class="dl" style="background:{color}" '
f'href="data:application/octet-stream;base64,{b64}" download="{name}">'
f'{label}</a>')
# ---------------------------------------------------------------------------
# HTML
# ---------------------------------------------------------------------------
STYLE = """
<style>
*{box-sizing:border-box}
body{font-family:system-ui,sans-serif;max-width:780px;margin:40px auto;padding:0 20px;
background:#0f172a;color:#e2e8f0}
h1{font-size:1.8em;margin-bottom:4px}
.sub{color:#94a3b8;margin-top:0;font-size:.95em}
/* tabs */
.tabs{display:flex;gap:4px;margin:24px 0 0}
.tab{padding:9px 18px;border-radius:8px 8px 0 0;cursor:pointer;font-size:.9em;font-weight:600;
background:#1e293b;color:#94a3b8;border:none;transition:.15s}
.tab.active{background:#6366f1;color:white}
.panel{display:none;background:#1e293b;border-radius:0 8px 8px 8px;padding:24px}
.panel.active{display:block}
/* form elements */
label{display:block;margin-bottom:6px;font-size:.88em;color:#94a3b8}
input[type=file]{width:100%;padding:10px;background:#0f172a;border:1px solid #334155;
border-radius:8px;color:#e2e8f0}
textarea{width:100%;padding:10px;background:#0f172a;border:1px solid #334155;border-radius:8px;
color:#e2e8f0;font-family:monospace;font-size:.85em;resize:vertical}
button[type=submit]{margin-top:12px;padding:10px 28px;background:#6366f1;color:white;
border:none;border-radius:8px;font-size:1em;font-weight:600;cursor:pointer}
button[type=submit]:hover{background:#4f46e5}
/* results */
.result{margin-top:18px;padding:16px;background:#0f172a;border-radius:8px;font-size:.9em}
.stats{color:#94a3b8;margin-bottom:10px;line-height:1.7}
.dl{display:inline-block;padding:9px 22px;border-radius:8px;text-decoration:none;
font-weight:600;font-size:.9em;margin-right:8px;margin-top:4px;color:white}
.b64box{margin-top:12px}
.b64box label{color:#7dd3fc;font-size:.82em;margin-bottom:4px}
.copy-btn{padding:5px 12px;background:#334155;border:none;border-radius:6px;color:#e2e8f0;
font-size:.78em;cursor:pointer;margin-left:8px;vertical-align:middle}
.copy-btn:hover{background:#475569}
.error{color:#f87171;font-size:.9em;margin-top:10px}
/* info table */
.info{margin-top:28px;background:#1e293b;border-radius:12px;padding:20px;font-size:.85em;color:#94a3b8}
.info b{color:#e2e8f0}
table{border-collapse:collapse;width:100%;margin-top:8px}
td,th{padding:6px 12px;border:1px solid #334155}
th{color:#7dd3fc;font-weight:600}
footer{margin-top:32px;font-size:.8em;color:#475569;text-align:center}
a{color:#6366f1}
.ex-btn{padding:5px 12px;background:#1e3a5f;border:1px solid #334155;border-radius:6px;
color:#7dd3fc;font-size:.78em;cursor:pointer;transition:.15s}
.ex-btn:hover{background:#1d4ed8;color:white;border-color:#1d4ed8}
</style>
"""
PAGE = STYLE + """
<h1>ποΈ Midicoth</h1>
<p class="sub">Micro-Diffusion Compression · Binary Tree Tweedie Denoising
· No neural network · No GPU</p>
<div class="tabs">
<button class="tab {% if tab=='ct' %}active{% endif %}" onclick="show('ct')">Compress Text</button>
<button class="tab {% if tab=='cf' %}active{% endif %}" onclick="show('cf')">Compress File</button>
<button class="tab {% if tab=='dc' %}active{% endif %}" onclick="show('dc')">Decompress</button>
</div>
<!-- Compress Text -->
<div class="panel {% if tab=='ct' %}active{% endif %}" id="ct">
<form method="post" action="/compress_text" id="ct-form">
<label>Input text (paste or type):</label>
<textarea name="text" id="ct-textarea" rows="7" placeholder="Paste text here...">{{ form_text or '' }}</textarea>
<button type="submit">Compress β</button>
</form>
<div style="margin-top:14px">
<p style="font-size:.82em;color:#64748b;margin:0 0 6px">Try an example:</p>
<div style="display:flex;flex-wrap:wrap;gap:6px">
<button type="button" class="ex-btn" onclick="setExample(0)">Alice in Wonderland</button>
<button type="button" class="ex-btn" onclick="setExample(1)">Wikipedia intro</button>
<button type="button" class="ex-btn" onclick="setExample(2)">Scientific abstract</button>
<button type="button" class="ex-btn" onclick="setExample(3)">Repeated structure</button>
<button type="button" class="ex-btn" onclick="setExample(4)">Source code</button>
</div>
</div>
{% if ct_error %}<p class="error">{{ ct_error }}</p>{% endif %}
{% if ct_result %}
<div class="result">
<div class="stats">{{ ct_result.stats | safe }}</div>
<a class="dl" href="{{ ct_result.dl_href }}" download="{{ ct_result.dl_name }}">
β¬ Download {{ ct_result.dl_name }} ({{ ct_result.out_sz }})
</a>
<div class="b64box">
<label>Compressed data (base64)
<button type="button" class="copy-btn" onclick="copyB64('ct-b64')">Copy</button>
</label>
<textarea id="ct-b64" rows="3" readonly>{{ ct_result.b64 }}</textarea>
</div>
</div>
{% endif %}
</div>
<!-- Compress File -->
<div class="panel {% if tab=='cf' %}active{% endif %}" id="cf">
<form method="post" action="/compress_file" enctype="multipart/form-data">
<label>Upload any file:</label>
<input type="file" name="file" required>
<button type="submit">Compress β</button>
</form>
{% if cf_error %}<p class="error">{{ cf_error }}</p>{% endif %}
{% if cf_result %}
<div class="result">
<div class="stats">{{ cf_result.stats | safe }}</div>
<a class="dl" href="{{ cf_result.dl_href }}" download="{{ cf_result.dl_name }}">
β¬ Download {{ cf_result.dl_name }} ({{ cf_result.out_sz }})
</a>
<div class="b64box">
<label>Compressed data (base64)
<button type="button" class="copy-btn" onclick="copyB64('cf-b64')">Copy</button>
</label>
<textarea id="cf-b64" rows="3" readonly>{{ cf_result.b64 }}</textarea>
</div>
</div>
{% endif %}
</div>
<!-- Decompress -->
<div class="panel {% if tab=='dc' %}active{% endif %}" id="dc">
<p style="color:#94a3b8;font-size:.88em;margin-top:0">
Upload a <code>.mdc</code> file <b>or</b> paste base64 data from the Compress tabs.
</p>
<form method="post" action="/decompress_file" enctype="multipart/form-data">
<label>Upload .mdc file:</label>
<input type="file" name="file" accept=".mdc">
<button type="submit">Decompress file β</button>
</form>
<hr style="border-color:#334155;margin:20px 0">
<form method="post" action="/decompress_b64">
<label>Or paste base64 data:</label>
<textarea name="b64" rows="4" placeholder="Paste base64 here...">{{ form_b64 or '' }}</textarea>
<button type="submit">Decompress base64 β</button>
</form>
{% if dc_error %}<p class="error">{{ dc_error }}</p>{% endif %}
{% if dc_result %}
<div class="result">
<div class="stats">{{ dc_result.stats | safe }}</div>
{% if dc_result.text is not none %}
<div class="b64box">
<label>Decompressed text</label>
<textarea rows="10" readonly>{{ dc_result.text }}</textarea>
</div>
{% endif %}
{% if dc_result.dl_href %}
<a class="dl" href="{{ dc_result.dl_href }}" download="{{ dc_result.dl_name }}">
β¬ Download {{ dc_result.dl_name }} ({{ dc_result.out_sz }})
</a>
{% endif %}
</div>
{% endif %}
</div>
<div class="info">
<b>How it works:</b> PPM (orders 0β4) β Match Model β Word Model β
High-Order Context (orders 5β8) β Micro-Diffusion Tweedie Denoiser β Arithmetic Coder.
<table>
<tr><th>Benchmark</th><th>Midicoth</th><th>xz -9</th><th>Improvement</th></tr>
<tr><td>alice29.txt (152 KB)</td><td>2.119 bpb</td><td>2.551 bpb</td><td>+16.9%</td></tr>
<tr><td>enwik8 (100 MB)</td><td>1.753 bpb</td><td>1.989 bpb</td><td>+11.9%</td></tr>
</table>
</div>
<footer>Apache 2.0 · <a href="https://github.com/robtacconelli/midicoth">GitHub</a>
· <a href="https://arxiv.org/abs/2603.08771">arXiv:2603.08771</a></footer>
<script>
var EXAMPLES=[
// 0 β Alice in Wonderland (the benchmark corpus)
"Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do: once or twice she had peeped into the book her sister was reading, but it had no pictures or conversations in it, 'and what is the use of a book,' thought Alice 'without pictures or conversations?' So she was considering in her own mind (as well as she could, for the hot day made her feel very sleepy and stupid), whether the pleasure of making a daisy-chain would be worth the trouble of getting up and picking the daisies, when suddenly a White Rabbit with pink eyes ran close by her.",
// 1 β Wikipedia-style prose
"The Internet is a global system of interconnected computer networks that uses the standard Internet protocol suite (TCP/IP) to communicate between networks and devices. It is a network of networks that consists of private, public, academic, business, and government networks of local to global scope, linked by a broad array of electronic, wireless, and optical networking technologies. The Internet carries a vast range of information resources and services, such as the inter-linked hypertext documents and applications of the World Wide Web, electronic mail, telephony, and file sharing.",
// 2 β Scientific abstract
"We introduce a novel lossless text compression algorithm based on micro-diffusion, a multi-step score-based reverse diffusion process applied to the blended probability distributions produced by a cascaded statistical model. The pipeline combines an adaptive PPM model (orders 0β4) with PPMC exclusion, a long-range match model, a trie-based word model, and a high-order context model (orders 5β8). Post-blend, a binary-tree Tweedie denoiser with James-Stein shrinkage corrects residual noise in three steps. On the Canterbury and Large Text Compression benchmarks the method achieves 2.119 bpb on alice29.txt and 1.753 bpb on enwik8, outperforming xz -9 by 16.9% and 11.9% respectively.",
// 3 β Repetitive / structured text (high compressibility)
"AAABBBCCCDDDEEEFFFGGGHHHIIIJJJKKKLLLMMMNNNOOOPPPQQQRRRSSSTTTUUUVVVWWWXXXYYYZZZ AAABBBCCCDDDEEEFFFGGGHHHIIIJJJKKKLLLMMMNNNOOOPPPQQQRRRSSSTTTUUUVVVWWWXXXYYYZZZ AAABBBCCCDDDEEEFFFGGGHHHIIIJJJKKKLLLMMMNNNOOOPPPQQQRRRSSSTTTUUUVVVWWWXXXYYYZZZ The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.",
// 4 β C source code snippet
"static void probs_to_cumfreqs(const double *probs, int64_t *cumfreqs, int64_t *out_total) {\\n cumfreqs[0] = 0;\\n for (int i = 0; i < 256; i++) {\\n int64_t f = (int64_t)(probs[i] * SCALE + 0.5);\\n if (f < 1) f = 1;\\n cumfreqs[i + 1] = cumfreqs[i] + f;\\n }\\n *out_total = cumfreqs[256];\\n}\\n\\nstatic void clamp_normalize(double *probs) {\\n double sum = 0.0;\\n for (int i = 0; i < 256; i++) {\\n if (probs[i] < 1e-10) probs[i] = 1e-10;\\n sum += probs[i];\\n }\\n double inv = 1.0 / sum;\\n for (int i = 0; i < 256; i++) probs[i] *= inv;\\n}"
];
function setExample(i){
document.getElementById('ct-textarea').value=EXAMPLES[i];
}
function show(id){
document.querySelectorAll('.tab').forEach(t=>t.classList.remove('active'));
document.querySelectorAll('.panel').forEach(p=>p.classList.remove('active'));
document.getElementById(id).classList.add('active');
event.target.classList.add('active');
}
function copyB64(id){
var ta=document.getElementById(id);
navigator.clipboard.writeText(ta.value).then(()=>{
var btn=ta.previousElementSibling.querySelector('.copy-btn');
btn.textContent='Copied!'; setTimeout(()=>btn.textContent='Copy',1500);
});
}
</script>
"""
# ---------------------------------------------------------------------------
# Compression logic
# ---------------------------------------------------------------------------
def run_compress(data, filename):
"""Returns (b64_str, in_sz, out_sz, bpb) or raises RuntimeError."""
with tempfile.TemporaryDirectory() as d:
inp = os.path.join(d, "input")
out = os.path.join(d, "output.mdc")
with open(inp, "wb") as fh:
fh.write(data)
r = subprocess.run([BINARY, "compress", inp, out], capture_output=True, text=True)
if r.returncode != 0:
raise RuntimeError(r.stderr or r.stdout)
in_sz = os.path.getsize(inp)
out_sz = os.path.getsize(out)
with open(out, "rb") as fh:
compressed = fh.read()
b64 = base64.b64encode(compressed).decode()
bpb = out_sz * 8 / in_sz
return b64, in_sz, out_sz, bpb
def run_decompress(data):
"""Returns (restored_bytes,) or raises RuntimeError."""
with tempfile.TemporaryDirectory() as d:
inp = os.path.join(d, "input.mdc")
out = os.path.join(d, "output")
with open(inp, "wb") as fh:
fh.write(data)
r = subprocess.run([BINARY, "decompress", inp, out], capture_output=True, text=True)
if r.returncode != 0:
raise RuntimeError(r.stderr or r.stdout)
with open(out, "rb") as fh:
return fh.read()
def compress_result(b64, in_sz, out_sz, bpb, out_name):
dl_href = f"data:application/octet-stream;base64,{b64}"
stats = (f"<b>Original:</b> {fmt(in_sz)} β "
f"<b>Compressed:</b> {fmt(out_sz)} | "
f"{out_sz/in_sz*100:.1f}% | "
f"<b>{bpb:.3f} bpb</b> | "
f"Saved {100-out_sz/in_sz*100:.1f}%")
return dict(stats=stats, dl_href=dl_href, dl_name=out_name,
out_sz=fmt(out_sz), b64=b64)
# ---------------------------------------------------------------------------
# Routes
# ---------------------------------------------------------------------------
@app.route("/health")
def health():
return "ok", 200
@app.route("/")
def index():
return render_template_string(PAGE, tab="ct")
@app.route("/compress_text", methods=["POST"])
def compress_text():
text = request.form.get("text", "").strip()
if not text:
return render_template_string(PAGE, tab="ct", ct_error="Please enter some text.")
try:
data = text.encode("utf-8")
b64, in_sz, out_sz, bpb = run_compress(data, "text")
result = compress_result(b64, in_sz, out_sz, bpb, "compressed.mdc")
return render_template_string(PAGE, tab="ct", ct_result=result, form_text=text)
except Exception as e:
return render_template_string(PAGE, tab="ct", ct_error=str(e), form_text=text)
@app.route("/compress_file", methods=["POST"])
def compress_file():
f = request.files.get("file")
if not f or not f.filename:
return render_template_string(PAGE, tab="cf", cf_error="No file uploaded.")
try:
data = f.read()
b64, in_sz, out_sz, bpb = run_compress(data, f.filename)
out_name = f.filename + ".mdc"
result = compress_result(b64, in_sz, out_sz, bpb, out_name)
return render_template_string(PAGE, tab="cf", cf_result=result)
except Exception as e:
return render_template_string(PAGE, tab="cf", cf_error=str(e))
def _decompress_response(data, tab, form_b64=""):
if data[:4] != b"MDC7":
return render_template_string(PAGE, tab=tab,
dc_error="Not a valid Midicoth (.mdc) file.",
form_b64=form_b64)
try:
restored = run_decompress(data)
except Exception as e:
return render_template_string(PAGE, tab=tab, dc_error=str(e), form_b64=form_b64)
in_sz = len(data)
out_sz = len(restored)
stats = (f"<b>Compressed:</b> {fmt(in_sz)} β "
f"<b>Restored:</b> {fmt(out_sz)} | Lossless β")
result = dict(stats=stats, text=None, dl_href=None, dl_name=None, out_sz=fmt(out_sz))
if is_utf8(restored):
result["text"] = restored.decode("utf-8")
else:
b64 = base64.b64encode(restored).decode()
result["dl_href"] = f"data:application/octet-stream;base64,{b64}"
result["dl_name"] = "restored.bin"
return render_template_string(PAGE, tab=tab, dc_result=result, form_b64=form_b64)
@app.route("/decompress_file", methods=["POST"])
def decompress_file():
f = request.files.get("file")
if not f or not f.filename:
return render_template_string(PAGE, tab="dc", dc_error="No file uploaded.")
return _decompress_response(f.read(), tab="dc")
@app.route("/decompress_b64", methods=["POST"])
def decompress_b64():
raw = request.form.get("b64", "").strip()
if not raw:
return render_template_string(PAGE, tab="dc", dc_error="Please paste base64 data.")
try:
data = base64.b64decode(raw)
except Exception:
return render_template_string(PAGE, tab="dc",
dc_error="Invalid base64 data.", form_b64=raw)
return _decompress_response(data, tab="dc", form_b64=raw)
if __name__ == "__main__":
app.run(host="0.0.0.0", port=7860, debug=False)
|