Spaces:
Build error
Build error
File size: 3,011 Bytes
d70b4f4 f9ff716 be3cbf2 0917767 d70b4f4 9f031d2 0a9e582 6e2743a d70b4f4 0917767 1016625 0917767 8f1ef29 0917767 8f1ef29 994b202 0917767 8f1ef29 0917767 8f1ef29 0917767 1b463cd be3cbf2 0917767 31c55d1 1b463cd 0917767 9f031d2 287476f 9f031d2 6e2743a 0661f88 0917767 8f1ef29 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | import streamlit as st
import zipfile, os, json
from io import StringIO
if 'data' not in st.session_state:
st.session_state.data = 0
if 'download' not in st.session_state:
st.session_state.download = False
if 'labels' not in st.session_state:
st.session_state.labels = []
if 'images' not in st.session_state:
st.session_state.images = []
c1, c2 = st.columns(2)
with c1:
if st.button("Add new data"):
st.session_state.data += 1
with c2:
if st.button('Remove last data'):
if st.session_state.data > 0 :
st.session_state.data -= 1
def json_to_tokens(data):
if isinstance(data, dict):
result = ""
for key, value in data.items():
result += f"<{key}>"
result += json_to_tokens(value)
result += f"</{key}>"
return result
elif isinstance(data, list):
result = ""
for item in data:
result += json_to_tokens(item)
result += "<sep/>"
result = result[:-6]
return result
else:
return str(data)
with st.form("my_form"):
col1, col2 = st.columns(2)
for x in range(st.session_state.data):
with col1:
f = st.file_uploader("Upload Image",type=['png', 'jpg', 'jpeg'], key=f'image_{x}')
# st.session_state.labels.append(f)
with col2:
txt = st.text_area("""Json Text""", key=f'label_{x}')
# st.session_state.labels.append(txt)
submitted = st.form_submit_button("Submit")
if submitted:
text = []
images = []
for x in range(st.session_state.data):
k = json.loads(st.session_state[f'label_{x}'])
text.append(k)
images.append(st.session_state[f'image_{x}'])
words = []
for i,m in enumerate(text):
words.append('<s_start>')
words[i] += json_to_tokens(m)
words[i] += '</s_start>'
with zipfile.ZipFile('images.zip', 'w') as img_zip:
for i,image in enumerate(images):
img_name = f"{i}_image.jpg"
img_zip.writestr(img_name, image.read())
with zipfile.ZipFile("labels.zip", "w") as zip:
for i,t in enumerate(words):
txt_name = f"{i}_label.txt"
zip.writestr(txt_name, t)
st.session_state.download = True
if st.session_state.download:
c_1, c_2 = st.columns(2)
with c_1:
with open("images.zip", "rb") as file:
btn = st.download_button(
label="Download Images",
data=file,
file_name="images.zip")
with c_2:
with open("labels.zip", "rb") as file:
btn = st.download_button(
label="Download Labels",
data=file,
file_name="labels.zip")
|