TuringsSolutions commited on
Commit
e9ffb0c
Β·
verified Β·
1 Parent(s): 530e015

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +134 -38
src/streamlit_app.py CHANGED
@@ -1,40 +1,136 @@
1
- import altair as alt
2
- import numpy as np
3
- import pandas as pd
4
  import streamlit as st
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
- """
7
- # Welcome to Streamlit!
8
-
9
- Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
10
- If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
11
- forums](https://discuss.streamlit.io).
12
-
13
- In the meantime, below is an example of what you can do with just a few lines of code:
14
- """
15
-
16
- num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
17
- num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
18
-
19
- indices = np.linspace(0, 1, num_points)
20
- theta = 2 * np.pi * num_turns * indices
21
- radius = indices
22
-
23
- x = radius * np.cos(theta)
24
- y = radius * np.sin(theta)
25
-
26
- df = pd.DataFrame({
27
- "x": x,
28
- "y": y,
29
- "idx": indices,
30
- "rand": np.random.randn(num_points),
31
- })
32
-
33
- st.altair_chart(alt.Chart(df, height=700, width=700)
34
- .mark_point(filled=True)
35
- .encode(
36
- x=alt.X("x", axis=None),
37
- y=alt.Y("y", axis=None),
38
- color=alt.Color("idx", legend=None, scale=alt.Scale()),
39
- size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
40
- ))
 
 
 
 
1
  import streamlit as st
2
+ import os
3
+ import tempfile
4
+ import numpy as np
5
+ from PIL import Image
6
+ from flc_core import flc_encode_file, flc_decode_file, cosine_similarity_bytes
7
+
8
+ st.set_page_config(page_title="FLC v1.3 | How it Works", layout="wide")
9
+
10
+ # Styling for a "Scientific Laboratory" look
11
+ st.markdown("""
12
+ <style>
13
+ .reportview-container { background: #0e1117; }
14
+ .main { color: #e0e0e0; }
15
+ h1, h2, h3 { color: #f1c40f !important; }
16
+ .stAlert { background-color: #1a1c24; border: 1px solid #f1c40f; }
17
+ </style>
18
+ """, unsafe_allow_html=True)
19
+
20
+ st.title("πŸŒ€ Fibonacci Lattice Compression (FLC)")
21
+ st.markdown("""
22
+ **FLC v1.3** is a bio-inspired data compression architecture. Unlike standard ZIP or JPEG formats,
23
+ FLC uses the **Golden Ratio ($\Phi$)** to decide which parts of your data are "essential" and which are "noise."
24
+ """)
25
+
26
+ # --- PILLAR 1: THE EXPLAINER ---
27
+ with st.expander("πŸ“– Step-by-Step: How does the 'Secret Sauce' work?"):
28
+ col1, col2, col3 = st.columns(3)
29
+
30
+ with col1:
31
+ st.markdown("### 1. Spectral Projection")
32
+ st.write("""
33
+ We treat your data like a sound wave. Using a **DCT (Discrete Cosine Transform)**,
34
+ we project the bits into frequency space.
35
+ * **Low Frequencies:** The "skeleton" of your data.
36
+ * **High Frequencies:** The "dust" and fine details.
37
+ """)
38
+
39
+ with col2:
40
+ st.markdown("### 2. The Golden Filter")
41
+ st.write("""
42
+ Instead of treating all frequencies equally, FLC uses **Fibonacci Bands**.
43
+ We compress the 'dust' using steps based on the **Golden Ratio ($\Phi \approx 1.618$)**.
44
+ As the frequency increases, the compression gets exponentially more aggressive.
45
+ """)
46
+
47
+ with col3:
48
+ with st.container():
49
+ st.markdown("### 3. Fibonacci Coding")
50
+ st.write("""
51
+ Standard computers use 8-bit bytes. FLC uses **Fibonacci Binary**.
52
+ It's a "universal code" that uses the sum of Fibonacci numbers to represent values,
53
+ making the compressed stream incredibly resilient and dense.
54
+ """)
55
+
56
+ st.divider()
57
+
58
+ # --- PILLAR 2: THE INTERACTIVE DEMO ---
59
+ st.header("πŸ§ͺ Test the Horizon")
60
+ with st.sidebar:
61
+ st.header("πŸŽ›οΈ Architecture Params")
62
+ st.info("Adjusting these changes how the 'Secret Sauce' math is applied.")
63
+
64
+ quality_map = {
65
+ "High Compression (Lossy)": {"bands": 6, "step": 0.08, "desc": "Aggressive $\Phi$-scaling."},
66
+ "Balanced": {"bands": 12, "step": 0.005, "desc": "The Golden Mean of fidelity."},
67
+ "Near-Lossless": {"bands": 24, "step": 0.0001, "desc": "Full spectral recovery."}
68
+ }
69
+
70
+ tier = st.radio("Fidelity Tier", list(quality_map.keys()), index=1)
71
+ st.caption(quality_map[tier]["desc"])
72
+
73
+ st.subheader("Visual Overlays")
74
+ show_spiral = st.checkbox("Fibonacci Spiral Outlines", value=True)
75
+ show_ring = st.checkbox("Event Horizon Ring", value=True)
76
+
77
+ uploaded_file = st.file_uploader("Upload a file (Image, Text, or Binary)", type=["bin", "png", "jpg", "txt"])
78
+
79
+ if uploaded_file is not None:
80
+ with tempfile.TemporaryDirectory() as tmpdir:
81
+ in_path = os.path.join(tmpdir, "input.bin")
82
+ out_flc = os.path.join(tmpdir, "output.flc")
83
+ out_gif = os.path.join(tmpdir, "unzip.gif")
84
+ recovered_path = os.path.join(tmpdir, "recovered.bin")
85
+
86
+ with open(in_path, "wb") as f:
87
+ f.write(uploaded_file.getbuffer())
88
+
89
+ if st.button("RUN HOLOGRAPHIC RECONSTRUCTION"):
90
+ with st.status("Initializing Fibonacci Manifolds...", expanded=True) as status:
91
+ st.write("Transforming data to Frequency Space...")
92
+ enc = flc_encode_file(
93
+ in_path, out_flc, unzip_gif=out_gif,
94
+ n_bands=quality_map[tier]["bands"],
95
+ base_step=quality_map[tier]["step"]
96
+ )
97
+
98
+ st.write("Applying $\Phi$-scaled quantization...")
99
+ dec = flc_decode_file(out_flc, recovered_path)
100
+
101
+ st.write("Generating Holographic Unzip visualization...")
102
+ status.update(label="Reconstruction Complete!", state="complete", expanded=False)
103
+
104
+ # Results Section
105
+ st.subheader("πŸ“Š Compression Performance")
106
+ c1, c2, c3, c4 = st.columns(4)
107
+ c1.metric("Original Size", f"{enc['n_bytes']} B")
108
+ c2.metric("Compressed Size", f"{enc['payload_len']} B")
109
+ c3.metric("Ratio", f"{enc['ratio']:.2%}")
110
+
111
+ # Calculate Similarity
112
+ orig_data = open(in_path, "rb").read()
113
+ reco_data = open(recovered_path, "rb").read()
114
+ fidelity = cosine_similarity_bytes(orig_data, reco_data)
115
+ c4.metric("Data Fidelity", f"{fidelity*100:.2f}%")
116
+
117
+ st.divider()
118
+
119
+ # Visualization
120
+ st.header("🎞️ The Unzip Sequence")
121
+ st.markdown("""
122
+ This animation shows the **Progressive Reconstruction**.
123
+ The 'Hologram' on the left shows the frequency data being added band-by-band.
124
+ The 'Spiral' on the right shows the bits filling the Fibonacci tiles in real-time.
125
+ """)
126
+
127
+ if os.path.exists(out_gif):
128
+ st.image(out_gif, use_container_width=True)
129
+
130
+ st.info("πŸ’‘ Notice how the general shape appears first, and the fine details (noise) appear last. This is the hallmark of Spectral Compression.")
131
+
132
+ with open(out_flc, "rb") as f:
133
+ st.download_button("πŸ“₯ Download Encoded .FLC File", f, file_name="demo.flc")
134
 
135
+ else:
136
+ st.warning("Please upload a file to visualize the Fibonacci transformation.")