rp / app.py
vishmay007's picture
Upload folder using huggingface_hub
e6ff9a8 verified
Raw
History Blame Contribute Delete
2.79 kB
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"
)