-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
741 lines (629 loc) Β· 35.1 KB
/
app.py
File metadata and controls
741 lines (629 loc) Β· 35.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
import streamlit as st
import pandas as pd
import io
from logic import (
get_excel_sheets,
excel_col_to_idx,
AppState,
ProjectManager
)
from logic.fsdm.service import FSDMService
from logic.mapping.config import MappingConfig
from logic.mapping.service import MappingService
from logic.utils import get_cell_value
from agent.agents.executor import AgentExecutor
from agent.agents.fsdm_metadata import generate_metadata
from agent.tools.tools import sample_table_data_logic
from ui import sidebar_config, display_logs, render_mapping_selection, render_fsdm_discovery_ui
# from agent.agents.test_fsdm import render_fsdm_test
st.set_page_config(page_title="Semantic Mapper AI", layout="wide")
# Initialize State
state = AppState()
# --- Project Selection ---
if not state.current_project:
st.title("Semantic Mapper AI π§ ")
st.markdown("### Select or Create a Project")
col1, col2 = st.columns(2)
with col1:
st.subheader("Create New Project")
new_proj_name = st.text_input("Project Name")
if st.button("Create Project", type="primary"):
if new_proj_name:
if ProjectManager.create_project(new_proj_name):
state.load_project(new_proj_name)
st.rerun()
else:
st.error("Project already exists.")
else:
st.error("Please enter a project name.")
with col2:
st.subheader("Open Existing Project")
projects = ProjectManager.list_projects()
if projects:
selected_proj = st.selectbox("Select Project", projects)
col_open, col_del = st.columns([1, 1])
if col_open.button("Open Project", type="primary", width='stretch'):
state.load_project(selected_proj)
st.rerun()
if col_del.button("Delete Project", type="secondary", width='stretch'):
ProjectManager.delete_project(selected_proj)
st.success(f"Deleted project: {selected_proj}")
st.rerun()
else:
st.info("No projects found.")
st.stop()
# --- Main App ---
# Project Sidebar
with st.sidebar:
st.markdown(f"### π Project: {state.current_project}")
if st.button("β©οΈ Switch Project", width='stretch'):
state.current_project = None
state.reset_kb()
st.rerun()
st.divider()
sidebar_config(state)
st.title("Semantic Mapper AI π§ ")
# Section 1: Knowledge Base Manager
st.header("1. Knowledge Base Manager")
# --- 1. Upload Section ---
uploaded_files = st.file_uploader("Upload PDFs or Excel Sheets", accept_multiple_files=True, type=["pdf", "xlsx"], key="uploader")
if uploaded_files:
inventory = state.kb_inventory
for f in uploaded_files:
if not any(item["name"] == f.name for item in inventory):
f.seek(0)
file_bytes = f.read()
# Save to disk
ProjectManager.save_file(state.current_project, f.name, file_bytes, sub_dir="files/vs")
if f.name.endswith(".pdf"):
inventory.append({
"name": f.name,
"type": "pdf",
"bytes": file_bytes,
"selected": True,
"indexed": False
})
elif f.name.endswith(".xlsx"):
sheets = get_excel_sheets(file_bytes)
inventory.append({
"name": f.name,
"type": "excel",
"bytes": file_bytes,
"sheets": {s: {"selected": True, "indexed": False} for s in sheets}
})
state.kb_inventory = inventory # Trigger update
state.save_project()
# --- 2. Dashboard Section ---
if state.kb_inventory:
st.subheader("Manage Documents")
needs_sync = False
inventory = state.kb_inventory
for idx, item in enumerate(inventory):
with st.container():
col_name, col_status, col_rm = st.columns([5, 2, 1])
if item["type"] == "pdf":
if item["indexed"] and item["selected"]:
col_status.success("β
Indexed")
elif not item["indexed"] and item["selected"]:
col_status.warning("β³ Pending")
needs_sync = True
elif item["indexed"] and not item["selected"]:
col_status.info("ποΈ To Remove")
needs_sync = True
new_sel = col_name.checkbox(f"π {item['name']}", value=item['selected'], key=f"sel_pdf_{idx}")
if new_sel != item["selected"]:
inventory[idx]["selected"] = new_sel
state.kb_inventory = inventory
state.save_project()
st.rerun()
else: # Excel
sheets_data = item["sheets"]
indexed_count = sum(1 for s in sheets_data.values() if s["indexed"])
selected_count = sum(1 for s in sheets_data.values() if s["selected"])
if indexed_count == selected_count and indexed_count > 0:
col_status.success(f"β
{indexed_count} Sheets")
elif indexed_count > 0:
col_status.warning(f"π {indexed_count}/{selected_count} Sync")
needs_sync = True
elif selected_count > 0:
col_status.info(f"β³ {selected_count} Pending")
needs_sync = True
if any(s["selected"] != s["indexed"] for s in sheets_data.values()):
needs_sync = True
col_name.markdown(f"π **{item['name']}**")
with col_name.expander("Show Sheets"):
for s_name, s_info in sheets_data.items():
s_col1, s_col2 = st.columns([3, 1])
checked = s_col1.checkbox(f"{s_name}", value=s_info["selected"], key=f"sel_{item['name']}_{s_name}")
if checked != s_info["selected"]:
inventory[idx]["sheets"][s_name]["selected"] = checked
state.kb_inventory = inventory
state.save_project()
st.rerun()
if s_info["indexed"]:
s_col2.markdown(":green[Indexed]")
if col_rm.button("ποΈ", key=f"del_file_{idx}"):
# Remove from vector store
state.v_service.remove_source(item["name"])
# Remove from disk
ProjectManager.delete_file(state.current_project, item["name"])
inventory.pop(idx)
state.kb_inventory = inventory
state.save_project()
st.rerun()
st.divider()
# --- 3. Action Buttons ---
col_btn1, col_btn2 = st.columns([1, 1])
if needs_sync:
if col_btn1.button("π Sync with Vector Store", type="primary", width='stretch'):
with st.spinner("Syncing changes..."):
state.v_service.sync_project(inventory)
state.kb_inventory = inventory
state.save_project()
st.success("Vector Store synced!")
st.rerun()
if col_btn2.button("π§Ή Clear All", width='stretch'):
state.reset_kb()
state.save_project()
st.rerun()
st.divider()
# Section 1.2: Knowledge Base DB Manager
st.header("1.2 Knowledge Base DB Manager")
# --- 1. Upload Section ---
fsdm_uploaded_files = st.file_uploader("Upload FSDM/ETL Excel Sheets", accept_multiple_files=True, type=["xlsx"], key="fsdm_uploader")
if fsdm_uploaded_files:
fsdm_inventory = state.fsdm_inventory
for f in fsdm_uploaded_files:
if not any(item["name"] == f.name for item in fsdm_inventory):
f.seek(0)
file_bytes = f.read()
# Save to disk
ProjectManager.save_file(state.current_project, f.name, file_bytes, sub_dir="files/fsdm")
sheets = get_excel_sheets(file_bytes)
fsdm_inventory.append({
"name": f.name,
"type": "excel",
"bytes": file_bytes,
"sheets": {s: {"selected": True, "indexed": False, "metadata": ""} for s in sheets}
})
state.fsdm_inventory = fsdm_inventory # Trigger update
state.save_project()
# --- 2. Dashboard Section ---
if state.fsdm_inventory:
st.subheader("Manage DB Documents")
needs_db_sync = False
fsdm_inventory = state.fsdm_inventory
for idx, item in enumerate(fsdm_inventory):
with st.container():
col_name, col_status, col_rm = st.columns([5, 2, 1])
sheets_data = item["sheets"]
indexed_count = sum(1 for s in sheets_data.values() if s["indexed"])
selected_count = sum(1 for s in sheets_data.values() if s["selected"])
if indexed_count == selected_count and indexed_count > 0:
col_status.success(f"β
{indexed_count} Tables")
elif indexed_count > 0:
col_status.warning(f"π {indexed_count}/{selected_count} Sync")
needs_db_sync = True
elif selected_count > 0:
col_status.info(f"β³ {selected_count} Pending")
needs_db_sync = True
if any(s["selected"] != s["indexed"] for s in sheets_data.values()):
needs_db_sync = True
col_name.markdown(f"π **{item['name']}**")
with col_name.expander("Show Sheets"):
for s_name, s_info in sheets_data.items():
s_col1, s_col2, s_col3 = st.columns([3, 1, 1])
checked = s_col1.checkbox(f"{s_name}", value=s_info["selected"], key=f"sel_fsdm_{item['name']}_{s_name}")
if checked != s_info["selected"]:
fsdm_inventory[idx]["sheets"][s_name]["selected"] = checked
state.fsdm_inventory = fsdm_inventory
state.save_project()
st.rerun()
if s_info["indexed"]:
s_col2.markdown(":green[In DB]")
s_col3.checkbox("Merge Headers", value=s_info.get("combine_headers", False), key=f"merge_locked_{item['name']}_{s_name}", disabled=True)
# Metadata Management
with st.expander("βοΈ Metadata"):
# Read directly from state
current_meta = state.fsdm_inventory[idx]["sheets"][s_name].get("metadata", "")
if st.button("β¨ Generate Metadata", key=f"gen_meta_{item['name']}_{s_name}"):
with st.spinner("Analyzing data..."):
table_name = ProjectManager.get_sanitized_table_name("FSDM/ETL_" + s_name)
sample_df = sample_table_data_logic(table_name, state.current_project)
print(sample_df)
new_meta = generate_metadata(
sample_df,
state.selected_model,
state.api_key,
state.base_url
)
state.fsdm_inventory[idx]["sheets"][s_name]["metadata"] = new_meta
state.save_project()
st.rerun()
new_val = st.text_area(
"Table Definitions/Instructions",
value=current_meta,
key=f"meta_{item['name']}_{s_name}_{hash(current_meta)}"
)
if new_val != current_meta:
state.fsdm_inventory[idx]["sheets"][s_name]["metadata"] = new_val
state.save_project()
st.rerun()
elif s_info["selected"]:
merge_check = s_col3.checkbox("Merge Headers", value=s_info.get("combine_headers", False), key=f"merge_{item['name']}_{s_name}")
if merge_check != s_info.get("combine_headers", False):
fsdm_inventory[idx]["sheets"][s_name]["combine_headers"] = merge_check
state.fsdm_inventory = fsdm_inventory
state.save_project()
st.rerun()
if col_rm.button("ποΈ", key=f"del_fsdm_file_{idx}"):
# --- NEW LOGIC START ---
# Drop associated tables from DB before deleting the file
FSDMService.delete_all_tables_for_item(state.current_project, item)
# --- NEW LOGIC END ---
# Remove from disk
ProjectManager.delete_file(state.current_project, item["name"])
fsdm_inventory.pop(idx)
state.fsdm_inventory = fsdm_inventory
state.save_project()
st.rerun()
st.divider()
# --- 3. Action Buttons ---
if needs_db_sync:
if st.button("ποΈ Create DB / Sync Tables", type="primary", width='stretch'):
with st.spinner("Syncing to SQLite..."):
for idx, item in enumerate(fsdm_inventory):
fsdm_inventory[idx] = FSDMService.sync(state.current_project, item)
state.fsdm_inventory = fsdm_inventory
state.save_project()
st.success("SQLite DB updated!")
st.rerun()
st.divider()
# Section 2: Mapping Configuration
st.header("2. Configure Mapping Documents")
# --- Instructions Management ---
st.subheader("βοΈ System Instructions")
col_g, col_f, col_m = st.columns([2, 1, 1])
# Fetch current instructions
current_global = ProjectManager.get_instructions(state.current_project, 'global')
current_fsdm = ProjectManager.get_instructions(state.current_project, 'fsdm')
current_mapping = ProjectManager.get_instructions(state.current_project, 'mapping')
with st.container():
global_instr = st.text_area("Global Instructions (Style, Tone, Standards)", value=current_global, height=100)
col_f1, col_m1 = st.columns(2)
with col_f1:
fsdm_instr = st.text_area("FSDM Discovery Instructions", value=current_fsdm, height=100)
with col_m1:
mapping_instr = st.text_area("Mapping Generation Instructions", value=current_mapping, height=100)
if st.button("πΎ Save All Instructions"):
ProjectManager.save_instructions(state.current_project, 'global', global_instr)
ProjectManager.save_instructions(state.current_project, 'fsdm', fsdm_instr)
ProjectManager.save_instructions(state.current_project, 'mapping', mapping_instr)
st.success("Instructions saved to database!")
# --- 1. Multi-File Uploader ---
mapping_files = st.file_uploader("Upload Mapping Excel Sheets", accept_multiple_files=True, type=["xlsx"], key="map_uploader")
if mapping_files:
inventory = state.mapping_inventory or []
# Inventory update logic
for f in mapping_files:
if not any(item["name"] == f.name for item in inventory):
f.seek(0)
file_bytes = f.read()
ProjectManager.save_file(state.current_project, f.name, file_bytes, sub_dir="files/mapping")
sheets = get_excel_sheets(file_bytes)
inventory.append({
"name": f.name,
"sheets": {s: {"selected": False, "config": MappingConfig().__dict__} for s in sheets}
})
state.mapping_inventory = inventory
state.save_project()
# --- 2. Mapping Dashboard ---
if state.mapping_inventory:
st.subheader("Manage Mapping Sheets")
for idx, item in enumerate(state.mapping_inventory):
with st.expander(f"π {item['name']}", expanded=False):
for s_name, s_info in item["sheets"].items():
s_col1, s_col2 = st.columns([3, 1])
checked = s_col1.checkbox(f"{s_name}", value=s_info["selected"], key=f"sel_map_{item['name']}_{s_name}")
# Sync status indicator
status = s_info.get("sync_status", "Pending")
s_col2.caption(f"Status: {status}")
if checked != s_info["selected"]:
state.mapping_inventory[idx]["sheets"][s_name]["selected"] = checked
state.save_project()
st.rerun()
if checked:
with st.expander(f"βοΈ Config for {s_name}"):
cfg = s_info["config"]
col1, col2 = st.columns(2)
with col1:
st.markdown("### Target Fields")
cfg["target_fields"]["subj"] = st.text_input("Target Subject Area", value=cfg["target_fields"]["subj"], key=f"t_subj_{item['name']}_{s_name}")
cfg["target_fields"]["db"] = st.text_input("Target DB Name", value=cfg["target_fields"]["db"], key=f"t_db_{item['name']}_{s_name}")
cfg["target_fields"]["tbl"] = st.text_input("Target Table Name", value=cfg["target_fields"]["tbl"], key=f"t_tbl_{item['name']}_{s_name}")
cfg["target_fields"]["col"] = st.text_input("Target Column Name", value=cfg["target_fields"]["col"], key=f"t_col_{item['name']}_{s_name}")
cfg["target_fields"]["type"] = st.text_input("Target Datatype", value=cfg["target_fields"]["type"], key=f"t_type_{item['name']}_{s_name}")
with col2:
st.markdown("### Source Fields")
cfg["source_fields"]["subj"] = st.text_input("Subject Area Column", value=cfg["source_fields"]["subj"], key=f"s_subj_{item['name']}_{s_name}")
cfg["source_fields"]["db"] = st.text_input("DB Name Column", value=cfg["source_fields"]["db"], key=f"s_db_{item['name']}_{s_name}")
cfg["source_fields"]["tbl"] = st.text_input("Table Name Column", value=cfg["source_fields"]["tbl"], key=f"s_tbl_{item['name']}_{s_name}")
cfg["source_fields"]["col"] = st.text_input("Column Name Column", value=cfg["source_fields"]["col"], key=f"s_col_{item['name']}_{s_name}")
cfg["source_fields"]["type"] = st.text_input("Datatype Column", value=cfg["source_fields"]["type"], key=f"s_type_{item['name']}_{s_name}")
st.subheader("Transformation Specs")
c_tr1, c_tr2, c_tr3, c_tr4 = st.columns(4)
cfg["trans_fields"]["type"] = c_tr1.text_input("Transf. Type Column", value=cfg["trans_fields"]["type"], key=f"tr_type_{item['name']}_{s_name}")
cfg["trans_fields"]["cond"] = c_tr2.text_input("Transf. Condition Column", value=cfg["trans_fields"]["cond"], key=f"tr_cond_{item['name']}_{s_name}")
cfg["trans_fields"]["remarks"] = c_tr3.text_input("Remarks Column", value=cfg["trans_fields"]["remarks"], key=f"tr_remarks_{item['name']}_{s_name}")
cfg["data_start_row"] = c_tr4.number_input("Data Row Start (1-based)", min_value=1, value=cfg.get("data_start_row", 1), key=f"dr_start_{item['name']}_{s_name}")
if st.button("Save & Preview", key=f"save_{item['name']}_{s_name}"):
state.mapping_inventory[idx]["sheets"][s_name]["config"] = cfg
state.mapping_inventory[idx]["sheets"][s_name]["sync_status"] = "Pending"
state.save_project()
# Perform individual sync
try:
MappingService.sync_sheet(state.current_project, item, s_name)
state.mapping_inventory[idx]["sheets"][s_name]["sync_status"] = "Synced"
state.save_project()
st.success(f"Synced {s_name} to DB!")
except Exception as e:
st.error(f"Sync failed: {e}")
st.rerun()
# Always try to fetch preview from DB if table exists (moved inside expander)
try:
tbl_name = ProjectManager.get_sanitized_table_name(f"mapping_{item['name']}_{s_name}")
preview_df = ProjectManager.load_df_from_sql(state.current_project, tbl_name)
if not preview_df.empty:
st.write("##### Table Preview (DB)")
st.dataframe(preview_df.head(5))
except:
pass
if st.button("π Sync Mappings to Master", type="primary", use_container_width=True):
with st.spinner("Syncing to Master Mapping Table..."):
MappingService.sync_mappings(state.current_project, state.mapping_inventory)
st.success("Master Mapping table updated!")
st.rerun()
else:
st.info("Please upload one or more Mapping Excel files to begin.")
# Add the new selection tree here
render_mapping_selection(state)
st.divider()
col_gen, col_stop = st.columns(2)
with col_gen:
btn_label = f"π Generate SQL Mappings ({len(state.selected_mapping_rows)} rows)" if len(state.selected_mapping_rows) > 0 else "π Generate SQL Mappings"
if st.button(btn_label, type="primary", use_container_width=True, disabled=len(state.selected_mapping_rows) == 0 or state.mapping_active):
state.mapping_active = True
state.mapping_idx = 0 # We'll iterate through rows_to_process
state.save_project()
st.rerun()
with col_stop:
if st.button("π Stop Mapping", type="secondary", use_container_width=True, disabled=not state.mapping_active):
state.mapping_active = False
state.save_project()
st.rerun()
st.divider()
# Section 3: Results
st.header("3. Transformation Results")
# Pull results from DB
available_tables = ProjectManager.get_unique_target_tables(state.current_project)
if available_tables:
# Use a selectbox to pick which table to view results for
selected_view_table = st.selectbox(
"Select Target Table to view results",
available_tables,
index=available_tables.index(state.selected_target_table) if state.selected_target_table in available_tables else 0
)
if selected_view_table != state.selected_target_table:
state.selected_target_table = selected_view_table
st.rerun()
db_results = ProjectManager.get_mappings_by_table(state.current_project, state.selected_target_table)
# Only show those with logic generated
completed_results = [r for r in db_results if r.get('transformation_logic')]
else:
completed_results = []
if not completed_results:
st.info("No SQL transformations generated yet for this table. Complete Step 2.5 above.")
else:
st.write(f"Showing {len(completed_results)} mappings for `{state.selected_target_table}`.")
for res in completed_results:
row_idx = res['row_idx']
with st.container(border=True):
# Header: Row + Type + SQL
col_l, col_r = st.columns([1, 4])
with col_l:
st.markdown(f"**Row #{row_idx}**")
st.caption(f"`{res['transformation_type']}`")
# Visual check for verified SQL
if res.get('validation_status') == 'SQL Verified':
st.success("Verified")
else:
st.info("Draft")
with col_r:
st.code(res['transformation_logic'], language="sql")
# Compact Metadata
s = res['source_info']
t = res['target_info']
st.caption(f"**Src:** `{s.get('db_name')}.{s.get('table_name')}.{s.get('column_name')}` | **Tgt:** `{t.get('db_name')}.{t.get('table_name')}.{t.get('column_name')}`")
# Details Expander
with st.expander("Details, Reasoning & Feedback", expanded=False):
# 1. FSDM Discovery Intelligence (Phase 1)
st.markdown("#### π§ Phase 1: FSDM Discovery Intelligence")
c1, c2 = st.columns(2)
with c1:
st.markdown(f"**Findings:**\n{res.get('fsdm_findings', 'N/A')}")
with c2:
st.markdown(f"**Recommended Sources:**\n`{res.get('fsdm_recommended_sources', 'N/A')}`")
st.markdown(f"**Discovery Reasoning:**\n{res.get('fsdm_reasoning', 'N/A')}")
st.markdown(f"**Discovery Report (Full):**\n{res.get('fsdm_intent', 'N/A')}")
st.divider()
# 2. SQL Engineering (Phase 2)
st.markdown("#### βοΈ Phase 2: SQL Engineering")
st.markdown(f"**Mapping Reasoning:**\n{res.get('reasoning', 'N/A')}")
# SQL Verification Toggle
current_v_status = res.get('validation_status', 'Mapping Complete')
sql_is_verified = st.toggle("Verify SQL (Golden Example)", value=(current_v_status == 'SQL Verified'), key=f"sql_v_{row_idx}")
if sql_is_verified and current_v_status != 'SQL Verified':
ProjectManager.update_mapping_validation(state.current_project, row_idx, {"validation_status": "SQL Verified"})
st.rerun()
elif not sql_is_verified and current_v_status == 'SQL Verified':
ProjectManager.update_mapping_validation(state.current_project, row_idx, {"validation_status": "Mapping Complete"})
st.rerun()
feedback = st.text_area("Feedback", value=st.session_state.get(f"feed_{row_idx}", ""), key=f"feed_{row_idx}", disabled=sql_is_verified)
def on_regen_fsdm(idx, row_data):
feed = st.session_state.get(f"feed_{idx}", "")
state.sync()
with st.spinner(f"Regenerating FSDM for row {idx}..."):
executor = AgentExecutor(state)
new_fsdm = executor.process_fsdm_only(row_data, idx, feedback=feed)
# Update DB with new FSDM intent
ProjectManager.update_mapping_row(state.current_project, idx, {
"fsdm_intent": new_fsdm.get("fsdm_intent", {}).get("lineage_intent", ""),
"fsdm_findings": new_fsdm.get("fsdm_intent", {}).get("findings", ""),
"fsdm_reasoning": new_fsdm.get("fsdm_intent", {}).get("reasoning", ""),
"fsdm_recommended_sources": new_fsdm.get("fsdm_intent", {}).get("recommended_sources", []),
"fsdm_status": new_fsdm.get("fsdm_status")
})
def on_regen_sql(idx, row_data):
feed = st.session_state.get(f"feed_{idx}", "")
state.sync()
with st.spinner(f"Regenerating SQL for row {idx}..."):
executor = AgentExecutor(state)
# We need to make sure row_data has the latest fsdm_intent from DB
latest_row = ProjectManager.get_mapping_by_row(state.current_project, idx)
row_data['fsdm_intent'] = {
"lineage_intent": latest_row.get('fsdm_intent'),
"findings": latest_row.get('fsdm_findings'),
"reasoning": latest_row.get('fsdm_reasoning'),
"recommended_sources": latest_row.get('fsdm_recommended_sources') or []
}
new_res = executor.process_mapping_only(row_data, idx, feedback=feed)
ProjectManager.update_mapping_row(state.current_project, idx, new_res)
c_btn1, c_btn2 = st.columns(2)
if c_btn1.button("π Regenerate FSDM", key=f"btn_fsdm_{row_idx}", on_click=on_regen_fsdm, args=(row_idx, res), disabled=sql_is_verified, use_container_width=True):
st.rerun()
if c_btn2.button("βοΈ Regenerate SQL", key=f"btn_sql_{row_idx}", on_click=on_regen_sql, args=(row_idx, res), disabled=sql_is_verified, use_container_width=True):
st.rerun()
# Export all tables from DB
if st.button("π¦ Export All Processed Tables to Excel", width='stretch'):
all_db_data = []
unique_tables = ProjectManager.get_unique_target_tables(state.current_project)
for tbl in unique_tables:
tbl_mappings = ProjectManager.get_mappings_by_table(state.current_project, tbl)
for m in tbl_mappings:
if m.get('transformation_logic'):
s = m['source_info']
t = m['target_info']
all_db_data.append({
"Target Table": m['target_table'],
"Row": m['row_idx'],
"Source Subject Area": s.get('subject_area'),
"Source DB Name": s.get('db_name'),
"Source Table Name": s.get('table_name'),
"Source Column Name": s.get('column_name'),
"Source Datatype": s.get('datatype'),
"Target Subject Area": t.get('subject_area'),
"Target DB Name": t.get('db_name'),
"Target Table Name": t.get('table_name'),
"Target Column Name": t.get('column_name'),
"Target Datatype": t.get('datatype'),
"Transformation Type": m['transformation_type'],
"Transformation Logic": m['transformation_logic'],
"SQL Reasoning": m['reasoning'],
"FSDM Findings": m.get('fsdm_findings'),
"FSDM Reasoning": m.get('fsdm_reasoning'),
"FSDM Recommended Sources": m.get('fsdm_recommended_sources'),
"FSDM Full Intent": m.get('fsdm_intent')
})
if all_db_data:
final_df = pd.DataFrame(all_db_data)
buffer = io.BytesIO()
with pd.ExcelWriter(buffer, engine='openpyxl') as writer:
final_df.to_excel(writer, index=False, sheet_name='Semantic Mappings')
st.download_button(
label="Download Final Mappings (Excel) π₯",
data=buffer.getvalue(),
file_name="semantic_mapping_results.xlsx",
mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
width='stretch',
type="primary"
)
else:
st.warning("No completed mappings found to export.")
st.divider()
# Section 4: Logs
st.header("4. Application Logs π" )
display_logs(state, height=400, key_prefix="main_logs")
# # --- Mapping Execution Loop (State Machine) ---
if state.mapping_active:
# Stop Button
if st.button("π Stop Mapping", type="secondary", use_container_width=True, key="stop_mapping_bottom"):
state.mapping_active = False
state.save_project()
st.session_state["mapping_idx"] = 0
st.rerun()
# Track progress index
if "mapping_idx" not in st.session_state:
st.session_state["mapping_idx"] = 0
selected_ids = state.selected_mapping_rows
total_rows = len(selected_ids)
if total_rows == 0:
st.warning("No rows selected.")
state.mapping_active = False
st.rerun()
if st.session_state["mapping_idx"] < total_rows:
unique_id = selected_ids[st.session_state["mapping_idx"]]
# Display progress
st.progress((st.session_state["mapping_idx"]) / total_rows)
st.info(f"Processing ({st.session_state['mapping_idx'] + 1}/{total_rows}): {unique_id}")
# Fetch actual row data from unified_mapping_view
unified_df = ProjectManager.load_df_from_sql(state.current_project, "unified_mapping_view")
if not unified_df.empty:
try:
parts = unique_id.split("|")
if len(parts) == 3:
f_name, s_name, r_idx_str = parts
r_idx = int(r_idx_str)
row_data_raw = unified_df.loc[r_idx]
target_table = row_data_raw.get('target_table', 'unknown_table')
target_col = row_data_raw.get('target_column', 'unknown_col')
source_table = row_data_raw.get('source_table', 'unknown_table')
source_col = row_data_raw.get('source_column', 'unknown_col')
row_data = {
"source_info": {
"subject_area": row_data_raw.get("source_subject"),
"db_name": row_data_raw.get("source_db"),
"table_name": source_table,
"column_name": source_col,
"datatype": row_data_raw.get("source_type")
},
"target_info": {
"subject_area": row_data_raw.get("target_subject"),
"db_name": row_data_raw.get("target_db"),
"table_name": target_table,
"column_name": target_col,
"datatype": row_data_raw.get("target_type")
},
"transformation_specs": {
"type": row_data_raw.get("trans_type"),
"condition": row_data_raw.get("trans_condition"),
"remarks": row_data_raw.get("remarks")
},
"target_table": target_table
}
executor = AgentExecutor(state)
res = executor.process_row(row_data, r_idx)
ProjectManager.save_mapping_row(state.current_project, res)
st.write(f"β
Saved result for {unique_id}")
else:
st.error(f"Invalid unique_id format: {unique_id}")
except Exception as e:
st.error(f"Error processing row {unique_id}: {e}")
st.session_state["mapping_idx"] += 1
st.rerun()
else:
# Completion
st.success("Mapping complete!")
state.mapping_active = False
st.session_state["mapping_idx"] = 0
state.save_project()
st.rerun()