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") # ---------------------------- # Session State # ---------------------------- if "df" not in st.session_state: st.session_state.df = pd.DataFrame(columns=["id", "text"]) # ---------------------------- # Raw Input # ---------------------------- raw_text = st.text_area( "Paste Raw Text", height=250, placeholder="""Hello How are you? My name is Alex """ ) # ---------------------------- # Convert # ---------------------------- 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) # ---------------------------- # Layout # ---------------------------- left, right = st.columns([2,1]) # ---------------------------- # Left Panel # ---------------------------- with left: st.subheader("📝 Edit Data") st.session_state.df = st.data_editor( st.session_state.df, use_container_width=True, num_rows="dynamic" ) # ---------------------------- # Right Panel # ---------------------------- with right: st.subheader("📦 JSON Preview") json_data = st.session_state.df.to_dict( orient="records" ) st.json(json_data) # ---------------------------- # Buttons # ---------------------------- 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" )