Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import os
|
| 3 |
+
import base64
|
| 4 |
+
import streamlit as st
|
| 5 |
+
|
| 6 |
+
# Define the fields for the JSONL file
|
| 7 |
+
FIELDS = [
|
| 8 |
+
"CodeValue",
|
| 9 |
+
"CodeType",
|
| 10 |
+
"Context",
|
| 11 |
+
"Question",
|
| 12 |
+
"AnswerText",
|
| 13 |
+
"UpVoteCount",
|
| 14 |
+
"DownVoteCount",
|
| 15 |
+
"VoteComment",
|
| 16 |
+
]
|
| 17 |
+
|
| 18 |
+
# Define the IO file pattern
|
| 19 |
+
IO_PATTERN = "*.jsonl"
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def read_jsonl_file(file_path):
|
| 23 |
+
"""Read a JSONL file and return a list of dictionaries."""
|
| 24 |
+
if not os.path.exists(file_path):
|
| 25 |
+
return []
|
| 26 |
+
with open(file_path, "r") as f:
|
| 27 |
+
lines = f.readlines()
|
| 28 |
+
records = [json.loads(line) for line in lines]
|
| 29 |
+
return records
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def write_jsonl_file(file_path, records):
|
| 33 |
+
"""Write a list of dictionaries to a JSONL file."""
|
| 34 |
+
with open(file_path, "w") as f:
|
| 35 |
+
for record in records:
|
| 36 |
+
f.write(json.dumps(record) + "\n")
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def list_files():
|
| 40 |
+
"""List all JSONL files in the current directory."""
|
| 41 |
+
return [f for f in os.listdir() if f.endswith(".jsonl")]
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def download_link(file_path):
|
| 45 |
+
"""Generate a base64 download link for a file."""
|
| 46 |
+
with open(file_path, "rb") as f:
|
| 47 |
+
contents = f.read()
|
| 48 |
+
b64 = base64.b64encode(contents).decode()
|
| 49 |
+
href = f'<a href="data:application/octet-stream;base64,{b64}" download="{file_path}">Download</a>'
|
| 50 |
+
return href
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
def main():
|
| 54 |
+
# Get the list of JSONL files in the current directory
|
| 55 |
+
jsonl_files = list_files()
|
| 56 |
+
|
| 57 |
+
# If there are no JSONL files, create one
|
| 58 |
+
if not jsonl_files:
|
| 59 |
+
st.warning("No JSONL files found. Creating new file.")
|
| 60 |
+
jsonl_files.append("data.jsonl")
|
| 61 |
+
|
| 62 |
+
# Add the ability to name or rename a file
|
| 63 |
+
selected_file = st.sidebar.text_input("Enter file name", value=jsonl_files[0])
|
| 64 |
+
if selected_file != jsonl_files[0]:
|
| 65 |
+
os.rename(jsonl_files[0], selected_file)
|
| 66 |
+
jsonl_files[0] = selected_file
|
| 67 |
+
|
| 68 |
+
# Display the list of JSONL files
|
| 69 |
+
st.sidebar.write("JSONL files:")
|
| 70 |
+
selected_file_index = st.sidebar.selectbox("", range(len(jsonl_files)))
|
| 71 |
+
for i, file_name in enumerate(jsonl_files):
|
| 72 |
+
if i == selected_file_index:
|
| 73 |
+
selected_file = file_name
|
| 74 |
+
st.sidebar.write(f"> {file_name}")
|
| 75 |
+
else:
|
| 76 |
+
st.sidebar.write(file_name)
|
| 77 |
+
|
| 78 |
+
# Display a download link for the selected JSONL file
|
| 79 |
+
st.sidebar.markdown(download_link(selected_file), unsafe_allow_html=True)
|
| 80 |
+
|
| 81 |
+
# Read the selected JSONL file
|
| 82 |
+
records = read_jsonl_file(selected_file)
|
| 83 |
+
|
| 84 |
+
# Autogenerate labels and inputs for the fields
|
| 85 |
+
for field in FIELDS:
|
| 86 |
+
value = st.text_input(field, key=field)
|
| 87 |
+
st.write(f"{field}: {value}")
|
| 88 |
+
|
| 89 |
+
# Add a record to the JSONL file
|
| 90 |
+
if st.button("Add Record"):
|
| 91 |
+
record = {field: st.session_state[field] for field in FIELDS}
|
| 92 |
+
records.append(record)
|
| 93 |
+
write_jsonl_file(selected_file, records)
|
| 94 |
+
st.success("Record added!")
|
| 95 |
+
|
| 96 |
+
# Display the current contents of the JSONL file
|
| 97 |
+
st.write(f"Current contents of {selected_file}:")
|
| 98 |
+
for record in records:
|
| 99 |
+
st.write(record
|