awacke1's picture
Update app.py
6f48802
raw
history blame
1.98 kB
import json
import os
import streamlit as st
# Define the fields for the JSONL file
FIELDS = [
"CodeValue",
"CodeType",
"Context",
"Question",
"AnswerText",
"UpVoteCount",
"DownVoteCount",
"VoteComment",
]
# Define the IO file pattern
IO_PATTERN = "*.jsonl"
def read_jsonl_file(file_path):
"""Read a JSONL file and return a list of dictionaries."""
if not os.path.exists(file_path):
return []
with open(file_path, "r") as f:
lines = f.readlines()
records = [json.loads(line) for line in lines]
return records
def write_jsonl_file(file_path, records):
"""Write a list of dictionaries to a JSONL file."""
with open(file_path, "w") as f:
for record in records:
f.write(json.dumps(record) + "\n")
def main():
# Get the list of JSONL files in the current directory
jsonl_files = [f for f in os.listdir() if f.endswith(".jsonl")]
# If there are no JSONL files, create one
if not jsonl_files:
st.warning("No JSONL files found. Creating new file.")
jsonl_files.append("data.jsonl")
# Display the list of JSONL files
selected_file = st.sidebar.selectbox("Select JSONL file", jsonl_files)
st.sidebar.write(f"Selected file: {selected_file}")
# Read the selected JSONL file
records = read_jsonl_file(selected_file)
# Autogenerate labels and inputs for the fields
for field in FIELDS:
value = st.text_input(field, key=field)
st.write(f"{field}: {value}")
# Add a record to the JSONL file
if st.button("Add Record"):
record = {field: st.session_state[field] for field in FIELDS}
records.append(record)
write_jsonl_file(selected_file, records)
st.success("Record added!")
# Display the current contents of the JSONL file
st.write(f"Current contents of {selected_file}:")
for record in records:
st.write(record)
if __name__ == "__main__":
main()