File size: 6,265 Bytes
b4d009a dfa691e e9ffb0c dfa691e e9ffb0c |
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 |
import sys
import os
# Get the absolute path of the directory where this script is located
current_dir = os.path.dirname(os.path.abspath(__file__))
# If the script is in /src, add that directory to sys.path so it can find flc_core.py
if current_dir not in sys.path:
sys.path.insert(0, current_dir)
# Now you can safely import your module
from flc_core import flc_encode_file, flc_decode_file, cosine_similarity_bytes
import streamlit as st
import os
import tempfile
import numpy as np
from PIL import Image
from flc_core import flc_encode_file, flc_decode_file, cosine_similarity_bytes
st.set_page_config(page_title="FLC v1.3 | How it Works", layout="wide")
# Styling for a "Scientific Laboratory" look
st.markdown("""
<style>
.reportview-container { background: #0e1117; }
.main { color: #e0e0e0; }
h1, h2, h3 { color: #f1c40f !important; }
.stAlert { background-color: #1a1c24; border: 1px solid #f1c40f; }
</style>
""", unsafe_allow_html=True)
st.title("π Fibonacci Lattice Compression (FLC)")
st.markdown("""
**FLC v1.3** is a bio-inspired data compression architecture. Unlike standard ZIP or JPEG formats,
FLC uses the **Golden Ratio ($\Phi$)** to decide which parts of your data are "essential" and which are "noise."
""")
# --- PILLAR 1: THE EXPLAINER ---
with st.expander("π Step-by-Step: How does the 'Secret Sauce' work?"):
col1, col2, col3 = st.columns(3)
with col1:
st.markdown("### 1. Spectral Projection")
st.write("""
We treat your data like a sound wave. Using a **DCT (Discrete Cosine Transform)**,
we project the bits into frequency space.
* **Low Frequencies:** The "skeleton" of your data.
* **High Frequencies:** The "dust" and fine details.
""")
with col2:
st.markdown("### 2. The Golden Filter")
st.write("""
Instead of treating all frequencies equally, FLC uses **Fibonacci Bands**.
We compress the 'dust' using steps based on the **Golden Ratio ($\Phi \approx 1.618$)**.
As the frequency increases, the compression gets exponentially more aggressive.
""")
with col3:
with st.container():
st.markdown("### 3. Fibonacci Coding")
st.write("""
Standard computers use 8-bit bytes. FLC uses **Fibonacci Binary**.
It's a "universal code" that uses the sum of Fibonacci numbers to represent values,
making the compressed stream incredibly resilient and dense.
""")
st.divider()
# --- PILLAR 2: THE INTERACTIVE DEMO ---
st.header("π§ͺ Test the Horizon")
with st.sidebar:
st.header("ποΈ Architecture Params")
st.info("Adjusting these changes how the 'Secret Sauce' math is applied.")
quality_map = {
"High Compression (Lossy)": {"bands": 6, "step": 0.08, "desc": "Aggressive $\Phi$-scaling."},
"Balanced": {"bands": 12, "step": 0.005, "desc": "The Golden Mean of fidelity."},
"Near-Lossless": {"bands": 24, "step": 0.0001, "desc": "Full spectral recovery."}
}
tier = st.radio("Fidelity Tier", list(quality_map.keys()), index=1)
st.caption(quality_map[tier]["desc"])
st.subheader("Visual Overlays")
show_spiral = st.checkbox("Fibonacci Spiral Outlines", value=True)
show_ring = st.checkbox("Event Horizon Ring", value=True)
uploaded_file = st.file_uploader("Upload a file (Image, Text, or Binary)", type=["bin", "png", "jpg", "txt"])
if uploaded_file is not None:
with tempfile.TemporaryDirectory() as tmpdir:
in_path = os.path.join(tmpdir, "input.bin")
out_flc = os.path.join(tmpdir, "output.flc")
out_gif = os.path.join(tmpdir, "unzip.gif")
recovered_path = os.path.join(tmpdir, "recovered.bin")
with open(in_path, "wb") as f:
f.write(uploaded_file.getbuffer())
if st.button("RUN HOLOGRAPHIC RECONSTRUCTION"):
with st.status("Initializing Fibonacci Manifolds...", expanded=True) as status:
st.write("Transforming data to Frequency Space...")
enc = flc_encode_file(
in_path, out_flc, unzip_gif=out_gif,
n_bands=quality_map[tier]["bands"],
base_step=quality_map[tier]["step"]
)
st.write("Applying $\Phi$-scaled quantization...")
dec = flc_decode_file(out_flc, recovered_path)
st.write("Generating Holographic Unzip visualization...")
status.update(label="Reconstruction Complete!", state="complete", expanded=False)
# Results Section
st.subheader("π Compression Performance")
c1, c2, c3, c4 = st.columns(4)
c1.metric("Original Size", f"{enc['n_bytes']} B")
c2.metric("Compressed Size", f"{enc['payload_len']} B")
c3.metric("Ratio", f"{enc['ratio']:.2%}")
# Calculate Similarity
orig_data = open(in_path, "rb").read()
reco_data = open(recovered_path, "rb").read()
fidelity = cosine_similarity_bytes(orig_data, reco_data)
c4.metric("Data Fidelity", f"{fidelity*100:.2f}%")
st.divider()
# Visualization
st.header("ποΈ The Unzip Sequence")
st.markdown("""
This animation shows the **Progressive Reconstruction**.
The 'Hologram' on the left shows the frequency data being added band-by-band.
The 'Spiral' on the right shows the bits filling the Fibonacci tiles in real-time.
""")
if os.path.exists(out_gif):
st.image(out_gif, use_container_width=True)
st.info("π‘ Notice how the general shape appears first, and the fine details (noise) appear last. This is the hallmark of Spectral Compression.")
with open(out_flc, "rb") as f:
st.download_button("π₯ Download Encoded .FLC File", f, file_name="demo.flc")
else:
st.warning("Please upload a file to visualize the Fibonacci transformation.") |