awacke1 commited on
Commit
566f3bf
·
1 Parent(s): f03e3f7

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -0
app.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import os
3
+ import streamlit as st
4
+
5
+ # Define the fields for the JSONL file
6
+ FIELDS = [
7
+ "CodeValue",
8
+ "CodeType",
9
+ "Context",
10
+ "Question",
11
+ "AnswerText",
12
+ "UpVoteCount",
13
+ "DownVoteCount",
14
+ "VoteComment",
15
+ ]
16
+
17
+ # Define the IO file pattern
18
+ IO_PATTERN = "*.jsonl"
19
+
20
+
21
+ def read_jsonl_file(file_path):
22
+ """Read a JSONL file and return a list of dictionaries."""
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
+ """Write a list of dictionaries to a JSONL file."""
31
+ with open(file_path, "w") as f:
32
+ for record in records:
33
+ f.write(json.dumps(record) + "\n")
34
+
35
+
36
+ def main():
37
+ # Get the list of JSONL files in the current directory
38
+ jsonl_files = [f for f in os.listdir() if f.endswith(".jsonl")]
39
+
40
+ # If there are no JSONL files, create one
41
+ if not jsonl_files:
42
+ st.warning("No JSONL files found. Creating new file.")
43
+ jsonl_files.append("data.jsonl")
44
+
45
+ # Display the list of JSONL files
46
+ selected_file = st.sidebar.selectbox("Select JSONL file", jsonl_files)
47
+ st.sidebar.write(f"Selected file: {selected_file}")
48
+
49
+ # Read the selected JSONL file
50
+ records = read_jsonl_file(selected_file)
51
+
52
+ # Autogenerate labels and inputs for the fields
53
+ for field in FIELDS:
54
+ value = st.text_input(field, key=field)
55
+ st.write(f"{field}: {value}")
56
+
57
+ # Add a record to the JSONL file
58
+ if st.button("Add Record"):
59
+ record = {field: st.session_state[field] for field in FIELDS}
60
+ records.append(record)
61
+ write_jsonl_file(selected_file, records)
62
+ st.success("Record added!")
63
+
64
+ # Display the current contents of the JSONL file
65
+ st.write(f"Current contents of {selected_file}:")
66
+ for record in records:
67
+ st.write(record)
68
+
69
+
70
+ if __name__ == "__main__":
71
+ main()