DJHumanRPT commited on
Commit
11ceb90
·
verified ·
1 Parent(s): 297b883

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +379 -330
app.py CHANGED
@@ -1117,9 +1117,7 @@ with st.sidebar:
1117
  st.title("Template Generator")
1118
 
1119
  # Create tabs for workflow
1120
- tab1, tab2, tab3, tab4 = st.tabs(
1121
- ["Setup", "Edit Template", "Use Template", "Generate Data"]
1122
- )
1123
 
1124
  with tab1:
1125
  st.header("Project Setup")
@@ -1269,13 +1267,23 @@ with tab2:
1269
  if st.session_state.show_template_editor and st.session_state.template_spec:
1270
  st.header("Template Editor")
1271
 
1272
- # Initialize suggested variables in session state if not present
1273
  if "suggested_variables" not in st.session_state:
1274
  st.session_state.suggested_variables = []
1275
-
1276
- # Initialize a tracking variable for added suggestions
1277
  if "added_suggestions" not in st.session_state:
1278
  st.session_state.added_suggestions = set()
 
 
 
 
 
 
 
 
 
 
 
 
1279
 
1280
  # Basic template information
1281
  with st.expander("Template Information", expanded=True):
@@ -1295,13 +1303,44 @@ with tab2:
1295
  height=100,
1296
  )
1297
 
1298
- # Knowledge Base Analysis
1299
- with st.expander("Knowledge Base Analysis", expanded=True):
1300
- if st.session_state.knowledge_base:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1301
  st.info("Analyze the knowledge base to suggest variables and values")
1302
 
1303
  if st.button(
1304
- "Analyze Knowledge Base for Variables", key="analyze_kb_button"
 
1305
  ):
1306
  client = get_openai_client()
1307
  if not client:
@@ -1315,6 +1354,7 @@ with tab2:
1315
  )
1316
  if suggested_vars:
1317
  st.session_state.suggested_variables = suggested_vars
 
1318
  st.success(
1319
  f"Found {len(suggested_vars)} potential variables in the knowledge base"
1320
  )
@@ -1324,10 +1364,12 @@ with tab2:
1324
  )
1325
 
1326
  # Display suggested variables if they exist
1327
- if st.session_state.suggested_variables:
 
 
 
1328
  st.subheader("Suggested Variables")
1329
 
1330
- # Create a container for the variables
1331
  for i, var in enumerate(st.session_state.suggested_variables):
1332
  # Generate a unique ID for this variable
1333
  var_id = f"{var['name']}_{i}"
@@ -1336,81 +1378,39 @@ with tab2:
1336
  if var_id in st.session_state.added_suggestions:
1337
  continue
1338
 
1339
- with st.container():
1340
- col1, col2 = st.columns([3, 1])
1341
- with col1:
1342
- st.markdown(
1343
- f"**{var['name']}** ({var['type']}): {var['description']}"
1344
- )
 
 
 
 
 
 
 
 
 
1345
  if var.get("options"):
1346
- st.markdown(f"Options: {', '.join(var['options'])}")
1347
- with col2:
1348
- # Use a unique key for each button
1349
- if st.button("Add", key=f"add_suggested_{var_id}"):
1350
- # Add this variable to the template
1351
- new_var = {
1352
- "name": var["name"],
1353
- "description": var["description"],
1354
- "type": var["type"],
1355
- }
1356
- if var.get("options"):
1357
- new_var["options"] = var["options"]
1358
- if var["type"] in ["string", "int", "float"]:
1359
- new_var["min"] = 1
1360
- new_var["max"] = 100
1361
-
1362
- # Add to input variables
1363
- st.session_state.template_spec["input"].append(
1364
- new_var
1365
- )
1366
-
1367
- # Mark this variable as added
1368
- st.session_state.added_suggestions.add(var_id)
1369
 
1370
- # Show success message
1371
- st.success(
1372
- f"Added {var['name']} to input variables!"
1373
- )
1374
- else:
1375
- st.warning(
1376
- "No knowledge base available. Please upload documents in the Setup tab first."
1377
- )
1378
 
1379
- # Prompt Template Section
1380
- with st.expander("Prompt Template", expanded=True):
1381
- st.info("Use {variable_name} to refer to input variables in your template")
1382
-
1383
- # Add buttons for prompt management
1384
- col1, col2 = st.columns([1, 1])
1385
- with col1:
1386
- rewrite_prompt = st.button("AI Rewrite Prompt")
1387
- with col2:
1388
- reroll_prompt = st.button("Reroll Prompt Variation")
1389
-
1390
- # Handle prompt rewriting
1391
- if rewrite_prompt or reroll_prompt:
1392
- with st.spinner("Generating improved prompt template..."):
1393
- improved_template = generate_improved_prompt_template(
1394
- st.session_state.template_spec, st.session_state.knowledge_base
1395
- )
1396
- # Only update if we got a valid result back
1397
- if improved_template and len(improved_template) > 10:
1398
- st.session_state.template_spec["prompt"] = improved_template
1399
- st.success("Prompt template updated!")
1400
 
1401
- # Display the prompt template
1402
- prompt_template = st.text_area(
1403
- "Edit the prompt template",
1404
- value=st.session_state.template_spec["prompt"],
1405
- height=200,
1406
- )
1407
- st.session_state.template_spec["prompt"] = prompt_template
1408
 
1409
- # Input Variables Editor
1410
  with st.expander("Input Variables", expanded=True):
1411
- st.subheader("Input Variables")
1412
-
1413
- # Add input variable button with smart functionality
1414
  col1, col2 = st.columns([3, 1])
1415
  with col1:
1416
  new_input_name = st.text_input(
@@ -1431,132 +1431,223 @@ with tab2:
1431
  }
1432
  st.session_state.template_spec["input"].append(new_var)
1433
 
1434
- # Display input variables
 
 
 
1435
  for i, input_var in enumerate(st.session_state.template_spec["input"]):
 
 
 
 
1436
  with st.container():
1437
- st.markdown(f"##### {input_var['name']}")
 
1438
 
1439
- col1, col2, col3 = st.columns([2, 2, 1])
 
1440
 
1441
  with col1:
1442
- input_var["name"] = st.text_input(
1443
- "Name", value=input_var["name"], key=f"input_name_{i}"
1444
- )
1445
-
1446
- input_var["description"] = st.text_input(
1447
- "Description",
1448
- value=input_var["description"],
1449
- key=f"input_desc_{i}",
1450
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1451
 
1452
  with col2:
1453
- var_type = st.selectbox(
1454
- "Type",
1455
- options=["string", "int", "float", "bool", "categorical"],
1456
- index=[
1457
- "string",
1458
- "int",
1459
- "float",
1460
- "bool",
1461
- "categorical",
1462
- ].index(input_var["type"]),
1463
- key=f"input_type_{i}",
1464
- )
1465
- input_var["type"] = var_type
1466
 
1467
- if var_type in ["string", "int", "float"]:
1468
- col_min, col_max = st.columns(2)
1469
- with col_min:
1470
- input_var["min"] = st.number_input(
1471
- "Min",
1472
- value=int(input_var.get("min", 0)),
1473
- key=f"input_min_{i}",
1474
- )
1475
- with col_max:
1476
- input_var["max"] = st.number_input(
1477
- "Max",
1478
- value=int(input_var.get("max", 100)),
1479
- key=f"input_max_{i}",
1480
- )
1481
 
1482
- if var_type == "categorical":
1483
- # Add a button to suggest options from knowledge base
1484
- kb_button_key = f"suggest_input_{i}_{input_var['name']}"
1485
- if st.button("Suggest Options from KB", key=kb_button_key):
1486
- client = get_openai_client()
1487
- if not client:
1488
- st.error(
1489
- "Please provide an OpenAI API key to suggest options."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1490
  )
1491
- elif not st.session_state.knowledge_base:
1492
- st.warning(
1493
- "No knowledge base available. Please upload documents first."
 
 
1494
  )
1495
- else:
1496
- with st.spinner(
1497
- f"Suggesting options for {input_var['name']}..."
1498
- ):
1499
- suggestions = suggest_variable_values_from_kb(
1500
- input_var["name"],
1501
- "categorical",
1502
- st.session_state.knowledge_base,
1503
- client,
 
1504
  )
1505
- if suggestions and "options" in suggestions:
1506
- # Update the options
1507
- input_var["options"] = suggestions[
1508
- "options"
1509
- ]
1510
- st.success(
1511
- f"Found {len(suggestions['options'])} options"
1512
- )
1513
- else:
1514
- st.warning(
1515
- "Could not find suitable options in the knowledge base"
 
 
 
 
1516
  )
1517
-
1518
- # Display and edit options
1519
- options = input_var.get("options", [])
1520
- options_str = st.text_area(
1521
- "Options (one per line)",
1522
- value="\n".join(options),
1523
- key=f"input_options_{i}",
1524
- )
1525
- input_var["options"] = [
1526
- opt.strip()
1527
- for opt in options_str.split("\n")
1528
- if opt.strip()
1529
- ]
1530
-
1531
- # Add min and max for categorical variables
1532
- col_min, col_max = st.columns(2)
1533
- with col_min:
1534
- input_var["min"] = st.number_input(
1535
- "Min selections",
1536
- value=int(input_var.get("min", 1)),
1537
- min_value=0,
1538
- key=f"input_cat_min_{i}",
1539
- )
1540
- with col_max:
1541
- input_var["max"] = st.number_input(
1542
- "Max selections",
1543
- value=int(input_var.get("max", 1)),
1544
- min_value=1,
1545
- key=f"input_cat_max_{i}",
1546
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1547
 
1548
- with col3:
1549
- if st.button("Remove", key=f"remove_input_{i}"):
1550
- st.session_state.template_spec["input"].pop(i)
1551
- st.rerun() # Only use rerun for removal
 
 
1552
 
1553
  st.divider()
1554
 
1555
- # Output Variables Editor
1556
  with st.expander("Output Variables", expanded=True):
1557
- st.subheader("Output Variables")
1558
-
1559
- # Add output variable button with smart functionality
1560
  col1, col2 = st.columns([3, 1])
1561
  with col1:
1562
  new_output_name = st.text_input(
@@ -1577,25 +1668,46 @@ with tab2:
1577
  }
1578
  st.session_state.template_spec["output"].append(new_var)
1579
 
1580
- # Display output variables
 
 
 
1581
  for i, output_var in enumerate(st.session_state.template_spec["output"]):
1582
- with st.container():
1583
- st.markdown(f"##### {output_var['name']}")
1584
 
1585
- col1, col2, col3 = st.columns([2, 2, 1])
 
 
 
1586
 
1587
- with col1:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1588
  output_var["name"] = st.text_input(
1589
  "Name", value=output_var["name"], key=f"output_name_{i}"
1590
  )
1591
-
1592
  output_var["description"] = st.text_input(
1593
  "Description",
1594
  value=output_var["description"],
1595
  key=f"output_desc_{i}",
1596
  )
1597
 
1598
- with col2:
1599
  var_type = st.selectbox(
1600
  "Type",
1601
  options=["string", "int", "float", "bool", "categorical"],
@@ -1610,15 +1722,16 @@ with tab2:
1610
  )
1611
  output_var["type"] = var_type
1612
 
 
1613
  if var_type in ["string", "int", "float"]:
1614
- col_min, col_max = st.columns(2)
1615
- with col_min:
1616
  output_var["min"] = st.number_input(
1617
  "Min",
1618
  value=int(output_var.get("min", 0)),
1619
  key=f"output_min_{i}",
1620
  )
1621
- with col_max:
1622
  output_var["max"] = st.number_input(
1623
  "Max",
1624
  value=int(output_var.get("max", 100)),
@@ -1626,9 +1739,10 @@ with tab2:
1626
  )
1627
 
1628
  if var_type == "categorical":
1629
- # Add a button to suggest options from knowledge base
1630
- kb_button_key = f"suggest_output_{i}_{output_var['name']}"
1631
- if st.button("Suggest Options from KB", key=kb_button_key):
 
1632
  client = get_openai_client()
1633
  if not client:
1634
  st.error(
@@ -1649,7 +1763,6 @@ with tab2:
1649
  client,
1650
  )
1651
  if suggestions and "options" in suggestions:
1652
- # Update the options
1653
  output_var["options"] = suggestions[
1654
  "options"
1655
  ]
@@ -1661,7 +1774,7 @@ with tab2:
1661
  "Could not find suitable options in the knowledge base"
1662
  )
1663
 
1664
- # Display and edit options
1665
  options = output_var.get("options", [])
1666
  options_str = st.text_area(
1667
  "Options (one per line)",
@@ -1674,16 +1787,16 @@ with tab2:
1674
  if opt.strip()
1675
  ]
1676
 
1677
- # Add min and max for categorical variables
1678
- col_min, col_max = st.columns(2)
1679
- with col_min:
1680
  output_var["min"] = st.number_input(
1681
  "Min selections",
1682
  value=int(output_var.get("min", 1)),
1683
  min_value=0,
1684
  key=f"output_cat_min_{i}",
1685
  )
1686
- with col_max:
1687
  output_var["max"] = st.number_input(
1688
  "Max selections",
1689
  value=int(output_var.get("max", 1)),
@@ -1691,127 +1804,46 @@ with tab2:
1691
  key=f"output_cat_max_{i}",
1692
  )
1693
 
1694
- with col3:
1695
- if st.button("Remove", key=f"remove_output_{i}"):
1696
- st.session_state.template_spec["output"].pop(i)
1697
- st.rerun() # Only use rerun for removal
1698
-
1699
- st.divider()
1700
-
1701
- # Template Specification and Download Section
1702
- with st.expander("Template JSON", expanded=False):
1703
- st.json(st.session_state.template_spec)
1704
-
1705
- # Download button
1706
- template_json = json.dumps(st.session_state.template_spec, indent=2)
1707
- st.download_button(
1708
- label="Download Template JSON",
1709
- data=template_json,
1710
- file_name="template_spec.json",
1711
- mime="application/json",
1712
- )
1713
- else:
1714
- st.info(
1715
- "No template has been generated yet. Go to the 'Setup' tab to create one."
1716
- )
1717
-
1718
- with tab3:
1719
- if st.session_state.show_template_editor and st.session_state.template_spec:
1720
- st.header("Use Template")
1721
-
1722
- # Reset user inputs when template changes
1723
- if (
1724
- "last_template" not in st.session_state
1725
- or st.session_state.last_template != st.session_state.template_spec
1726
- ):
1727
- st.session_state.user_inputs = {}
1728
- st.session_state.last_template = st.session_state.template_spec
1729
 
1730
- # Create input fields based on the template specification
1731
- for input_var in st.session_state.template_spec["input"]:
1732
- var_name = input_var["name"]
1733
- var_type = input_var["type"]
1734
- var_desc = input_var["description"]
1735
-
1736
- st.markdown(f"##### {var_desc}")
1737
-
1738
- if var_type == "string":
1739
- st.session_state.user_inputs[var_name] = st.text_input(
1740
- f"Enter value for {var_name}", key=f"use_{var_name}"
1741
- )
1742
-
1743
- elif var_type == "int":
1744
- st.session_state.user_inputs[var_name] = st.number_input(
1745
- f"Enter value for {var_name}",
1746
- min_value=input_var.get("min", None),
1747
- max_value=input_var.get("max", None),
1748
- step=1,
1749
- key=f"use_{var_name}",
1750
- )
1751
-
1752
- elif var_type == "float":
1753
- st.session_state.user_inputs[var_name] = st.number_input(
1754
- f"Enter value for {var_name}",
1755
- min_value=float(input_var.get("min", 0)),
1756
- max_value=float(input_var.get("max", 100)),
1757
- key=f"use_{var_name}",
1758
- )
1759
-
1760
- elif var_type == "bool":
1761
- st.session_state.user_inputs[var_name] = st.checkbox(
1762
- f"Select value for {var_name}", key=f"use_{var_name}"
1763
- )
1764
-
1765
- elif var_type == "categorical":
1766
- options = input_var.get("options", [])
1767
- min_selections = input_var.get("min", 1)
1768
- max_selections = input_var.get("max", 1)
1769
-
1770
- if options:
1771
- if min_selections == 1 and max_selections == 1:
1772
- # Single selection
1773
- st.session_state.user_inputs[var_name] = st.selectbox(
1774
- f"Select value for {var_name}",
1775
- options=options,
1776
- key=f"use_{var_name}",
1777
- )
1778
- else:
1779
- # Multi-selection
1780
- st.session_state.user_inputs[var_name] = st.multiselect(
1781
- f"Select {min_selections}-{max_selections} values for {var_name}",
1782
- options=options,
1783
- default=(
1784
- options[:min_selections]
1785
- if len(options) >= min_selections
1786
- else options
1787
- ),
1788
- key=f"use_{var_name}",
1789
- )
1790
- else:
1791
- st.warning(f"No options defined for {var_name}")
1792
 
1793
  # Handle the lore/knowledge base as a special variable
1794
  prompt_template = st.session_state.template_spec["prompt"]
1795
  if "{lore}" in prompt_template:
1796
- st.markdown("##### Document Knowledge Base")
1797
-
1798
- # Display info about the knowledge base
1799
- if st.session_state.knowledge_base:
1800
- st.success(
1801
- f"Using content from {len(st.session_state.uploaded_filenames) if 'uploaded_filenames' in st.session_state else 'uploaded'} documents as knowledge base"
1802
- )
1803
 
1804
- with st.expander("View knowledge base content"):
1805
- st.text_area(
1806
- "Knowledge base content",
1807
- value=st.session_state.knowledge_base[:2000]
1808
- + (
1809
- "..." if len(st.session_state.knowledge_base) > 2000 else ""
1810
- ),
1811
- height=200,
1812
- disabled=True,
1813
  )
1814
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1815
  # Add option to edit if needed
1816
  use_edited_lore = st.checkbox("Edit knowledge base content")
1817
  if use_edited_lore:
@@ -1824,13 +1856,31 @@ with tab3:
1824
  st.session_state.user_inputs["lore"] = (
1825
  st.session_state.knowledge_base
1826
  )
1827
- else:
1828
- st.warning("No documents uploaded. You can provide custom lore below.")
1829
- st.session_state.user_inputs["lore"] = st.text_area(
1830
- "Enter background information or context",
1831
- placeholder="Enter custom lore or background information here...",
1832
- height=150,
1833
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1834
 
1835
  # Generate Output button
1836
  if st.button("Generate Output", key="generate_button"):
@@ -1863,9 +1913,8 @@ with tab3:
1863
  st.session_state.generated_output = generated_output
1864
 
1865
  # Display generated output
1866
- if st.session_state.generated_output:
1867
  st.header("Generated Output")
1868
- st.markdown("### Result")
1869
 
1870
  # Check if the output is a dictionary (JSON)
1871
  if isinstance(st.session_state.generated_output, dict):
@@ -1896,7 +1945,7 @@ with tab3:
1896
  "No template has been generated yet. Go to the 'Setup' tab to create one."
1897
  )
1898
 
1899
- with tab4:
1900
  if st.session_state.show_template_editor and st.session_state.template_spec:
1901
  st.header("Generate Synthetic Data")
1902
 
 
1117
  st.title("Template Generator")
1118
 
1119
  # Create tabs for workflow
1120
+ tab1, tab2, tab3 = st.tabs(["Setup", "Edit and Use Template", "Generate Data"])
 
 
1121
 
1122
  with tab1:
1123
  st.header("Project Setup")
 
1267
  if st.session_state.show_template_editor and st.session_state.template_spec:
1268
  st.header("Template Editor")
1269
 
1270
+ # Initialize session state variables
1271
  if "suggested_variables" not in st.session_state:
1272
  st.session_state.suggested_variables = []
 
 
1273
  if "added_suggestions" not in st.session_state:
1274
  st.session_state.added_suggestions = set()
1275
+ if (
1276
+ "last_template" not in st.session_state
1277
+ or st.session_state.last_template != st.session_state.template_spec
1278
+ ):
1279
+ st.session_state.user_inputs = {}
1280
+ st.session_state.last_template = st.session_state.template_spec
1281
+ if "show_variable_editor" not in st.session_state:
1282
+ st.session_state.show_variable_editor = None
1283
+ if "show_output_editor" not in st.session_state:
1284
+ st.session_state.show_output_editor = None
1285
+ if "show_suggested_vars" not in st.session_state:
1286
+ st.session_state.show_suggested_vars = False
1287
 
1288
  # Basic template information
1289
  with st.expander("Template Information", expanded=True):
 
1303
  height=100,
1304
  )
1305
 
1306
+ # Prompt Template Section
1307
+ with st.expander("Prompt Template", expanded=True):
1308
+ st.info("Use {variable_name} to refer to input variables in your template")
1309
+
1310
+ # Add buttons for prompt management
1311
+ col1, col2 = st.columns([1, 1])
1312
+ with col1:
1313
+ rewrite_prompt = st.button("AI Rewrite Prompt")
1314
+ with col2:
1315
+ reroll_prompt = st.button("Reroll Prompt Variation")
1316
+
1317
+ # Handle prompt rewriting
1318
+ if rewrite_prompt or reroll_prompt:
1319
+ with st.spinner("Generating improved prompt template..."):
1320
+ improved_template = generate_improved_prompt_template(
1321
+ st.session_state.template_spec, st.session_state.knowledge_base
1322
+ )
1323
+ # Only update if we got a valid result back
1324
+ if improved_template and len(improved_template) > 10:
1325
+ st.session_state.template_spec["prompt"] = improved_template
1326
+ st.success("Prompt template updated!")
1327
+
1328
+ # Display the prompt template
1329
+ prompt_template = st.text_area(
1330
+ "Edit the prompt template",
1331
+ value=st.session_state.template_spec["prompt"],
1332
+ height=200,
1333
+ )
1334
+ st.session_state.template_spec["prompt"] = prompt_template
1335
+
1336
+ # Knowledge Base Analysis Section - Moved outside of Input Variables expander
1337
+ if st.session_state.knowledge_base:
1338
+ with st.expander("Knowledge Base Analysis", expanded=False):
1339
  st.info("Analyze the knowledge base to suggest variables and values")
1340
 
1341
  if st.button(
1342
+ "Analyze Knowledge Base for Variables",
1343
+ key="analyze_kb_button_input",
1344
  ):
1345
  client = get_openai_client()
1346
  if not client:
 
1354
  )
1355
  if suggested_vars:
1356
  st.session_state.suggested_variables = suggested_vars
1357
+ st.session_state.show_suggested_vars = True
1358
  st.success(
1359
  f"Found {len(suggested_vars)} potential variables in the knowledge base"
1360
  )
 
1364
  )
1365
 
1366
  # Display suggested variables if they exist
1367
+ if (
1368
+ st.session_state.suggested_variables
1369
+ and st.session_state.show_suggested_vars
1370
+ ):
1371
  st.subheader("Suggested Variables")
1372
 
 
1373
  for i, var in enumerate(st.session_state.suggested_variables):
1374
  # Generate a unique ID for this variable
1375
  var_id = f"{var['name']}_{i}"
 
1378
  if var_id in st.session_state.added_suggestions:
1379
  continue
1380
 
1381
+ col1, col2 = st.columns([4, 1])
1382
+ with col1:
1383
+ st.markdown(
1384
+ f"**{var['name']}** ({var['type']}): {var['description']}"
1385
+ )
1386
+ if var.get("options"):
1387
+ st.markdown(f"Options: {', '.join(var['options'])}")
1388
+ with col2:
1389
+ if st.button("Add", key=f"add_suggested_{var_id}"):
1390
+ # Add this variable to the template
1391
+ new_var = {
1392
+ "name": var["name"],
1393
+ "description": var["description"],
1394
+ "type": var["type"],
1395
+ }
1396
  if var.get("options"):
1397
+ new_var["options"] = var["options"]
1398
+ if var["type"] in ["string", "int", "float"]:
1399
+ new_var["min"] = 1
1400
+ new_var["max"] = 100
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1401
 
1402
+ # Add to input variables
1403
+ st.session_state.template_spec["input"].append(new_var)
 
 
 
 
 
 
1404
 
1405
+ # Mark this variable as added
1406
+ st.session_state.added_suggestions.add(var_id)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1407
 
1408
+ # Show success message
1409
+ st.success(f"Added {var['name']} to input variables!")
 
 
 
 
 
1410
 
1411
+ # Input Variables Section
1412
  with st.expander("Input Variables", expanded=True):
1413
+ # Add input variable button
 
 
1414
  col1, col2 = st.columns([3, 1])
1415
  with col1:
1416
  new_input_name = st.text_input(
 
1431
  }
1432
  st.session_state.template_spec["input"].append(new_var)
1433
 
1434
+ # Display input variables with integrated input fields
1435
+ st.subheader("Input Variables")
1436
+
1437
+ # Create a container for the variables
1438
  for i, input_var in enumerate(st.session_state.template_spec["input"]):
1439
+ var_name = input_var["name"]
1440
+ var_type = input_var["type"]
1441
+ var_desc = input_var["description"]
1442
+
1443
  with st.container():
1444
+ # Variable header with description
1445
+ st.markdown(f"##### {var_desc}")
1446
 
1447
+ # Create columns for the variable controls
1448
+ col1, col2, col3 = st.columns([3, 1, 1])
1449
 
1450
  with col1:
1451
+ # Create the appropriate input field based on variable type
1452
+ if var_type == "string":
1453
+ st.session_state.user_inputs[var_name] = st.text_input(
1454
+ f"Enter value for {var_name}", key=f"use_{var_name}"
1455
+ )
1456
+ elif var_type == "int":
1457
+ st.session_state.user_inputs[var_name] = st.number_input(
1458
+ f"Enter value for {var_name}",
1459
+ min_value=input_var.get("min", None),
1460
+ max_value=input_var.get("max", None),
1461
+ step=1,
1462
+ key=f"use_{var_name}",
1463
+ )
1464
+ elif var_type == "float":
1465
+ st.session_state.user_inputs[var_name] = st.number_input(
1466
+ f"Enter value for {var_name}",
1467
+ min_value=float(input_var.get("min", 0)),
1468
+ max_value=float(input_var.get("max", 100)),
1469
+ key=f"use_{var_name}",
1470
+ )
1471
+ elif var_type == "bool":
1472
+ st.session_state.user_inputs[var_name] = st.checkbox(
1473
+ f"Select value for {var_name}", key=f"use_{var_name}"
1474
+ )
1475
+ elif var_type == "categorical":
1476
+ options = input_var.get("options", [])
1477
+ min_selections = input_var.get("min", 1)
1478
+ max_selections = input_var.get("max", 1)
1479
+
1480
+ if options:
1481
+ if min_selections == 1 and max_selections == 1:
1482
+ # Single selection
1483
+ st.session_state.user_inputs[var_name] = (
1484
+ st.selectbox(
1485
+ f"Select value for {var_name}",
1486
+ options=options,
1487
+ key=f"use_{var_name}",
1488
+ )
1489
+ )
1490
+ else:
1491
+ # Multi-selection
1492
+ st.session_state.user_inputs[var_name] = (
1493
+ st.multiselect(
1494
+ f"Select {min_selections}-{max_selections} values for {var_name}",
1495
+ options=options,
1496
+ default=(
1497
+ options[:min_selections]
1498
+ if len(options) >= min_selections
1499
+ else options
1500
+ ),
1501
+ key=f"use_{var_name}",
1502
+ )
1503
+ )
1504
+ else:
1505
+ st.warning(f"No options defined for {var_name}")
1506
 
1507
  with col2:
1508
+ # Button to edit this variable
1509
+ if st.button("Edit Settings", key=f"edit_input_{i}"):
1510
+ st.session_state.show_variable_editor = i
 
 
 
 
 
 
 
 
 
 
1511
 
1512
+ with col3:
1513
+ # Button to remove this variable
1514
+ if st.button("Remove", key=f"remove_input_{i}"):
1515
+ st.session_state.template_spec["input"].pop(i)
1516
+ st.rerun()
 
 
 
 
 
 
 
 
 
1517
 
1518
+ # Show editor if this variable is selected
1519
+ if st.session_state.show_variable_editor == i:
1520
+ with st.container():
1521
+ st.markdown("---")
1522
+ st.markdown(f"##### Variable Settings: {input_var['name']}")
1523
+
1524
+ # Name and description
1525
+ input_var["name"] = st.text_input(
1526
+ "Name", value=input_var["name"], key=f"input_name_{i}"
1527
+ )
1528
+ input_var["description"] = st.text_input(
1529
+ "Description",
1530
+ value=input_var["description"],
1531
+ key=f"input_desc_{i}",
1532
+ )
1533
+
1534
+ # Type selection
1535
+ var_type = st.selectbox(
1536
+ "Type",
1537
+ options=[
1538
+ "string",
1539
+ "int",
1540
+ "float",
1541
+ "bool",
1542
+ "categorical",
1543
+ ],
1544
+ index=[
1545
+ "string",
1546
+ "int",
1547
+ "float",
1548
+ "bool",
1549
+ "categorical",
1550
+ ].index(input_var["type"]),
1551
+ key=f"input_type_{i}",
1552
+ )
1553
+ input_var["type"] = var_type
1554
+
1555
+ # Type-specific settings
1556
+ if var_type in ["string", "int", "float"]:
1557
+ col1, col2 = st.columns(2)
1558
+ with col1:
1559
+ input_var["min"] = st.number_input(
1560
+ "Min",
1561
+ value=int(input_var.get("min", 0)),
1562
+ key=f"input_min_{i}",
1563
  )
1564
+ with col2:
1565
+ input_var["max"] = st.number_input(
1566
+ "Max",
1567
+ value=int(input_var.get("max", 100)),
1568
+ key=f"input_max_{i}",
1569
  )
1570
+
1571
+ if var_type == "categorical":
1572
+ # Suggest options from KB button
1573
+ if st.button(
1574
+ "Suggest Options from KB", key=f"suggest_input_{i}"
1575
+ ):
1576
+ client = get_openai_client()
1577
+ if not client:
1578
+ st.error(
1579
+ "Please provide an OpenAI API key to suggest options."
1580
  )
1581
+ elif not st.session_state.knowledge_base:
1582
+ st.warning(
1583
+ "No knowledge base available. Please upload documents first."
1584
+ )
1585
+ else:
1586
+ with st.spinner(
1587
+ f"Suggesting options for {input_var['name']}..."
1588
+ ):
1589
+ suggestions = (
1590
+ suggest_variable_values_from_kb(
1591
+ input_var["name"],
1592
+ "categorical",
1593
+ st.session_state.knowledge_base,
1594
+ client,
1595
+ )
1596
  )
1597
+ if suggestions and "options" in suggestions:
1598
+ input_var["options"] = suggestions[
1599
+ "options"
1600
+ ]
1601
+ st.success(
1602
+ f"Found {len(suggestions['options'])} options"
1603
+ )
1604
+ else:
1605
+ st.warning(
1606
+ "Could not find suitable options in the knowledge base"
1607
+ )
1608
+
1609
+ # Options editor
1610
+ options = input_var.get("options", [])
1611
+ options_str = st.text_area(
1612
+ "Options (one per line)",
1613
+ value="\n".join(options),
1614
+ key=f"input_options_{i}",
 
 
 
 
 
 
 
 
 
 
 
1615
  )
1616
+ input_var["options"] = [
1617
+ opt.strip()
1618
+ for opt in options_str.split("\n")
1619
+ if opt.strip()
1620
+ ]
1621
+
1622
+ # Min/max selections
1623
+ col1, col2 = st.columns(2)
1624
+ with col1:
1625
+ input_var["min"] = st.number_input(
1626
+ "Min selections",
1627
+ value=int(input_var.get("min", 1)),
1628
+ min_value=0,
1629
+ key=f"input_cat_min_{i}",
1630
+ )
1631
+ with col2:
1632
+ input_var["max"] = st.number_input(
1633
+ "Max selections",
1634
+ value=int(input_var.get("max", 1)),
1635
+ min_value=1,
1636
+ key=f"input_cat_max_{i}",
1637
+ )
1638
 
1639
+ # Close editor button
1640
+ if st.button("Done Editing", key=f"done_input_{i}"):
1641
+ st.session_state.show_variable_editor = None
1642
+ st.rerun()
1643
+
1644
+ st.markdown("---")
1645
 
1646
  st.divider()
1647
 
1648
+ # Output Variables Section
1649
  with st.expander("Output Variables", expanded=True):
1650
+ # Add output variable button
 
 
1651
  col1, col2 = st.columns([3, 1])
1652
  with col1:
1653
  new_output_name = st.text_input(
 
1668
  }
1669
  st.session_state.template_spec["output"].append(new_var)
1670
 
1671
+ # Display output variables in a table-like format
1672
+ st.subheader("Output Variables")
1673
+
1674
+ # Create a container for the variables
1675
  for i, output_var in enumerate(st.session_state.template_spec["output"]):
1676
+ col1, col2, col3 = st.columns([3, 1, 1])
 
1677
 
1678
+ with col1:
1679
+ st.markdown(
1680
+ f"**{output_var['name']}** - {output_var['description']}"
1681
+ )
1682
 
1683
+ with col2:
1684
+ # Button to edit this variable
1685
+ if st.button("Edit", key=f"edit_output_{i}"):
1686
+ st.session_state.show_output_editor = i
1687
+
1688
+ with col3:
1689
+ # Button to remove this variable
1690
+ if st.button("Remove", key=f"remove_output_{i}"):
1691
+ st.session_state.template_spec["output"].pop(i)
1692
+ st.rerun()
1693
+
1694
+ # Show editor if this variable is selected
1695
+ if st.session_state.show_output_editor == i:
1696
+ with st.container():
1697
+ st.markdown("---")
1698
+ st.markdown(f"##### Edit Output Variable: {output_var['name']}")
1699
+
1700
+ # Name and description
1701
  output_var["name"] = st.text_input(
1702
  "Name", value=output_var["name"], key=f"output_name_{i}"
1703
  )
 
1704
  output_var["description"] = st.text_input(
1705
  "Description",
1706
  value=output_var["description"],
1707
  key=f"output_desc_{i}",
1708
  )
1709
 
1710
+ # Type selection
1711
  var_type = st.selectbox(
1712
  "Type",
1713
  options=["string", "int", "float", "bool", "categorical"],
 
1722
  )
1723
  output_var["type"] = var_type
1724
 
1725
+ # Type-specific settings
1726
  if var_type in ["string", "int", "float"]:
1727
+ col1, col2 = st.columns(2)
1728
+ with col1:
1729
  output_var["min"] = st.number_input(
1730
  "Min",
1731
  value=int(output_var.get("min", 0)),
1732
  key=f"output_min_{i}",
1733
  )
1734
+ with col2:
1735
  output_var["max"] = st.number_input(
1736
  "Max",
1737
  value=int(output_var.get("max", 100)),
 
1739
  )
1740
 
1741
  if var_type == "categorical":
1742
+ # Suggest options from KB button
1743
+ if st.button(
1744
+ "Suggest Options from KB", key=f"suggest_output_{i}"
1745
+ ):
1746
  client = get_openai_client()
1747
  if not client:
1748
  st.error(
 
1763
  client,
1764
  )
1765
  if suggestions and "options" in suggestions:
 
1766
  output_var["options"] = suggestions[
1767
  "options"
1768
  ]
 
1774
  "Could not find suitable options in the knowledge base"
1775
  )
1776
 
1777
+ # Options editor
1778
  options = output_var.get("options", [])
1779
  options_str = st.text_area(
1780
  "Options (one per line)",
 
1787
  if opt.strip()
1788
  ]
1789
 
1790
+ # Min/max selections
1791
+ col1, col2 = st.columns(2)
1792
+ with col1:
1793
  output_var["min"] = st.number_input(
1794
  "Min selections",
1795
  value=int(output_var.get("min", 1)),
1796
  min_value=0,
1797
  key=f"output_cat_min_{i}",
1798
  )
1799
+ with col2:
1800
  output_var["max"] = st.number_input(
1801
  "Max selections",
1802
  value=int(output_var.get("max", 1)),
 
1804
  key=f"output_cat_max_{i}",
1805
  )
1806
 
1807
+ # Close editor button
1808
+ if st.button("Done Editing", key=f"done_output_{i}"):
1809
+ st.session_state.show_output_editor = None
1810
+ st.rerun()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1811
 
1812
+ st.markdown("---")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1813
 
1814
  # Handle the lore/knowledge base as a special variable
1815
  prompt_template = st.session_state.template_spec["prompt"]
1816
  if "{lore}" in prompt_template:
1817
+ with st.expander("Document Knowledge Base", expanded=True):
1818
+ st.markdown("##### Document Knowledge Base")
 
 
 
 
 
1819
 
1820
+ # Display info about the knowledge base
1821
+ if st.session_state.knowledge_base:
1822
+ st.success(
1823
+ f"Using content from {len(st.session_state.uploaded_filenames) if 'uploaded_filenames' in st.session_state else 'uploaded'} documents as knowledge base"
 
 
 
 
 
1824
  )
1825
 
1826
+ # Use a button to toggle knowledge base content view instead of an expander
1827
+ if st.button(
1828
+ "View/Hide Knowledge Base Content", key="toggle_kb_view"
1829
+ ):
1830
+ st.session_state.show_kb_content = not st.session_state.get(
1831
+ "show_kb_content", False
1832
+ )
1833
+
1834
+ if st.session_state.get("show_kb_content", False):
1835
+ st.text_area(
1836
+ "Knowledge base content",
1837
+ value=st.session_state.knowledge_base[:2000]
1838
+ + (
1839
+ "..."
1840
+ if len(st.session_state.knowledge_base) > 2000
1841
+ else ""
1842
+ ),
1843
+ height=200,
1844
+ disabled=True,
1845
+ )
1846
+
1847
  # Add option to edit if needed
1848
  use_edited_lore = st.checkbox("Edit knowledge base content")
1849
  if use_edited_lore:
 
1856
  st.session_state.user_inputs["lore"] = (
1857
  st.session_state.knowledge_base
1858
  )
1859
+ else:
1860
+ st.warning(
1861
+ "No documents uploaded. You can provide custom lore below."
1862
+ )
1863
+ st.session_state.user_inputs["lore"] = st.text_area(
1864
+ "Enter background information or context",
1865
+ placeholder="Enter custom lore or background information here...",
1866
+ height=150,
1867
+ )
1868
+
1869
+ # Template JSON
1870
+ with st.expander("Template JSON", expanded=False):
1871
+ st.json(st.session_state.template_spec)
1872
+
1873
+ # Download button
1874
+ template_json = json.dumps(st.session_state.template_spec, indent=2)
1875
+ st.download_button(
1876
+ label="Download Template JSON",
1877
+ data=template_json,
1878
+ file_name="template_spec.json",
1879
+ mime="application/json",
1880
+ )
1881
+
1882
+ # Generate Output Section
1883
+ st.header("Generate Output")
1884
 
1885
  # Generate Output button
1886
  if st.button("Generate Output", key="generate_button"):
 
1913
  st.session_state.generated_output = generated_output
1914
 
1915
  # Display generated output
1916
+ if "generated_output" in st.session_state and st.session_state.generated_output:
1917
  st.header("Generated Output")
 
1918
 
1919
  # Check if the output is a dictionary (JSON)
1920
  if isinstance(st.session_state.generated_output, dict):
 
1945
  "No template has been generated yet. Go to the 'Setup' tab to create one."
1946
  )
1947
 
1948
+ with tab3:
1949
  if st.session_state.show_template_editor and st.session_state.template_spec:
1950
  st.header("Generate Synthetic Data")
1951