File size: 1,982 Bytes
566f3bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6f48802
 
566f3bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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()