|
import streamlit as st |
|
import json |
|
import pandas as pd |
|
import os |
|
|
|
st.set_page_config(page_title="Dataset Builder", layout="wide") |
|
st.title("π JSONL Dataset Editor") |
|
|
|
TMP_DIR = "temp" |
|
TMP_FILE = os.path.join(TMP_DIR, "session_dataset.jsonl") |
|
|
|
|
|
os.makedirs(TMP_DIR, exist_ok=True) |
|
|
|
|
|
|
|
def get_all_fields(data): |
|
all_keys = set() |
|
for record in data: |
|
all_keys.update(record.keys()) |
|
return sorted(all_keys) |
|
|
|
|
|
|
|
if "data" not in st.session_state: |
|
if os.path.exists(TMP_FILE): |
|
with open(TMP_FILE, "r", encoding="utf-8") as f: |
|
st.session_state.data = [json.loads(line) for line in f] |
|
st.session_state.all_fields = get_all_fields(st.session_state.data) |
|
else: |
|
st.session_state.data = [] |
|
st.session_state.all_fields = [] |
|
|
|
|
|
uploaded_file = st.file_uploader("Upload a JSONL file", type=["jsonl"]) |
|
|
|
if uploaded_file: |
|
content = uploaded_file.read().decode("utf-8") |
|
st.session_state.data = [json.loads(line) for line in content.strip().splitlines()] |
|
st.session_state.all_fields = get_all_fields(st.session_state.data) |
|
|
|
|
|
with open(TMP_FILE, "w", encoding="utf-8") as f: |
|
for item in st.session_state.data: |
|
f.write(json.dumps(item, ensure_ascii=False) + "\n") |
|
|
|
st.success( |
|
f"Loaded {len(st.session_state.data)} records with fields: {st.session_state.all_fields}" |
|
) |
|
|
|
|
|
if not st.session_state.data and not st.session_state.all_fields: |
|
st.session_state.all_fields = ["context", "question", "answer"] |
|
|
|
|
|
st.markdown("### βοΈ Edit Records") |
|
|
|
df = pd.DataFrame(st.session_state.data) |
|
df = df.reindex(columns=st.session_state.all_fields) |
|
|
|
|
|
for field in st.session_state.all_fields: |
|
if field.lower() in ["context", "answer", "question"]: |
|
df[field] = df[field].astype(str) |
|
|
|
|
|
column_configs = { |
|
field: ( |
|
st.column_config.TextColumn(label=field, width="large") |
|
if field.lower() in ["context", "answer", "question"] |
|
else None |
|
) |
|
for field in st.session_state.all_fields |
|
} |
|
|
|
|
|
edited_df = st.data_editor( |
|
df, |
|
use_container_width=True, |
|
num_rows="dynamic", |
|
column_config=column_configs, |
|
) |
|
|
|
|
|
if not edited_df.equals(df): |
|
st.session_state.data = edited_df.fillna("").to_dict(orient="records") |
|
|
|
|
|
with open(TMP_FILE, "w", encoding="utf-8") as f: |
|
for item in st.session_state.data: |
|
f.write(json.dumps(item, ensure_ascii=False) + "\n") |
|
|
|
st.toast("β
Auto-saved!", icon="πΎ") |
|
st.rerun() |
|
|
|
|
|
|
|
st.markdown("### β Add New Entry") |
|
|
|
|
|
with st.form("new_entry_form"): |
|
new_record = {} |
|
for field in st.session_state.all_fields: |
|
new_record[field] = st.text_area(f"{field}", key=f"input_{field}") |
|
|
|
submitted = st.form_submit_button("Add Entry") |
|
if submitted: |
|
st.session_state.data.append(new_record) |
|
|
|
|
|
with open(TMP_FILE, "w", encoding="utf-8") as f: |
|
for item in st.session_state.data: |
|
f.write(json.dumps(item, ensure_ascii=False) + "\n") |
|
|
|
st.success("β
New entry added!") |
|
st.rerun() |
|
|
|
|
|
with st.expander("β Add New Field"): |
|
new_field = st.text_input("New field name", key="new_field_name") |
|
if st.button("Add Field"): |
|
if new_field and new_field not in st.session_state.all_fields: |
|
st.session_state.all_fields.append(new_field) |
|
st.success(f"β
Field '{new_field}' added!") |
|
st.rerun() |
|
|
|
|
|
|
|
st.markdown("### π€ Export Dataset") |
|
|
|
|
|
export_path = st.text_input( |
|
"Custom save path (e.g., ./exports/my_dataset.jsonl)", |
|
value="./exports/exported_dataset.jsonl", |
|
) |
|
|
|
col1, col2 = st.columns(2) |
|
|
|
|
|
with col1: |
|
if st.button("π Export JSONL"): |
|
if not os.path.exists(os.path.dirname(export_path)): |
|
os.makedirs(os.path.dirname(export_path)) |
|
|
|
|
|
with open(export_path, "w", encoding="utf-8") as f_out: |
|
for row in st.session_state.data: |
|
f_out.write(json.dumps(row, ensure_ascii=False) + "\n") |
|
|
|
st.success(f"β
Dataset saved to {export_path}") |
|
|
|
|
|
with open(export_path, "r", encoding="utf-8") as f_download: |
|
exported_content = f_download.read() |
|
|
|
st.download_button( |
|
"β¬οΈ Download JSONL", |
|
exported_content, |
|
file_name=os.path.basename(export_path), |
|
mime="application/json", |
|
) |
|
|
|
|
|
if os.path.exists(TMP_FILE): |
|
os.remove(TMP_FILE) |
|
st.session_state.clear() |
|
st.success("π§Ή Temporary session cleared. You're starting fresh!") |
|
st.rerun() |
|
|
|
|
|
with col2: |
|
if os.path.exists(TMP_FILE): |
|
with open(TMP_FILE, "r", encoding="utf-8") as f_tmp: |
|
tmp_content = f_tmp.read() |
|
|
|
st.download_button( |
|
"β¬οΈ Download Temp File", |
|
tmp_content, |
|
file_name="session_dataset.jsonl", |
|
mime="application/json", |
|
) |
|
else: |
|
st.warning("β οΈ No temp file found to download.") |
|
|