Team Ai
Apppublic

awacke1/Human.Feedback.Dynamic.JSONL.Fields

sourceHugging Facemitupdated 4y agoView on Hugging Face
1likes
app.py74 linesDownload Raw Back to root
1import json2import os3import streamlit as st4 5# Define the fields for the JSONL file6FIELDS = [7    "CodeValue",8    "CodeType",9    "Context",10    "Question",11    "AnswerText",12    "UpVoteCount",13    "DownVoteCount",14    "VoteComment",15]16 17# Define the IO file pattern18IO_PATTERN = "*.jsonl"19 20 21def read_jsonl_file(file_path):22    """Read a JSONL file and return a list of dictionaries."""23    if not os.path.exists(file_path):24        return []25    with open(file_path, "r") as f:26        lines = f.readlines()27    records = [json.loads(line) for line in lines]28    return records29 30 31def write_jsonl_file(file_path, records):32    """Write a list of dictionaries to a JSONL file."""33    with open(file_path, "w") as f:34        for record in records:35            f.write(json.dumps(record) + "\n")36 37 38def main():39    # Get the list of JSONL files in the current directory40    jsonl_files = [f for f in os.listdir() if f.endswith(".jsonl")]41 42    # If there are no JSONL files, create one43    if not jsonl_files:44        st.warning("No JSONL files found. Creating new file.")45        jsonl_files.append("data.jsonl")46 47    # Display the list of JSONL files48    selected_file = st.sidebar.selectbox("Select JSONL file", jsonl_files)49    st.sidebar.write(f"Selected file: {selected_file}")50 51    # Read the selected JSONL file52    records = read_jsonl_file(selected_file)53 54    # Autogenerate labels and inputs for the fields55    for field in FIELDS:56        value = st.text_input(field, key=field)57        st.write(f"{field}: {value}")58 59    # Add a record to the JSONL file60    if st.button("Add Record"):61        record = {field: st.session_state[field] for field in FIELDS}62        records.append(record)63        write_jsonl_file(selected_file, records)64        st.success("Record added!")65 66    # Display the current contents of the JSONL file67    st.write(f"Current contents of {selected_file}:")68    for record in records:69        st.write(record)70 71 72if __name__ == "__main__":73    main()74