import json import os import base64 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 list_files(): """List all JSONL files in the current directory.""" return [f for f in os.listdir() if f.endswith(".jsonl")] def download_link(file_path): """Generate a base64 download link for a file.""" with open(file_path, "rb") as f: contents = f.read() b64 = base64.b64encode(contents).decode() href = f'Download' return href def main(): # Get the list of JSONL files in the current directory jsonl_files = list_files() # 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") # Add the ability to name or rename a file selected_file = st.sidebar.text_input("Enter file name", value=jsonl_files[0]) if selected_file != jsonl_files[0]: os.rename(jsonl_files[0], selected_file) jsonl_files[0] = selected_file # Display the list of JSONL files st.sidebar.write("JSONL files:") selected_file_index = st.sidebar.selectbox("", range(len(jsonl_files))) for i, file_name in enumerate(jsonl_files): if i == selected_file_index: selected_file = file_name st.sidebar.write(f"> {file_name}") else: st.sidebar.write(file_name) # Display a download link for the selected JSONL file st.sidebar.markdown(download_link(selected_file), unsafe_allow_html=True) # 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()