Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -30,6 +30,23 @@ def image_to_binary_labels(img: Image.Image, max_pixels: int = 256) -> list[int]
|
|
| 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,
|
|
@@ -48,21 +65,17 @@ with tab1:
|
|
| 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 = []
|
|
@@ -121,4 +134,9 @@ with tab2:
|
|
| 121 |
mime="text/csv"
|
| 122 |
)
|
| 123 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
# Future: integrate DNA editor mapping for each mutation site here
|
|
|
|
| 30 |
binarized = (flat > 128).astype(int) # Threshold at 128 to 0/1
|
| 31 |
return binarized.tolist()
|
| 32 |
|
| 33 |
+
def binary_labels_to_image(binary_labels: list[int], width: int = None, height: int = None) -> Image.Image:
|
| 34 |
+
"""
|
| 35 |
+
Convert binary labels (0/1) back into an image.
|
| 36 |
+
If width and height are not provided, assumes a square image.
|
| 37 |
+
"""
|
| 38 |
+
total_pixels = len(binary_labels)
|
| 39 |
+
if width is None or height is None:
|
| 40 |
+
side = int(np.ceil(np.sqrt(total_pixels)))
|
| 41 |
+
width = height = side
|
| 42 |
+
needed_pixels = width * height
|
| 43 |
+
if total_pixels < needed_pixels:
|
| 44 |
+
binary_labels += [0] * (needed_pixels - total_pixels)
|
| 45 |
+
array = np.array(binary_labels, dtype=np.uint8) * 255
|
| 46 |
+
image_array = array.reshape((height, width))
|
| 47 |
+
img = Image.fromarray(image_array, mode='L')
|
| 48 |
+
return img
|
| 49 |
+
|
| 50 |
# Predefined headers for the 32 mutation sites
|
| 51 |
mutation_site_headers = [
|
| 52 |
3244, 3297, 3350, 3399, 3455, 3509, 3562, 3614,
|
|
|
|
| 65 |
user_input = st.text_input("Text Input", value="DNA")
|
| 66 |
|
| 67 |
if user_input:
|
|
|
|
| 68 |
ascii_codes = [ord(c) for c in user_input]
|
|
|
|
| 69 |
binary_labels = string_to_binary_labels(user_input)
|
| 70 |
|
| 71 |
st.subheader("ASCII Codes")
|
| 72 |
st.write(ascii_codes)
|
| 73 |
|
| 74 |
st.subheader("Binary Labels per Character")
|
|
|
|
| 75 |
grouped_chars = [binary_labels[i:i+8] for i in range(0, len(binary_labels), 8)]
|
| 76 |
for idx, bits in enumerate(grouped_chars):
|
| 77 |
st.write(f"'{user_input[idx]}' → {bits}")
|
| 78 |
|
|
|
|
| 79 |
st.subheader("Binary Labels (32-bit groups)")
|
| 80 |
num_groups = (len(binary_labels) + 31) // 32
|
| 81 |
table_data = []
|
|
|
|
| 134 |
mime="text/csv"
|
| 135 |
)
|
| 136 |
|
| 137 |
+
st.subheader("Reconstruct Image from Binary Labels")
|
| 138 |
+
if st.button("Reconstruct Image"):
|
| 139 |
+
reconstructed_img = binary_labels_to_image(binary_labels)
|
| 140 |
+
st.image(reconstructed_img, caption="Reconstructed Image", use_column_width=True)
|
| 141 |
+
|
| 142 |
# Future: integrate DNA editor mapping for each mutation site here
|