Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
|
| 3 |
+
# Simple app: convert user input into ASCII codes and binary labels
|
| 4 |
+
|
| 5 |
+
def string_to_binary_labels(s: str) -> list[int]:
|
| 6 |
+
"""
|
| 7 |
+
Convert a string into a flat list of binary labels (0 or 1) representing
|
| 8 |
+
each character's 8-bit ASCII code.
|
| 9 |
+
"""
|
| 10 |
+
bits: list[int] = []
|
| 11 |
+
for char in s:
|
| 12 |
+
ascii_code = ord(char)
|
| 13 |
+
# Extract 8-bit binary representation (MSB first)
|
| 14 |
+
char_bits = [(ascii_code >> bit) & 1 for bit in range(7, -1, -1)]
|
| 15 |
+
bits.extend(char_bits)
|
| 16 |
+
return bits
|
| 17 |
+
|
| 18 |
+
st.title("ASCII & Binary Label Converter")
|
| 19 |
+
st.write("Enter text to see its ASCII codes and corresponding binary labels:")
|
| 20 |
+
|
| 21 |
+
user_input = st.text_input("Text Input", value="DNA")
|
| 22 |
+
|
| 23 |
+
if user_input:
|
| 24 |
+
# Compute ASCII codes
|
| 25 |
+
ascii_codes = [ord(c) for c in user_input]
|
| 26 |
+
# Compute binary labels
|
| 27 |
+
binary_labels = string_to_binary_labels(user_input)
|
| 28 |
+
|
| 29 |
+
st.subheader("ASCII Codes")
|
| 30 |
+
st.write(ascii_codes)
|
| 31 |
+
|
| 32 |
+
st.subheader("Binary Labels")
|
| 33 |
+
# Display bits grouped per character for readability
|
| 34 |
+
grouped = [binary_labels[i:i+8] for i in range(0, len(binary_labels), 8)]
|
| 35 |
+
for idx, bits in enumerate(grouped):
|
| 36 |
+
st.write(f"'{user_input[idx]}' → {bits}")
|
| 37 |
+
|
| 38 |
+
st.download_button(
|
| 39 |
+
label="Download Binary Labels as CSV",
|
| 40 |
+
data=','.join(str(b) for b in binary_labels),
|
| 41 |
+
file_name="binary_labels.csv",
|
| 42 |
+
mime="text/csv"
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
# Future: integrate DNA editor mapping for each mutation site here
|