Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,7 +3,6 @@ import os
|
|
| 3 |
import base64
|
| 4 |
import streamlit as st
|
| 5 |
|
| 6 |
-
# Define the fields for the JSONL file
|
| 7 |
FIELDS = [
|
| 8 |
"CodeValue",
|
| 9 |
"CodeType",
|
|
@@ -15,12 +14,10 @@ FIELDS = [
|
|
| 15 |
"VoteComment",
|
| 16 |
]
|
| 17 |
|
| 18 |
-
# Define the IO file pattern
|
| 19 |
IO_PATTERN = "*.jsonl"
|
| 20 |
|
| 21 |
|
| 22 |
def read_jsonl_file(file_path):
|
| 23 |
-
"""Read a JSONL file and return a list of dictionaries."""
|
| 24 |
if not os.path.exists(file_path):
|
| 25 |
return []
|
| 26 |
with open(file_path, "r") as f:
|
|
@@ -30,19 +27,16 @@ def read_jsonl_file(file_path):
|
|
| 30 |
|
| 31 |
|
| 32 |
def write_jsonl_file(file_path, records):
|
| 33 |
-
"""Write a list of dictionaries to a JSONL file."""
|
| 34 |
with open(file_path, "w") as f:
|
| 35 |
for record in records:
|
| 36 |
f.write(json.dumps(record) + "\n")
|
| 37 |
|
| 38 |
|
| 39 |
def list_files():
|
| 40 |
-
"""List all JSONL files in the current directory."""
|
| 41 |
return [f for f in os.listdir() if f.endswith(".jsonl")]
|
| 42 |
|
| 43 |
|
| 44 |
def download_link(file_path):
|
| 45 |
-
"""Generate a base64 download link for a file."""
|
| 46 |
with open(file_path, "rb") as f:
|
| 47 |
contents = f.read()
|
| 48 |
b64 = base64.b64encode(contents).decode()
|
|
@@ -51,21 +45,18 @@ def download_link(file_path):
|
|
| 51 |
|
| 52 |
|
| 53 |
def main():
|
| 54 |
-
# Get the list of JSONL files in the current directory
|
| 55 |
jsonl_files = list_files()
|
| 56 |
|
| 57 |
-
# If there are no JSONL files, create one
|
| 58 |
if not jsonl_files:
|
| 59 |
st.warning("No JSONL files found. Creating new file.")
|
| 60 |
jsonl_files.append("data.jsonl")
|
|
|
|
| 61 |
|
| 62 |
-
# Add the ability to name or rename a file
|
| 63 |
selected_file = st.sidebar.text_input("Enter file name", value=jsonl_files[0])
|
| 64 |
if selected_file != jsonl_files[0]:
|
| 65 |
os.rename(jsonl_files[0], selected_file)
|
| 66 |
jsonl_files[0] = selected_file
|
| 67 |
|
| 68 |
-
# Display the list of JSONL files
|
| 69 |
st.sidebar.write("JSONL files:")
|
| 70 |
selected_file_index = st.sidebar.selectbox("", range(len(jsonl_files)))
|
| 71 |
for i, file_name in enumerate(jsonl_files):
|
|
@@ -75,28 +66,24 @@ def main():
|
|
| 75 |
else:
|
| 76 |
st.sidebar.write(file_name)
|
| 77 |
|
| 78 |
-
# Display a download link for the selected JSONL file
|
| 79 |
st.sidebar.markdown(download_link(selected_file), unsafe_allow_html=True)
|
| 80 |
|
| 81 |
-
# Read the selected JSONL file
|
| 82 |
records = read_jsonl_file(selected_file)
|
| 83 |
|
| 84 |
-
# Autogenerate labels and inputs for the fields
|
| 85 |
for field in FIELDS:
|
| 86 |
value = st.text_input(field, key=field)
|
| 87 |
st.write(f"{field}: {value}")
|
| 88 |
|
| 89 |
-
# Add a record to the JSONL file
|
| 90 |
if st.button("Add Record"):
|
| 91 |
record = {field: st.session_state[field] for field in FIELDS}
|
| 92 |
records.append(record)
|
| 93 |
write_jsonl_file(selected_file, records)
|
| 94 |
st.success("Record added!")
|
| 95 |
|
| 96 |
-
# Display the current contents of the JSONL file
|
| 97 |
st.write(f"Current contents of {selected_file}:")
|
| 98 |
for record in records:
|
| 99 |
st.write(record)
|
| 100 |
|
|
|
|
| 101 |
if __name__ == "__main__":
|
| 102 |
main()
|
|
|
|
| 3 |
import base64
|
| 4 |
import streamlit as st
|
| 5 |
|
|
|
|
| 6 |
FIELDS = [
|
| 7 |
"CodeValue",
|
| 8 |
"CodeType",
|
|
|
|
| 14 |
"VoteComment",
|
| 15 |
]
|
| 16 |
|
|
|
|
| 17 |
IO_PATTERN = "*.jsonl"
|
| 18 |
|
| 19 |
|
| 20 |
def read_jsonl_file(file_path):
|
|
|
|
| 21 |
if not os.path.exists(file_path):
|
| 22 |
return []
|
| 23 |
with open(file_path, "r") as f:
|
|
|
|
| 27 |
|
| 28 |
|
| 29 |
def write_jsonl_file(file_path, records):
|
|
|
|
| 30 |
with open(file_path, "w") as f:
|
| 31 |
for record in records:
|
| 32 |
f.write(json.dumps(record) + "\n")
|
| 33 |
|
| 34 |
|
| 35 |
def list_files():
|
|
|
|
| 36 |
return [f for f in os.listdir() if f.endswith(".jsonl")]
|
| 37 |
|
| 38 |
|
| 39 |
def download_link(file_path):
|
|
|
|
| 40 |
with open(file_path, "rb") as f:
|
| 41 |
contents = f.read()
|
| 42 |
b64 = base64.b64encode(contents).decode()
|
|
|
|
| 45 |
|
| 46 |
|
| 47 |
def main():
|
|
|
|
| 48 |
jsonl_files = list_files()
|
| 49 |
|
|
|
|
| 50 |
if not jsonl_files:
|
| 51 |
st.warning("No JSONL files found. Creating new file.")
|
| 52 |
jsonl_files.append("data.jsonl")
|
| 53 |
+
write_jsonl_file("data.jsonl", [])
|
| 54 |
|
|
|
|
| 55 |
selected_file = st.sidebar.text_input("Enter file name", value=jsonl_files[0])
|
| 56 |
if selected_file != jsonl_files[0]:
|
| 57 |
os.rename(jsonl_files[0], selected_file)
|
| 58 |
jsonl_files[0] = selected_file
|
| 59 |
|
|
|
|
| 60 |
st.sidebar.write("JSONL files:")
|
| 61 |
selected_file_index = st.sidebar.selectbox("", range(len(jsonl_files)))
|
| 62 |
for i, file_name in enumerate(jsonl_files):
|
|
|
|
| 66 |
else:
|
| 67 |
st.sidebar.write(file_name)
|
| 68 |
|
|
|
|
| 69 |
st.sidebar.markdown(download_link(selected_file), unsafe_allow_html=True)
|
| 70 |
|
|
|
|
| 71 |
records = read_jsonl_file(selected_file)
|
| 72 |
|
|
|
|
| 73 |
for field in FIELDS:
|
| 74 |
value = st.text_input(field, key=field)
|
| 75 |
st.write(f"{field}: {value}")
|
| 76 |
|
|
|
|
| 77 |
if st.button("Add Record"):
|
| 78 |
record = {field: st.session_state[field] for field in FIELDS}
|
| 79 |
records.append(record)
|
| 80 |
write_jsonl_file(selected_file, records)
|
| 81 |
st.success("Record added!")
|
| 82 |
|
|
|
|
| 83 |
st.write(f"Current contents of {selected_file}:")
|
| 84 |
for record in records:
|
| 85 |
st.write(record)
|
| 86 |
|
| 87 |
+
|
| 88 |
if __name__ == "__main__":
|
| 89 |
main()
|