| import json
|
| import pandas as pd
|
| import streamlit as st
|
|
|
| st.set_page_config(
|
| page_title="Raw Text β JSON Editor",
|
| layout="wide"
|
| )
|
|
|
| st.title("π Raw Text to JSON Converter & Editor")
|
|
|
|
|
|
|
|
|
|
|
| if "df" not in st.session_state:
|
| st.session_state.df = pd.DataFrame(columns=["id", "text"])
|
|
|
|
|
|
|
|
|
|
|
| raw_text = st.text_area(
|
| "Paste Raw Text",
|
| height=250,
|
| placeholder="""Hello
|
| How are you?
|
| My name is Alex
|
| """
|
| )
|
|
|
|
|
|
|
|
|
|
|
| if st.button("π Convert to JSON"):
|
|
|
| lines = [i.strip() for i in raw_text.split("\n") if i.strip()]
|
|
|
| data = []
|
|
|
| for index, line in enumerate(lines):
|
|
|
| data.append(
|
| {
|
| "id": index + 1,
|
| "text": line
|
| }
|
| )
|
|
|
| st.session_state.df = pd.DataFrame(data)
|
|
|
|
|
|
|
|
|
|
|
| left, right = st.columns([2,1])
|
|
|
|
|
|
|
|
|
|
|
| with left:
|
|
|
| st.subheader("π Edit Data")
|
|
|
| st.session_state.df = st.data_editor(
|
| st.session_state.df,
|
| use_container_width=True,
|
| num_rows="dynamic"
|
| )
|
|
|
|
|
|
|
|
|
|
|
| with right:
|
|
|
| st.subheader("π¦ JSON Preview")
|
|
|
| json_data = st.session_state.df.to_dict(
|
| orient="records"
|
| )
|
|
|
| st.json(json_data)
|
|
|
|
|
|
|
|
|
|
|
| col1,col2,col3 = st.columns(3)
|
|
|
| with col1:
|
|
|
| if st.button("π Reformat JSON"):
|
|
|
| json_data = st.session_state.df.to_dict(
|
| orient="records"
|
| )
|
|
|
| st.success("JSON Reformatted Successfully!")
|
|
|
| with col2:
|
|
|
| json_string = json.dumps(
|
| st.session_state.df.to_dict(orient="records"),
|
| indent=4,
|
| ensure_ascii=False
|
| )
|
|
|
| st.download_button(
|
| "β¬ Download JSON",
|
| json_string,
|
| file_name="dataset.json",
|
| mime="application/json"
|
| )
|
|
|
| with col3:
|
|
|
| st.download_button(
|
| "β¬ Download JSONL",
|
| "\n".join(
|
| json.dumps(i, ensure_ascii=False)
|
| for i in st.session_state.df.to_dict(orient="records")
|
| ),
|
| file_name="dataset.jsonl",
|
| mime="text/plain"
|
| )
|
|
|
| st.divider()
|
|
|
| st.subheader("Formatted JSON")
|
|
|
| st.code(
|
| json.dumps(
|
| st.session_state.df.to_dict(orient="records"),
|
| indent=4,
|
| ensure_ascii=False
|
| ),
|
| language="json"
|
| ) |