Update src/streamlit_app.py
Browse files- src/streamlit_app.py +93 -73
src/streamlit_app.py
CHANGED
|
@@ -3,8 +3,8 @@ import os
|
|
| 3 |
import json
|
| 4 |
import streamlit as st
|
| 5 |
from app_assets import app_logo
|
| 6 |
-
|
| 7 |
-
|
| 8 |
from workflow_nodes import (
|
| 9 |
think_sections_node,
|
| 10 |
enhance_plan_node,
|
|
@@ -14,37 +14,40 @@ from workflow_nodes import (
|
|
| 14 |
correct_notebook_node,
|
| 15 |
write_insights_node
|
| 16 |
)
|
| 17 |
-
|
| 18 |
from workflow_nodes import InteractiveCaseStudyState
|
| 19 |
|
| 20 |
-
|
| 21 |
st.set_page_config(layout="wide")
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
st.title("π NBforge Demo")
|
| 24 |
|
| 25 |
-
st.logo(app_logo,size='large',link='https://www.mygreatlearning.com/')
|
|
|
|
| 26 |
|
| 27 |
# βββ Session State Initialization βββββββββββββββββββββββββββββββββββββββββββββ
|
| 28 |
if "state" not in st.session_state:
|
| 29 |
st.session_state.state = InteractiveCaseStudyState()
|
| 30 |
-
st.session_state.plan_ready
|
| 31 |
-
st.session_state.plan_approved
|
| 32 |
-
st.session_state.notebook_ready
|
| 33 |
-
st.session_state.notebook_approved
|
| 34 |
-
st.session_state.exec_ready
|
| 35 |
-
st.session_state.execution_approved
|
| 36 |
|
| 37 |
s = st.session_state.state
|
| 38 |
|
| 39 |
# βββ 1) Collect Inputs ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 40 |
st.header("1. π§ Provide Your Inputs")
|
| 41 |
-
s["domain"]
|
| 42 |
-
"EDA","Machine Learning","Deep Learning","Natural Language Processing",
|
| 43 |
-
"Computer Vision","Generative AI Prompt Engineering","Generative AI RAG"
|
| 44 |
])
|
| 45 |
-
s["topics_and_subtopics"]
|
| 46 |
-
s["problem_statement"]
|
| 47 |
-
s["dataset_type"]
|
| 48 |
|
| 49 |
uploaded = st.file_uploader("β¬οΈ Upload Dataset", accept_multiple_files=False)
|
| 50 |
if uploaded:
|
|
@@ -55,47 +58,44 @@ if uploaded:
|
|
| 55 |
s["dataset_file_path"] = path
|
| 56 |
st.success(f"Dataset saved to `{path}`")
|
| 57 |
|
| 58 |
-
# Data dictionary input
|
| 59 |
s["data_dictionary"] = {}
|
| 60 |
dd = st.text_area("π Data Dictionary")
|
| 61 |
s["data_dictionary"] = parse_data_dictionary(dd)
|
| 62 |
-
# try:
|
| 63 |
-
# s["data_dictionary"] = json.loads(dd) if dd.strip() else {}
|
| 64 |
-
# except json.JSONDecodeError:
|
| 65 |
-
# st.error("Invalid JSON in Data Dictionary")
|
| 66 |
|
| 67 |
-
s["additional_instructions"] = st.text_area(
|
| 68 |
-
|
| 69 |
-
)
|
| 70 |
|
| 71 |
# Kick off plan generation
|
| 72 |
-
if st.session_state.plan_ready
|
| 73 |
if st.button("π Generate Plan") and uploaded:
|
| 74 |
with st.spinner("Designing notebook planβ¦"):
|
| 75 |
s = think_sections_node(s)
|
| 76 |
-
st.session_state.plan_ready
|
| 77 |
-
st.session_state.plan_approved
|
| 78 |
-
st.session_state.notebook_ready
|
| 79 |
-
st.session_state.exec_ready
|
| 80 |
|
| 81 |
# βββ 2) Review & Enhance Plan ββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 82 |
if st.session_state.plan_ready:
|
| 83 |
st.subheader("2. π Review Notebook Plan")
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
|
|
|
|
|
|
| 87 |
if st.session_state.plan_approved is None:
|
| 88 |
-
st.session_state.plan_approved = st.radio("Approve this plan?", index=None, options=["YES","NO"])
|
| 89 |
st.write(f"Plan approved: {st.session_state.plan_approved}")
|
| 90 |
|
| 91 |
-
|
| 92 |
# on approval, let user move on
|
| 93 |
if st.session_state.plan_approved == "YES":
|
| 94 |
if st.button("β‘οΈ Next: Write Code"):
|
| 95 |
with st.spinner("Writing notebook skeletonβ¦"):
|
| 96 |
s = write_code_node(s)
|
| 97 |
-
st.session_state.notebook_ready
|
| 98 |
st.session_state.notebook_approved = None
|
|
|
|
| 99 |
# on rejection, collect feedback & loop back
|
| 100 |
elif st.session_state.plan_approved == "NO":
|
| 101 |
st.text_area("What should be changed in the plan?", key="plan_fb")
|
|
@@ -104,6 +104,7 @@ if st.session_state.plan_ready:
|
|
| 104 |
s["plan_feedback"] = st.session_state.plan_fb
|
| 105 |
with st.spinner("Modifying notebook planβ¦"):
|
| 106 |
s = enhance_plan_node(s)
|
|
|
|
| 107 |
# reset approval so we review again
|
| 108 |
st.session_state.plan_approved = None
|
| 109 |
st.rerun()
|
|
@@ -111,19 +112,29 @@ if st.session_state.plan_ready:
|
|
| 111 |
# βββ 3) Review & Modify Notebook βββββββββββββββββββββββββββββββββββββββββββββ
|
| 112 |
if st.session_state.notebook_ready:
|
| 113 |
st.subheader("3. π» Review Notebook Skeleton")
|
| 114 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
if st.session_state.notebook_approved is None:
|
| 117 |
-
st.session_state.notebook_approved = st.radio("Looks good?", index=None, options=["YES","NO"])
|
| 118 |
st.write(f"notebook approved: {st.session_state.notebook_approved}")
|
| 119 |
|
| 120 |
if st.session_state.notebook_approved == "YES":
|
| 121 |
if st.button("β‘οΈ Next: Execute Notebook"):
|
| 122 |
with st.spinner("Executing notebookβ¦"):
|
| 123 |
s = execute_notebook_node(s)
|
| 124 |
-
st.session_state.exec_ready
|
| 125 |
st.session_state.execution_approved = None
|
| 126 |
-
|
| 127 |
elif st.session_state.notebook_approved == "NO":
|
| 128 |
st.write("π Please provide your feedback on the notebook skeleton:")
|
| 129 |
st.text_area("What needs changing in the notebook skeleton?", key="nb_fb")
|
|
@@ -135,10 +146,22 @@ if st.session_state.notebook_ready:
|
|
| 135 |
st.session_state.notebook_approved = None
|
| 136 |
st.rerun()
|
| 137 |
|
|
|
|
|
|
|
| 138 |
# βββ 4) Review & Correct Execution βββββββββββββββββββββββββββββββββββββββββββ
|
| 139 |
-
if st.session_state.exec_ready:
|
| 140 |
st.subheader("4. π Execution Results")
|
| 141 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
|
| 143 |
if s.get("execution_error"):
|
| 144 |
# Always reset approval until they choose Download As-Is
|
|
@@ -157,11 +180,7 @@ if st.session_state.exec_ready:
|
|
| 157 |
st.session_state.execution_approved = True
|
| 158 |
|
| 159 |
elif st.session_state.exec_action == "Provide Feedback":
|
| 160 |
-
fb = st.text_area(
|
| 161 |
-
"π Describe how I should fix the error:",
|
| 162 |
-
height=80,
|
| 163 |
-
key="exec_feedback"
|
| 164 |
-
)
|
| 165 |
st.write(f"Execution feedback: {st.session_state.exec_feedback}")
|
| 166 |
if st.button("π Apply Feedback"):
|
| 167 |
s["execution_feedback"] = fb
|
|
@@ -173,7 +192,7 @@ if st.session_state.exec_ready:
|
|
| 173 |
st.session_state.execution_approved = None
|
| 174 |
st.rerun()
|
| 175 |
|
| 176 |
-
elif st.session_state.exec_action=="Auto-Correct & Retry":
|
| 177 |
if st.button("π§ Auto-Correct & Retry"):
|
| 178 |
s["execution_feedback"] = ""
|
| 179 |
with st.spinner("Auto-correcting and re-executingβ¦"):
|
|
@@ -187,27 +206,25 @@ if st.session_state.exec_ready:
|
|
| 187 |
# If execution succeeded OR user chose βDownload As-Isβ, show downloads
|
| 188 |
if not s.get("execution_error") or st.session_state.execution_approved:
|
| 189 |
st.success("β
Ready to download your artifacts!")
|
| 190 |
-
st.download_button(
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
# βββ 5) Optionally Write Insights βββββββββββββββββββββββββββββββββββββββββββββ
|
| 211 |
# Only show this once the user has downloaded (or chosen) the executed notebook
|
| 212 |
if st.session_state.exec_ready and (
|
| 213 |
not s.get("execution_error") or st.session_state.execution_approved
|
|
@@ -221,22 +238,25 @@ if st.session_state.exec_ready and (
|
|
| 221 |
else:
|
| 222 |
st.success("Notebook executed end-to-end without errors!")
|
| 223 |
st.info("You can now generate insights and conclusions based on the executed notebook.")
|
| 224 |
-
|
| 225 |
# Ask permission
|
| 226 |
st.session_state.insights_opt = st.radio(
|
| 227 |
"Shall I proceed to write the insights and conclusion for this notebook?", index=None,
|
| 228 |
options=["YES", "NO"]
|
| 229 |
-
|
| 230 |
|
| 231 |
if st.session_state.insights_opt == "YES":
|
| 232 |
if st.button("π Generate Insights Now"):
|
| 233 |
with st.spinner("Writing observations & final recommendationsβ¦"):
|
| 234 |
s = write_insights_node(s)
|
| 235 |
st.success("π Insights generated!")
|
|
|
|
| 236 |
# Render the final notebook with insights inline
|
| 237 |
st.markdown("#### Final Notebook with Insights")
|
| 238 |
-
st.
|
| 239 |
-
|
|
|
|
|
|
|
| 240 |
st.download_button(
|
| 241 |
"π₯ Download Final Notebook (.ipynb)",
|
| 242 |
data=json.dumps(s["final_notebook"], indent=2),
|
|
|
|
| 3 |
import json
|
| 4 |
import streamlit as st
|
| 5 |
from app_assets import app_logo
|
| 6 |
+
import io
|
| 7 |
+
from app_utils import parse_data_dictionary, display_notebook, style_css
|
| 8 |
from workflow_nodes import (
|
| 9 |
think_sections_node,
|
| 10 |
enhance_plan_node,
|
|
|
|
| 14 |
correct_notebook_node,
|
| 15 |
write_insights_node
|
| 16 |
)
|
|
|
|
| 17 |
from workflow_nodes import InteractiveCaseStudyState
|
| 18 |
|
|
|
|
| 19 |
st.set_page_config(layout="wide")
|
| 20 |
|
| 21 |
+
# Apply CSS styling
|
| 22 |
+
# This will apply the styles defined in style.css to the Streamlit app
|
| 23 |
+
style_css("style.css")
|
| 24 |
+
|
| 25 |
st.title("π NBforge Demo")
|
| 26 |
|
| 27 |
+
st.logo(app_logo, size='large', link='https://www.mygreatlearning.com/')
|
| 28 |
+
|
| 29 |
|
| 30 |
# βββ Session State Initialization βββββββββββββββββββββββββββββββββββββββββββββ
|
| 31 |
if "state" not in st.session_state:
|
| 32 |
st.session_state.state = InteractiveCaseStudyState()
|
| 33 |
+
st.session_state.plan_ready = False
|
| 34 |
+
st.session_state.plan_approved = None
|
| 35 |
+
st.session_state.notebook_ready = False
|
| 36 |
+
st.session_state.notebook_approved = None
|
| 37 |
+
st.session_state.exec_ready = False
|
| 38 |
+
st.session_state.execution_approved = None
|
| 39 |
|
| 40 |
s = st.session_state.state
|
| 41 |
|
| 42 |
# βββ 1) Collect Inputs ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 43 |
st.header("1. π§ Provide Your Inputs")
|
| 44 |
+
s["domain"] = st.selectbox("π Select Domain", [
|
| 45 |
+
"EDA", "Machine Learning", "Deep Learning", "Natural Language Processing",
|
| 46 |
+
"Computer Vision", "Generative AI Prompt Engineering", "Generative AI RAG"
|
| 47 |
])
|
| 48 |
+
s["topics_and_subtopics"] = st.text_area("π Weekly Topics & Subtopics", height=80)
|
| 49 |
+
s["problem_statement"] = st.text_area("π§© Problem Statement", height=80)
|
| 50 |
+
s["dataset_type"] = st.selectbox("π Dataset Type", ["csv", "pdf", "images", "json"])
|
| 51 |
|
| 52 |
uploaded = st.file_uploader("β¬οΈ Upload Dataset", accept_multiple_files=False)
|
| 53 |
if uploaded:
|
|
|
|
| 58 |
s["dataset_file_path"] = path
|
| 59 |
st.success(f"Dataset saved to `{path}`")
|
| 60 |
|
|
|
|
| 61 |
s["data_dictionary"] = {}
|
| 62 |
dd = st.text_area("π Data Dictionary")
|
| 63 |
s["data_dictionary"] = parse_data_dictionary(dd)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
+
s["additional_instructions"] = st.text_area("π Additional Instructions (optional)", height=60)
|
| 66 |
+
|
|
|
|
| 67 |
|
| 68 |
# Kick off plan generation
|
| 69 |
+
if not st.session_state.plan_ready:
|
| 70 |
if st.button("π Generate Plan") and uploaded:
|
| 71 |
with st.spinner("Designing notebook planβ¦"):
|
| 72 |
s = think_sections_node(s)
|
| 73 |
+
st.session_state.plan_ready = True
|
| 74 |
+
st.session_state.plan_approved = None
|
| 75 |
+
st.session_state.notebook_ready = False
|
| 76 |
+
st.session_state.exec_ready = False
|
| 77 |
|
| 78 |
# βββ 2) Review & Enhance Plan ββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 79 |
if st.session_state.plan_ready:
|
| 80 |
st.subheader("2. π Review Notebook Plan")
|
| 81 |
+
|
| 82 |
+
with st.expander("π View Generated Plan"):
|
| 83 |
+
st.json(s["plan"])
|
| 84 |
+
|
| 85 |
+
# ask approval once
|
| 86 |
if st.session_state.plan_approved is None:
|
| 87 |
+
st.session_state.plan_approved = st.radio("Approve this plan?", index=None, options=["YES", "NO"])
|
| 88 |
st.write(f"Plan approved: {st.session_state.plan_approved}")
|
| 89 |
|
| 90 |
+
|
| 91 |
# on approval, let user move on
|
| 92 |
if st.session_state.plan_approved == "YES":
|
| 93 |
if st.button("β‘οΈ Next: Write Code"):
|
| 94 |
with st.spinner("Writing notebook skeletonβ¦"):
|
| 95 |
s = write_code_node(s)
|
| 96 |
+
st.session_state.notebook_ready = True
|
| 97 |
st.session_state.notebook_approved = None
|
| 98 |
+
|
| 99 |
# on rejection, collect feedback & loop back
|
| 100 |
elif st.session_state.plan_approved == "NO":
|
| 101 |
st.text_area("What should be changed in the plan?", key="plan_fb")
|
|
|
|
| 104 |
s["plan_feedback"] = st.session_state.plan_fb
|
| 105 |
with st.spinner("Modifying notebook planβ¦"):
|
| 106 |
s = enhance_plan_node(s)
|
| 107 |
+
|
| 108 |
# reset approval so we review again
|
| 109 |
st.session_state.plan_approved = None
|
| 110 |
st.rerun()
|
|
|
|
| 112 |
# βββ 3) Review & Modify Notebook βββββββββββββββββββββββββββββββββββββββββββββ
|
| 113 |
if st.session_state.notebook_ready:
|
| 114 |
st.subheader("3. π» Review Notebook Skeleton")
|
| 115 |
+
with st.expander("π View Notebook Skeleton"):
|
| 116 |
+
display_notebook(io.StringIO(json.dumps(s["raw_notebook"], indent=2)))
|
| 117 |
+
|
| 118 |
+
st.info("----------------------------------------------------------------")
|
| 119 |
+
st.write("**NOTE**: If itβs hard to read here, download and open in Colab:")
|
| 120 |
+
st.download_button(
|
| 121 |
+
"Download raw_notebook.ipynb",
|
| 122 |
+
data=json.dumps(s["raw_notebook"], indent=2),
|
| 123 |
+
file_name="raw_notebook.ipynb",
|
| 124 |
+
mime="application/json",
|
| 125 |
+
)
|
| 126 |
|
| 127 |
if st.session_state.notebook_approved is None:
|
| 128 |
+
st.session_state.notebook_approved = st.radio("Looks good?", index=None, options=["YES", "NO"])
|
| 129 |
st.write(f"notebook approved: {st.session_state.notebook_approved}")
|
| 130 |
|
| 131 |
if st.session_state.notebook_approved == "YES":
|
| 132 |
if st.button("β‘οΈ Next: Execute Notebook"):
|
| 133 |
with st.spinner("Executing notebookβ¦"):
|
| 134 |
s = execute_notebook_node(s)
|
| 135 |
+
st.session_state.exec_ready = True
|
| 136 |
st.session_state.execution_approved = None
|
| 137 |
+
|
| 138 |
elif st.session_state.notebook_approved == "NO":
|
| 139 |
st.write("π Please provide your feedback on the notebook skeleton:")
|
| 140 |
st.text_area("What needs changing in the notebook skeleton?", key="nb_fb")
|
|
|
|
| 146 |
st.session_state.notebook_approved = None
|
| 147 |
st.rerun()
|
| 148 |
|
| 149 |
+
|
| 150 |
+
|
| 151 |
# βββ 4) Review & Correct Execution βββββββββββββββββββββββββββββββββββββββββββ
|
| 152 |
+
if st.session_state.exec_ready:
|
| 153 |
st.subheader("4. π Execution Results")
|
| 154 |
+
with st.expander("π View Executed Notebook"):
|
| 155 |
+
display_notebook(io.StringIO(s["executed_notebook"]))
|
| 156 |
+
|
| 157 |
+
st.info("----------------------------------------------------------------")
|
| 158 |
+
st.write("**NOTE**: If itβs hard to read here, download and open in Colab:")
|
| 159 |
+
st.download_button(
|
| 160 |
+
"Download executed_notebook.ipynb",
|
| 161 |
+
data=s["executed_notebook"],
|
| 162 |
+
file_name="executed_notebook.ipynb",
|
| 163 |
+
mime="application/json",
|
| 164 |
+
)
|
| 165 |
|
| 166 |
if s.get("execution_error"):
|
| 167 |
# Always reset approval until they choose Download As-Is
|
|
|
|
| 180 |
st.session_state.execution_approved = True
|
| 181 |
|
| 182 |
elif st.session_state.exec_action == "Provide Feedback":
|
| 183 |
+
fb = st.text_area("π Describe how I should fix the error:", height=80, key="exec_feedback")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
st.write(f"Execution feedback: {st.session_state.exec_feedback}")
|
| 185 |
if st.button("π Apply Feedback"):
|
| 186 |
s["execution_feedback"] = fb
|
|
|
|
| 192 |
st.session_state.execution_approved = None
|
| 193 |
st.rerun()
|
| 194 |
|
| 195 |
+
elif st.session_state.exec_action == "Auto-Correct & Retry":
|
| 196 |
if st.button("π§ Auto-Correct & Retry"):
|
| 197 |
s["execution_feedback"] = ""
|
| 198 |
with st.spinner("Auto-correcting and re-executingβ¦"):
|
|
|
|
| 206 |
# If execution succeeded OR user chose βDownload As-Isβ, show downloads
|
| 207 |
if not s.get("execution_error") or st.session_state.execution_approved:
|
| 208 |
st.success("β
Ready to download your artifacts!")
|
| 209 |
+
st.download_button("π₯ Download plan.json",
|
| 210 |
+
data=json.dumps(s["plan"], indent=2),
|
| 211 |
+
file_name="plan.json",
|
| 212 |
+
mime="application/json"
|
| 213 |
+
)
|
| 214 |
+
st.download_button("π₯ Download raw_notebook.ipynb",
|
| 215 |
+
data=json.dumps(s["raw_notebook"], indent=2),
|
| 216 |
+
file_name="raw_notebook.ipynb",
|
| 217 |
+
mime="application/json"
|
| 218 |
+
)
|
| 219 |
+
st.download_button("π₯ Download executed_notebook.ipynb",
|
| 220 |
+
data=s["executed_notebook"],
|
| 221 |
+
file_name="executed_notebook.ipynb",
|
| 222 |
+
mime="application/json"
|
| 223 |
+
)
|
| 224 |
+
|
| 225 |
+
|
| 226 |
+
|
| 227 |
+
# βββ 5) Generate Insights (Optional) ββββββββββββββββββββββββββββββββββββββββββ
|
|
|
|
|
|
|
| 228 |
# Only show this once the user has downloaded (or chosen) the executed notebook
|
| 229 |
if st.session_state.exec_ready and (
|
| 230 |
not s.get("execution_error") or st.session_state.execution_approved
|
|
|
|
| 238 |
else:
|
| 239 |
st.success("Notebook executed end-to-end without errors!")
|
| 240 |
st.info("You can now generate insights and conclusions based on the executed notebook.")
|
| 241 |
+
|
| 242 |
# Ask permission
|
| 243 |
st.session_state.insights_opt = st.radio(
|
| 244 |
"Shall I proceed to write the insights and conclusion for this notebook?", index=None,
|
| 245 |
options=["YES", "NO"]
|
| 246 |
+
)
|
| 247 |
|
| 248 |
if st.session_state.insights_opt == "YES":
|
| 249 |
if st.button("π Generate Insights Now"):
|
| 250 |
with st.spinner("Writing observations & final recommendationsβ¦"):
|
| 251 |
s = write_insights_node(s)
|
| 252 |
st.success("π Insights generated!")
|
| 253 |
+
|
| 254 |
# Render the final notebook with insights inline
|
| 255 |
st.markdown("#### Final Notebook with Insights")
|
| 256 |
+
with st.expander("π View Final Notebook with Insights"):
|
| 257 |
+
display_notebook(io.StringIO(json.dumps(s["final_notebook"], indent=2)))
|
| 258 |
+
|
| 259 |
+
# Offer download
|
| 260 |
st.download_button(
|
| 261 |
"π₯ Download Final Notebook (.ipynb)",
|
| 262 |
data=json.dumps(s["final_notebook"], indent=2),
|