Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,7 @@
|
|
| 1 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
# Simple app: convert user input into ASCII codes and binary labels
|
| 4 |
|
|
@@ -15,31 +18,107 @@ def string_to_binary_labels(s: str) -> list[int]:
|
|
| 15 |
bits.extend(char_bits)
|
| 16 |
return bits
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
st.title("ASCII & Binary Label Converter")
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
st.
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
# Future: integrate DNA editor mapping for each mutation site here
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import numpy as np
|
| 4 |
+
import pandas as pd
|
| 5 |
|
| 6 |
# Simple app: convert user input into ASCII codes and binary labels
|
| 7 |
|
|
|
|
| 18 |
bits.extend(char_bits)
|
| 19 |
return bits
|
| 20 |
|
| 21 |
+
def image_to_binary_labels(img: Image.Image, max_pixels: int = 256) -> list[int]:
|
| 22 |
+
"""
|
| 23 |
+
Convert an image to binary labels.
|
| 24 |
+
Convert to grayscale, resize to limit pixel count, and binarize (threshold 128).
|
| 25 |
+
"""
|
| 26 |
+
img = img.convert("L") # Convert to grayscale
|
| 27 |
+
img.thumbnail((int(np.sqrt(max_pixels)), int(np.sqrt(max_pixels)))) # Resize while maintaining aspect ratio
|
| 28 |
+
img_array = np.array(img)
|
| 29 |
+
flat = img_array.flatten()
|
| 30 |
+
binarized = (flat > 128).astype(int) # Threshold at 128 to 0/1
|
| 31 |
+
return binarized.tolist()
|
| 32 |
+
|
| 33 |
+
# Predefined headers for the 32 mutation sites
|
| 34 |
+
mutation_site_headers = [
|
| 35 |
+
3244, 3297, 3350, 3399, 3455, 3509, 3562, 3614,
|
| 36 |
+
3665, 3720, 3773, 3824, 3879, 3933, 3985, 4039,
|
| 37 |
+
4089, 4145, 4190, 4245, 4298, 4349, 4402, 4455,
|
| 38 |
+
4510, 4561, 4615, 4668, 4720, 4773, 4828, 4882
|
| 39 |
+
]
|
| 40 |
+
|
| 41 |
st.title("ASCII & Binary Label Converter")
|
| 42 |
+
|
| 43 |
+
# Create tabs
|
| 44 |
+
tab1, tab2 = st.tabs(["Text to Binary Labels", "Image to Binary Labels"])
|
| 45 |
+
|
| 46 |
+
with tab1:
|
| 47 |
+
st.write("Enter text to see its ASCII codes and corresponding binary labels:")
|
| 48 |
+
user_input = st.text_input("Text Input", value="DNA")
|
| 49 |
+
|
| 50 |
+
if user_input:
|
| 51 |
+
# Compute ASCII codes
|
| 52 |
+
ascii_codes = [ord(c) for c in user_input]
|
| 53 |
+
# Compute binary labels
|
| 54 |
+
binary_labels = string_to_binary_labels(user_input)
|
| 55 |
+
|
| 56 |
+
st.subheader("ASCII Codes")
|
| 57 |
+
st.write(ascii_codes)
|
| 58 |
+
|
| 59 |
+
st.subheader("Binary Labels per Character")
|
| 60 |
+
# Display bits grouped per character for readability
|
| 61 |
+
grouped_chars = [binary_labels[i:i+8] for i in range(0, len(binary_labels), 8)]
|
| 62 |
+
for idx, bits in enumerate(grouped_chars):
|
| 63 |
+
st.write(f"'{user_input[idx]}' → {bits}")
|
| 64 |
+
|
| 65 |
+
# New section: 32-bit group display in table format
|
| 66 |
+
st.subheader("Binary Labels (32-bit groups)")
|
| 67 |
+
num_groups = (len(binary_labels) + 31) // 32
|
| 68 |
+
table_data = []
|
| 69 |
+
for grp_idx in range(num_groups):
|
| 70 |
+
start = grp_idx * 32
|
| 71 |
+
end = start + 32
|
| 72 |
+
group = binary_labels[start:end]
|
| 73 |
+
if len(group) < 32:
|
| 74 |
+
group += [0] * (32 - len(group))
|
| 75 |
+
edited_sites = sum(group)
|
| 76 |
+
row = group + [edited_sites]
|
| 77 |
+
table_data.append(row)
|
| 78 |
+
|
| 79 |
+
df = pd.DataFrame(table_data, columns=[str(h) for h in mutation_site_headers] + ["Edited Sites"])
|
| 80 |
+
st.dataframe(df)
|
| 81 |
+
|
| 82 |
+
st.download_button(
|
| 83 |
+
label="Download Binary Labels as CSV",
|
| 84 |
+
data=','.join(str(b) for b in binary_labels),
|
| 85 |
+
file_name="binary_labels.csv",
|
| 86 |
+
mime="text/csv"
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
with tab2:
|
| 90 |
+
st.write("Upload an image (JPG or PNG) to convert it into binary labels:")
|
| 91 |
+
uploaded_file = st.file_uploader("Choose an image file", type=["jpg", "jpeg", "png"])
|
| 92 |
+
|
| 93 |
+
if uploaded_file is not None:
|
| 94 |
+
img = Image.open(uploaded_file)
|
| 95 |
+
st.image(img, caption="Uploaded Image", use_column_width=True)
|
| 96 |
+
|
| 97 |
+
max_pixels = st.slider("Max number of pixels to encode", min_value=32, max_value=1024, value=256, step=32)
|
| 98 |
+
|
| 99 |
+
binary_labels = image_to_binary_labels(img, max_pixels=max_pixels)
|
| 100 |
+
|
| 101 |
+
st.subheader("Binary Labels from Image")
|
| 102 |
+
num_groups = (len(binary_labels) + 31) // 32
|
| 103 |
+
table_data = []
|
| 104 |
+
for grp_idx in range(num_groups):
|
| 105 |
+
start = grp_idx * 32
|
| 106 |
+
end = start + 32
|
| 107 |
+
group = binary_labels[start:end]
|
| 108 |
+
if len(group) < 32:
|
| 109 |
+
group += [0] * (32 - len(group))
|
| 110 |
+
edited_sites = sum(group)
|
| 111 |
+
row = group + [edited_sites]
|
| 112 |
+
table_data.append(row)
|
| 113 |
+
|
| 114 |
+
df = pd.DataFrame(table_data, columns=[str(h) for h in mutation_site_headers] + ["Edited Sites"])
|
| 115 |
+
st.dataframe(df)
|
| 116 |
+
|
| 117 |
+
st.download_button(
|
| 118 |
+
label="Download Image Binary Labels as CSV",
|
| 119 |
+
data=','.join(str(b) for b in binary_labels),
|
| 120 |
+
file_name="image_binary_labels.csv",
|
| 121 |
+
mime="text/csv"
|
| 122 |
+
)
|
| 123 |
|
| 124 |
# Future: integrate DNA editor mapping for each mutation site here
|