Spaces:
Sleeping
Sleeping
Update tab/tab5_randomness_visualizer.py
Browse files
tab/tab5_randomness_visualizer.py
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import matplotlib.pyplot as plt
|
| 3 |
from io import BytesIO
|
| 4 |
from PIL import Image
|
| 5 |
import random
|
| 6 |
-
from matplotlib.patches import Patch
|
| 7 |
|
| 8 |
-
# ----------
|
| 9 |
def plot_key_randomness(key_str):
|
| 10 |
bits = [int(b) for b in key_str.strip() if b in '01']
|
| 11 |
total_bits = len(bits)
|
|
@@ -22,7 +24,6 @@ def plot_key_randomness(key_str):
|
|
| 22 |
ax.set_xlabel("Bit Position")
|
| 23 |
ax.set_yticks([])
|
| 24 |
|
| 25 |
-
# Legend
|
| 26 |
legend_handles = [
|
| 27 |
Patch(color='#1f77b4', label='0 (Blue)'),
|
| 28 |
Patch(color='#ff7f0e', label='1 (Orange)')
|
|
@@ -35,7 +36,6 @@ def plot_key_randomness(key_str):
|
|
| 35 |
plt.close()
|
| 36 |
buf.seek(0)
|
| 37 |
|
| 38 |
-
# Verdict based on balance
|
| 39 |
if diff <= total_bits * 0.1:
|
| 40 |
verdict = f"β
Balanced Key: {zero_count} zeros & {one_count} ones β good randomness!"
|
| 41 |
else:
|
|
@@ -43,8 +43,7 @@ def plot_key_randomness(key_str):
|
|
| 43 |
|
| 44 |
return Image.open(buf), verdict
|
| 45 |
|
| 46 |
-
|
| 47 |
-
# ---------- Helper Function 2: Add Noise and Compare ----------
|
| 48 |
def add_noise_to_key(key_str, flip_percent=0.1):
|
| 49 |
bits = list(key_str.strip())
|
| 50 |
total_bits = len(bits)
|
|
@@ -53,8 +52,8 @@ def add_noise_to_key(key_str, flip_percent=0.1):
|
|
| 53 |
|
| 54 |
for i in flip_indices:
|
| 55 |
bits[i] = '1' if bits[i] == '0' else '0'
|
| 56 |
-
return ''.join(bits), flip_indices
|
| 57 |
|
|
|
|
| 58 |
|
| 59 |
def compare_original_vs_noisy(key_str):
|
| 60 |
key_str = ''.join([b for b in key_str.strip() if b in '01'])
|
|
@@ -91,25 +90,19 @@ def compare_original_vs_noisy(key_str):
|
|
| 91 |
|
| 92 |
return Image.open(buf), summary
|
| 93 |
|
| 94 |
-
|
| 95 |
# ---------- Combined Gradio Tab ----------
|
| 96 |
-
def
|
| 97 |
with gr.Tab("π QKD Key Analysis (Randomness + Noise Simulation)"):
|
| 98 |
-
# --- Input Binary Key ---
|
| 99 |
input_key = gr.Textbox(label="π Enter Your QKD Key (binary)", lines=3, placeholder="Example: 0101011100...")
|
| 100 |
|
| 101 |
-
# --- Action Button ---
|
| 102 |
process_btn = gr.Button("Analyze Key")
|
| 103 |
|
| 104 |
-
# --- Output 1: Randomness Graph + Verdict ---
|
| 105 |
randomness_graph = gr.Image(label="π§ͺ Bit Distribution Graph")
|
| 106 |
randomness_text = gr.Textbox(label="Randomness Insight", lines=2)
|
| 107 |
|
| 108 |
-
# --- Output 2: Noise Simulation Graph + Summary ---
|
| 109 |
noise_graph = gr.Image(label="π Noise Simulation (Original vs Flipped Bits)")
|
| 110 |
noise_summary = gr.Textbox(label="Noise Impact Summary", lines=2)
|
| 111 |
|
| 112 |
-
# --- Connect everything ---
|
| 113 |
def full_analysis(binary_key):
|
| 114 |
return (
|
| 115 |
*plot_key_randomness(binary_key),
|
|
|
|
| 1 |
+
# tab5_randomness_visualizer.py
|
| 2 |
+
|
| 3 |
import gradio as gr
|
| 4 |
import matplotlib.pyplot as plt
|
| 5 |
from io import BytesIO
|
| 6 |
from PIL import Image
|
| 7 |
import random
|
| 8 |
+
from matplotlib.patches import Patch # For legend
|
| 9 |
|
| 10 |
+
# ---------- Randomness Graph Function ----------
|
| 11 |
def plot_key_randomness(key_str):
|
| 12 |
bits = [int(b) for b in key_str.strip() if b in '01']
|
| 13 |
total_bits = len(bits)
|
|
|
|
| 24 |
ax.set_xlabel("Bit Position")
|
| 25 |
ax.set_yticks([])
|
| 26 |
|
|
|
|
| 27 |
legend_handles = [
|
| 28 |
Patch(color='#1f77b4', label='0 (Blue)'),
|
| 29 |
Patch(color='#ff7f0e', label='1 (Orange)')
|
|
|
|
| 36 |
plt.close()
|
| 37 |
buf.seek(0)
|
| 38 |
|
|
|
|
| 39 |
if diff <= total_bits * 0.1:
|
| 40 |
verdict = f"β
Balanced Key: {zero_count} zeros & {one_count} ones β good randomness!"
|
| 41 |
else:
|
|
|
|
| 43 |
|
| 44 |
return Image.open(buf), verdict
|
| 45 |
|
| 46 |
+
# ---------- Noise Addition & Visualizer ----------
|
|
|
|
| 47 |
def add_noise_to_key(key_str, flip_percent=0.1):
|
| 48 |
bits = list(key_str.strip())
|
| 49 |
total_bits = len(bits)
|
|
|
|
| 52 |
|
| 53 |
for i in flip_indices:
|
| 54 |
bits[i] = '1' if bits[i] == '0' else '0'
|
|
|
|
| 55 |
|
| 56 |
+
return ''.join(bits), flip_indices
|
| 57 |
|
| 58 |
def compare_original_vs_noisy(key_str):
|
| 59 |
key_str = ''.join([b for b in key_str.strip() if b in '01'])
|
|
|
|
| 90 |
|
| 91 |
return Image.open(buf), summary
|
| 92 |
|
|
|
|
| 93 |
# ---------- Combined Gradio Tab ----------
|
| 94 |
+
def get_tab5_randomness():
|
| 95 |
with gr.Tab("π QKD Key Analysis (Randomness + Noise Simulation)"):
|
|
|
|
| 96 |
input_key = gr.Textbox(label="π Enter Your QKD Key (binary)", lines=3, placeholder="Example: 0101011100...")
|
| 97 |
|
|
|
|
| 98 |
process_btn = gr.Button("Analyze Key")
|
| 99 |
|
|
|
|
| 100 |
randomness_graph = gr.Image(label="π§ͺ Bit Distribution Graph")
|
| 101 |
randomness_text = gr.Textbox(label="Randomness Insight", lines=2)
|
| 102 |
|
|
|
|
| 103 |
noise_graph = gr.Image(label="π Noise Simulation (Original vs Flipped Bits)")
|
| 104 |
noise_summary = gr.Textbox(label="Noise Impact Summary", lines=2)
|
| 105 |
|
|
|
|
| 106 |
def full_analysis(binary_key):
|
| 107 |
return (
|
| 108 |
*plot_key_randomness(binary_key),
|