awacke1/Human.Feedback.Dynamic.JSONL.Dataset.Download
1
1import json2import os3import base644import streamlit as st5 6FIELDS = [7 "CodeValue",8 "CodeType",9 "Context",10 "Question",11 "AnswerText",12 "UpVoteCount",13 "DownVoteCount",14 "VoteComment",15]16 17IO_PATTERN = "*.jsonl"18 19 20def read_jsonl_file(file_path):21 if not os.path.exists(file_path):22 return []23 with open(file_path, "r") as f:24 lines = f.readlines()25 records = [json.loads(line) for line in lines]26 return records27 28 29def 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 35def list_files():36 return [f for f in os.listdir() if f.endswith(".jsonl")]37 38 39def download_link(file_path):40 with open(file_path, "rb") as f:41 contents = f.read()42 b64 = base64.b64encode(contents).decode()43 href = f'<a href="data:application/octet-stream;base64,{b64}" download="{file_path}">Download</a>'44 return href45 46 47def 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_file59 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):63 if i == selected_file_index:64 selected_file = file_name65 st.sidebar.write(f"> {file_name}")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 88if __name__ == "__main__":89 main()90 