Update app.py
Browse files
app.py
CHANGED
|
@@ -23,14 +23,14 @@ if "ingested_batches" not in st.session_state:
|
|
| 23 |
if "messages" not in st.session_state:
|
| 24 |
st.session_state.messages = []
|
| 25 |
|
| 26 |
-
st.set_page_config(page_title="Chat with Your JSON Vectors (
|
| 27 |
-
st.title("Chat with Your Vectorized JSON Files (Hybrid Retrieval, SQLite, LLM
|
| 28 |
|
| 29 |
uploaded_files = st.file_uploader(
|
| 30 |
"Upload JSON files in batches (any structure)", type="json", accept_multiple_files=True
|
| 31 |
)
|
| 32 |
|
| 33 |
-
# --- Enhanced flattening
|
| 34 |
def flatten_json_obj(obj, parent_key="", sep="."):
|
| 35 |
items = {}
|
| 36 |
if isinstance(obj, dict):
|
|
@@ -63,13 +63,8 @@ if uploaded_files:
|
|
| 63 |
file.seek(0)
|
| 64 |
try:
|
| 65 |
raw = json.load(file)
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
elif isinstance(raw, dict):
|
| 69 |
-
main_lists = [v for v in raw.values() if isinstance(v, list)]
|
| 70 |
-
records = main_lists[0] if main_lists else [raw]
|
| 71 |
-
else:
|
| 72 |
-
records = [raw]
|
| 73 |
for idx, rec in enumerate(records):
|
| 74 |
st.code(flatten_json_obj(rec))
|
| 75 |
except Exception as e:
|
|
@@ -104,13 +99,8 @@ def ingest_json_files(files):
|
|
| 104 |
file.seek(0)
|
| 105 |
raw = json.load(file)
|
| 106 |
source_name = file.name
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
elif isinstance(raw, dict):
|
| 110 |
-
main_lists = [v for v in raw.values() if isinstance(v, list)]
|
| 111 |
-
records = main_lists[0] if main_lists else [raw]
|
| 112 |
-
else:
|
| 113 |
-
records = [raw]
|
| 114 |
for rec in records:
|
| 115 |
flat = flatten_json_obj(rec)
|
| 116 |
flat_text = "; ".join([f"{k}: {v}" for k, v in flat.items()])
|
|
|
|
| 23 |
if "messages" not in st.session_state:
|
| 24 |
st.session_state.messages = []
|
| 25 |
|
| 26 |
+
st.set_page_config(page_title="Chat with Your JSON Vectors (Hybrid, Enhanced)", layout="wide")
|
| 27 |
+
st.title("Chat with Your Vectorized JSON Files (Hybrid Retrieval, SQLite, LLM)")
|
| 28 |
|
| 29 |
uploaded_files = st.file_uploader(
|
| 30 |
"Upload JSON files in batches (any structure)", type="json", accept_multiple_files=True
|
| 31 |
)
|
| 32 |
|
| 33 |
+
# --- Enhanced flattening (never loses parent fields)
|
| 34 |
def flatten_json_obj(obj, parent_key="", sep="."):
|
| 35 |
items = {}
|
| 36 |
if isinstance(obj, dict):
|
|
|
|
| 63 |
file.seek(0)
|
| 64 |
try:
|
| 65 |
raw = json.load(file)
|
| 66 |
+
# NEW: Don't try to pull lists out of dicts; treat the whole dict as a record
|
| 67 |
+
records = raw if isinstance(raw, list) else [raw]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
for idx, rec in enumerate(records):
|
| 69 |
st.code(flatten_json_obj(rec))
|
| 70 |
except Exception as e:
|
|
|
|
| 99 |
file.seek(0)
|
| 100 |
raw = json.load(file)
|
| 101 |
source_name = file.name
|
| 102 |
+
# NEW: Always treat the whole dict as a record, even if it contains lists
|
| 103 |
+
records = raw if isinstance(raw, list) else [raw]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
for rec in records:
|
| 105 |
flat = flatten_json_obj(rec)
|
| 106 |
flat_text = "; ".join([f"{k}: {v}" for k, v in flat.items()])
|