ceckenrode commited on
Commit
2614d41
·
1 Parent(s): 5eb4111

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +89 -0
app.py ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import os
3
+ import base64
4
+ import streamlit as st
5
+
6
+ FIELDS = [
7
+ "CodeValue",
8
+ "CodeType",
9
+ "Context",
10
+ "Question",
11
+ "AnswerText",
12
+ "UpVoteCount",
13
+ "DownVoteCount",
14
+ "VoteComment",
15
+ ]
16
+
17
+ IO_PATTERN = "*.jsonl"
18
+
19
+
20
+ def 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 records
27
+
28
+
29
+ def 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
+
35
+ def list_files():
36
+ return [f for f in os.listdir() if f.endswith(".jsonl")]
37
+
38
+
39
+ def 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 href
45
+
46
+
47
+ def 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_file
59
+
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_name
65
+ 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
+
88
+ if __name__ == "__main__":
89
+ main()