Upload 8 files
Browse files- src/app_assets.py +2 -0
- src/app_utils.py +99 -0
- src/exception.py +35 -0
- src/logger.py +18 -0
- src/prompts.py +1340 -0
- src/requirements.txt +13 -0
- src/workflow_nodes.py +1266 -0
- src/workflow_utils.py +614 -0
src/app_assets.py
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
app_logo = "https://mma.prnewswire.com/media/1458111/Great_Learning_Logo.jpg?p=facebook"
|
| 2 |
+
app_icon = "https://www.google.com/imgres?q=Great%20learning%20images&imgurl=https%3A%2F%2Flookaside.fbsbx.com%2Flookaside%2Fcrawler%2Fmedia%2F%3Fmedia_id%3D100068746724814&imgrefurl=https%3A%2F%2Fwww.facebook.com%2FGreatLearningOfficial%2F&docid=6RoTx3PPo1UrRM&tbnid=57yBHLRcHmvNlM&vet=12ahUKEwinsvHC2rCNAxX6S2cHHbcKB44QM3oECGQQAA..i&w=1388&h=1388&hcb=2&ved=2ahUKEwinsvHC2rCNAxX6S2cHHbcKB44QM3oECGQQAA"
|
src/app_utils.py
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
from langchain.schema import SystemMessage, HumanMessage
|
| 3 |
+
from typing import Dict, Any
|
| 4 |
+
from langchain_openai import ChatOpenAI
|
| 5 |
+
import nbformat
|
| 6 |
+
from io import StringIO
|
| 7 |
+
from nbconvert import HTMLExporter
|
| 8 |
+
|
| 9 |
+
# Setting up LLM
|
| 10 |
+
|
| 11 |
+
## Create a ChatOpenAI model instance using LangChain
|
| 12 |
+
model = ChatOpenAI(
|
| 13 |
+
openai_api_base="https://aibe.mygreatlearning.com/openai/v1",
|
| 14 |
+
openai_api_key="gl-U2FsdGVkX1/DTLQlsmj+RdJjPy3igB9qINuaX940XtJ0CPnGc/5sbBkPKah/C829",
|
| 15 |
+
model="gpt-4o",
|
| 16 |
+
streaming=False # Explicitly disabling streaming
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
def parse_data_dictionary(input_str: str) -> dict:
|
| 20 |
+
"""
|
| 21 |
+
Converts a plain text data dictionary into a structured JSON format.
|
| 22 |
+
|
| 23 |
+
Args:
|
| 24 |
+
input_str (str): Data dictionary in plain text format.
|
| 25 |
+
|
| 26 |
+
Returns:
|
| 27 |
+
dict: Structured JSON representation of the data dictionary.
|
| 28 |
+
"""
|
| 29 |
+
lines = input_str.strip().split("\n")
|
| 30 |
+
data_dict = {}
|
| 31 |
+
for line in lines:
|
| 32 |
+
parts = line.split(":")
|
| 33 |
+
if len(parts) == 2:
|
| 34 |
+
key = parts[0].strip()
|
| 35 |
+
value = parts[1].strip()
|
| 36 |
+
data_dict[key] = value
|
| 37 |
+
return data_dict
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
def generate_notebook_plan_text(plan: Dict[str, Any], output_txt_file: str = "notebook_plan.txt") -> str:
|
| 41 |
+
"""
|
| 42 |
+
Converts a JSON notebook plan into a human-readable text document.
|
| 43 |
+
|
| 44 |
+
Args:
|
| 45 |
+
plan (Dict[str, Any]): The JSON notebook plan.
|
| 46 |
+
output_txt_file (str): The filename to save the generated text document.
|
| 47 |
+
|
| 48 |
+
Returns:
|
| 49 |
+
str: The generated text content.
|
| 50 |
+
"""
|
| 51 |
+
# Define the system prompt to guide the LLM's behavior
|
| 52 |
+
system_prompt = """
|
| 53 |
+
You are an expert AI curriculum designer. Given a structured JSON notebook plan, your task is to convert it into a clear, readable text document. The document should outline the notebook's structure, including sections and subsections, specifying whether each is a markdown or code block. For code blocks, include the 'code_rules' provided. For markdown blocks, include the 'description'. Format the output with appropriate headings and bullet points for clarity.
|
| 54 |
+
"""
|
| 55 |
+
|
| 56 |
+
# Convert the JSON plan to a formatted string
|
| 57 |
+
plan_str = json.dumps(plan, indent=2)
|
| 58 |
+
|
| 59 |
+
# Create the messages for the LLM
|
| 60 |
+
messages = [
|
| 61 |
+
SystemMessage(content=system_prompt),
|
| 62 |
+
HumanMessage(content=f"Here is the JSON notebook plan:\n\n{plan_str}")
|
| 63 |
+
]
|
| 64 |
+
|
| 65 |
+
# Invoke the LLM with the messages
|
| 66 |
+
response = model.invoke(messages)
|
| 67 |
+
|
| 68 |
+
# Extract the generated text content
|
| 69 |
+
text_content = response.content
|
| 70 |
+
|
| 71 |
+
# Save the generated text to a file
|
| 72 |
+
with open(output_txt_file, "w", encoding="utf-8") as f:
|
| 73 |
+
f.write(text_content)
|
| 74 |
+
|
| 75 |
+
return text_content
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
def render_notebook_from_json(notebook_json: dict) -> str:
|
| 79 |
+
"""
|
| 80 |
+
Renders a Jupyter notebook from raw JSON.
|
| 81 |
+
|
| 82 |
+
Args:
|
| 83 |
+
notebook_json (dict): The raw notebook JSON.
|
| 84 |
+
|
| 85 |
+
Returns:
|
| 86 |
+
str: The HTML representation of the notebook.
|
| 87 |
+
"""
|
| 88 |
+
# Convert the JSON to a notebook node
|
| 89 |
+
notebook = nbformat.from_dict(notebook_json)
|
| 90 |
+
|
| 91 |
+
# Use nbconvert to convert the notebook to HTML
|
| 92 |
+
html_exporter = HTMLExporter()
|
| 93 |
+
body, resources = html_exporter.from_notebook_node(notebook)
|
| 94 |
+
|
| 95 |
+
return body
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
|
src/exception.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sys
|
| 2 |
+
|
| 3 |
+
def error_message_detail(error, error_detail):
|
| 4 |
+
"""
|
| 5 |
+
Generate a detailed error message for the NBforge application.
|
| 6 |
+
|
| 7 |
+
:param error: The exception instance or error message.
|
| 8 |
+
:param error_detail: The sys module (for accessing traceback details via sys.exc_info()).
|
| 9 |
+
:return: A formatted string with the file name and line number where the error occurred.
|
| 10 |
+
"""
|
| 11 |
+
_, _, exc_tb = error_detail.exc_info()
|
| 12 |
+
file_name = exc_tb.tb_frame.f_code.co_filename
|
| 13 |
+
|
| 14 |
+
error_message = "Error occurred python script name [{0}] line number [{1}] error message [{2}]".format(
|
| 15 |
+
file_name, exc_tb.tb_lineno, str(error)
|
| 16 |
+
)
|
| 17 |
+
return error_message
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
class NBForgeException(Exception):
|
| 21 |
+
"""
|
| 22 |
+
Custom exception class for handling errors in the NBforge notebook pipeline.
|
| 23 |
+
"""
|
| 24 |
+
def __init__(self, error_message, error_detail):
|
| 25 |
+
"""
|
| 26 |
+
Initialize the exception with a detailed error message.
|
| 27 |
+
|
| 28 |
+
:param error_message: A description of the error.
|
| 29 |
+
:param error_detail: Pass the sys module to extract traceback information.
|
| 30 |
+
"""
|
| 31 |
+
super().__init__(error_message)
|
| 32 |
+
self.error_message = error_message_detail(error_message, error_detail=error_detail)
|
| 33 |
+
|
| 34 |
+
def __str__(self):
|
| 35 |
+
return self.error_message
|
src/logger.py
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import logging
|
| 2 |
+
import os
|
| 3 |
+
from datetime import datetime
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
LOG_FILE = f"{datetime.now().strftime('%m_%d_%Y_%H_%M_%S')}.log"
|
| 7 |
+
|
| 8 |
+
logs_path = os.path.join(os.getcwd(), "logs", LOG_FILE)
|
| 9 |
+
|
| 10 |
+
os.makedirs(logs_path, exist_ok=True)
|
| 11 |
+
|
| 12 |
+
LOG_FILE_PATH = os.path.join(logs_path, LOG_FILE)
|
| 13 |
+
|
| 14 |
+
logging.basicConfig(
|
| 15 |
+
filename=LOG_FILE_PATH,
|
| 16 |
+
format="[ %(asctime)s ] %(lineno)d %(name)s - %(levelname)s - %(message)s",
|
| 17 |
+
level=logging.INFO,
|
| 18 |
+
)
|
src/prompts.py
ADDED
|
@@ -0,0 +1,1340 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Exploratory_Data_Analysis = """
|
| 2 |
+
# Exploratory Data Analysis Task Notebook Structure and Content Guidelines
|
| 3 |
+
|
| 4 |
+
This outline guides the LLM to structure an EDA task notebook. It's adaptable to **any tabular dataset** for comprehensive Exploratory Data Analysis. The LLM must **THINK, ANALYZE, AND DECIDE** which operations fit the specific case study, dataset, and weekly topics.
|
| 5 |
+
|
| 6 |
+
## 1. Problem Statement (First-level heading markdown)
|
| 7 |
+
## Business Context (Second-level heading markdown)
|
| 8 |
+
* Write the user's business context as is in a separate markdown cell.
|
| 9 |
+
|
| 10 |
+
## Objective (Second-level heading markdown)
|
| 11 |
+
* Write the user's objective as is in a separate markdown cell.
|
| 12 |
+
|
| 13 |
+
## Data Dictionary (Second-level heading markdown)
|
| 14 |
+
* Write the user's data dictionary as is in a separate markdown cell.
|
| 15 |
+
* Enhance column descriptions with necessary details (e.g., units, specific formats).
|
| 16 |
+
|
| 17 |
+
*(This section will have 6 markdown cells in total).*
|
| 18 |
+
|
| 19 |
+
---
|
| 20 |
+
|
| 21 |
+
# 2. Install and import the necessary libraries (First-level heading markdown)
|
| 22 |
+
## Install Dependencies (Second-level heading markdown)
|
| 23 |
+
* **Think and decide** on necessary libraries with specific versions (e.g., `pandas`, `numpy`, `matplotlib`, `seaborn`).
|
| 24 |
+
* **Code Cell:** Install all dependencies in one cell.
|
| 25 |
+
|
| 26 |
+
## Import Libraries (Second-level heading markdown)
|
| 27 |
+
* Import all required libraries.
|
| 28 |
+
* Group imports with clear comments (e.g., "# Data manipulation", "# Visualization").
|
| 29 |
+
* Add comments for less common libraries.
|
| 30 |
+
* **Code Cell:** Import all libraries in one cell.
|
| 31 |
+
|
| 32 |
+
*(This section will have 2 markdown and 2 code cells).*
|
| 33 |
+
---
|
| 34 |
+
|
| 35 |
+
# 3. Loading the dataset (First-level heading markdown)
|
| 36 |
+
* Datasets are primarily tabular (CSV, Excel).
|
| 37 |
+
* **Think and decide** the best method to load your specific data.
|
| 38 |
+
|
| 39 |
+
## Read Data from Path (Second-level heading markdown)
|
| 40 |
+
* Read data from the provided path (e.g., data = `pd.read_csv`, `pd.read_excel`).
|
| 41 |
+
* **Code Cell:** Load data in one cell.
|
| 42 |
+
|
| 43 |
+
## Copy Data to New Variable and Preview (Second-level heading markdown)
|
| 44 |
+
* Copy data to a new variable (e.g., `df = data.copy()`).
|
| 45 |
+
* For multiple datasets, **think and decide** what to do here (to copy into different variables or some other).
|
| 46 |
+
* **Code Cell:** Copy and preview data in one cell.
|
| 47 |
+
|
| 48 |
+
*(This section will have 2 markdown and 2 code cells).*
|
| 49 |
+
|
| 50 |
+
---
|
| 51 |
+
|
| 52 |
+
# 4. Data Overview (First-level heading markdown)
|
| 53 |
+
* Analyze dataset inputs (type, path, data dictionary, dataset details (rows, columns, dtypes)).
|
| 54 |
+
* **Think, reason, and decide** on operations to show initial data sanity and structure. The below are few you should consider in this section.
|
| 55 |
+
|
| 56 |
+
## Display the first few rows of the dataset (Second-level heading markdown)
|
| 57 |
+
* **Examples of operations:** `df.head()`, `df.sample()`.
|
| 58 |
+
* If multiple datasets are involved, consider the most appropriate way to display them (e.g., merged or separately) based on the problem statement.
|
| 59 |
+
* **Code Cell:** Display first few rows in one cell.
|
| 60 |
+
|
| 61 |
+
## Checking the shape of the dataset (Second-level heading markdown)
|
| 62 |
+
* **Examples of operations:** `df.shape`.
|
| 63 |
+
* **Code Cell:** Display shape in one cell.
|
| 64 |
+
|
| 65 |
+
## Displaying data types (Second-level heading markdown)
|
| 66 |
+
* **Examples of operations:** `df.dtypes`, `df.info()`.
|
| 67 |
+
* **Code Cell:** Display data types in one cell.
|
| 68 |
+
|
| 69 |
+
## Statistical summary of the dataset (Second-level heading markdown)
|
| 70 |
+
* **Examples of operations:** `df.describe()`, `df.describe(include='object')`.
|
| 71 |
+
* **Code Cell:** Display statistical summary in one cell.
|
| 72 |
+
|
| 73 |
+
## Missing values (Second-level heading markdown)
|
| 74 |
+
* **Think and decide** on checking and handling missing values.
|
| 75 |
+
* **Examples of operations:** `df.isnull().sum()`, `df.dropna()`, `df.fillna()`.
|
| 76 |
+
* **Code Cell(s):** Display nulls in one cell; handle in separate cells (if needed).
|
| 77 |
+
|
| 78 |
+
## Duplicate values (Second-level heading markdown)
|
| 79 |
+
* **Think and decide** on checking and handling duplicate values.
|
| 80 |
+
* **Examples of operations:** `df.duplicated().sum()`, `df.drop_duplicates()`.
|
| 81 |
+
* **Code Cell(s):** Display duplicates in one cell; remove in another (if needed).
|
| 82 |
+
|
| 83 |
+
* Identify data quality issues (e.g., inconsistent entries, wrong data types such as date/time as object, integer into object columns).
|
| 84 |
+
* For each column, **think, analyze, and decide** on operations to convert to the correct format for smooth EDA.
|
| 85 |
+
* Perform operations in separate cells. Ensure the dataset is ready for EDA.
|
| 86 |
+
* **Think and decide** on basic EDA to extract info using `groupby`, `loc`, `iloc`.
|
| 87 |
+
* **Code Cell(s):** Perform type conversions, basic EDA, and other cleanups in separate cells.
|
| 88 |
+
|
| 89 |
+
*(This section will have 10-15 code cells with their necessary second-level markdown cells. Total: 20-25 cells).*
|
| 90 |
+
* **NOTE:** Do not introduce analysis beyond weekly topics. Ensure data is fully ready for EDA.
|
| 91 |
+
|
| 92 |
+
---
|
| 93 |
+
|
| 94 |
+
# 5. Exploratory Data Analysis (First-level heading markdown)
|
| 95 |
+
* **NOTE:** Do not introduce analysis beyond weekly topics.
|
| 96 |
+
* Focus on visualizations to understand data distributions and relationships.
|
| 97 |
+
|
| 98 |
+
## Univariate analysis (Second-level heading markdown)
|
| 99 |
+
* **Think and decide** on relevant visualizations for individual features.
|
| 100 |
+
* **Examples of operations:** Histograms, box plots, count plots for categorical data.
|
| 101 |
+
* Justify why each plot helps answer business questions.
|
| 102 |
+
* **Code Cell(s):** Each plot in its own cell with proper comments.
|
| 103 |
+
|
| 104 |
+
## Bivariate Analysis (Second-level heading markdown)
|
| 105 |
+
* **Think and decide** on visualizations exploring relationships between two variables crucial for the problem.
|
| 106 |
+
* **Examples of operations:** Scatter plots, grouped bar charts, line plots, heatmaps for correlations.
|
| 107 |
+
* Justify why each plot surfaces meaningful patterns.
|
| 108 |
+
* **Code Cell(s):** Each plot in its own cell with proper comments.
|
| 109 |
+
|
| 110 |
+
## Multivariate Analysis (Second-level heading markdown)
|
| 111 |
+
* **Think and decide** if multi-feature interactions are necessary for objectives.
|
| 112 |
+
* **Examples of operations:** Pair plots, 3D scatter plots, advanced conditional plots, dimensionality reduction techniques (if covered).
|
| 113 |
+
* Justify how these uncover deeper insights.
|
| 114 |
+
* **Code Cell(s):** Each plot in its own cell woth proper comments.
|
| 115 |
+
|
| 116 |
+
*(In overall this 5th section will have around 30 cells minimum including code and markdown cells of these three subsection in this).*
|
| 117 |
+
|
| 118 |
+
---
|
| 119 |
+
|
| 120 |
+
# 6. Feature Engineering (First-level heading markdown)
|
| 121 |
+
* For this EDA-focused case study, we won't perform complex transformations here.
|
| 122 |
+
* **Think and decide** which new features could be derived from existing columns that might yield useful insights.
|
| 123 |
+
* **Examples of considerations:** Binning numerical data, encoding categorical variables, creating ratio features, extracting date/time components.
|
| 124 |
+
* Explain the rationale for each proposed feature in the business context.
|
| 125 |
+
* **Code Cell(s):** Implement simple feature derivations if relevant for EDA (e.g., creating age groups from birthdate).
|
| 126 |
+
|
| 127 |
+
*(This section will have 3 markdown cells and 7 code cells, depending on relevance).*
|
| 128 |
+
---
|
| 129 |
+
|
| 130 |
+
# 7. Insights & Recommendations (First-level heading markdown)
|
| 131 |
+
* Keep this section empty.
|
| 132 |
+
* We will write insights based on outputs after the notebook is executed.
|
| 133 |
+
|
| 134 |
+
---
|
| 135 |
+
|
| 136 |
+
**FINAL NOTE:** This is your high-level reference, in overall the entire notebook will consist of atleast 80 to 90 cells of all sections. You must **ONLY THINK, ANALYZE, AND DECIDE** based on the specific problem statement, dataset, and weekly subtopics. Do NOT go beyond the weekly topics. Every decision and operation must be carefully considered and justified in the context of the EDA problem.
|
| 137 |
+
"""
|
| 138 |
+
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
Macine_Learning = """
|
| 142 |
+
# ML Task Notebook Structure and Content Guidelines
|
| 143 |
+
|
| 144 |
+
This outline guides the LLM to structure a Machine Learning task notebook. It's adaptable to **any tabular dataset** and **any ML problem type** (e.g., classification, regression, clustering). The LLM must **THINK, ANALYZE, AND DECIDE** which operations fit the specific case study, dataset, and weekly topics.
|
| 145 |
+
|
| 146 |
+
# 1. Problem Statement (First-level heading markdown)
|
| 147 |
+
## Business Context (Second-level heading markdown)
|
| 148 |
+
* Write the user’s Business context as is in a separate markdown cell.
|
| 149 |
+
|
| 150 |
+
## Objective (Second-level heading markdown)
|
| 151 |
+
* Write the user's objective as is in a separate markdown cell.
|
| 152 |
+
|
| 153 |
+
## Data Dictionary (Second-level heading markdown)
|
| 154 |
+
* Write the user's data dictionary as is in a separate markdown cell.
|
| 155 |
+
* Enhance column descriptions with necessary details (e.g., units for money, distance).
|
| 156 |
+
|
| 157 |
+
*(This section will have 6 markdown cells in total).*
|
| 158 |
+
|
| 159 |
+
---
|
| 160 |
+
|
| 161 |
+
# 2. Install and import the necessary libraries (First-level heading markdown)
|
| 162 |
+
## Install Dependencies (Second-level heading markdown)
|
| 163 |
+
* **Think and decide** on necessary libraries with specific versions (e.g., `pandas`, `numpy`, `matplotlib`, `seaborn`, `scikit-learn`).
|
| 164 |
+
* **Code Cell:** Install all dependencies in one cell.
|
| 165 |
+
|
| 166 |
+
## Import Libraries (Second-level heading markdown)
|
| 167 |
+
* Import all required libraries.
|
| 168 |
+
* Group imports with clear comments (e.g., "# Data manipulation", "# Visualization", "# Machine Learning Models").
|
| 169 |
+
* Add comments for less common libraries.
|
| 170 |
+
* **Code Cell:** Import all libraries in one cell.
|
| 171 |
+
|
| 172 |
+
*(This section will have 2 markdown and 2 code cells).*
|
| 173 |
+
|
| 174 |
+
---
|
| 175 |
+
|
| 176 |
+
# 3. Loading the dataset (First-level heading markdown)
|
| 177 |
+
* Datasets are primarily tabular (CSV, Excel).
|
| 178 |
+
* **Think and decide** the best method to load your specific data.
|
| 179 |
+
|
| 180 |
+
## Read Data from Path (Second-level heading markdown)
|
| 181 |
+
* Read data from the provided path (e.g., `pd.read_csv`).
|
| 182 |
+
* **Code Cell:** Load data in one cell.
|
| 183 |
+
|
| 184 |
+
## Copy Data to New Variable and Preview (Second-level heading markdown)
|
| 185 |
+
* Copy data to a new variable (e.g., `df_copy = df.copy()`).
|
| 186 |
+
* Display a preview to confirm data integrity (e.g., `df_copy.head()`). For multiple datasets, **think and decide** how to display or merge them.
|
| 187 |
+
* **Code Cell:** Copy and preview data in one cell.
|
| 188 |
+
|
| 189 |
+
*(This section will have 2 markdown and 2 code cells).*
|
| 190 |
+
|
| 191 |
+
---
|
| 192 |
+
|
| 193 |
+
# 4. Data Overview and Preliminary Checks (First-level heading markdown)
|
| 194 |
+
* Analyze dataset inputs (type, path, data dictionary).
|
| 195 |
+
* **Think, reason, and decide** on operations to show initial data sanity and structure.
|
| 196 |
+
|
| 197 |
+
## Display the first few rows of the dataset (Second-level heading markdown)
|
| 198 |
+
* **Examples of operations:** `df.head()`, `df.sample()`.
|
| 199 |
+
* **Code Cell:** Display first few rows in one cell.
|
| 200 |
+
|
| 201 |
+
## Checking the shape of the dataset (Second-level heading markdown)
|
| 202 |
+
* **Examples of operations:** `df.shape`.
|
| 203 |
+
* **Code Cell:** Display shape in one cell.
|
| 204 |
+
|
| 205 |
+
## Displaying data types (Second-level heading markdown)
|
| 206 |
+
* **Examples of operations:** `df.dtypes`, `df.info()`.
|
| 207 |
+
* **Code Cell:** Display data types in one cell.
|
| 208 |
+
|
| 209 |
+
## Statistical summary of the dataset (Second-level heading markdown)
|
| 210 |
+
* **Examples of operations:** `df.describe()`, `df.describe(include='object')`.
|
| 211 |
+
* **Code Cell:** Display statistical summary in one cell.
|
| 212 |
+
|
| 213 |
+
## Missing values (Second-level heading markdown)
|
| 214 |
+
* **Think and decide** on checking and handling missing values.
|
| 215 |
+
* **Examples of operations:** `df.isnull().sum()`, `df.dropna()`, `df.fillna()`.
|
| 216 |
+
* **Code Cell(s):** Display nulls in one cell; handle in separate cells (if needed).
|
| 217 |
+
|
| 218 |
+
## Duplicate values (Second-level heading markdown)
|
| 219 |
+
* **Think and decide** on checking and handling duplicate values.
|
| 220 |
+
* **Examples of operations:** `df.duplicated().sum()`, `df.drop_duplicates()`.
|
| 221 |
+
* **Code Cell(s):** Display duplicates in one cell; remove in another (if needed).
|
| 222 |
+
|
| 223 |
+
* Identify data quality issues (e.g., inconsistent entries, wrong data types like date/time as object).
|
| 224 |
+
* For each column, **think, analyze, and decide** on operations to convert them to the correct format for smooth analysis.
|
| 225 |
+
* Perform operations in separate cells. Ensure the dataset is preliminarily ready.
|
| 226 |
+
* **Think and decide** on basic EDA to extract info using `groupby`, `loc`, `iloc`.
|
| 227 |
+
* **Code Cell(s):** Perform type conversions, basic EDA, and other cleanups in separate cells.
|
| 228 |
+
|
| 229 |
+
*(This section will have 8-10 markdown cells and 10-15 code cells. Total: 18-25 cells).*
|
| 230 |
+
* **NOTE:** Do not introduce analysis beyond weekly topics. Ensure data is fully ready for EDA.
|
| 231 |
+
|
| 232 |
+
---
|
| 233 |
+
|
| 234 |
+
# 5. Exploratory Data Analysis (EDA) (First-level heading markdown)
|
| 235 |
+
* **NOTE:** Do not introduce analysis beyond weekly topics.
|
| 236 |
+
* Focus on visualizations to understand data distributions and relationships for ML model design. Aim for 5-8 plots.
|
| 237 |
+
|
| 238 |
+
## Univariate Analysis (Second-level heading markdown)
|
| 239 |
+
* **Think and decide** on relevant visualizations for individual features.
|
| 240 |
+
* **Examples of operations:** Histograms, box plots (numerical); count plots (categorical).
|
| 241 |
+
* Justify why each plot helps answer business questions.
|
| 242 |
+
* **Code Cell(s):** Each plot in its own cell.
|
| 243 |
+
|
| 244 |
+
## Bivariate Analysis (Second-level heading markdown)
|
| 245 |
+
* **Think and decide** on visualizations exploring relationships between two variables crucial for the problem.
|
| 246 |
+
* **Examples of operations:** Scatter plots, grouped bar charts, line plots, heatmaps for correlations.
|
| 247 |
+
* Justify why each plot surfaces meaningful patterns.
|
| 248 |
+
* **Code Cell(s):** Each plot in its own cell.
|
| 249 |
+
|
| 250 |
+
*(This section will have 2 markdown cells and 8 code cells. Total: 10 cells).*
|
| 251 |
+
|
| 252 |
+
---
|
| 253 |
+
|
| 254 |
+
# 6. Data Preprocessing (First-level heading markdown)
|
| 255 |
+
* Prepare the data for machine learning models.
|
| 256 |
+
* **Think, analyze, and decide** on preprocessing steps based on dataset characteristics and chosen ML model requirements.
|
| 257 |
+
* Each distinct operation in its own cell.
|
| 258 |
+
|
| 259 |
+
## Handling Missing Values (Second-level heading markdown)
|
| 260 |
+
* **Think and decide** on the most suitable strategy for treating missing values (e.g., imputation, removal).
|
| 261 |
+
* **Examples of operations:** `SimpleImputer`, `dropna()`, contextual imputation.
|
| 262 |
+
* **Code Cell(s):** Implement the chosen strategy.
|
| 263 |
+
|
| 264 |
+
## Handling Outliers (Second-level heading markdown)
|
| 265 |
+
* **Think and decide** if outlier detection and treatment are necessary.
|
| 266 |
+
* **Examples of operations:** Capping (e.g., `IQR` method), transformation (e.g., log transform), removal.
|
| 267 |
+
* **Code Cell(s):** Implement the chosen strategy.
|
| 268 |
+
|
| 269 |
+
## Feature Engineering (Second-level heading markdown)
|
| 270 |
+
* **Think and decide** which new features can be derived from existing columns to improve model performance.
|
| 271 |
+
* **Examples of operations:** Creating interaction terms, polynomial features, datetime components, binning numerical features.
|
| 272 |
+
* **Code Cell(s):** Implement code to derive and add new features.
|
| 273 |
+
|
| 274 |
+
## Encoding Categorical Variables (Second-level heading markdown)
|
| 275 |
+
* **Think and decide** on the most appropriate encoding scheme for categorical variables.
|
| 276 |
+
* **Examples of operations:** One-hot encoding (`pd.get_dummies`), Label encoding (`LabelEncoder`), Target encoding.
|
| 277 |
+
* **Code Cell(s):** Implement the chosen encoding strategy.
|
| 278 |
+
|
| 279 |
+
## Feature Scaling (Second-level heading markdown)
|
| 280 |
+
* **Think and decide** whether feature scaling is necessary and the most appropriate method.
|
| 281 |
+
* **Examples of operations:** Standardization (`StandardScaler`), Normalization (`MinMaxScaler`), Robust Scaling (`RobustScaler`).
|
| 282 |
+
* **Code Cell(s):** Apply the appropriate scaling technique.
|
| 283 |
+
|
| 284 |
+
*(This section will have 5-7 markdown cells and 10 code cells. Total: 10-17 cells).*
|
| 285 |
+
* **NOTE:** Justify each preprocessing step by its relevance to model training and alignment with the problem.
|
| 286 |
+
|
| 287 |
+
---
|
| 288 |
+
|
| 289 |
+
# 7. Model Building (First-level heading markdown)
|
| 290 |
+
* Define, train, and optimize your machine learning models.
|
| 291 |
+
* **Think and decide** whether a single model or multiple models are appropriate based on problem and data.
|
| 292 |
+
* Each distinct operation in its own cell.
|
| 293 |
+
|
| 294 |
+
## Train-Test Split (Second-level heading markdown)
|
| 295 |
+
* **Think and decide** on the splitting strategy for training and testing sets.
|
| 296 |
+
* **Key Considerations:** Split ratios (e.g., 70-30), `random_state`, stratification for classification.
|
| 297 |
+
* **Code Cell:** Implement the train-test split.
|
| 298 |
+
|
| 299 |
+
## Model Training (Second-level heading markdown)
|
| 300 |
+
* **Think and decide** on the most suitable machine learning model(s) to train.
|
| 301 |
+
* **Examples of models:** Logistic Regression, Decision Tree, Random Forest, SVM, Gradient Boosting (for classification/regression); K-Means (clustering).
|
| 302 |
+
* **Code Cell(s):** Initialize and fit your selected model(s) to the training data. Each model training ideally in its own cell.
|
| 303 |
+
|
| 304 |
+
## Hyperparameter Tuning (Second-level heading markdown)
|
| 305 |
+
* **Think and decide** if hyperparameter tuning is necessary and if it aligns with weekly topics.
|
| 306 |
+
* **Examples of techniques:** Grid Search (`GridSearchCV`), Random Search (`RandomizedSearchCV`).
|
| 307 |
+
* **Considerations:** Cross-validation within tuning for robust estimates.
|
| 308 |
+
* **Code Cell(s):** Implement hyperparameter tuning if needed.
|
| 309 |
+
|
| 310 |
+
*(This section will have 4 markdown cells and 2-6 code cells. Total: 6-10 cells).*
|
| 311 |
+
* **NOTE:** All chosen models, splitting strategies, and tuning techniques must be covered in weekly topics. Justify decisions.
|
| 312 |
+
|
| 313 |
+
---
|
| 314 |
+
|
| 315 |
+
# 8. Model Evaluation (First-level heading markdown)
|
| 316 |
+
* Rigorously evaluate model performance and select the best one.
|
| 317 |
+
* **Crucially, select metrics that align with your problem's business goal.**
|
| 318 |
+
* Each distinct operation in its own cell.
|
| 319 |
+
|
| 320 |
+
## Metric Selection and Justification (Second-level heading markdown)
|
| 321 |
+
* **Think and decide** on the most appropriate evaluation metric(s).
|
| 322 |
+
* **Examples of metrics:**
|
| 323 |
+
* **Regression:** Mean Squared Error (MSE), Mean Absolute Error (MAE), R-squared.
|
| 324 |
+
* **Classification:** Accuracy, Precision, Recall, F1-score, ROC-AUC.
|
| 325 |
+
* Justify your choice of metrics.
|
| 326 |
+
* **Code Cell:** Display and explain the chosen metrics.
|
| 327 |
+
|
| 328 |
+
## Model Performance Evaluation (Second-level heading markdown)
|
| 329 |
+
* Evaluate the trained model(s) on the unseen test set.
|
| 330 |
+
* **Examples of visualizations:** Confusion Matrix, ROC Curve, Residual Plots.
|
| 331 |
+
* **Code Cell(s):** Make predictions, calculate metrics, and include relevant visualizations.
|
| 332 |
+
|
| 333 |
+
## Final Best Model Selection (Second-level heading markdown)
|
| 334 |
+
* If multiple models were trained, compare their performance across all chosen metrics.
|
| 335 |
+
* **Think and decide** on the definitive criteria to select the best performing model.
|
| 336 |
+
* **Code Cell:** Present comparison, identify the best model (and optionally save it).
|
| 337 |
+
|
| 338 |
+
*(This section will have 4 markdown cells and 3-5 code cells. Total: 7-10 cells).*
|
| 339 |
+
* **NOTE:** All evaluation methods and metrics must be covered in weekly topics.
|
| 340 |
+
|
| 341 |
+
---
|
| 342 |
+
|
| 343 |
+
# 9. Insights & Recommendations (First-level heading markdown)
|
| 344 |
+
* Keep this section empty.
|
| 345 |
+
* We will write insights based on outputs after the notebook is executed.
|
| 346 |
+
|
| 347 |
+
---
|
| 348 |
+
|
| 349 |
+
**FINAL NOTE:** This is your high-level reference, and in overall the entire notebook will consist of atleast 70 to 80 cells of all sections. You must **ONLY THINK, ANALYZE, AND DECIDE** based on the specific problem statement, dataset information, and the user's specific weekly subtopics. Do NOT go beyond the weekly topics. Every decision and every operation performed must be carefully considered and justified in the context of the specific problem given.
|
| 350 |
+
"""
|
| 351 |
+
|
| 352 |
+
|
| 353 |
+
|
| 354 |
+
Deep_Learning = """
|
| 355 |
+
# Deep Learning Task Notebook Structure and Content Guidelines
|
| 356 |
+
|
| 357 |
+
This outline guides the LLM to structure a Deep Learning task notebook. It's adaptable to **any problem statement** and **any dataset type** (tabular, image, text, or mixed). The LLM must **THINK, ANALYZE, AND DECIDE** which operations fit the specific case study, dataset, and weekly topics.
|
| 358 |
+
|
| 359 |
+
## 1. Problem Statement (First-level heading markdown)
|
| 360 |
+
## Business Context (Second-level heading markdown)
|
| 361 |
+
* Write the user’s Business context as is in a markdown cell.
|
| 362 |
+
|
| 363 |
+
## Objective (Second-level heading markdown)
|
| 364 |
+
* Write the user’s objective as is in a markdown cell.
|
| 365 |
+
|
| 366 |
+
## Data Dictionary (Second-level heading markdown)
|
| 367 |
+
* Write the user's data dictionary as is in a markdown cell.
|
| 368 |
+
* Enhance column descriptions with necessary details (e.g., units).
|
| 369 |
+
|
| 370 |
+
*(This section will have 6 markdown cells in total).*
|
| 371 |
+
|
| 372 |
+
---
|
| 373 |
+
|
| 374 |
+
# 2. Install and import the necessary libraries (First-level heading markdown)
|
| 375 |
+
## Install Dependencies (Second-level heading markdown)
|
| 376 |
+
* **Think and decide** on necessary libraries with specific versions (e.g., `tensorflow`, `torch`, `keras`, `numpy`, `pandas`, `matplotlib`, `seaborn`, `scikit-learn`).
|
| 377 |
+
* **Code Cell:** Install all dependencies in one cell.
|
| 378 |
+
|
| 379 |
+
## Import Libraries (Second-level heading markdown)
|
| 380 |
+
* Import all required libraries.
|
| 381 |
+
* Group imports with clear comments (e.g., "# Deep Learning Frameworks", "# Data manipulation", "# Visualization").
|
| 382 |
+
* Add comments for less common libraries.
|
| 383 |
+
* **Code Cell:** Import all libraries in one cell.
|
| 384 |
+
|
| 385 |
+
*(This section will have 2 markdown and 2 code cells).*
|
| 386 |
+
|
| 387 |
+
---
|
| 388 |
+
|
| 389 |
+
# 3. Loading the dataset (First-level heading markdown)
|
| 390 |
+
* DL datasets are diverse: tabular, image files, text documents, or mixed.
|
| 391 |
+
* **Think and decide** the best method to load your specific data type and source.
|
| 392 |
+
|
| 393 |
+
## Read Data from Path (Second-level heading markdown)
|
| 394 |
+
* Read data from the provided path.
|
| 395 |
+
* **Examples of operations:** `pd.read_csv` (tabular), `tf.keras.utils.image_dataset_from_directory` or `torchvision.datasets.ImageFolder` (images), custom loading for text files.
|
| 396 |
+
* **Code Cell:** Load data in one cell.
|
| 397 |
+
|
| 398 |
+
## Copy Data to New Variable and Preview (Second-level heading markdown)
|
| 399 |
+
* Copy data to a new variable (if applicable).
|
| 400 |
+
* Display a preview to confirm data integrity. For multiple datasets, **think and decide** how to display or merge them.
|
| 401 |
+
* **Examples of operations:** `df.head()` (tabular), showing sample image paths/labels (image), text snippets (text).
|
| 402 |
+
* **Code Cell:** Copy and preview data in one cell.
|
| 403 |
+
|
| 404 |
+
*(This section will have 2 markdown and 2 code cells).*
|
| 405 |
+
|
| 406 |
+
---
|
| 407 |
+
|
| 408 |
+
# 4. Data Overview and Preliminary Checks (First-level heading markdown)
|
| 409 |
+
* Analyze dataset inputs (type, path, data dictionary if applicable).
|
| 410 |
+
* **Think, reason, and decide** on preliminary operations to show data sanity and structure.
|
| 411 |
+
|
| 412 |
+
## Display Basic Dataset Information (Second-level heading markdown)
|
| 413 |
+
* Display initial information.
|
| 414 |
+
* **Examples of operations:** `df.head()`, `df.info()` (tabular); sample image file paths/dimensions (image); text snippets/lengths (text).
|
| 415 |
+
* **Code Cell:** Display info in one cell.
|
| 416 |
+
|
| 417 |
+
## Checking Data Characteristics (Second-level heading markdown)
|
| 418 |
+
* **Think and decide** what basic characteristics to check based on data type and problem.
|
| 419 |
+
* **Examples of checks:** `df.shape`, `df.dtypes`, `df.describe()` (tabular); image dimensions/channels, file integrity (image); text length distribution, vocabulary size (text).
|
| 420 |
+
* **Code Cell(s):** Each distinct check in its own cell.
|
| 421 |
+
|
| 422 |
+
## Preliminary Missing Values and Duplicates (Second-level heading markdown)
|
| 423 |
+
* **Think and decide** how to check for missing values and duplicates relevant to your data type.
|
| 424 |
+
* **Examples of checks:** `df.isnull().sum()`, `df.duplicated().sum()` (tabular); checking for empty/corrupt files (image/text).
|
| 425 |
+
* **Code Cell(s):** Display checks in one cell; handle in separate cells (if straightforward).
|
| 426 |
+
|
| 427 |
+
* Identify data quality issues (e.g., inconsistent entries, incorrect data types, corrupted files).
|
| 428 |
+
* For each column/sample, **think, analyze, and decide** which operations convert them to the correct format for further steps.
|
| 429 |
+
* Perform operations in separate cells. Ensure the dataset is preliminarily ready.
|
| 430 |
+
* **Code Cell(s):** Perform type conversions and other preliminary cleanups in separate cells.
|
| 431 |
+
|
| 432 |
+
*(This section will have 5-7 markdown cells and 6-10 code cells. Total: 11-17 cells).*
|
| 433 |
+
* **NOTE:** Focus on aspects relevant to DL preprocessing and model building. Do not introduce analysis beyond weekly topics.
|
| 434 |
+
|
| 435 |
+
---
|
| 436 |
+
|
| 437 |
+
# 5. Exploratory Data Analysis (EDA) (First-level heading markdown)
|
| 438 |
+
* **NOTE:** Do not introduce analysis beyond weekly topics.
|
| 439 |
+
* Focus on key visualizations to understand data distributions and relationships for DL model design. Aim for 5-8 plots.
|
| 440 |
+
|
| 441 |
+
## Univariate Analysis (Second-level heading markdown)
|
| 442 |
+
* **Think and decide** on up to 4 plots/analyses revealing individual feature characteristics.
|
| 443 |
+
* **Examples of operations:** Histograms, box plots (tabular); image dimension/aspect ratio distributions, pixel intensity histograms (image); text/sequence length distributions, word frequency plots (text).
|
| 444 |
+
* Justify why each plot helps answer business questions.
|
| 445 |
+
* **Code Cell(s):** Each plot/analysis in its own cell.
|
| 446 |
+
|
| 447 |
+
## Bivariate/Multivariate Analysis (Second-level heading markdown)
|
| 448 |
+
* **Think and decide** on up to 4 visualizations showing meaningful patterns or critical relationships between features.
|
| 449 |
+
* **Examples of operations:** Scatter plots, heatmaps (tabular); sample images from different classes (image); word clouds per class (text); relationships between mixed data types.
|
| 450 |
+
* Justify why each plot helps answer business questions.
|
| 451 |
+
* **Code Cell(s):** Each plot/analysis in its own cell.
|
| 452 |
+
|
| 453 |
+
*(This section will have 2 markdown cells and 8 code cells. Total: 10 cells).*
|
| 454 |
+
|
| 455 |
+
---
|
| 456 |
+
|
| 457 |
+
# 6. Data Preprocessing (First-level heading markdown)
|
| 458 |
+
* Transform raw data into a format suitable for Deep Learning models.
|
| 459 |
+
* **Think, analyze, and decide** on preprocessing steps based on data type and chosen DL architecture.
|
| 460 |
+
* Each distinct operation in its own cell.
|
| 461 |
+
|
| 462 |
+
## Handling Missing Values and Outliers (Second-level heading markdown)
|
| 463 |
+
* **Think and decide** if explicit handling of missing values or outliers is still needed (e.g., for tabular components, corrupted files).
|
| 464 |
+
* **Examples of operations:** Contextual imputation, removing corrupted samples.
|
| 465 |
+
* **Code Cell(s):** Implement the chosen strategy.
|
| 466 |
+
|
| 467 |
+
## Data Type-Specific Preprocessing (Second-level heading markdown)
|
| 468 |
+
* **Think and decide** on exact transformations for your specific data type(s).
|
| 469 |
+
|
| 470 |
+
### For Image Data
|
| 471 |
+
* **Think and decide** on transformations.
|
| 472 |
+
* **Examples of operations:** Resizing/Rescaling, Pixel Normalization, Data Augmentation (rotation, flipping, zooming), Channel Manipulation.
|
| 473 |
+
* **Code Cell(s):** Implement these image preprocessing steps.
|
| 474 |
+
|
| 475 |
+
### For Text Data
|
| 476 |
+
* **Think and decide** on text preprocessing steps.
|
| 477 |
+
* **Examples of operations:** Tokenization, Numericalization, Vocabulary Building, Sequence Padding/Truncation, Embedding Layer preparation.
|
| 478 |
+
* **Code Cell(s):** Implement these text preprocessing steps.
|
| 479 |
+
|
| 480 |
+
### For Tabular Data (if mixed or standalone)
|
| 481 |
+
* **Think and decide** if further transformations are needed.
|
| 482 |
+
* **Examples of operations:** Feature Scaling (Standardization/Normalization), Encoding Categorical Variables (One-hot, Label encoding).
|
| 483 |
+
* **Code Cell(s):** Implement these tabular data preprocessing steps.
|
| 484 |
+
|
| 485 |
+
## Data Loading Pipelines (Second-level heading markdown)
|
| 486 |
+
* **Think and decide** on efficient data pipelines for DL frameworks (e.g., `tf.data.Dataset`, `torch.utils.data.Dataset`/`DataLoader`).
|
| 487 |
+
* **Considerations:** Batching, shuffling, caching, prefetching.
|
| 488 |
+
* **Code Cell(s):** Implement your data loading pipeline.
|
| 489 |
+
|
| 490 |
+
*(This section will have 5-8 markdown cells and 8-15 code cells. Total: 13-23 cells).*
|
| 491 |
+
* **NOTE:** Justify each step by its relevance to the DL architecture and problem.
|
| 492 |
+
|
| 493 |
+
---
|
| 494 |
+
|
| 495 |
+
# 7. Model Building (First-level heading markdown)
|
| 496 |
+
* Define and train your Deep Learning models.
|
| 497 |
+
* **Think, analyze, and decide** on the most appropriate DL architecture based on problem, data, and objective.
|
| 498 |
+
* Each distinct operation in its own cell.
|
| 499 |
+
|
| 500 |
+
## Train-Validation-Test Split (Second-level heading markdown)
|
| 501 |
+
* **Think and decide** on the splitting strategy (e.g., 70-15-15).
|
| 502 |
+
* **Considerations:** `random_state`, stratification for classification.
|
| 503 |
+
* **Code Cell:** Implement data split.
|
| 504 |
+
|
| 505 |
+
## Model Architecture Definition (Second-level heading markdown)
|
| 506 |
+
* **Think and decide** on the most suitable DL architecture for your data type and problem.
|
| 507 |
+
* **Examples of architectures:** Dense Neural Networks (DNNs), Convolutional Neural Networks (CNNs), Recurrent Neural Networks (RNNs - LSTMs/GRUs).
|
| 508 |
+
* **Considerations:** Number of layers, neurons, activation functions, regularization (Dropout), output layer.
|
| 509 |
+
* **Code Cell:** Define your DL model architecture.
|
| 510 |
+
|
| 511 |
+
## Compile Model (Second-level heading markdown)
|
| 512 |
+
* **Think and decide** on the appropriate loss function and optimizer.
|
| 513 |
+
* **Examples of choices:** `CategoricalCrossentropy`, `MeanSquaredError`, `Adam` optimizer.
|
| 514 |
+
* **Code Cell:** Compile the model.
|
| 515 |
+
|
| 516 |
+
## Model Training (Second-level heading markdown)
|
| 517 |
+
* Train the compiled DL model.
|
| 518 |
+
* **Key Parameters:** Epochs, Batch Size, Callbacks (e.g., `EarlyStopping`, `ModelCheckpoint`, `ReduceLROnPlateau`).
|
| 519 |
+
* **Code Cell:** Train the model.
|
| 520 |
+
|
| 521 |
+
*(This section will have 4 markdown cells and 4-6 code cells. Total: 8-10 cells).*
|
| 522 |
+
* **NOTE:** All choices must align with weekly topics. Justify decisions based on problem and data.
|
| 523 |
+
|
| 524 |
+
---
|
| 525 |
+
|
| 526 |
+
# 8. Model Evaluation (First-level heading markdown)
|
| 527 |
+
* Rigorously evaluate model performance.
|
| 528 |
+
* **Crucially, select metrics that align with your problem's business goal.**
|
| 529 |
+
* Each distinct operation in its own cell.
|
| 530 |
+
|
| 531 |
+
## Metric Selection and Justification (Second-level heading markdown)
|
| 532 |
+
* **Think and decide** on the most appropriate evaluation metric(s).
|
| 533 |
+
* **Examples of metrics:** Accuracy, Precision, Recall, F1-score, ROC-AUC (classification); MSE, MAE, R-squared (regression); Perplexity (language models).
|
| 534 |
+
* Justify your choice of metrics.
|
| 535 |
+
* **Code Cell:** Display and explain metrics.
|
| 536 |
+
|
| 537 |
+
## Model Performance Evaluation (Second-level heading markdown)
|
| 538 |
+
* Evaluate on the unseen test set.
|
| 539 |
+
* **Examples of operations:** Plotting training/validation loss/metric curves, visualizing sample predictions/outputs (images/text), displaying confusion matrix (classification).
|
| 540 |
+
* **Code Cell(s):** Make predictions and calculate/visualize metrics.
|
| 541 |
+
|
| 542 |
+
## Final Best Model Selection (Second-level heading markdown)
|
| 543 |
+
* If multiple models/configurations were trained, **think, analyze, and decide** on criteria to select the best.
|
| 544 |
+
* Compare performance across chosen metrics.
|
| 545 |
+
* **Code Cell:** Present comparison and identify the best model (and optionally save it).
|
| 546 |
+
|
| 547 |
+
*(This section will have 3 markdown cells and 3-5 code cells. Total: 6-8 cells).*
|
| 548 |
+
* **NOTE:** All methods/metrics must be covered in weekly topics.
|
| 549 |
+
|
| 550 |
+
---
|
| 551 |
+
|
| 552 |
+
# 9. Insights & Recommendations (First-level heading markdown)
|
| 553 |
+
* Keep this section empty.
|
| 554 |
+
* We will write insights based on outputs after the notebook is executed.
|
| 555 |
+
|
| 556 |
+
---
|
| 557 |
+
|
| 558 |
+
**FINAL NOTE:** This is your high-level reference, and in overall the entire notebook will consist of atleast 70 to 80 cells of all sections. You must **ONLY THINK, ANALYZE, AND DECIDE** based on the specific problem statement, dataset information (including its type – tabular, image, text, or mixed), and the user's specific weekly subtopics. Do NOT go beyond the weekly topics. Every decision and every operation performed must be carefully considered and justified in the context of the specific Deep Learning problem given.
|
| 559 |
+
"""
|
| 560 |
+
|
| 561 |
+
|
| 562 |
+
Natural_Language_Processing = """
|
| 563 |
+
# Natural Language Processing Task Notebook Structure and Content Guidelines
|
| 564 |
+
|
| 565 |
+
This outline guides the LLM to structure a Deep Learning task notebook. It's adaptable to **any NLP problem** (e.g., text classification, summarization, NER, translation) and **any text dataset format** (e.g., CSV text columns, raw files, PDF/web extractions). The LLM must **THINK, ANALYZE, AND DECIDE** which operations fit the specific case study, dataset, and weekly topics.
|
| 566 |
+
|
| 567 |
+
## 1. Problem Statement (First-level heading markdown)
|
| 568 |
+
## Business Context (Second-level heading markdown)
|
| 569 |
+
* Write the user’s Business context as is in a separate markdown cell.
|
| 570 |
+
|
| 571 |
+
## Objective (Second-level heading markdown)
|
| 572 |
+
* Write the user's objective as is in a separate markdown cell.
|
| 573 |
+
|
| 574 |
+
## Data Dictionary (Second-level heading markdown)
|
| 575 |
+
* Write the user's data dictionary as is in a markdown cell.
|
| 576 |
+
* Enhance column descriptions with necessary details (e.g., units, specific formats).
|
| 577 |
+
|
| 578 |
+
*(This section will have 6 markdown cells in total).*
|
| 579 |
+
|
| 580 |
+
---
|
| 581 |
+
|
| 582 |
+
# 2. Install and import the necessary libraries (First-level heading markdown)
|
| 583 |
+
## Install Dependencies (Second-level heading markdown)
|
| 584 |
+
* **Think and decide** on necessary libraries with specific versions (e.g., `transformers`, `nltk`, `spacy`, `tensorflow`/`torch`).
|
| 585 |
+
* **Code Cell:** Install all dependencies in one cell.
|
| 586 |
+
|
| 587 |
+
## Import Libraries (Second-level heading markdown)
|
| 588 |
+
* Import all required libraries.
|
| 589 |
+
* Group imports with clear comments (e.g., "# Data manipulation", "# NLP tools").
|
| 590 |
+
* Add comments for less common libraries.
|
| 591 |
+
* **Code Cell:** Import all libraries in one cell.
|
| 592 |
+
|
| 593 |
+
*(This section will have 2 markdown and 2 code cells).*
|
| 594 |
+
|
| 595 |
+
---
|
| 596 |
+
|
| 597 |
+
# 3. Loading the dataset (First-level heading markdown)
|
| 598 |
+
* NLP datasets are diverse: text in CSVs, raw `.txt` files, text from PDFs/web pages.
|
| 599 |
+
* **Think and decide** the best method to load your specific data type and source.
|
| 600 |
+
|
| 601 |
+
## Read Data from Path/Source (Second-level heading markdown)
|
| 602 |
+
* Read data from the provided path/source.
|
| 603 |
+
* **Examples of operations:** `pd.read_csv` (tabular with text), iterating `.txt` files, PDF text extraction, web scraping libraries.
|
| 604 |
+
* **Code Cell:** Load data in one cell.
|
| 605 |
+
|
| 606 |
+
## Copy Data to New Variable and Preview (Second-level heading markdown)
|
| 607 |
+
* Copy data to a new variable (if applicable).
|
| 608 |
+
* Display a preview to confirm data integrity.
|
| 609 |
+
* **Examples of operations:** `df.head()`, sample text snippets with labels.
|
| 610 |
+
* **Code Cell:** Copy and preview data in one cell.
|
| 611 |
+
|
| 612 |
+
*(This section will have 2 markdown and 2 code cells).*
|
| 613 |
+
|
| 614 |
+
---
|
| 615 |
+
|
| 616 |
+
# 4. Data Overview and Preliminary Checks (First-level heading markdown)
|
| 617 |
+
* Analyze your text data: number of samples, text lengths, initial vocabulary, special characters, class distribution (if classification).
|
| 618 |
+
* **Think, reason, and decide** on preliminary operations to show initial data sanity and structure.
|
| 619 |
+
|
| 620 |
+
## Display Basic Dataset Information (Second-level heading markdown)
|
| 621 |
+
* Display initial information.
|
| 622 |
+
* **Examples of operations:** `df.info()` (if tabular), sample raw texts, total sample count.
|
| 623 |
+
* **Code Cell:** Display info in one cell.
|
| 624 |
+
|
| 625 |
+
## Checking Text Characteristics (Second-level heading markdown)
|
| 626 |
+
* **Think and decide** what basic text characteristics to check based on your problem.
|
| 627 |
+
* **Examples of checks:** Text length distribution (histograms, descriptive stats), top N most frequent raw words, unique character sets, encoding issues.
|
| 628 |
+
* **Code Cell(s):** Each distinct check in its own cell.
|
| 629 |
+
|
| 630 |
+
## Preliminary Missing Values and Duplicates (Second-level heading markdown)
|
| 631 |
+
* **Think and decide** how to check for and handle missing text entries or duplicate samples.
|
| 632 |
+
* **Examples of checks:** `df.isnull().sum()`, `df.duplicated().sum()` (tabular); checking for empty/corrupt text files.
|
| 633 |
+
* **Code Cell(s):** Display checks in one cell; handle issues in separate cells (if straightforward).
|
| 634 |
+
|
| 635 |
+
* Identify data quality issues (e.g., inconsistent formatting, irrelevant boilerplate, erroneous characters).
|
| 636 |
+
* **Think, analyze, and decide** which initial cleaning/conversion operations are most suitable.
|
| 637 |
+
* Perform all chosen operations in separate cells. Ensure the dataset is preliminarily ready.
|
| 638 |
+
* **Code Cell(s):** Perform initial cleaning operations in separate cells.
|
| 639 |
+
|
| 640 |
+
*(This section will have 5-7 markdown cells and 6-10 code cells. Total: 11-17 cells).*
|
| 641 |
+
* **NOTE:** Focus on text-specific insights relevant to preprocessing and modeling.
|
| 642 |
+
|
| 643 |
+
---
|
| 644 |
+
|
| 645 |
+
# 5. Exploratory Data Analysis (EDA) (First-level heading markdown)
|
| 646 |
+
* **NOTE:** Do not introduce analysis beyond weekly topics.
|
| 647 |
+
* Focus on key text visualizations to understand textual properties, patterns, and relationships. Aim for 5-8 plots.
|
| 648 |
+
|
| 649 |
+
## Univariate Analysis (Second-level heading markdown)
|
| 650 |
+
* **Think and decide** on up to 4 plots/analyses revealing individual text feature characteristics.
|
| 651 |
+
* **Examples of operations:** Histograms of text lengths (word/character count), bar charts of most frequent unigrams/bigrams.
|
| 652 |
+
* Justify why each plot helps answer business questions.
|
| 653 |
+
* **Code Cell(s):** Each plot/analysis in its own cell.
|
| 654 |
+
|
| 655 |
+
## Bivariate/Multivariate Analysis (Second-level heading markdown)
|
| 656 |
+
* **Think and decide** on up to 4 visualizations showing meaningful patterns or critical relationships within the text.
|
| 657 |
+
* **Examples of operations:** Word clouds or bar plots of top N-grams specific to different classes, relationship between text length and target variable.
|
| 658 |
+
* Justify why each plot helps address business questions or inform model design.
|
| 659 |
+
* **Code Cell(s):** Each plot/analysis in its own cell.
|
| 660 |
+
|
| 661 |
+
*(This section will have 2 markdown cells and 8 code cells. Total: 10 cells).*
|
| 662 |
+
|
| 663 |
+
---
|
| 664 |
+
|
| 665 |
+
# 6. Data Preprocessing (First-level heading markdown)
|
| 666 |
+
* Transform raw text data into a numerical format suitable for ML/DL models.
|
| 667 |
+
* **Think, analyze, and decide** on preprocessing steps based on text data, NLP task, and model requirements.
|
| 668 |
+
* Each distinct operation in its own cell.
|
| 669 |
+
|
| 670 |
+
## Text Extraction and Initial Cleaning (Second-level heading markdown)
|
| 671 |
+
* If needed, **think and decide** on robust steps to extract clean, plain text from complex sources (e.g., PDF, HTML).
|
| 672 |
+
* **Think and decide** on initial cleaning steps.
|
| 673 |
+
* **Examples of operations:** Lowercasing, punctuation handling, number handling, special character removal, URL/HTML tag removal.
|
| 674 |
+
* **Code Cell(s):** Implement chosen text extraction and initial cleaning.
|
| 675 |
+
|
| 676 |
+
## Text Normalization and Stop Word Removal (Second-level heading markdown)
|
| 677 |
+
* **Think and decide** if normalization (stemming/lemmatization) is beneficial.
|
| 678 |
+
* **Think and decide** if stop word removal is appropriate for your specific NLP task.
|
| 679 |
+
* **Code Cell(s):** Implement chosen normalization and/or stop word removal.
|
| 680 |
+
|
| 681 |
+
## Tokenization (Second-level heading markdown)
|
| 682 |
+
* Break raw text into meaningful units (tokens).
|
| 683 |
+
* **Think and decide** on the most suitable tokenization strategy (word, sentence, subword, or character-level).
|
| 684 |
+
* **Code Cell(s):** Implement chosen tokenization method.
|
| 685 |
+
|
| 686 |
+
## Numerical Representation (Vectorization/Embedding) (Second-level heading markdown)
|
| 687 |
+
* Convert textual tokens into numerical representations. This is a key decision.
|
| 688 |
+
* **Think and decide** on the most appropriate strategy based on problem, data, and model.
|
| 689 |
+
* **Examples of operations:** Bag-of-Words (CountVectorizer, TfidfVectorizer), static word embeddings (Word2Vec, GloVe, FastText), or contextual embeddings (from Transformers).
|
| 690 |
+
* **Code Cell(s):** Implement chosen text vectorization or embedding generation/loading.
|
| 691 |
+
|
| 692 |
+
## Sequence Handling (Second-level heading markdown)
|
| 693 |
+
* For sequence-based DL models, **think and decide** on strategies for varying sequence lengths.
|
| 694 |
+
* **Examples of operations:** Padding, truncation, masking.
|
| 695 |
+
* **Code Cell(s):** Implement sequence padding/truncation and masking if needed.
|
| 696 |
+
|
| 697 |
+
## Data Loading Pipelines (Second-level heading markdown)
|
| 698 |
+
* **Think and decide** on efficient data pipelines for batching preprocessed data to DL frameworks.
|
| 699 |
+
* **Considerations:** Batching, shuffling, caching, prefetching.
|
| 700 |
+
* **Code Cell(s):** Implement your data loading pipeline.
|
| 701 |
+
|
| 702 |
+
*(This section will have 6-9 markdown cells and 10-18 code cells. Total: 16-27 cells).*
|
| 703 |
+
* **NOTE:** Justify each preprocessing step by its relevance to the NLP task and model.
|
| 704 |
+
|
| 705 |
+
---
|
| 706 |
+
|
| 707 |
+
# 7. Model Building (First-level heading markdown)
|
| 708 |
+
* Define and train your NLP models.
|
| 709 |
+
* **Think, analyze, and decide** on the most appropriate model architecture based on problem, data, and objective.
|
| 710 |
+
* Each distinct operation in its own cell.
|
| 711 |
+
|
| 712 |
+
## Train-Validation-Test Split (Second-level heading markdown)
|
| 713 |
+
* **Think and decide** on the most appropriate strategy for splitting your processed dataset.
|
| 714 |
+
* **Key Considerations:** Split ratios, `random_state`, stratification (for classification).
|
| 715 |
+
* **Code Cell:** Implement the data split.
|
| 716 |
+
|
| 717 |
+
## Model Architecture Definition (Second-level heading markdown)
|
| 718 |
+
* **Think and decide** on the most suitable DL architecture for your NLP task.
|
| 719 |
+
* **Examples of architectures:** Recurrent Neural Networks (LSTMs/GRUs), 1D CNNs, Attention Mechanisms, Transformer-based Architectures.
|
| 720 |
+
* **Considerations:** Layers, neurons, activation functions, regularization, output layer.
|
| 721 |
+
* **Code Cell:** Define your NLP model architecture.
|
| 722 |
+
|
| 723 |
+
## Compile Model (Second-level heading markdown)
|
| 724 |
+
* **Think and decide** on the appropriate loss function and optimizer.
|
| 725 |
+
* **Examples of choices:** `CategoricalCrossentropy`, `BinaryCrossentropy`, `Adam` optimizer.
|
| 726 |
+
* **Code Cell:** Compile the defined NLP model.
|
| 727 |
+
|
| 728 |
+
## Model Training (Second-level heading markdown)
|
| 729 |
+
* Train your compiled NLP model.
|
| 730 |
+
* **Key Parameters:** Epochs, Batch Size, Callbacks (e.g., `EarlyStopping`, `ModelCheckpoint`, `ReduceLROnPlateau`).
|
| 731 |
+
* **Code Cell:** Train the model.
|
| 732 |
+
|
| 733 |
+
## Transfer Learning (Second-level heading markdown)
|
| 734 |
+
* **Think, analyze, and decide** if transfer learning is applicable and beneficial for your NLP task.
|
| 735 |
+
* **Examples of operations:**
|
| 736 |
+
* Using pre-trained word embeddings (Word2Vec, GloVe).
|
| 737 |
+
* Fine-tuning pre-trained **Hugging Face Transformer models** (e.g., BERT, RoBERTa) for specific tasks.
|
| 738 |
+
* Using **Sentence Transformers** for semantic similarity.
|
| 739 |
+
* Employing **prompt-based models** (e.g., GPT-style) for text generation/few-shot learning.
|
| 740 |
+
* If applicable, explain rationale and implementation.
|
| 741 |
+
* **Code Cell(s):** Implement transfer learning.
|
| 742 |
+
|
| 743 |
+
*(This section will have 5-6 markdown cells and 10-15 code cells. Total: 20-25 cells).*
|
| 744 |
+
* **NOTE:** All choices must align with weekly topics. Justify decisions based on problem and data.
|
| 745 |
+
|
| 746 |
+
---
|
| 747 |
+
|
| 748 |
+
# 8. Model Evaluation (First-level heading markdown)
|
| 749 |
+
* Rigorously evaluate model performance.
|
| 750 |
+
* **Crucially, select metrics that align with your NLP problem's business goal.**
|
| 751 |
+
* Each distinct operation in its own cell.
|
| 752 |
+
|
| 753 |
+
## Metric Selection and Justification (Second-level heading markdown)
|
| 754 |
+
* **Think and decide** on the most appropriate evaluation metric(s).
|
| 755 |
+
* **Examples of metrics:**
|
| 756 |
+
* **Classification/Tagging:** Accuracy, Precision, Recall, F1-score, ROC-AUC, Classification Report.
|
| 757 |
+
* **Generation/Summarization/Translation:** BLEU, ROUGE, Perplexity.
|
| 758 |
+
* Justify your choice of metrics.
|
| 759 |
+
* **Code Cell:** Display and explain the chosen metrics.
|
| 760 |
+
|
| 761 |
+
## Model Performance Evaluation (Second-level heading markdown)
|
| 762 |
+
* Evaluate the trained NLP model(s) on the unseen test set.
|
| 763 |
+
* **Examples of operations:** Plotting training/validation loss/metric curves, visualizing sample predictions/generated text, displaying confusion matrix.
|
| 764 |
+
* **Code Cell(s):** Make predictions and calculate/visualize metrics.
|
| 765 |
+
|
| 766 |
+
## Final Best Model Selection (Second-level heading markdown)
|
| 767 |
+
* If multiple models/configurations were trained, compare their performance.
|
| 768 |
+
* **Think and decide** on the definitive criteria to select the best performing model.
|
| 769 |
+
* **Code Cell:** Present comparison, identify the best model (and optionally save it).
|
| 770 |
+
|
| 771 |
+
*(This section will have 4 markdown cells and 4-8 code cells. Total: 8-12 cells).*
|
| 772 |
+
* **NOTE:** All evaluation methods and metrics must be covered in weekly topics.
|
| 773 |
+
|
| 774 |
+
---
|
| 775 |
+
|
| 776 |
+
# 9. Insights & Recommendations (First-level heading markdown)
|
| 777 |
+
* Keep this section empty.
|
| 778 |
+
* We will write insights based on outputs after the notebook is executed.
|
| 779 |
+
|
| 780 |
+
---
|
| 781 |
+
|
| 782 |
+
**FINAL NOTE:** This is your high-level reference. You must **ONLY THINK, ANALYZE, AND DECIDE** based on the specific problem statement, dataset information (including its type – tabular, image, text, or mixed), and the user's specific weekly subtopics. Do NOT go beyond the weekly topics. Every decision and every operation performed must be carefully considered and justified in the context of the specific Deep Learning problem given.
|
| 783 |
+
"""
|
| 784 |
+
|
| 785 |
+
Computer_Vision = """
|
| 786 |
+
# Computer Vision Task Notebook Structure and Content Guidelines
|
| 787 |
+
|
| 788 |
+
This outline guides the LLM to structure a Computer Vision task notebook. It's adaptable to **various CV problems** (e.g., image classification, object detection, image segmentation) and **common CV dataset types** (e.g., directories of image files, images with JSON/XML annotations, TFRecords). The LLM must **THINK, ANALYZE, AND DECIDE** which operations fit the specific case study, dataset, and weekly topics.
|
| 789 |
+
|
| 790 |
+
## 1. Problem Statement (First-level heading markdown)
|
| 791 |
+
## Business Context (Second-level heading markdown)
|
| 792 |
+
* Write the user’s Business context as is in a markdown cell.
|
| 793 |
+
|
| 794 |
+
## Objective (Second-level heading markdown)
|
| 795 |
+
* Write the user's objective as is in a markdown cell.
|
| 796 |
+
|
| 797 |
+
## Data Dictionary (Second-level heading markdown)
|
| 798 |
+
* Write the user's data dictionary as is in a markdown cell.
|
| 799 |
+
* Enhance column/label descriptions with necessary details (e.g., class meanings, annotation formats).
|
| 800 |
+
|
| 801 |
+
*(This section will have 6 markdown cells in total).*
|
| 802 |
+
|
| 803 |
+
---
|
| 804 |
+
|
| 805 |
+
# 2. Install and import the necessary libraries (First-level heading markdown)
|
| 806 |
+
## Install Dependencies (Second-level heading markdown)
|
| 807 |
+
* **Think and decide** on necessary libraries with specific versions (e.g., `tensorflow`/`torch`, `keras`, `opencv-python` (cv2), `scikit-image`, `Pillow`, `tqdm`, `albumentations` for augmentation).
|
| 808 |
+
* **Code Cell:** Install all dependencies in one cell.
|
| 809 |
+
|
| 810 |
+
## Import Libraries (Second-level heading markdown)
|
| 811 |
+
* Import all required libraries.
|
| 812 |
+
* Group imports with clear comments (e.g., "# DL Frameworks", "# Image Processing", "# Visualization").
|
| 813 |
+
* Add comments for less common libraries.
|
| 814 |
+
* **Code Cell:** Import all libraries in one cell.
|
| 815 |
+
|
| 816 |
+
*(This section will have 2 markdown and 2 code cells).*
|
| 817 |
+
|
| 818 |
+
---
|
| 819 |
+
|
| 820 |
+
# 3. Loading the dataset (First-level heading markdown)
|
| 821 |
+
* CV datasets consist of image files (e.g., `.jpg`, `.png`), often with accompanying annotation files (e.g., `.json`, `.xml`, mask images), directory or Zip file of directories structure defining classes.
|
| 822 |
+
* **Think and decide** the best method to load your specific image data and annotations.
|
| 823 |
+
|
| 824 |
+
## Read Data from Path/Source (Second-level heading markdown)
|
| 825 |
+
* Read image data and (if applicable) annotations from the provided path/structure.
|
| 826 |
+
* **Examples of operations:** `tf.keras.utils.image_dataset_from_directory` or `torchvision.datasets.ImageFolder` (classification), custom loops for reading image files and parsing annotation files (detection/segmentation).
|
| 827 |
+
* **Code Cell:** Load data in one cell.
|
| 828 |
+
|
| 829 |
+
## Copy Data to New Variable and Preview (Second-level heading markdown)
|
| 830 |
+
* Copy data to a new variable (if applicable, e.g., list of image paths, dataframe of annotations).
|
| 831 |
+
* Display a preview to confirm data integrity.
|
| 832 |
+
* **Examples of operations:** Displaying a few sample images, showing image paths and corresponding labels/annotations.
|
| 833 |
+
* **Code Cell:** Copy and preview data in one cell.
|
| 834 |
+
|
| 835 |
+
*(This section will have 2 markdown and 2 code cells).*
|
| 836 |
+
|
| 837 |
+
---
|
| 838 |
+
|
| 839 |
+
# 4. Data Overview and Preliminary Checks (First-level heading markdown)
|
| 840 |
+
* Analyze your image dataset: total images, typical dimensions, channels, file formats, class distribution, potential corruption, annotation format.
|
| 841 |
+
* **Think, reason, and decide** on preliminary operations to show data sanity and structure.
|
| 842 |
+
|
| 843 |
+
## Display Basic Dataset Information (Second-level heading markdown)
|
| 844 |
+
* Display initial information about the dataset.
|
| 845 |
+
* **Examples of operations:** Total number of images, number of classes (if classification), paths to a few sample images, basic info on image file types.
|
| 846 |
+
* **Code Cell:** Display info in one cell.
|
| 847 |
+
|
| 848 |
+
## Checking Image Characteristics (Second-level heading markdown)
|
| 849 |
+
* **Think and decide** what basic image characteristics need to be checked based on your problem.
|
| 850 |
+
* **Examples of checks:** Distribution of image dimensions (width/height/aspect ratio), channel distribution (RGB/Grayscale), check for corrupted/unreadable image files, summary statistics of pixel values (e.g., mean/std across channels).
|
| 851 |
+
* **Code Cell(s):** Each distinct check in its own cell.
|
| 852 |
+
|
| 853 |
+
## Preliminary Missing/Corrupt Samples and Duplicates (Second-level heading markdown)
|
| 854 |
+
* **Think and decide** how to check for and handle missing/corrupted image files or duplicate images/annotations.
|
| 855 |
+
* **Examples of checks:** Iterating through file paths to catch read errors, hashing images to find duplicates, checking for empty/malformed annotation files.
|
| 856 |
+
* **Code Cell(s):** Display checks in one cell; handle issues in separate cells (if straightforward).
|
| 857 |
+
|
| 858 |
+
* Identify any data quality issues (e.g., inconsistent image sizes, incorrect labels/annotations, irrelevant images).
|
| 859 |
+
* **Think, analyze, and decide** which initial cleaning/conversion operations are most suitable.
|
| 860 |
+
* Perform all chosen operations in separate cells. Ensure the dataset is preliminarily ready.
|
| 861 |
+
* **Code Cell(s):** Perform initial quality checks and cleanups in separate cells.
|
| 862 |
+
|
| 863 |
+
*(This section will have 5-7 markdown cells and 6-10 code cells. Total: 11-17 cells).*
|
| 864 |
+
* **NOTE:** Focus on image-specific insights relevant to preprocessing and model building.
|
| 865 |
+
|
| 866 |
+
---
|
| 867 |
+
|
| 868 |
+
# 5. Exploratory Data Analysis (EDA) (First-level heading markdown)
|
| 869 |
+
* **NOTE:** Do not introduce analysis beyond weekly topics.
|
| 870 |
+
* Focus on key visualizations to understand image properties, label distributions, and (if applicable) annotation patterns. Aim for 5-8 plots/visualizations.
|
| 871 |
+
|
| 872 |
+
## Univariate Analysis (Second-level heading markdown)
|
| 873 |
+
* **Think and decide** on up to 4 plots/analyses revealing individual image or label characteristics.
|
| 874 |
+
* **Examples of operations:** Bar chart of class distribution, histograms of image dimensions (width, height), pixel intensity histograms for sample images (e.g., mean pixel value distribution).
|
| 875 |
+
* Justify why each visualization helps answer business questions or inform model design.
|
| 876 |
+
* **Code Cell(s):** Each plot/analysis in its own cell.
|
| 877 |
+
|
| 878 |
+
## Bivariate/Multivariate Analysis (Second-level heading markdown)
|
| 879 |
+
* **Think and decide** on up to 4 visualizations showing relationships or patterns across multiple dimensions.
|
| 880 |
+
* **Examples of operations:** Displaying sample images from different classes, visualizing bounding boxes/masks on images (for detection/segmentation), average image per class, relationships between image properties and labels.
|
| 881 |
+
* Justify why each visualization helps uncover deeper insights relevant to the CV task.
|
| 882 |
+
* **Code Cell(s):** Each plot/analysis in its own cell.
|
| 883 |
+
|
| 884 |
+
*(This section will have 2 markdown cells and 8 code cells. Total: 10 cells).*
|
| 885 |
+
|
| 886 |
+
---
|
| 887 |
+
|
| 888 |
+
# 6. Data Preprocessing (First-level heading markdown)
|
| 889 |
+
* Transform raw image data into a format suitable for Deep Learning models.
|
| 890 |
+
* **Think, analyze, and decide** on preprocessing steps based on image data characteristics, CV task, and chosen model requirements.
|
| 891 |
+
* Each distinct operation must be in its own separate code cell.
|
| 892 |
+
|
| 893 |
+
## Image-Specific Transformations (Second-level heading markdown)
|
| 894 |
+
* **Think and decide** on necessary transformations for your specific image data.
|
| 895 |
+
* **Examples of operations:**
|
| 896 |
+
* **Resizing/Rescaling:** Standardizing image dimensions to match model input.
|
| 897 |
+
* **Pixel Normalization:** Scaling pixel values (e.g., to [0, 1] or [-1, 1]).
|
| 898 |
+
* **Channel Manipulation:** Converting to grayscale, ensuring consistent channel count (e.g., 3 channels for RGB).
|
| 899 |
+
* **Data Augmentation:** Applying transformations like rotation, flipping, zooming, brightness/contrast adjustments to increase dataset size and improve model generalization (crucial for DL in CV).
|
| 900 |
+
* **Code Cell(s):** Implement these image preprocessing steps.
|
| 901 |
+
|
| 902 |
+
## Annotation Preprocessing (if applicable) (Second-level heading markdown)
|
| 903 |
+
* For object detection or segmentation, **think and decide** on necessary annotation processing.
|
| 904 |
+
* **Examples of operations:**
|
| 905 |
+
* Parsing raw annotations (XML, JSON) into structured formats.
|
| 906 |
+
* Resizing/scaling bounding box coordinates or mask dimensions to match resized images.
|
| 907 |
+
* Converting mask images to appropriate tensor formats.
|
| 908 |
+
* Handling class IDs and one-hot encoding if needed.
|
| 909 |
+
* **Code Cell(s):** Implement annotation preprocessing steps.
|
| 910 |
+
|
| 911 |
+
## Data Loading Pipelines (Second-level heading markdown)
|
| 912 |
+
* **Think and decide** on the most efficient way to create data pipelines for loading and batching your preprocessed image data for Deep Learning frameworks.
|
| 913 |
+
* **Key Considerations:** Batching, shuffling, caching, prefetching, parallel processing to optimize training speed.
|
| 914 |
+
* **Code Cell(s):** Implement your data loading pipeline (e.g., `tf.data.Dataset`, `torch.utils.data.Dataset` and `DataLoader`).
|
| 915 |
+
|
| 916 |
+
*(This section will have 3-5 markdown cells and 10-15 code cells. Total: 15-20 cells).*
|
| 917 |
+
* **NOTE:** Justify each preprocessing step by explaining how it prepares the data for specific DL architectures and aligns with the problem objective.
|
| 918 |
+
|
| 919 |
+
---
|
| 920 |
+
|
| 921 |
+
# 7. Model Building (First-level heading markdown)
|
| 922 |
+
* Define and train your Computer Vision models.
|
| 923 |
+
* **Think, analyze, and decide** on the most appropriate Deep Learning architecture(s) based on the CV problem, data, and objective.
|
| 924 |
+
* Each distinct operation in its own cell.
|
| 925 |
+
|
| 926 |
+
## Train-Validation-Test Split (Second-level heading markdown)
|
| 927 |
+
* **Think and decide** on the most appropriate strategy for splitting your processed dataset into training, validation, and testing sets.
|
| 928 |
+
* **Key Considerations:** Choose appropriate split ratios, use `random_state` for reproducibility, decide if stratification is necessary (especially for imbalanced classification tasks).
|
| 929 |
+
* **Code Cell:** Implement the data split.
|
| 930 |
+
|
| 931 |
+
## Model Architecture Definition (Second-level heading markdown)
|
| 932 |
+
* **Think and decide** on the most suitable Deep Learning architecture for your CV task.
|
| 933 |
+
* **Examples of architectures:**
|
| 934 |
+
* **Image Classification:** Convolutional Neural Networks (CNNs), pre-trained models from Keras Applications (e.g., ResNet, VGG, EfficientNet) or PyTorch Models.
|
| 935 |
+
* **Object Detection:** Architectures like YOLO, Faster R-CNN (often involve pre-trained backbones).
|
| 936 |
+
* **Image Segmentation:** U-Net, FCN (Fully Convolutional Networks).
|
| 937 |
+
* **Considerations:** Number of layers, activation functions, regularization (Dropout, Batch Normalization), output layer matching the problem type.
|
| 938 |
+
* **Code Cell:** Define your CV model architecture.
|
| 939 |
+
|
| 940 |
+
## Compile Model (Second-level heading markdown)
|
| 941 |
+
* **Think and decide** on the appropriate loss function and optimizer for your specific CV problem.
|
| 942 |
+
* **Loss Functions:** `CategoricalCrossentropy`, `BinaryCrossentropy` (classification); `MeanSquaredError` (regression); `Dice Loss`, `IoU Loss` (segmentation); specialized losses for object detection.
|
| 943 |
+
* **Optimizers:** `Adam`, `SGD` with momentum, `RMSprop`.
|
| 944 |
+
* **Code Cell:** Compile the defined Deep Learning model.
|
| 945 |
+
|
| 946 |
+
## Model Training (Second-level heading markdown)
|
| 947 |
+
* Train your compiled Deep Learning model on the training data, using the validation set for monitoring performance.
|
| 948 |
+
* **Key Training Parameters:** Epochs, Batch Size, Callbacks (e.g., `EarlyStopping`, `ModelCheckpoint`, `ReduceLROnPlateau`).
|
| 949 |
+
* **Code Cell:** Write the code to train your Deep Learning model.
|
| 950 |
+
|
| 951 |
+
*(This section will have 4 markdown cells and 15-20 code cells. Total: 20-25 cells).*
|
| 952 |
+
* **NOTE:** All chosen architectures, splitting strategies, and training parameters must be covered in weekly topics. Justify all decisions based on the specific CV problem and data.
|
| 953 |
+
|
| 954 |
+
---
|
| 955 |
+
|
| 956 |
+
# 8. Model Evaluation (First-level heading markdown)
|
| 957 |
+
* Rigorously evaluate the performance of your trained Computer Vision model(s).
|
| 958 |
+
* It is crucial to **deeply analyze the problem statement and objective** to select the correct evaluation metrics that directly align with the business goal.
|
| 959 |
+
* Each distinct evaluation operation must be performed in its own separate code cell.
|
| 960 |
+
|
| 961 |
+
## Metric Selection and Justification (Second-level heading markdown)
|
| 962 |
+
* **Think and decide** on the most appropriate evaluation metric(s) for your CV task.
|
| 963 |
+
* **Examples of metrics:**
|
| 964 |
+
* **Image Classification:** Accuracy, Precision, Recall, F1-score (macro/micro/weighted), ROC-AUC.
|
| 965 |
+
* **Image Segmentation:** Intersection over Union (IoU), Dice Coefficient.
|
| 966 |
+
* **Object Detection:** Mean Average Precision (mAP), IoU for bounding box accuracy.
|
| 967 |
+
* Justify your choice of metrics.
|
| 968 |
+
* **Code Cell:** Display and explain the chosen metrics.
|
| 969 |
+
|
| 970 |
+
## Model Performance Evaluation (Second-level heading markdown)
|
| 971 |
+
* Evaluate the trained CV model(s) on the unseen test set using the selected metrics.
|
| 972 |
+
* **Examples of visualizations:**
|
| 973 |
+
* Plotting training/validation loss/metric curves over epochs.
|
| 974 |
+
* Displaying confusion matrix for classification.
|
| 975 |
+
* Visualizing sample predictions with overlaid bounding boxes (detection) or masks (segmentation).
|
| 976 |
+
* **Code Cell(s):** Make predictions on the test set, calculate metrics, and include code for any relevant visualizations.
|
| 977 |
+
|
| 978 |
+
## Final Best Model Selection (Second-level heading markdown)
|
| 979 |
+
* If multiple models/configurations were trained, **think, analyze, and decide** on criteria to select the best one.
|
| 980 |
+
* Compare performance across chosen metrics.
|
| 981 |
+
* **Code Cell:** Present comparison results clearly and explicitly identify the final best model (and optionally save it).
|
| 982 |
+
|
| 983 |
+
*(This section will have 4 markdown cells and 3-6 code cells. Total: 7-10 cells).*
|
| 984 |
+
* **NOTE:** All evaluation methods and metrics must be covered in weekly topics.
|
| 985 |
+
|
| 986 |
+
---
|
| 987 |
+
|
| 988 |
+
# 9. Insights & Recommendations (First-level heading markdown)
|
| 989 |
+
* Keep this section empty.
|
| 990 |
+
* We will write insights based on outputs after the notebook is executed.
|
| 991 |
+
|
| 992 |
+
---
|
| 993 |
+
|
| 994 |
+
**FINAL NOTE:** This is your high-level reference. You must **ONLY THINK, ANALYZE, AND DECIDE** based on the specific problem statement, dataset information (including its type – tabular, image, text, or mixed), and the user's specific weekly subtopics. Do NOT go beyond the weekly topics. Every decision and every operation performed must be carefully considered and justified in the context of the specific Computer Vision problem given.
|
| 995 |
+
"""
|
| 996 |
+
|
| 997 |
+
|
| 998 |
+
GenerativeAI_Prompt_Engineering = """
|
| 999 |
+
# Generative AI: Prompt Engineering Task Notebook Structure and Content Guidelines
|
| 1000 |
+
|
| 1001 |
+
This outline guides the LLM to structure a Prompt Engineering task notebook. It's designed to adapt to **any task solvable via LLMs** (e.g., content generation, summarization, translation, Q&A, code generation) by focusing on **optimizing LLM inputs**. The LLM must **THINK, ANALYZE, AND DECIDE** which prompt engineering techniques fit the specific case study, LLM capabilities, and weekly topics.
|
| 1002 |
+
|
| 1003 |
+
## 1. Problem Statement (First-level heading markdown)
|
| 1004 |
+
## Business Context (Second-level heading markdown)
|
| 1005 |
+
* Write the user’s Business context as is in a markdown cell.
|
| 1006 |
+
|
| 1007 |
+
## Objective (Second-level heading markdown)
|
| 1008 |
+
* Write the user's objective as is in a markdown cell.
|
| 1009 |
+
|
| 1010 |
+
## Key Prompt Components & Desired Output Characteristics (Second-level heading markdown)
|
| 1011 |
+
* This section defines the critical elements that will form your prompts (e.g., `[Instruction]`, `[Context]`, `[Examples]`, `[Constraints]`) and the specific characteristics of the *desired output* from the LLM (e.g., JSON format, summary length, specific tone, accuracy requirements).
|
| 1012 |
+
* Provide detailed descriptions for each component and output characteristic, **ensuring they align with the business problem and the nature of the input text data source.**
|
| 1013 |
+
|
| 1014 |
+
*(This section will have 6 markdown cells in total).*
|
| 1015 |
+
|
| 1016 |
+
---
|
| 1017 |
+
|
| 1018 |
+
# 2. Install and import the necessary libraries (First-level heading markdown)
|
| 1019 |
+
## Install Dependencies (Second-level heading markdown)
|
| 1020 |
+
* **Think and decide** on necessary libraries with specific versions. These will primarily be for LLM API interaction, basic data handling (e.g., for CSV, JSON, plain text files), and potentially evaluation tools. **Ensure selected libraries are within the scope of weekly topics.**
|
| 1021 |
+
* **Examples:** `openai`, `google-generativeai`, `anthropic` (for LLM APIs); `pandas`, `numpy` (for handling tabular/structured prompt components or simple evaluations); `nltk`, `rouge` (for potential automated evaluation metrics for specific tasks like summarization).
|
| 1022 |
+
* **Code Cell:** Install all dependencies in one cell.
|
| 1023 |
+
|
| 1024 |
+
## Import Libraries (Second-level heading markdown)
|
| 1025 |
+
* Import all required libraries.
|
| 1026 |
+
* Group imports with clear comments (e.g., "# LLM API Clients", "# Data Handling & Utilities", "# Evaluation Tools").
|
| 1027 |
+
* Add comments for less common libraries.
|
| 1028 |
+
* **Code Cell:** Import all libraries in one cell.
|
| 1029 |
+
|
| 1030 |
+
*(This section will have 2 markdown and 2 code cells).*
|
| 1031 |
+
|
| 1032 |
+
---
|
| 1033 |
+
|
| 1034 |
+
# 3. LLM API Initialization and Configuration (First-level heading markdown)
|
| 1035 |
+
This section sets up the connection to the Large Language Model and defines initial generation parameters.
|
| 1036 |
+
|
| 1037 |
+
## LLM API Initialization (Second-level heading markdown)
|
| 1038 |
+
* Initialize the client for your chosen LLM API using your API key or authentication method. **This step must align with the LLM APIs taught in weekly topics.**
|
| 1039 |
+
* **Examples of operations:** `OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))`, `GenerativeModel(model_name="gemini-pro")`.
|
| 1040 |
+
* **Code Cell:** Initialize the LLM client.
|
| 1041 |
+
|
| 1042 |
+
## Define Generation Parameters (Second-level heading markdown)
|
| 1043 |
+
* **Think, analyze, and decide** on initial settings for parameters that control the LLM's output behavior (e.g., creativity, length). These can be tuned later. **Parameter choices should be relevant to the business problem's desired output and within weekly topic scope.**
|
| 1044 |
+
* **Examples of parameters:** `temperature` (controls randomness), `max_output_tokens` (controls response length), `top_p` (nucleus sampling), `top_k` (top-k sampling).
|
| 1045 |
+
* **Code Cell:** Define a dictionary or variables for these generation parameters.
|
| 1046 |
+
|
| 1047 |
+
*(This section will have 2 markdown cells and 2 code cells).*
|
| 1048 |
+
|
| 1049 |
+
---
|
| 1050 |
+
|
| 1051 |
+
# 4. Understanding the Task and Raw Prompt Inputs (First-level heading markdown)
|
| 1052 |
+
This section focuses on analyzing the underlying raw content or context that will be used to construct your prompts. This is akin to "data overview" but specifically tailored for prompt components derived from any text data source.
|
| 1053 |
+
|
| 1054 |
+
## Define/Load Raw Input/Context (Second-level heading markdown)
|
| 1055 |
+
* Based on the problem statement and the specific text data source, define the primary raw input or context the LLM will work with.
|
| 1056 |
+
* **Think, analyze, and decide** the most appropriate method to load or define this content **based on its source format (e.g., CSV, JSON, plain text file, database query results, or directly typed text) and **within weekly topic capabilities**.
|
| 1057 |
+
* **Examples of operations:** Define a string variable for a query, load text from a `.txt` file, read a specific column from a `pandas.DataFrame` (if CSV/structured), parse a JSON object, or extract specific content from a simulated web page/document.
|
| 1058 |
+
* **Code Cell:** Define/Load the initial raw input/context from its source.
|
| 1059 |
+
|
| 1060 |
+
## Define Few-Shot Examples (Optional) (Second-level heading markdown)
|
| 1061 |
+
* If your problem benefits from few-shot learning, define or load a small set of high-quality input-output examples. These examples will be inserted directly into your prompts.
|
| 1062 |
+
* **Think, analyze, and decide** the best way to represent these examples **based on your data source and the problem's needs**, **staying within weekly topics**.
|
| 1063 |
+
* **Examples of operations:** Define a list of dictionaries where each dictionary is an input-output pair, load from a small JSON or CSV file, or hardcode simple examples.
|
| 1064 |
+
* **Code Cell:** Define/Load few-shot examples (if applicable).
|
| 1065 |
+
|
| 1066 |
+
## Analysis of Raw Input Characteristics (Second-level heading markdown)
|
| 1067 |
+
* Analyze basic characteristics of your raw input components relevant to prompt construction. This analysis should inform your prompt design, considering the specific text data source.
|
| 1068 |
+
* **Think, analyze, and decide** what checks are relevant **given the data source (e.g., is it clean text, does it have formatting issues, is it short/long?) and the problem objective, always within weekly topic capabilities**.
|
| 1069 |
+
* **Examples of checks:** Character/word count of the primary input, structure of sample few-shot examples, identify any special characters, encoding issues, or specific formatting from the source that might need attention (e.g., HTML tags if scraped, JSON key structure).
|
| 1070 |
+
* **Code Cell(s):** Display samples and perform basic characteristic checks relevant to the text data source.
|
| 1071 |
+
|
| 1072 |
+
*(This section will have 3 markdown cells and 3-5 code cells).*
|
| 1073 |
+
|
| 1074 |
+
---
|
| 1075 |
+
|
| 1076 |
+
# 5. Prompt Design Patterns & Construction (First-level heading markdown)
|
| 1077 |
+
This is the core "engineering" section, where you systematically design and build your prompts using various patterns. Each pattern's implementation should demonstrate its structure and effect, **always adapting to the specific business problem and the processed raw input from your data source.**
|
| 1078 |
+
|
| 1079 |
+
## Baseline Prompt Construction (Second-level heading markdown)
|
| 1080 |
+
* **Think, analyze, and decide** on a simple, direct prompt to establish a baseline. This will usually involve clear instructions and incorporating the processed raw input from your data source. **Ensure this is within weekly topic scope.**
|
| 1081 |
+
* **Code Cell:** Construct the baseline prompt string or message list, dynamically inserting content from your raw input as needed.
|
| 1082 |
+
* **Markdown Cell:** Explain the rationale for this baseline.
|
| 1083 |
+
|
| 1084 |
+
## Few-Shot Prompting (Second-level heading markdown)
|
| 1085 |
+
* **Think, analyze, and decide** if providing explicit examples in the prompt would guide the LLM's style, format, or reasoning better, **based on the specific business problem and the nature of your raw input/desired output. Ensure this pattern is covered in weekly topics.**
|
| 1086 |
+
* **Examples of operations:** Concatenate few-shot examples (from Section 4) into the prompt string or add them as message history in a chat API call. The format of these examples must match the LLM's expectation and the problem's requirements.
|
| 1087 |
+
* **Code Cell:** Construct the few-shot prompt.
|
| 1088 |
+
* **Markdown Cell:** Explain why few-shot is chosen for this problem and how it's implemented.
|
| 1089 |
+
|
| 1090 |
+
## Chain-of-Thought (CoT) / Step-by-Step Prompting (Second-level heading markdown)
|
| 1091 |
+
* **Think, analyze, and decide** if breaking down complex tasks into intermediate reasoning steps, or instructing the LLM to "think step-by-step," would improve accuracy or transparency for your business problem. **This must align with weekly topics.**
|
| 1092 |
+
* **Examples of operations:** Add phrases like "Let's think step by step," or structure the prompt to elicit intermediate outputs before the final answer, integrating the raw input effectively.
|
| 1093 |
+
* **Code Cell:** Construct the CoT prompt.
|
| 1094 |
+
* **Markdown Cell:** Explain when CoT is useful and its implementation for your specific task.
|
| 1095 |
+
|
| 1096 |
+
## Persona-Based / Role-Playing Prompting (Second-level heading markdown)
|
| 1097 |
+
* **Think, analyze, and decide** if assigning a specific role or persona to the LLM (e.g., "You are an expert copywriter," "You are a customer service agent") improves the tone, style, or focus of the output for your business problem. **Ensure this is within weekly topics.**
|
| 1098 |
+
* **Examples of operations:** Add a system message or an instruction like "You are an expert [role]" at the beginning of the prompt.
|
| 1099 |
+
* **Code Cell:** Construct the persona-based prompt.
|
| 1100 |
+
* **Markdown Cell:** Explain the benefit of using a persona in the context of the business problem.
|
| 1101 |
+
|
| 1102 |
+
## Output Formatting and Constraints (Second-level heading markdown)
|
| 1103 |
+
* **Think, analyze, and decide** how to explicitly instruct the LLM on the desired output format (e.g., JSON, markdown, bullet points) or apply constraints (e.g., maximum length, no repetition). This is crucial for matching the business requirement. **This must align with weekly topics.**
|
| 1104 |
+
* **Examples of operations:** Add instructions like "Output in JSON format," "Limit response to 3 sentences," "Do not include introductory phrases."
|
| 1105 |
+
* **Code Cell:** Construct the prompt with formatting instructions, dynamically incorporating raw input as needed.
|
| 1106 |
+
* **Markdown Cell:** Explain the importance of output constraints for solving the business problem.
|
| 1107 |
+
|
| 1108 |
+
*(This section will generally consist of approximately 5-7 markdown cells and 10-15 code cells. Total: 15-20 cells).*
|
| 1109 |
+
* **NOTE:** Focus on explaining *why* each pattern is chosen for the problem and its specific implementation, always considering the raw input from the data source and staying within weekly topics.
|
| 1110 |
+
|
| 1111 |
+
---
|
| 1112 |
+
|
| 1113 |
+
# 6. Generating Outputs with Designed Prompts (First-level heading markdown)
|
| 1114 |
+
This section focuses on executing the LLM calls using the prompts designed in the previous step and collecting the generated outputs. Operations here are generally consistent across different text data sources and problems.
|
| 1115 |
+
|
| 1116 |
+
## Generating Baseline Output (Second-level heading markdown)
|
| 1117 |
+
* Use the baseline prompt and initial generation parameters (from Sections 3 & 5) to get an initial output from the LLM. This serves as a point of comparison for engineered prompts.
|
| 1118 |
+
* **Code Cell:** Call the LLM API with the baseline prompt and display the generated output.
|
| 1119 |
+
|
| 1120 |
+
## Generating Outputs with Engineered Prompts (Second-level heading markdown)
|
| 1121 |
+
* For each significant prompt variation (e.g., few-shot, CoT, persona-based) designed in Section 5, generate the LLM's output. The method of calling the LLM remains consistent, but the prompt content changes.
|
| 1122 |
+
* **Code Cell(s):** Call the LLM API for each engineered prompt. Store the generated outputs for later evaluation. Display selected outputs.
|
| 1123 |
+
|
| 1124 |
+
*(This section will have 2 markdown cells and 5 code cells).*
|
| 1125 |
+
|
| 1126 |
+
---
|
| 1127 |
+
|
| 1128 |
+
# 7. Evaluation Strategy & Initial Assessment (First-level heading markdown)
|
| 1129 |
+
This section focuses on planning *how* to evaluate the generated outputs and performing an initial assessment of quality. The chosen evaluation methods must align perfectly with the specific business problem, the type of output generated, and the text data source.
|
| 1130 |
+
|
| 1131 |
+
## Define Evaluation Criteria and Metrics (Second-level heading markdown)
|
| 1132 |
+
* **Think, analyze, and decide** on the most appropriate evaluation methods for your task, aligning directly with the business objective and the nature of the LLM's output (e.g., text, structured data). **Ensure these methods are covered in weekly topics.**
|
| 1133 |
+
* **Examples of methods:**
|
| 1134 |
+
* **Qualitative Review:** Human inspection against a defined rubric (e.g., factual accuracy, coherence, relevance, tone, conciseness, adherence to source material).
|
| 1135 |
+
* **Quantitative Metrics (if applicable):** BLEU/ROUGE score (for summarization/translation if a reference is available), semantic similarity scores (using embeddings), custom parsing/regex for structured output, accuracy for classification-like tasks. The choice depends heavily on the output's format and the problem.
|
| 1136 |
+
* Justify your choice of metrics/criteria, relating them directly to the business problem's success.
|
| 1137 |
+
* **Markdown Cell:** Display and explain the chosen evaluation criteria.
|
| 1138 |
+
|
| 1139 |
+
## Initial Performance Assessment (Second-level heading markdown)
|
| 1140 |
+
* Perform a preliminary assessment of the generated outputs based on your defined criteria. This involves inspecting the outputs against the original input and desired outcome.
|
| 1141 |
+
* **Think, analyze, and decide** on the most effective way to display and initially review the outputs, **considering their format and the evaluation criteria. Stay within weekly topics.**
|
| 1142 |
+
* **Examples of operations:** Displaying generated outputs side-by-side with original inputs or expected outputs for quick comparison, manually scoring a few samples based on the rubric, or performing quick checks for structured output.
|
| 1143 |
+
* **Code Cell(s):** Implement code to display outputs for review and/or perform initial metric calculations.
|
| 1144 |
+
|
| 1145 |
+
*(This section will have 2 markdown cells and 5 code cells).*
|
| 1146 |
+
|
| 1147 |
+
---
|
| 1148 |
+
|
| 1149 |
+
# 8. Insights & Recommendations (First-level heading markdown)
|
| 1150 |
+
* Keep this section empty.
|
| 1151 |
+
* We will write insights based on outputs after the notebook is executed.
|
| 1152 |
+
|
| 1153 |
+
---
|
| 1154 |
+
|
| 1155 |
+
**FINAL NOTE:** This is your high-level reference. You must **ONLY THINK, ANALYZE, AND DECIDE** based on the specific problem statement, the **exact text data source (e.g., CSV, JSON, raw text file)**, the capabilities of the chosen LLM, and the user's specific weekly subtopics. **Do NOT go beyond the weekly topics.** Every decision and every operation performed must be carefully considered and justified in the context of the specific Prompt Engineering problem given.
|
| 1156 |
+
"""
|
| 1157 |
+
|
| 1158 |
+
|
| 1159 |
+
|
| 1160 |
+
GenerativeAI_Retrieval_Augmented_Generation = """
|
| 1161 |
+
# Generative AI: Retrieval Augmented Generation (RAG) Application Notebook Structure and Content Guidelines
|
| 1162 |
+
|
| 1163 |
+
This outline guides the LLM to structure a RAG application notebook. It's designed to build a RAG system that can adapt to **any business problem requiring factual, source-grounded answers** and **any text-based data source** as its knowledge base. The LLM must **THINK, ANALYZE, AND DECIDE** on the specific implementation details (e.g., chunking strategy, embedding model, vector store, retrieval method) based on the unique problem, the nature of the knowledge base data, and **strictly within the scope of weekly topics**.
|
| 1164 |
+
|
| 1165 |
+
## 1. Problem Statement (First-level heading markdown)
|
| 1166 |
+
## Business Context (Second-level heading markdown)
|
| 1167 |
+
* Write the user’s Business context as is in a markdown cell. This should clearly state the need for factual, source-grounded information.
|
| 1168 |
+
|
| 1169 |
+
## Objective (Second-level heading markdown)
|
| 1170 |
+
* Write the user's objective as is in a markdown cell. This should describe the desired outcome of the RAG system (e.g., accurately answer questions about internal policies, summarize research papers with citations).
|
| 1171 |
+
|
| 1172 |
+
## Knowledge Base Overview & Desired Output Characteristics (Second-level heading markdown)
|
| 1173 |
+
* Define the type of knowledge base data that the RAG system will use (e.g., "a collection of PDF manuals," "JSON product specifications," "plain text articles").
|
| 1174 |
+
* Describe the specific characteristics of the *desired output* from the RAG system (e.g., factual accuracy, conciseness, inclusion of sources, specific tone). This should align with the business objective and the nature of the information in the knowledge base.
|
| 1175 |
+
|
| 1176 |
+
*(This section will have 6 markdown cells in total).*
|
| 1177 |
+
|
| 1178 |
+
---
|
| 1179 |
+
|
| 1180 |
+
# 2. Install and import the necessary libraries (First-level heading markdown)
|
| 1181 |
+
## Install Dependencies (Second-level heading markdown)
|
| 1182 |
+
* **Think, analyze, and decide** on necessary libraries with specific versions. These will include LLM API clients, embedding model clients, vector database libraries, document loaders/parsers (e.g., `pypdf`, `python-docx`), and text processing tools. **All selected libraries must be within the scope of weekly topics.**
|
| 1183 |
+
* **Examples:** `openai`, `google-generativeai`, `langchain` (or similar orchestration framework if within scope), `sentence-transformers` (for embeddings), `chromadb` (or `faiss-cpu`, `pinecone-client` depending on weekly topics for vector store), `pypdf`, `python-docx`, `tiktoken` (for token counting/chunking).
|
| 1184 |
+
* **Code Cell:** Install all dependencies in one cell.
|
| 1185 |
+
|
| 1186 |
+
## Import Libraries (Second-level heading markdown)
|
| 1187 |
+
* Import all required libraries.
|
| 1188 |
+
* Group imports with clear comments (e.g., "# LLM & Embedding Clients", "# Document Loaders", "# Vector Store", "# Text Processing").
|
| 1189 |
+
* Add comments for less common libraries.
|
| 1190 |
+
* **Code Cell:** Import all libraries in one cell.
|
| 1191 |
+
|
| 1192 |
+
*(This section will have 2 markdown and 2 code cells).*
|
| 1193 |
+
|
| 1194 |
+
---
|
| 1195 |
+
|
| 1196 |
+
# 3. LLM & Embedding Model Initialization and Configuration (First-level heading markdown)
|
| 1197 |
+
This section sets up the core AI models that power the RAG system: the Large Language Model for generation and the Embedding Model for semantic search.
|
| 1198 |
+
|
| 1199 |
+
## LLM Initialization (Second-level heading markdown)
|
| 1200 |
+
* Initialize the client for your chosen Large Language Model. **This must align with LLM APIs taught in weekly topics.**
|
| 1201 |
+
* **Examples of operations:** `OpenAI(api_key=...)`, `GenerativeModel(model_name="gemini-pro")`.
|
| 1202 |
+
* **Code Cell:** Initialize the LLM client.
|
| 1203 |
+
|
| 1204 |
+
## Embedding Model Initialization (Second-level heading markdown)
|
| 1205 |
+
* Initialize the client or load the model for your chosen Embedding Model. This model converts text into numerical vectors. **The chosen embedding model must be within weekly topics.**
|
| 1206 |
+
* **Examples of operations:** `SentenceTransformer('all-MiniLM-L6-v2')`, `OpenAIEmbeddings()`.
|
| 1207 |
+
* **Code Cell:** Initialize the Embedding Model.
|
| 1208 |
+
|
| 1209 |
+
## Define Generation Parameters (Second-level heading markdown)
|
| 1210 |
+
* **Think, analyze, and decide** on initial settings for LLM generation parameters (e.g., `temperature`, `max_output_tokens`). These will influence the style and length of the RAG system's answers. **Parameter choices should be relevant to the business problem's desired output and within weekly topic scope.**
|
| 1211 |
+
* **Examples of parameters:** `temperature` (controls creativity), `max_output_tokens` (controls response length).
|
| 1212 |
+
* **Code Cell:** Define a dictionary or variables for these generation parameters.
|
| 1213 |
+
|
| 1214 |
+
*(This section will have 3 markdown cells and 6 code cells (2 for each sub section)).*
|
| 1215 |
+
|
| 1216 |
+
---
|
| 1217 |
+
|
| 1218 |
+
# 4. Data Ingestion & Preprocessing for Knowledge Base (First-level heading markdown)
|
| 1219 |
+
This crucial section involves loading the raw knowledge base documents, extracting their text content, and performing initial cleaning. The operations **must explicitly adapt to the specific text data source(s)** identified in the problem statement.
|
| 1220 |
+
|
| 1221 |
+
## Load Knowledge Base Documents (Second-level heading markdown)
|
| 1222 |
+
* Load the documents that will form your RAG knowledge base.
|
| 1223 |
+
* **Think, analyze, and decide** the most appropriate method to load these documents **based on their specific format (e.g., PDF, DOCX, TXT, JSON, CSV records, web pages) and their location (local files, cloud storage, database). Ensure this is within weekly topic capabilities.**
|
| 1224 |
+
* **Examples of operations:** Using `PyPDFLoader` or `PyMuPDF` for PDFs, reading text files, parsing JSON/XML to extract relevant text fields, loading `pandas.DataFrame` from CSV/database and concatenating text columns.
|
| 1225 |
+
* **Code Cell(s):** Implement document loading.
|
| 1226 |
+
|
| 1227 |
+
## Extract and Clean Text (Second-level heading markdown)
|
| 1228 |
+
* Extract raw text content from the loaded documents and perform initial cleaning steps.
|
| 1229 |
+
* **Think, analyze, and decide** on necessary cleaning steps **based on the raw data source's characteristics (e.g., removing boilerplate from HTML, handling OCR errors from PDFs, stripping extra whitespace) and the impact on retrieval. Stay within weekly topics.**
|
| 1230 |
+
* **Examples of operations:** Using `.page_content` from document loaders, regex for specific pattern removal, basic string cleaning, lowercasing, handling special characters.
|
| 1231 |
+
* **Code Cell(s):** Implement text extraction and cleaning. Display samples of cleaned text.
|
| 1232 |
+
|
| 1233 |
+
*(This section will have 2 markdown cells and 8 code cells).*
|
| 1234 |
+
|
| 1235 |
+
---
|
| 1236 |
+
|
| 1237 |
+
# 5. Knowledge Base Chunking Strategy (First-level heading markdown)
|
| 1238 |
+
This section defines how the cleaned text is broken into smaller, semantically meaningful "chunks" for indexing and retrieval. The chunking strategy is highly dependent on the nature of the knowledge base and the type of queries expected.
|
| 1239 |
+
|
| 1240 |
+
## Choose and Implement Chunking Strategy (Second-level heading markdown)
|
| 1241 |
+
* Implement a strategy to divide the cleaned text into manageable chunks.
|
| 1242 |
+
* **Think, analyze, and decide** the optimal chunking method, chunk size, and overlap **based on the length and structure of your source documents (e.g., paragraphs, sentences, fixed token counts), the expected query length, and the semantic meaning of the content. Ensure this is within weekly topics.**
|
| 1243 |
+
* **Examples of operations:** `RecursiveCharacterTextSplitter`, fixed-size chunking, sentence splitting.
|
| 1244 |
+
* **Code Cell:** Implement the chosen chunking strategy and display samples of chunks.
|
| 1245 |
+
|
| 1246 |
+
## Analyze Chunk Characteristics (Second-level heading markdown)
|
| 1247 |
+
* Analyze basic properties of the generated text chunks.
|
| 1248 |
+
* **Think, analyze, and decide** what characteristics are important to check (e.g., average chunk length, distribution of chunk lengths, presence of key information in chunks) **to ensure effective retrieval for the business problem. Stay within weekly topics.**
|
| 1249 |
+
* **Examples of checks:** Plotting chunk length distribution, inspecting a random sample of chunks.
|
| 1250 |
+
* **Code Cell(s):** Perform chunk characteristic analysis.
|
| 1251 |
+
|
| 1252 |
+
*(This section will have 2 markdown cells and 8 code cells).*
|
| 1253 |
+
|
| 1254 |
+
---
|
| 1255 |
+
|
| 1256 |
+
# 6. Embedding Generation & Vector Store Indexing (First-level heading markdown)
|
| 1257 |
+
This section covers converting text chunks into numerical embeddings and storing them in a searchable vector database.
|
| 1258 |
+
|
| 1259 |
+
## Generate Embeddings for Chunks (Second-level heading markdown)
|
| 1260 |
+
* Convert each text chunk into a high-dimensional numerical vector (embedding) using the initialized embedding model.
|
| 1261 |
+
* **Code Cell:** Generate embeddings for all chunks.
|
| 1262 |
+
|
| 1263 |
+
## Initialize and Populate Vector Store (Second-level heading markdown)
|
| 1264 |
+
* Initialize your chosen vector store and populate it with the generated embeddings and their corresponding text chunks (metadata).
|
| 1265 |
+
* **Think, analyze, and decide** on the most suitable vector store **based on the scale of your knowledge base, performance requirements, and whether an in-memory, local, or cloud-based solution aligns with weekly topics.**
|
| 1266 |
+
* **Examples of operations:** `Chroma.from_documents()`, `FAISS.from_documents()`, adding embeddings to a cloud vector database such as Azure AI search.
|
| 1267 |
+
* **Code Cell:** Initialize and populate the vector store.
|
| 1268 |
+
|
| 1269 |
+
*(This section will have 2 markdown cells and 8 code cells).*
|
| 1270 |
+
|
| 1271 |
+
---
|
| 1272 |
+
|
| 1273 |
+
# 7. Retrieval Mechanism Design (First-level heading markdown)
|
| 1274 |
+
This section focuses on how the RAG system will find the most relevant chunks from the vector store given a user's query.
|
| 1275 |
+
|
| 1276 |
+
## Define and Implement Retrieval Strategy (Second-level heading markdown)
|
| 1277 |
+
* Design the method for retrieving relevant chunks from the vector store based on a user's query.
|
| 1278 |
+
* **Think, analyze, and decide** on the most appropriate retrieval strategy **based on the nature of typical user queries, the structure of your knowledge base, and the techniques covered in weekly topics (e.g., simple similarity search, Maximum Marginal Relevance (MMR), hybrid search).**
|
| 1279 |
+
* **Examples of operations:** Using `vectorstore.as_retriever()`, configuring search parameters like `k` (number of top results).
|
| 1280 |
+
* **Code Cell:** Implement the retrieval strategy.
|
| 1281 |
+
|
| 1282 |
+
## Test Retrieval with Sample Queries (Second-level heading markdown)
|
| 1283 |
+
* Test the retrieval mechanism with a few representative sample queries to ensure it's returning relevant chunks.
|
| 1284 |
+
* **Think, analyze, and decide** on sample queries that cover various aspects of the business problem and the knowledge base content. **Observe if the top-k retrieved chunks are semantically relevant.**
|
| 1285 |
+
* **Code Cell(s):** Perform sample retrievals and display the retrieved chunks and their content.
|
| 1286 |
+
|
| 1287 |
+
*(This section will have 2 markdown cells and 5 code cells).*
|
| 1288 |
+
|
| 1289 |
+
---
|
| 1290 |
+
|
| 1291 |
+
# 8. Augmentation & LLM Generation (First-level heading markdown)
|
| 1292 |
+
This section brings together the retrieved context and the LLM to generate the final response.
|
| 1293 |
+
|
| 1294 |
+
## Construct Augmented Prompt (Second-level heading markdown)
|
| 1295 |
+
* Design how the retrieved context will be integrated into the prompt sent to the LLM, along with the user's original query.
|
| 1296 |
+
* **Think, analyze, and decide** on the optimal prompt structure to clearly present the retrieved information to the LLM (e.g., "Use the following context to answer the question: [context] Question: [query]"). **Ensure this prompt engineering technique is within weekly topics.**
|
| 1297 |
+
* **Code Cell:** Construct the augmented prompt template.
|
| 1298 |
+
|
| 1299 |
+
## Generate RAG Response (Second-level heading markdown)
|
| 1300 |
+
* Send the augmented prompt to the LLM and generate the final response.
|
| 1301 |
+
* **Examples of operations:** Using `llm.invoke()` or `llm.predict()` with the augmented prompt.
|
| 1302 |
+
* **Code Cell:** Generate responses for sample queries using the augmented prompt and display them.
|
| 1303 |
+
|
| 1304 |
+
*(This section will have 2 markdown cells and 5 code cells).*
|
| 1305 |
+
|
| 1306 |
+
---
|
| 1307 |
+
|
| 1308 |
+
# 9. RAG System Evaluation (First-level heading markdown)
|
| 1309 |
+
This section outlines how to systematically evaluate the performance of the entire RAG system, considering both retrieval and generation quality. The evaluation strategy must be aligned with the business problem's objectives.
|
| 1310 |
+
|
| 1311 |
+
## Define Evaluation Metrics and Strategy (Second-level heading markdown)
|
| 1312 |
+
* **Think, analyze, and decide** on comprehensive metrics and a strategy to evaluate the RAG system's performance. This should cover:
|
| 1313 |
+
* **Retrieval Quality:** How relevant are the retrieved chunks? (e.g., precision, recall of retrieved documents/chunks if ground truth exists).
|
| 1314 |
+
* **Generation Quality:** How good is the LLM's answer based on the retrieved context? (e.g., faithfulness to context, answer relevance, conciseness, fluency).
|
| 1315 |
+
* **End-to-End RAG Metrics:** (e.g., RAGAS metrics like Faithfulness, Answer Relevance, Context Recall/Precision if within scope).
|
| 1316 |
+
* **Crucially, align all chosen metrics with the business problem's success criteria and **ensure they are covered in weekly topics.**
|
| 1317 |
+
* **Markdown Cell:** Describe the chosen evaluation metrics and strategy.
|
| 1318 |
+
|
| 1319 |
+
## Perform Evaluation (Second-level heading markdown)
|
| 1320 |
+
* Implement the chosen evaluation methods on a set of test queries.
|
| 1321 |
+
* **Think, analyze, and decide** how to execute the evaluation (e.g., manual human review of outputs, automated scoring if ground truth is available). **Stay within weekly topics.**
|
| 1322 |
+
* **Examples of operations:** Loop through test queries, generate RAG responses, apply scoring functions, present results.
|
| 1323 |
+
* **Code Cell(s):** Implement and display evaluation results.
|
| 1324 |
+
|
| 1325 |
+
## Identify Areas for Improvement (Second-level heading markdown)
|
| 1326 |
+
* Based on the evaluation results, identify specific weaknesses in the RAG system (e.g., poor retrieval for certain query types, hallucinations in generation).
|
| 1327 |
+
* **Markdown Cell:** Summarize evaluation findings and highlight areas needing improvement.
|
| 1328 |
+
|
| 1329 |
+
*(This section will have 3 markdown cells and 7 code cells).*
|
| 1330 |
+
|
| 1331 |
+
---
|
| 1332 |
+
|
| 1333 |
+
# 10. Insights & Recommendations (First-level heading markdown)
|
| 1334 |
+
* Keep this section empty.
|
| 1335 |
+
* We will write insights based on outputs after the notebook is executed.
|
| 1336 |
+
|
| 1337 |
+
---
|
| 1338 |
+
|
| 1339 |
+
**FINAL NOTE:** This is your high-level reference. You must **ONLY THINK, ANALYZE, AND DECIDE** based on the specific problem statement, the **exact text data source(s) for the knowledge base**, the capabilities of the chosen LLM and embedding model, and the user's specific weekly subtopics. **Do NOT go beyond the weekly topics.** Every decision and every operation performed must be carefully considered and justified in the context of the specific RAG application given.
|
| 1340 |
+
"""
|
src/requirements.txt
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
langchain==0.3.24
|
| 2 |
+
langchain-core==0.3.57
|
| 3 |
+
langchain-openai==0.3.15
|
| 4 |
+
langchain_community
|
| 5 |
+
langchain_experimental
|
| 6 |
+
langgraph
|
| 7 |
+
openai
|
| 8 |
+
tiktoken
|
| 9 |
+
papermill
|
| 10 |
+
streamlit
|
| 11 |
+
pandas
|
| 12 |
+
numpy
|
| 13 |
+
pydantic
|
src/workflow_nodes.py
ADDED
|
@@ -0,0 +1,1266 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Standard Libraries
|
| 2 |
+
import os
|
| 3 |
+
import json
|
| 4 |
+
import nbformat
|
| 5 |
+
import shutil
|
| 6 |
+
import sys
|
| 7 |
+
import subprocess
|
| 8 |
+
import re
|
| 9 |
+
from pathlib import Path
|
| 10 |
+
|
| 11 |
+
from typing import Any, Dict, Optional
|
| 12 |
+
from IPython.display import Image, display
|
| 13 |
+
|
| 14 |
+
# LangChain and OpenAI related imports
|
| 15 |
+
from langchain_openai import ChatOpenAI # LLM for invoking prompts
|
| 16 |
+
from langchain.schema import SystemMessage
|
| 17 |
+
#from langchain.chat_models import ChatOpenAI
|
| 18 |
+
|
| 19 |
+
import papermill as pm
|
| 20 |
+
from datetime import datetime
|
| 21 |
+
|
| 22 |
+
from typing import Any, Dict
|
| 23 |
+
from typing_extensions import TypedDict
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
from workflow_utils import (
|
| 27 |
+
extract_json_from_response,
|
| 28 |
+
get_notebook_blueprint,
|
| 29 |
+
is_plot_code,
|
| 30 |
+
stage_dependencies,
|
| 31 |
+
build_plot_insight_agent_executor)
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
# Setting up LLM
|
| 36 |
+
|
| 37 |
+
## Create a ChatOpenAI model instance using LangChain
|
| 38 |
+
model = ChatOpenAI(
|
| 39 |
+
openai_api_base="https://aibe.mygreatlearning.com/openai/v1",
|
| 40 |
+
openai_api_key="gl-U2FsdGVkX1/DTLQlsmj+RdJjPy3igB9qINuaX940XtJ0CPnGc/5sbBkPKah/C829",
|
| 41 |
+
model="gpt-4o",
|
| 42 |
+
streaming=False # Explicitly disabling streaming
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
#**Case Study Generator - Prod**
|
| 47 |
+
# **Interactive Case Study State**
|
| 48 |
+
|
| 49 |
+
class InteractiveCaseStudyState(TypedDict):
|
| 50 |
+
"""
|
| 51 |
+
Shared state for the interactive, human-in-the-loop Case Study generation workflow.
|
| 52 |
+
This state dictionary is passed and updated across all workflow nodes.
|
| 53 |
+
|
| 54 |
+
Each node reads from and writes to this state, and human review/enhancer nodes
|
| 55 |
+
use it to store feedback, approval flags, and intermediate artifacts.
|
| 56 |
+
"""
|
| 57 |
+
|
| 58 |
+
# ─── User Inputs ────────────────────────────────────────────────────────────
|
| 59 |
+
|
| 60 |
+
domain: str
|
| 61 |
+
# Domain of the case study (e.g., "ml", "dl", "nlp", "cv", "rag", "genai", etc.)
|
| 62 |
+
|
| 63 |
+
topics_and_subtopics: str
|
| 64 |
+
# User-specified weekly topics and subtopics for notebook content focus.
|
| 65 |
+
|
| 66 |
+
problem_statement: str
|
| 67 |
+
# The core business problem or use case the notebook should solve.
|
| 68 |
+
|
| 69 |
+
dataset_type: str
|
| 70 |
+
# Type of dataset uploaded. Accepted values: "csv", "pdf", "images", "json".
|
| 71 |
+
# Drives downstream logic for loading, preprocessing, and code generation.
|
| 72 |
+
|
| 73 |
+
dataset_file_path: str
|
| 74 |
+
# Automatically populated backend path to the uploaded dataset
|
| 75 |
+
# (CSV, PDF folder, image folder, etc.). Not entered by the user.
|
| 76 |
+
|
| 77 |
+
data_dictionary: Dict[str, str]
|
| 78 |
+
# Column→description mapping for structured data.
|
| 79 |
+
# For PDFs or images, may include folder structure or document descriptions.
|
| 80 |
+
|
| 81 |
+
additional_instructions: str
|
| 82 |
+
# Optional user guidance—preprocessing steps, desired models, notebook style, etc.
|
| 83 |
+
# If omitted, the LLM uses sensible defaults for the chosen domain.
|
| 84 |
+
|
| 85 |
+
# ─── Intermediate Artifacts ────────────────────────────────────────────────
|
| 86 |
+
|
| 87 |
+
blueprint: str
|
| 88 |
+
# Text blueprint selected based on `domain`, used to guide Think-Sections.
|
| 89 |
+
|
| 90 |
+
plan: Dict[str, Any]
|
| 91 |
+
# Notebook outline (JSON) generated by the Think-Sections node.
|
| 92 |
+
|
| 93 |
+
raw_notebook: Dict[str, Any]
|
| 94 |
+
# Notebook skeleton with markdown & code cells but no outputs.
|
| 95 |
+
|
| 96 |
+
executed_notebook: Dict[str, Any]
|
| 97 |
+
# Notebook JSON after Papermill execution—includes real outputs.
|
| 98 |
+
|
| 99 |
+
annotated_notebook: Dict[str, Any]
|
| 100 |
+
# Executed notebook interleaved with Observations cells.
|
| 101 |
+
|
| 102 |
+
final_notebook: Dict[str, Any]
|
| 103 |
+
# Final notebook JSON (execution + insights), ready for download.
|
| 104 |
+
|
| 105 |
+
#─── Execution Error Capture ──────────────────────────────────────────────
|
| 106 |
+
|
| 107 |
+
execution_error: str
|
| 108 |
+
#If execution failed, the captured exception text from Papermill.
|
| 109 |
+
|
| 110 |
+
#─── Human-in-the-Loop Flags & Feedback ─────────────────────────────────────
|
| 111 |
+
|
| 112 |
+
_plan_approved: Optional[bool]
|
| 113 |
+
# False by default. Set True when the user approves the plan.
|
| 114 |
+
|
| 115 |
+
plan_feedback: str
|
| 116 |
+
# If _plan_approved is False, the user's feedback on how to improve the plan.
|
| 117 |
+
|
| 118 |
+
_notebook_approved: Optional[bool]
|
| 119 |
+
# False by default. Set True when the user approves the code skeleton.
|
| 120 |
+
|
| 121 |
+
notebook_feedback: str
|
| 122 |
+
# If _notebook_approved is False, the user's feedback on the notebook before execution.
|
| 123 |
+
|
| 124 |
+
_execution_approved: Optional[bool]
|
| 125 |
+
# False by default. Set True when the user approves the executed notebook.
|
| 126 |
+
|
| 127 |
+
execution_feedback: str
|
| 128 |
+
# If _execution_approved is False, the user's feedback or notes about runtime errors.
|
| 129 |
+
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
## **Think-Sections**
|
| 133 |
+
def think_sections_node(state):
|
| 134 |
+
"""
|
| 135 |
+
Think‑Sections Node:
|
| 136 |
+
- Reads user inputs including domain, topics, problem, dataset_file_path,
|
| 137 |
+
data_dictionary, and optional additional_instructions.
|
| 138 |
+
- Retrieves a domain blueprint via get_notebook_blueprint().
|
| 139 |
+
- Prompts the LLM to generate a structured JSON-based notebook outline.
|
| 140 |
+
"""
|
| 141 |
+
import json
|
| 142 |
+
from langchain.schema import SystemMessage
|
| 143 |
+
|
| 144 |
+
# 1. Extract inputs from state
|
| 145 |
+
domain = state["domain"]
|
| 146 |
+
topics = state["topics_and_subtopics"]
|
| 147 |
+
problem = state["problem_statement"]
|
| 148 |
+
dataset_type = state["dataset_type"]
|
| 149 |
+
dataset_path = state["dataset_file_path"]
|
| 150 |
+
data_dict = state.get("data_dictionary", {})
|
| 151 |
+
instructions = state.get("additional_instructions", "")
|
| 152 |
+
|
| 153 |
+
# 2. Get domain-specific blueprint
|
| 154 |
+
blueprint = get_notebook_blueprint(domain)
|
| 155 |
+
state["blueprint"] = blueprint
|
| 156 |
+
|
| 157 |
+
# 3. Build the prompt
|
| 158 |
+
prompt = f"""
|
| 159 |
+
You are a world-class AI curriculum architect and notebook planner.
|
| 160 |
+
|
| 161 |
+
## Domain Blueprint
|
| 162 |
+
Below is the domain-specific blueprint for **{domain}**:
|
| 163 |
+
{blueprint}
|
| 164 |
+
|
| 165 |
+
---
|
| 166 |
+
|
| 167 |
+
## Task
|
| 168 |
+
Design a structured, beginner-friendly, and pedagogically sound **notebook plan** in JSON format.
|
| 169 |
+
This notebook will help users solve a real-world problem using their data and provided topics.
|
| 170 |
+
|
| 171 |
+
---
|
| 172 |
+
|
| 173 |
+
## User Inputs
|
| 174 |
+
**Domain**: {domain}
|
| 175 |
+
|
| 176 |
+
**Topics and Subtopics**:
|
| 177 |
+
{topics}
|
| 178 |
+
|
| 179 |
+
**Problem Statement**:
|
| 180 |
+
{problem}
|
| 181 |
+
|
| 182 |
+
**Dataset Type**:
|
| 183 |
+
{dataset_type}
|
| 184 |
+
|
| 185 |
+
**Dataset Location**:
|
| 186 |
+
{dataset_path}
|
| 187 |
+
|
| 188 |
+
**Data Dictionary**:
|
| 189 |
+
{json.dumps(data_dict, indent=2)}
|
| 190 |
+
|
| 191 |
+
**Additional Instructions**:
|
| 192 |
+
{instructions if instructions.strip() else "None provided"}
|
| 193 |
+
|
| 194 |
+
---
|
| 195 |
+
|
| 196 |
+
## Output Format (JSON Only)
|
| 197 |
+
Return a JSON object with a top-level key `"sections"` — an **ordered list** of notebook sections.
|
| 198 |
+
Each section must follow the rules below:
|
| 199 |
+
|
| 200 |
+
### Section (1st-level)
|
| 201 |
+
- Represented as a heading with `#` markdown
|
| 202 |
+
- Keys:
|
| 203 |
+
- `title`: Section heading
|
| 204 |
+
- `type`: `"markdown"`
|
| 205 |
+
- `subsections`: List of second-level content blocks
|
| 206 |
+
|
| 207 |
+
### Subsection (2nd-level)
|
| 208 |
+
- Represented using `##` markdown
|
| 209 |
+
- Keys:
|
| 210 |
+
- `title`: Subsection heading
|
| 211 |
+
- `type`: `"markdown"` or `"code"`
|
| 212 |
+
- For `markdown`:
|
| 213 |
+
- `description`: Markdown block with supporting explanation
|
| 214 |
+
- For `code`:
|
| 215 |
+
- `code_rules`: List of instructions or rules that must be implemented in code
|
| 216 |
+
---
|
| 217 |
+
|
| 218 |
+
## Constraints to Follow
|
| 219 |
+
- Use names from `data_dictionary` or inferred data context when referring to columns or fields.
|
| 220 |
+
- Always begin with a section for the **Problem Statement** (with subsections for Business Context and Data Dictionary).
|
| 221 |
+
- Align section structure with domain blueprint and user context.
|
| 222 |
+
- Respect all user-given topics, problem, and instructions.
|
| 223 |
+
- Return **only valid JSON**—no extra markdown or commentary outside the JSON object.
|
| 224 |
+
---
|
| 225 |
+
|
| 226 |
+
Now generate the structured JSON plan for the notebook.
|
| 227 |
+
""".strip()
|
| 228 |
+
|
| 229 |
+
# 4. Invoke LLM and parse
|
| 230 |
+
response = model.invoke([SystemMessage(content=prompt)])
|
| 231 |
+
plan = extract_json_from_response(response.content)
|
| 232 |
+
|
| 233 |
+
# 5. Update state
|
| 234 |
+
state["plan"] = plan
|
| 235 |
+
# state["_plan_approved"] = False
|
| 236 |
+
# state["plan_feedback"] = ""
|
| 237 |
+
|
| 238 |
+
return state
|
| 239 |
+
|
| 240 |
+
# def think_sections_node(state):
|
| 241 |
+
# """
|
| 242 |
+
# Think-Sections Node:
|
| 243 |
+
# - Reads user inputs including domain, topics, problem, dataset_file_path,
|
| 244 |
+
# data_dictionary, and optional additional_instructions.
|
| 245 |
+
# - Retrieves a domain blueprint via get_notebook_blueprint().
|
| 246 |
+
# - Prompts the LLM to generate a detailed, text-based notebook plan with
|
| 247 |
+
# granular code cell operations, aiming for 70-80 total cells.
|
| 248 |
+
# """
|
| 249 |
+
# import json
|
| 250 |
+
# from langchain.schema import SystemMessage
|
| 251 |
+
|
| 252 |
+
# # 1. Extract inputs from state
|
| 253 |
+
# domain = state["domain"]
|
| 254 |
+
# topics = state["topics_and_subtopics"]
|
| 255 |
+
# problem = state["problem_statement"]
|
| 256 |
+
# dataset_type = state["dataset_type"]
|
| 257 |
+
# dataset_path = state["dataset_file_path"]
|
| 258 |
+
# data_dict = state.get("data_dictionary", {})
|
| 259 |
+
# instructions = state.get("additional_instructions", "")
|
| 260 |
+
|
| 261 |
+
# # 2. Get domain-specific blueprint
|
| 262 |
+
# # This function would now return the text-based blueprint (e.g., EDA_blueprint_text, ML_blueprint_text)
|
| 263 |
+
# # as described in our previous discussion, NOT the high-level JSON structure.
|
| 264 |
+
# # For demonstration, I'm using a placeholder. In a real system, you'd fetch the actual text.
|
| 265 |
+
# blueprint = get_notebook_blueprint(domain)
|
| 266 |
+
# state["blueprint"] = blueprint # Store the blueprint for later use if needed
|
| 267 |
+
|
| 268 |
+
# # 3. Build the prompt
|
| 269 |
+
# prompt = f"""
|
| 270 |
+
# You are a world-class AI curriculum architect and notebook planner. Your task is to generate a highly detailed,
|
| 271 |
+
# step-by-step notebook plan for a user, ensuring it's beginner-friendly and pedagogically sound.
|
| 272 |
+
|
| 273 |
+
# ## Your Goal
|
| 274 |
+
# Your primary goal is to **expand the provided domain blueprint into a complete, operational notebook plan**.
|
| 275 |
+
# This plan must meticulously detail every single operation, especially within code cells,
|
| 276 |
+
# to guide the user through solving their problem. The final plan should aim for a total of **70 to 80 cells**.
|
| 277 |
+
|
| 278 |
+
# ---
|
| 279 |
+
|
| 280 |
+
# ## Domain Blueprint for {domain}
|
| 281 |
+
# This is the comprehensive guideline for structuring a notebook in the **{domain}** domain. You must strictly
|
| 282 |
+
# adhere to its sections, subsections, and overall flow. For each section, and particularly for each code operation,
|
| 283 |
+
# you will expand on the general rule to create highly specific, actionable steps.
|
| 284 |
+
|
| 285 |
+
# {blueprint}
|
| 286 |
+
|
| 287 |
+
# ---
|
| 288 |
+
|
| 289 |
+
# ## User Inputs to Contextualize the Plan
|
| 290 |
+
|
| 291 |
+
# Here are the specific details provided by the user. You must integrate these details to
|
| 292 |
+
# make the plan highly relevant and actionable for their specific case.
|
| 293 |
+
|
| 294 |
+
# **Domain**: {domain}
|
| 295 |
+
# **Topics and Subtopics**: {topics}
|
| 296 |
+
# * **CRITICAL**: Only include techniques and operations that fall under these specified weekly topics.
|
| 297 |
+
# Do NOT go beyond this scope.
|
| 298 |
+
|
| 299 |
+
# **Problem Statement**: {problem}
|
| 300 |
+
# * **CRITICAL**: Every step and explanation in the plan must directly contribute to addressing this business problem.
|
| 301 |
+
|
| 302 |
+
# **Dataset Type**: {dataset_type}
|
| 303 |
+
# **Dataset Location**: {dataset_path}
|
| 304 |
+
# **Data Dictionary**: {json.dumps(data_dict, indent=2)}
|
| 305 |
+
# * **CRITICAL**: Refer to specific columns/fields from the Data Dictionary and dataset details
|
| 306 |
+
# (e.g., shape, columns, types) when describing operations. Tailor operations to the specific
|
| 307 |
+
# characteristics of this dataset.
|
| 308 |
+
|
| 309 |
+
# **Additional Instructions**: {instructions if instructions.strip() else "None provided"}
|
| 310 |
+
# * Integrate any additional instructions provided by the user.
|
| 311 |
+
|
| 312 |
+
# ---
|
| 313 |
+
|
| 314 |
+
# ## Output Format (Plain Text Notebook Plan)
|
| 315 |
+
|
| 316 |
+
# Your output must be a single, continuous text document representing the notebook plan.
|
| 317 |
+
# Do NOT output JSON. Follow this strict markdown structure:
|
| 318 |
+
|
| 319 |
+
# - **First-level headings (`#`):** For main sections (e.g., `# 1. Problem Statement`).
|
| 320 |
+
# - **Second-level headings (`##`):** For subsections (e.g., `## Business Context`).
|
| 321 |
+
# - **Code Cell Details:** For every operation that requires a code cell, describe it using the format:
|
| 322 |
+
# `Code Cell X: <Detailed, prescriptive description of the operation>`
|
| 323 |
+
# - `X` should be a sequential number within that specific subsection.
|
| 324 |
+
# - The description must be **verbose and explicit**, detailing *what* the user should perform in that cell, *why* (linking to problem/data), and *how* (conceptually, not actual code).
|
| 325 |
+
# - **Example:** "Code Cell 1: In this cell, we will load the dataset from '{dataset_path}' using the `pandas.read_csv()` function. This is crucial as it brings our raw data into a DataFrame for initial inspection and subsequent analysis, directly addressing the need to analyze '{dataset_type}' data for the '{problem}' problem."
|
| 326 |
+
|
| 327 |
+
# ---
|
| 328 |
+
|
| 329 |
+
# ## Key Constraints for Plan Generation
|
| 330 |
+
|
| 331 |
+
# * **Granularity:** Expand each conceptual step from the blueprint into multiple granular code cells if needed, aiming for the 70-80 cell target.
|
| 332 |
+
# * **Specificity:** Refer to specific columns and data types from the `data_dictionary` and inferred dataset characteristics.
|
| 333 |
+
# * **Relevance:** Every operation must be justified by its relevance to the `problem_statement` and the `dataset`.
|
| 334 |
+
# * **Scope:** **ABSOLUTELY DO NOT INTRODUCE CONCEPTS, LIBRARIES, OR TECHNIQUES THAT ARE NOT EXPLICITLY MENTIONED OR IMPLICITLY COVERED WITHIN THE `Topics and Subtopics` provided.** This is paramount.
|
| 335 |
+
# * **Pedagogical Soundness:** The plan should logically progress, making sense for a learner.
|
| 336 |
+
# * **No Actual Code:** Provide detailed *instructions* for the code, not the code itself.
|
| 337 |
+
# * **No Commentary:** Do not include any conversational filler or extra markdown outside the specified plan structure.
|
| 338 |
+
|
| 339 |
+
# Now, generate the detailed notebook plan as a plain text document.
|
| 340 |
+
# """.strip()
|
| 341 |
+
|
| 342 |
+
# # 4. Invoke LLM
|
| 343 |
+
# # Assuming 'model' is your configured LLM client
|
| 344 |
+
# response = model.invoke([SystemMessage(content=prompt)])
|
| 345 |
+
# plan_text = response.content
|
| 346 |
+
|
| 347 |
+
# # 5. Update state
|
| 348 |
+
# state["plan"] = plan_text
|
| 349 |
+
# # state["_plan_approved"] = False # Reset approval status
|
| 350 |
+
# # state["plan_feedback"] = "" # Clear feedback
|
| 351 |
+
|
| 352 |
+
# return state
|
| 353 |
+
|
| 354 |
+
|
| 355 |
+
# ## **Review Plan**
|
| 356 |
+
# def review_plan_node(state: InteractiveCaseStudyState) -> InteractiveCaseStudyState:
|
| 357 |
+
# """
|
| 358 |
+
# Review‑Plan Node (UI‑driven):
|
| 359 |
+
# - Expects that the front‑end will render the JSON plan for user review.
|
| 360 |
+
# - The front‑end should display two buttons: 'YES' and 'NO'.
|
| 361 |
+
# - If 'NO' is clicked, the front‑end should show a text input for feedback.
|
| 362 |
+
# - Stores:
|
| 363 |
+
# state['_plan_approved'] = True/False
|
| 364 |
+
# state['plan_feedback'] = "" or the user’s feedback.
|
| 365 |
+
# """
|
| 366 |
+
# # 1) Grab the generated plan
|
| 367 |
+
# plan = state.get("plan", {})
|
| 368 |
+
|
| 369 |
+
# # 2) In a console environment, we’d pretty‑print it; in Streamlit, you’d st.json(plan)
|
| 370 |
+
# print("\n===== PROPOSED NOTEBOOK PLAN =====")
|
| 371 |
+
# print(json.dumps(plan, indent=2))
|
| 372 |
+
# print("==================================\n")
|
| 373 |
+
|
| 374 |
+
# # 3) UI layer responsibility:
|
| 375 |
+
# # Render two buttons: YES / NO.
|
| 376 |
+
# # If YES: call this node again with user_action="yes"
|
| 377 |
+
# # If NO: call this node again with user_action="no" and then collect feedback.
|
| 378 |
+
|
| 379 |
+
# # For console fallback, we still allow text input:
|
| 380 |
+
# ans = input("Do you approve this plan? (YES/NO): ").strip().lower()
|
| 381 |
+
# approved = ans in ("yes", "y")
|
| 382 |
+
# state["_plan_approved"] = approved
|
| 383 |
+
|
| 384 |
+
# if not approved:
|
| 385 |
+
# # show feedback prompt
|
| 386 |
+
# feedback = input("Please provide feedback to improve the plan:\n")
|
| 387 |
+
# state["plan_feedback"] = feedback
|
| 388 |
+
# else:
|
| 389 |
+
# # clear any old feedback
|
| 390 |
+
# state["plan_feedback"] = ""
|
| 391 |
+
|
| 392 |
+
# return state
|
| 393 |
+
|
| 394 |
+
|
| 395 |
+
|
| 396 |
+
## **Enhance Plan**
|
| 397 |
+
def enhance_plan_node(state):
|
| 398 |
+
"""
|
| 399 |
+
Enhance‑Plan Node:
|
| 400 |
+
- Inputs:
|
| 401 |
+
• state["plan"]: the last JSON plan
|
| 402 |
+
• state["plan_feedback"]: user’s free‑text feedback on how to improve it
|
| 403 |
+
• state["domain"], state["topics_and_subtopics"], state["problem_statement"]
|
| 404 |
+
• state["dataset_type"], state["dataset_file_path"], state["data_dictionary"]
|
| 405 |
+
• state["additional_instructions"]
|
| 406 |
+
- Action: Re‑prompt the LLM, injecting the feedback to revise the plan.
|
| 407 |
+
- Output: Overwrites state["plan"] with the refined JSON,
|
| 408 |
+
clears plan_feedback, and resets state["_plan_approved"] to False.
|
| 409 |
+
"""
|
| 410 |
+
|
| 411 |
+
# 1. Extract only the needed inputs
|
| 412 |
+
original_plan = state["plan"]
|
| 413 |
+
feedback = state.get("plan_feedback", "")
|
| 414 |
+
domain = state["domain"]
|
| 415 |
+
topics = state["topics_and_subtopics"]
|
| 416 |
+
problem = state["problem_statement"]
|
| 417 |
+
dataset_type = state["dataset_type"]
|
| 418 |
+
dataset_path = state["dataset_file_path"]
|
| 419 |
+
data_dict = state.get("data_dictionary", {})
|
| 420 |
+
additional_instr = state.get("additional_instructions", "")
|
| 421 |
+
|
| 422 |
+
# 2. Build the enhancement prompt
|
| 423 |
+
prompt = f"""
|
| 424 |
+
You are an expert AI notebook planner. The user has reviewed the following plan
|
| 425 |
+
and given feedback on how to improve it:
|
| 426 |
+
|
| 427 |
+
---
|
| 428 |
+
**Original Plan** (JSON):
|
| 429 |
+
{json.dumps(original_plan, indent=2)}
|
| 430 |
+
|
| 431 |
+
**User Feedback**:
|
| 432 |
+
{feedback}
|
| 433 |
+
|
| 434 |
+
---
|
| 435 |
+
**Context for Revision**:
|
| 436 |
+
- Domain: {domain}
|
| 437 |
+
- Topics & Subtopics: {topics}
|
| 438 |
+
- Problem Statement: {problem}
|
| 439 |
+
- Dataset Type: {dataset_type}
|
| 440 |
+
- Dataset Location: {dataset_path}
|
| 441 |
+
- Data Dictionary:
|
| 442 |
+
{json.dumps(data_dict, indent=2)}
|
| 443 |
+
- Additional Instructions:
|
| 444 |
+
{additional_instr if additional_instr.strip() else "None"}
|
| 445 |
+
|
| 446 |
+
Please revise the original plan to address the user’s feedback.
|
| 447 |
+
- Keep the same JSON structure (top‑level "sections", each with "title", "type", "subsections").
|
| 448 |
+
- Adjust section titles, ordering, and code_rules as needed to satisfy the feedback.
|
| 449 |
+
- Do not add sections beyond the user’s specified topics.
|
| 450 |
+
- Return **only** the updated plan JSON — no extra commentary.
|
| 451 |
+
""".strip()
|
| 452 |
+
|
| 453 |
+
# 3. Invoke the LLM
|
| 454 |
+
response = model.invoke([SystemMessage(content=prompt)])
|
| 455 |
+
new_plan = extract_json_from_response(response.content)
|
| 456 |
+
|
| 457 |
+
# 4. Save and reset flags
|
| 458 |
+
state["plan"] = new_plan
|
| 459 |
+
# state["_plan_approved"] = False
|
| 460 |
+
# state["plan_feedback"] = ""
|
| 461 |
+
|
| 462 |
+
return state
|
| 463 |
+
|
| 464 |
+
|
| 465 |
+
|
| 466 |
+
## **Write Code**
|
| 467 |
+
def write_code_node(state: InteractiveCaseStudyState) -> InteractiveCaseStudyState:
|
| 468 |
+
"""
|
| 469 |
+
Write‑Code Node:
|
| 470 |
+
- Reads state["plan"] plus user inputs (dataset_type, dataset_file_path,
|
| 471 |
+
data_dictionary, domain, additional_instructions).
|
| 472 |
+
- Prompts the LLM to generate a flattened Jupyter notebook JSON skeleton (nbformat v4)
|
| 473 |
+
where each task becomes its own cell:
|
| 474 |
+
• A Markdown cell for each section heading (# Section)
|
| 475 |
+
• A Markdown cell for each subsection heading (## Subsection)
|
| 476 |
+
• For `type=="markdown"` subsections: one Markdown cell containing the description
|
| 477 |
+
• For `type=="code"` subsections: one code cell per rule, with inline comments
|
| 478 |
+
explaining each line of code
|
| 479 |
+
• A final Markdown cell `# Insights & Recommendations`
|
| 480 |
+
All code cells must have execution_count=null and outputs=[].
|
| 481 |
+
- Stores the result in state["raw_notebook"].
|
| 482 |
+
- Resets notebook review flags for the UI.
|
| 483 |
+
"""
|
| 484 |
+
|
| 485 |
+
# 1) Extract inputs
|
| 486 |
+
plan = state["plan"]
|
| 487 |
+
dataset_type = state["dataset_type"]
|
| 488 |
+
dataset_path = state["dataset_file_path"]
|
| 489 |
+
data_dict = state.get("data_dictionary", {})
|
| 490 |
+
domain = state["domain"]
|
| 491 |
+
additional_instr = state.get("additional_instructions", "")
|
| 492 |
+
|
| 493 |
+
|
| 494 |
+
# 2) Build the comprehensive prompt
|
| 495 |
+
prompt = f"""
|
| 496 |
+
You are an expert notebook generator. Generate a Jupyter notebook JSON (nbformat v4)
|
| 497 |
+
that implements the following plan exactly and by flattening each plan subsection into individual cells.
|
| 498 |
+
Do NOT include any outputs—only code & markdown cells.
|
| 499 |
+
|
| 500 |
+
PLAN:
|
| 501 |
+
{json.dumps(plan, indent=2)}
|
| 502 |
+
|
| 503 |
+
CONTEXT:
|
| 504 |
+
- Domain: {domain}
|
| 505 |
+
- Dataset type: {dataset_type}
|
| 506 |
+
- Dataset location: {dataset_path}
|
| 507 |
+
- Data Dictionary:
|
| 508 |
+
{json.dumps(data_dict, indent=2)}
|
| 509 |
+
- Additional Instructions:
|
| 510 |
+
{additional_instr if additional_instr.strip() else "None provided"}
|
| 511 |
+
|
| 512 |
+
OUTPUT SPEC:
|
| 513 |
+
1. Return only a valid notebook JSON with keys: nbformat, nbformat_minor, metadata, cells.
|
| 514 |
+
2. For each section in PLAN:
|
| 515 |
+
a. Add a **markdown** cell: `# <Section Title>`
|
| 516 |
+
b. For each subsection:
|
| 517 |
+
i. Add a **markdown** cell: `## <Subsection Title>`
|
| 518 |
+
ii. If subsection `type` is "markdown":
|
| 519 |
+
- Add a separate markdown cell with the `description` text.
|
| 520 |
+
iii. If subsection `type` is "code":
|
| 521 |
+
- For each rule in `code_rules`:
|
| 522 |
+
• Create one **code** cell.
|
| 523 |
+
• Write only the code implementing that rule.
|
| 524 |
+
• Ensure each line of code has an appropriate inline comment explaining its purpose.
|
| 525 |
+
3. After all sections, append one **markdown** cell:
|
| 526 |
+
`# Insights & Recommendations`
|
| 527 |
+
4. Each **code** cell must include:
|
| 528 |
+
- `"cell_type": "code"`, `"execution_count": null`, `"metadata": `, `"outputs": []`, `"source": […]`
|
| 529 |
+
5. Each **markdown** cell must include:
|
| 530 |
+
- `"cell_type": "markdown"`, `"metadata": `, `"source": […]`
|
| 531 |
+
|
| 532 |
+
Return **only** the complete notebook JSON object.
|
| 533 |
+
"""
|
| 534 |
+
|
| 535 |
+
# 3) Invoke the model
|
| 536 |
+
response = model.invoke([SystemMessage(content=prompt)])
|
| 537 |
+
|
| 538 |
+
# 4) Parse and store the raw notebook skeleton
|
| 539 |
+
raw_nb = extract_json_from_response(response.content)
|
| 540 |
+
state["raw_notebook"] = raw_nb
|
| 541 |
+
|
| 542 |
+
# 5) Reset review flags so the UI will prompt the user next
|
| 543 |
+
# state["_notebook_approved"] = False
|
| 544 |
+
# state["notebook_feedback"] = ""
|
| 545 |
+
|
| 546 |
+
return state
|
| 547 |
+
|
| 548 |
+
|
| 549 |
+
|
| 550 |
+
## **Review Notebook**
|
| 551 |
+
# def review_notebook_node(state: InteractiveCaseStudyState) -> InteractiveCaseStudyState:
|
| 552 |
+
# """
|
| 553 |
+
# Review-Notebook Node:
|
| 554 |
+
# - Reads state["raw_notebook"] (the skeleton JSON).
|
| 555 |
+
# - Expects the UI to render that notebook for the user.
|
| 556 |
+
# - Reads two new state flags set by the UI:
|
| 557 |
+
# • state["_notebook_approved"] (bool)
|
| 558 |
+
# • state["notebook_feedback"] (str, empty if approved)
|
| 559 |
+
# - Returns the updated state for downstream branching.
|
| 560 |
+
# """
|
| 561 |
+
|
| 562 |
+
# # 1) Sanity check
|
| 563 |
+
# raw_nb = state.get("raw_notebook")
|
| 564 |
+
# if raw_nb is None:
|
| 565 |
+
# raise ValueError("No raw_notebook found in state")
|
| 566 |
+
|
| 567 |
+
# # At this point your Streamlit app should:
|
| 568 |
+
# # • Render `raw_nb` as a notebook preview
|
| 569 |
+
# # • Present two buttons: “Yes” and “No”
|
| 570 |
+
# # • If “No” is clicked, show a text_area for feedback
|
| 571 |
+
|
| 572 |
+
# # 2) Read back user’s choice from the state (set by the UI)
|
| 573 |
+
# approved = state.get("_notebook_approved", False)
|
| 574 |
+
# feedback = state.get("notebook_feedback", "").strip()
|
| 575 |
+
|
| 576 |
+
# # 3) Ensure feedback is only set when rejected
|
| 577 |
+
# if approved:
|
| 578 |
+
# # clear any spurious feedback
|
| 579 |
+
# state["notebook_feedback"] = ""
|
| 580 |
+
# else:
|
| 581 |
+
# # if user hasn’t provided feedback yet, ensure it’s initialized
|
| 582 |
+
# state["notebook_feedback"] = feedback
|
| 583 |
+
|
| 584 |
+
# # 4) State flags remain as set by the UI.
|
| 585 |
+
# # Downstream graph will branch on _notebook_approved.
|
| 586 |
+
|
| 587 |
+
# return state
|
| 588 |
+
|
| 589 |
+
|
| 590 |
+
|
| 591 |
+
## **Modify Notebook**
|
| 592 |
+
def modify_notebook_node(state: InteractiveCaseStudyState) -> InteractiveCaseStudyState:
|
| 593 |
+
"""
|
| 594 |
+
Modify-Notebook Node:
|
| 595 |
+
- Reads the existing notebook skeleton from state["raw_notebook"].
|
| 596 |
+
- Reads user feedback from state["notebook_feedback"].
|
| 597 |
+
- Uses additional context (domain, topics, problem, dataset_path, data_dictionary)
|
| 598 |
+
to re-prompt the LLM to refine the notebook skeleton.
|
| 599 |
+
- Overwrites state["raw_notebook"] with the updated JSON.
|
| 600 |
+
- Resets approval flag for the next review cycle.
|
| 601 |
+
"""
|
| 602 |
+
|
| 603 |
+
# 1. Extract required fields
|
| 604 |
+
raw_nb = state.get("raw_notebook")
|
| 605 |
+
feedback = state.get("notebook_feedback", "").strip()
|
| 606 |
+
domain = state.get("domain", "")
|
| 607 |
+
topics = state.get("topics_and_subtopics", "")
|
| 608 |
+
problem = state.get("problem_statement", "")
|
| 609 |
+
dataset_path = state.get("dataset_file_path", "")
|
| 610 |
+
data_dict = state.get("data_dictionary", {})
|
| 611 |
+
|
| 612 |
+
if raw_nb is None:
|
| 613 |
+
raise ValueError("No raw_notebook in state to modify")
|
| 614 |
+
if not feedback:
|
| 615 |
+
raise ValueError("No notebook_feedback in state to guide modification")
|
| 616 |
+
|
| 617 |
+
# 2. Build the prompt with extra context
|
| 618 |
+
prompt = f"""
|
| 619 |
+
You are an expert AI notebook refiner. The user is working in the **{domain}** domain,
|
| 620 |
+
with the following context:
|
| 621 |
+
|
| 622 |
+
• **Topics & Subtopics:**
|
| 623 |
+
{topics}
|
| 624 |
+
|
| 625 |
+
• **Problem Statement:**
|
| 626 |
+
{problem}
|
| 627 |
+
|
| 628 |
+
• **Dataset Location:**
|
| 629 |
+
{dataset_path}
|
| 630 |
+
|
| 631 |
+
• **Data Dictionary:**
|
| 632 |
+
{json.dumps(data_dict, indent=2)}
|
| 633 |
+
|
| 634 |
+
The user reviewed the current Jupyter notebook skeleton (nbformat v4).
|
| 635 |
+
They provided the following feedback on what to change:
|
| 636 |
+
|
| 637 |
+
---
|
| 638 |
+
**User Feedback:**
|
| 639 |
+
{feedback}
|
| 640 |
+
|
| 641 |
+
---
|
| 642 |
+
**Current Notebook Skeleton (JSON):**
|
| 643 |
+
{json.dumps(raw_nb, indent=2)}
|
| 644 |
+
|
| 645 |
+
---
|
| 646 |
+
Please produce a revised notebook skeleton JSON that:
|
| 647 |
+
1. Incorporates the user’s feedback precisely.
|
| 648 |
+
2. Maintains valid nbformat v4 structure (keys: nbformat, nbformat_minor, metadata, cells).
|
| 649 |
+
3. Preserves existing code and markdown formatting except where edits are requested.
|
| 650 |
+
4. Uses domain‑appropriate libraries and patterns (per the domain context above).
|
| 651 |
+
5. Outputs **only** the updated JSON object — no extra text or commentary.
|
| 652 |
+
"""
|
| 653 |
+
|
| 654 |
+
# 3. Invoke the LLM
|
| 655 |
+
response = model.invoke([SystemMessage(content=prompt)])
|
| 656 |
+
updated_nb = extract_json_from_response(response.content)
|
| 657 |
+
|
| 658 |
+
# 4. Store updated skeleton & reset approval flag
|
| 659 |
+
state["raw_notebook"] = updated_nb
|
| 660 |
+
state["_notebook_approved"] = False
|
| 661 |
+
|
| 662 |
+
return state
|
| 663 |
+
|
| 664 |
+
|
| 665 |
+
|
| 666 |
+
# # **Execute Notebook**
|
| 667 |
+
# def execute_notebook_node(state: InteractiveCaseStudyState) -> InteractiveCaseStudyState:
|
| 668 |
+
# """
|
| 669 |
+
# Execute-Notebook Node:
|
| 670 |
+
# - Reads state['raw_notebook'] (nbformat v4 JSON, cells with empty outputs).
|
| 671 |
+
# - Writes it to a timestamped temp .ipynb alongside the dataset.
|
| 672 |
+
# - Runs it via Papermill, capturing outputs.
|
| 673 |
+
# - On success: stores full executed notebook in state['executed_notebook'].
|
| 674 |
+
# - On failure: stores the partially executed notebook in state['executed_notebook']
|
| 675 |
+
# and the exception text in state['execution_error'].
|
| 676 |
+
# """
|
| 677 |
+
|
| 678 |
+
# # 1. Extract inputs
|
| 679 |
+
# raw_nb = state.get("raw_notebook", {})
|
| 680 |
+
# dataset_path = state.get("dataset_file_path", "")
|
| 681 |
+
|
| 682 |
+
# # 2. Prepare unique temp directory
|
| 683 |
+
# run_id = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 684 |
+
# tmp_dir = f"tmp_notebooks/run_{run_id}"
|
| 685 |
+
# os.makedirs(tmp_dir, exist_ok=True)
|
| 686 |
+
|
| 687 |
+
# raw_path = os.path.join(tmp_dir, "raw_notebook.ipynb")
|
| 688 |
+
# exec_path = os.path.join(tmp_dir, "executed_notebook.ipynb")
|
| 689 |
+
|
| 690 |
+
# # 3. Write the skeleton notebook to disk
|
| 691 |
+
# nb_node = nbformat.from_dict(raw_nb)
|
| 692 |
+
# nb_node.metadata.setdefault("kernelspec", {"name": "python3"})
|
| 693 |
+
# nb_node.metadata.setdefault("language_info", {"name": "python"})
|
| 694 |
+
# with open(raw_path, "w", encoding="utf-8") as f:
|
| 695 |
+
# nbformat.write(nb_node, f)
|
| 696 |
+
|
| 697 |
+
# # 4. Stage dataset file
|
| 698 |
+
# if dataset_path and os.path.exists(dataset_path):
|
| 699 |
+
# shutil.copy(dataset_path, tmp_dir)
|
| 700 |
+
|
| 701 |
+
# # 5. Execute via Papermill
|
| 702 |
+
# try:
|
| 703 |
+
# pm.execute_notebook(
|
| 704 |
+
# input_path=raw_path,
|
| 705 |
+
# output_path=exec_path,
|
| 706 |
+
# parameters={},
|
| 707 |
+
# log_output=True,
|
| 708 |
+
# kernel_name="python3"
|
| 709 |
+
# )
|
| 710 |
+
# # 6a. Success: read full executed notebook
|
| 711 |
+
# executed_nb = nbformat.read(exec_path, as_version=4)
|
| 712 |
+
# state["executed_notebook"] = nbformat.writes(executed_nb)
|
| 713 |
+
# state.pop("execution_error", None)
|
| 714 |
+
# except Exception as e:
|
| 715 |
+
# # 6b. Failure: read whatever Papermill wrote, or fallback to skeleton
|
| 716 |
+
# if os.path.exists(exec_path):
|
| 717 |
+
# partial_nb = nbformat.read(exec_path, as_version=4)
|
| 718 |
+
# else:
|
| 719 |
+
# partial_nb = nbformat.read(raw_path, as_version=4)
|
| 720 |
+
|
| 721 |
+
# state["executed_notebook"] = nbformat.writes(partial_nb)
|
| 722 |
+
# state["execution_error"] = str(e)
|
| 723 |
+
|
| 724 |
+
# return state
|
| 725 |
+
|
| 726 |
+
|
| 727 |
+
import os
|
| 728 |
+
import sys
|
| 729 |
+
import subprocess
|
| 730 |
+
import shutil
|
| 731 |
+
import json
|
| 732 |
+
import re
|
| 733 |
+
import nbformat
|
| 734 |
+
from pathlib import Path
|
| 735 |
+
from datetime import datetime
|
| 736 |
+
import papermill as pm
|
| 737 |
+
|
| 738 |
+
def execute_notebook_node(state: InteractiveCaseStudyState) -> InteractiveCaseStudyState:
|
| 739 |
+
"""
|
| 740 |
+
Execute-Notebook Node with venv isolation and proper pip-install handling:
|
| 741 |
+
- Reads state['raw_notebook'] (nbformat v4 JSON).
|
| 742 |
+
- Writes it to a timestamped temp folder as raw_notebook.ipynb.
|
| 743 |
+
- Creates a Python venv in that folder.
|
| 744 |
+
- Detects any '!pip install' lines: extracts package names and installs them in venv.
|
| 745 |
+
- Comments out the original '!pip install' lines in the notebook to avoid re-running them.
|
| 746 |
+
- Registers an ipykernel for the venv (if possible).
|
| 747 |
+
- Runs the notebook via Papermill under that venv/kernel, capturing outputs.
|
| 748 |
+
- On success: stores executed notebook JSON in state["executed_notebook"], clears state["execution_error"].
|
| 749 |
+
- On failure: injects an error markdown cell at the top, stores partially executed notebook JSON in state["executed_notebook"], and error text in state["execution_error"].
|
| 750 |
+
"""
|
| 751 |
+
raw_nb = state.get("raw_notebook", {})
|
| 752 |
+
dataset_path = state.get("dataset_file_path", "")
|
| 753 |
+
|
| 754 |
+
# 1. Prepare unique temp directory
|
| 755 |
+
run_id = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 756 |
+
tmp_dir = Path(f"tmp_notebooks/run_{run_id}")
|
| 757 |
+
tmp_dir.mkdir(parents=True, exist_ok=True)
|
| 758 |
+
|
| 759 |
+
raw_path = tmp_dir / "raw_notebook.ipynb"
|
| 760 |
+
exec_path = tmp_dir / "executed_notebook.ipynb"
|
| 761 |
+
|
| 762 |
+
# 2. Write the skeleton notebook to disk
|
| 763 |
+
nb_node = nbformat.from_dict(raw_nb)
|
| 764 |
+
nb_node.metadata.setdefault("kernelspec", {"name": "python3"})
|
| 765 |
+
nb_node.metadata.setdefault("language_info", {"name": "python"})
|
| 766 |
+
with raw_path.open("w", encoding="utf-8") as f:
|
| 767 |
+
nbformat.write(nb_node, f)
|
| 768 |
+
|
| 769 |
+
# 3. Stage dataset file if exists
|
| 770 |
+
if dataset_path and os.path.exists(dataset_path):
|
| 771 |
+
try:
|
| 772 |
+
shutil.copy(dataset_path, tmp_dir)
|
| 773 |
+
except Exception:
|
| 774 |
+
pass # ignore if copy fails
|
| 775 |
+
|
| 776 |
+
# 4. Helpers to detect and comment out '!pip install' lines
|
| 777 |
+
def extract_pip_packages(ipynb_path: Path) -> list:
|
| 778 |
+
"""Return list of package strings from '!pip install ...' lines."""
|
| 779 |
+
pkgs = []
|
| 780 |
+
try:
|
| 781 |
+
notebook = json.loads(ipynb_path.read_text(encoding="utf-8"))
|
| 782 |
+
except Exception:
|
| 783 |
+
return pkgs
|
| 784 |
+
pattern = r'!pip\s+install\s+(.+)'
|
| 785 |
+
for cell in notebook.get('cells', []):
|
| 786 |
+
if cell.get('cell_type') == 'code':
|
| 787 |
+
source = ''.join(cell.get('source', []))
|
| 788 |
+
for line in source.splitlines():
|
| 789 |
+
m = re.match(pattern, line.strip())
|
| 790 |
+
if m:
|
| 791 |
+
rest = m.group(1).strip()
|
| 792 |
+
# Remove inline comments after '#'
|
| 793 |
+
rest = rest.split('#', 1)[0].strip()
|
| 794 |
+
# Split by whitespace to get package tokens
|
| 795 |
+
parts = rest.split()
|
| 796 |
+
for p in parts:
|
| 797 |
+
if p:
|
| 798 |
+
pkgs.append(p)
|
| 799 |
+
return pkgs
|
| 800 |
+
|
| 801 |
+
def comment_out_pip_lines(ipynb_path: Path):
|
| 802 |
+
"""
|
| 803 |
+
Modify the notebook in-place: prefix any '!pip install...' lines with '# ',
|
| 804 |
+
preserving indentation, so they won't run.
|
| 805 |
+
"""
|
| 806 |
+
try:
|
| 807 |
+
nb = nbformat.read(str(ipynb_path), as_version=4)
|
| 808 |
+
except Exception:
|
| 809 |
+
return
|
| 810 |
+
modified = False
|
| 811 |
+
for cell in nb.cells:
|
| 812 |
+
if cell.cell_type == 'code':
|
| 813 |
+
lines = cell.source.splitlines()
|
| 814 |
+
new_lines = []
|
| 815 |
+
for line in lines:
|
| 816 |
+
# Detect leading whitespace
|
| 817 |
+
leading_ws = line[:len(line) - len(line.lstrip())]
|
| 818 |
+
stripped = line.lstrip()
|
| 819 |
+
if stripped.startswith("!pip install"):
|
| 820 |
+
# Comment out, preserving indentation
|
| 821 |
+
new_line = leading_ws + "# " + stripped
|
| 822 |
+
new_lines.append(new_line)
|
| 823 |
+
modified = True
|
| 824 |
+
else:
|
| 825 |
+
new_lines.append(line)
|
| 826 |
+
cell.source = "\n".join(new_lines)
|
| 827 |
+
if modified:
|
| 828 |
+
try:
|
| 829 |
+
nbformat.write(nb, str(ipynb_path))
|
| 830 |
+
except Exception:
|
| 831 |
+
pass
|
| 832 |
+
|
| 833 |
+
try:
|
| 834 |
+
# 5. Create and prepare virtual environment
|
| 835 |
+
env_path = tmp_dir / "venv"
|
| 836 |
+
if not env_path.exists():
|
| 837 |
+
subprocess.run([sys.executable, "-m", "venv", str(env_path)], check=True)
|
| 838 |
+
|
| 839 |
+
# Determine python executable in venv
|
| 840 |
+
if os.name == "nt":
|
| 841 |
+
python_exec = env_path / "Scripts" / "python.exe"
|
| 842 |
+
else:
|
| 843 |
+
python_exec = env_path / "bin" / "python"
|
| 844 |
+
|
| 845 |
+
# 6. Bootstrap pip in venv (if needed)
|
| 846 |
+
try:
|
| 847 |
+
subprocess.run([str(python_exec), "-m", "ensurepip", "--upgrade"], check=True)
|
| 848 |
+
except Exception:
|
| 849 |
+
pass
|
| 850 |
+
try:
|
| 851 |
+
subprocess.run([str(python_exec), "-m", "pip", "install", "--upgrade", "pip"], check=True)
|
| 852 |
+
except Exception:
|
| 853 |
+
pass
|
| 854 |
+
|
| 855 |
+
# 7. Detect packages from '!pip install' lines and install them in venv
|
| 856 |
+
packages = extract_pip_packages(raw_path)
|
| 857 |
+
if packages:
|
| 858 |
+
install_cmd = [str(python_exec), "-m", "pip", "install"] + packages
|
| 859 |
+
try:
|
| 860 |
+
subprocess.run(install_cmd, check=True, capture_output=True, text=True)
|
| 861 |
+
except subprocess.CalledProcessError as e:
|
| 862 |
+
err = e.stderr or e.stdout or str(e)
|
| 863 |
+
# Record installation error in state; execution may still proceed and fail later
|
| 864 |
+
prev = state.get("execution_error", "")
|
| 865 |
+
state["execution_error"] = (prev + "\n" if prev else "") + f"Failed to install packages {packages}: {err}"
|
| 866 |
+
# After installing, comment out original pip lines so notebook won't try again
|
| 867 |
+
comment_out_pip_lines(raw_path)
|
| 868 |
+
|
| 869 |
+
# 8. Install papermill, ipykernel, nbformat in venv so we can run papermill under venv
|
| 870 |
+
try:
|
| 871 |
+
subprocess.run(
|
| 872 |
+
[str(python_exec), "-m", "pip", "install", "papermill", "ipykernel", "nbformat"],
|
| 873 |
+
check=True, capture_output=True, text=True
|
| 874 |
+
)
|
| 875 |
+
except Exception as e:
|
| 876 |
+
err = getattr(e, 'stderr', None) or str(e)
|
| 877 |
+
prev = state.get("execution_error", "")
|
| 878 |
+
state["execution_error"] = (prev + "\n" if prev else "") + f"Failed to install papermill/ipykernel: {err}"
|
| 879 |
+
|
| 880 |
+
# 9. Register ipykernel for this venv (optional; if fails, Papermill may use default kernel)
|
| 881 |
+
kernel_name = f"venv_{run_id}"
|
| 882 |
+
try:
|
| 883 |
+
subprocess.run(
|
| 884 |
+
[
|
| 885 |
+
str(python_exec), "-m", "ipykernel", "install",
|
| 886 |
+
"--user",
|
| 887 |
+
"--name", kernel_name,
|
| 888 |
+
"--display-name", f"Python ({kernel_name})"
|
| 889 |
+
],
|
| 890 |
+
check=True, capture_output=True, text=True
|
| 891 |
+
)
|
| 892 |
+
except Exception as e:
|
| 893 |
+
err = getattr(e, 'stderr', None) or str(e)
|
| 894 |
+
prev = state.get("execution_error", "")
|
| 895 |
+
state["execution_error"] = (prev + "\n" if prev else "") + f"Failed to register ipykernel: {err}"
|
| 896 |
+
|
| 897 |
+
# 10. Execute via Papermill under venv
|
| 898 |
+
input_nb = raw_path.resolve()
|
| 899 |
+
output_nb = exec_path.resolve()
|
| 900 |
+
cmd = [str(python_exec), "-m", "papermill", str(input_nb), str(output_nb), "-k", kernel_name]
|
| 901 |
+
try:
|
| 902 |
+
result = subprocess.run(
|
| 903 |
+
cmd,
|
| 904 |
+
capture_output=True,
|
| 905 |
+
text=True,
|
| 906 |
+
cwd=str(tmp_dir),
|
| 907 |
+
timeout=60 * 30 # adjust as needed
|
| 908 |
+
)
|
| 909 |
+
stderr = result.stderr or ""
|
| 910 |
+
returncode = result.returncode
|
| 911 |
+
|
| 912 |
+
# 11. Read output notebook if created
|
| 913 |
+
if output_nb.exists():
|
| 914 |
+
executed_nb = nbformat.read(str(output_nb), as_version=4)
|
| 915 |
+
else:
|
| 916 |
+
# Fallback to raw or partially executed
|
| 917 |
+
executed_nb = nbformat.read(str(raw_path), as_version=4)
|
| 918 |
+
|
| 919 |
+
# 12. Handle return code: if non-zero, inject error cell
|
| 920 |
+
if returncode != 0:
|
| 921 |
+
error_msg = f"Papermill exited with code {returncode}.\nStderr:\n{stderr}"
|
| 922 |
+
err_cell = nbformat.v4.new_markdown_cell(f"**Execution Error:**\n```\n{error_msg}\n```")
|
| 923 |
+
executed_nb.cells.insert(0, err_cell)
|
| 924 |
+
prev = state.get("execution_error", "")
|
| 925 |
+
state["execution_error"] = (prev + "\n" if prev else "") + error_msg
|
| 926 |
+
else:
|
| 927 |
+
# Success: clear any previous execution_error
|
| 928 |
+
state.pop("execution_error", None)
|
| 929 |
+
|
| 930 |
+
# 13. Save executed notebook JSON into state
|
| 931 |
+
state["executed_notebook"] = nbformat.writes(executed_nb)
|
| 932 |
+
|
| 933 |
+
except subprocess.TimeoutExpired as te:
|
| 934 |
+
# Timeout: capture partial output and inject timeout error
|
| 935 |
+
if exec_path.exists():
|
| 936 |
+
partial_nb = nbformat.read(str(exec_path), as_version=4)
|
| 937 |
+
else:
|
| 938 |
+
partial_nb = nbformat.read(str(raw_path), as_version=4)
|
| 939 |
+
timeout_msg = f"Timeout: notebook execution exceeded limit ({te})."
|
| 940 |
+
err_cell = nbformat.v4.new_markdown_cell(f"**Execution Error:**\n```\n{timeout_msg}\n```")
|
| 941 |
+
partial_nb.cells.insert(0, err_cell)
|
| 942 |
+
state["executed_notebook"] = nbformat.writes(partial_nb)
|
| 943 |
+
state["execution_error"] = timeout_msg
|
| 944 |
+
|
| 945 |
+
except Exception as e:
|
| 946 |
+
# General execution error: inject error cell
|
| 947 |
+
if exec_path.exists():
|
| 948 |
+
partial_nb = nbformat.read(str(exec_path), as_version=4)
|
| 949 |
+
else:
|
| 950 |
+
partial_nb = nbformat.read(str(raw_path), as_version=4)
|
| 951 |
+
err = str(e)
|
| 952 |
+
err_cell = nbformat.v4.new_markdown_cell(f"**Execution Error:**\n```\n{err}\n```")
|
| 953 |
+
partial_nb.cells.insert(0, err_cell)
|
| 954 |
+
state["executed_notebook"] = nbformat.writes(partial_nb)
|
| 955 |
+
state["execution_error"] = err
|
| 956 |
+
|
| 957 |
+
except Exception as e:
|
| 958 |
+
# Any errors in venv setup or earlier steps: inject error at top of raw notebook
|
| 959 |
+
raw_nb_node = nbformat.from_dict(raw_nb)
|
| 960 |
+
err = str(e)
|
| 961 |
+
err_cell = nbformat.v4.new_markdown_cell(f"**Execution Setup Error:**\n```\n{err}\n```")
|
| 962 |
+
raw_nb_node.cells.insert(0, err_cell)
|
| 963 |
+
state["executed_notebook"] = nbformat.writes(raw_nb_node)
|
| 964 |
+
state["execution_error"] = f"Setup failed: {err}"
|
| 965 |
+
|
| 966 |
+
return state
|
| 967 |
+
|
| 968 |
+
|
| 969 |
+
|
| 970 |
+
## **Review Execution**
|
| 971 |
+
# def review_execution_node(state: InteractiveCaseStudyState) -> InteractiveCaseStudyState:
|
| 972 |
+
# """
|
| 973 |
+
# Review-Execution Node:
|
| 974 |
+
# - Reads state['executed_notebook'] (the .ipynb JSON with outputs).
|
| 975 |
+
# - Reads state.get('execution_error') to know if something failed.
|
| 976 |
+
# - Updates:
|
| 977 |
+
# state['_execution_approved']: bool
|
| 978 |
+
# state['execution_feedback']: str (if any)
|
| 979 |
+
# """
|
| 980 |
+
|
| 981 |
+
# # 1. Extract executed notebook and error flag
|
| 982 |
+
# executed_nb_json = state.get("executed_notebook", "")
|
| 983 |
+
# error_msg = state.get("execution_error")
|
| 984 |
+
|
| 985 |
+
# # 2. Validate JSON
|
| 986 |
+
# try:
|
| 987 |
+
# nb = json.loads(executed_nb_json)
|
| 988 |
+
# except Exception:
|
| 989 |
+
# # Malformed notebook JSON → automatic reject
|
| 990 |
+
# state["_execution_approved"] = False
|
| 991 |
+
# state["execution_feedback"] = "Executed notebook JSON could not be parsed."
|
| 992 |
+
# return state
|
| 993 |
+
|
| 994 |
+
# # 3. Decide success vs failure
|
| 995 |
+
# if error_msg:
|
| 996 |
+
# # FAILURE scenario
|
| 997 |
+
# # In Streamlit you’d render `nb` plus highlight error_msg prominently.
|
| 998 |
+
# print("😢 Sadly, an error occurred while executing the notebook:")
|
| 999 |
+
# print(f">>> {error_msg}\n")
|
| 1000 |
+
# # Prompt user: feedback or auto‑handle
|
| 1001 |
+
# ans = input(
|
| 1002 |
+
# "Do you have feedback on this error, or should I handle it myself? (feedback/auto): "
|
| 1003 |
+
# ).strip().lower()
|
| 1004 |
+
# if ans == "auto":
|
| 1005 |
+
# # Let the system attempt auto‑correction
|
| 1006 |
+
# state["_execution_approved"] = False
|
| 1007 |
+
# state["execution_feedback"] = "" # no user feedback
|
| 1008 |
+
# else:
|
| 1009 |
+
# # Collect user feedback
|
| 1010 |
+
# feedback = input("Please describe how I should fix this:\n").strip()
|
| 1011 |
+
# state["_execution_approved"] = False
|
| 1012 |
+
# state["execution_feedback"] = feedback or "No feedback provided."
|
| 1013 |
+
# else:
|
| 1014 |
+
# # SUCCESS scenario
|
| 1015 |
+
# # Streamlit: celebrate with "Notebook executed successfully end‑to‑end!"
|
| 1016 |
+
# print("🎉 Notebook executed successfully end‑to‑end!")
|
| 1017 |
+
# # Ask for final proceed approval
|
| 1018 |
+
# ans = input("Shall I proceed to write the insights? (YES/NO): ").strip().lower()
|
| 1019 |
+
# state["_execution_approved"] = (ans in ("yes", "y"))
|
| 1020 |
+
# if not state["_execution_approved"]:
|
| 1021 |
+
# # If user declines, optionally capture why
|
| 1022 |
+
# feedback = input("Please share any concerns before writing insights:\n").strip()
|
| 1023 |
+
# state["execution_feedback"] = feedback or ""
|
| 1024 |
+
|
| 1025 |
+
# return state
|
| 1026 |
+
|
| 1027 |
+
|
| 1028 |
+
|
| 1029 |
+
## **Correct Notebook**
|
| 1030 |
+
def correct_notebook_node(state: InteractiveCaseStudyState) -> InteractiveCaseStudyState:
|
| 1031 |
+
"""
|
| 1032 |
+
Correct-Notebook Node:
|
| 1033 |
+
- Inputs:
|
| 1034 |
+
• state['raw_notebook']: the notebook skeleton JSON (cells only, no outputs)
|
| 1035 |
+
• state['execution_error']: the error string produced by Papermill
|
| 1036 |
+
• state['execution_feedback']: optional user feedback string from review_execution_node
|
| 1037 |
+
- Action:
|
| 1038 |
+
• Prompt an LLM to minimally patch the notebook JSON to fix errors.
|
| 1039 |
+
- Output:
|
| 1040 |
+
• Overwrites state['raw_notebook'] with corrected notebook JSON.
|
| 1041 |
+
• Resets state['_execution_approved'] to False to trigger re-execution.
|
| 1042 |
+
"""
|
| 1043 |
+
|
| 1044 |
+
# 1. Extract from state
|
| 1045 |
+
raw_nb = state.get("raw_notebook", {})
|
| 1046 |
+
exec_error = state.get("execution_error", "").strip()
|
| 1047 |
+
feedback = state.get("execution_feedback", "").strip()
|
| 1048 |
+
|
| 1049 |
+
if not raw_nb:
|
| 1050 |
+
raise ValueError("Missing required key: 'raw_notebook'")
|
| 1051 |
+
|
| 1052 |
+
# 2. Build the LLM prompt
|
| 1053 |
+
prompt = f"""
|
| 1054 |
+
You are a Python notebook repair assistant.
|
| 1055 |
+
|
| 1056 |
+
Below is the original notebook code (nbformat v4 JSON):
|
| 1057 |
+
{json.dumps(raw_nb, indent=2)}
|
| 1058 |
+
|
| 1059 |
+
The following error occurred during execution:
|
| 1060 |
+
{exec_error or '(no error provided)'}
|
| 1061 |
+
|
| 1062 |
+
{('In addition, the user provided the following correction feedback:' if feedback else 'The user did not provide any manual feedback.')}
|
| 1063 |
+
|
| 1064 |
+
Please return a corrected version of the notebook (still in nbformat v4 JSON).
|
| 1065 |
+
Make only the **minimal** necessary changes to fix the above error and satisfy the feedback if present.
|
| 1066 |
+
Preserve all other cell content and metadata exactly as they are.
|
| 1067 |
+
|
| 1068 |
+
Return only the JSON. Do not include any extra explanation or comments.
|
| 1069 |
+
"""
|
| 1070 |
+
|
| 1071 |
+
# 3. Send to LLM
|
| 1072 |
+
response = model.invoke([SystemMessage(content=prompt)])
|
| 1073 |
+
corrected_nb = extract_json_from_response(response.content)
|
| 1074 |
+
|
| 1075 |
+
# 4. Save updated notebook back into state
|
| 1076 |
+
state["raw_notebook"] = corrected_nb
|
| 1077 |
+
state["_execution_approved"] = False # force re-execution after correction
|
| 1078 |
+
|
| 1079 |
+
return state
|
| 1080 |
+
|
| 1081 |
+
|
| 1082 |
+
|
| 1083 |
+
## **Write Insights**
|
| 1084 |
+
# ─────────────── write_insights_node ──────────────────
|
| 1085 |
+
def write_insights_node(state: InteractiveCaseStudyState) -> InteractiveCaseStudyState:
|
| 1086 |
+
"""
|
| 1087 |
+
Write-Insights Node:
|
| 1088 |
+
- Inputs: state['executed_notebook'] (JSON str), state['problem_statement'], state['dataset_path']
|
| 1089 |
+
- Stages dataset for any REPL plotting.
|
| 1090 |
+
- For each code cell with outputs:
|
| 1091 |
+
• If plot: re‑execute all code so far via PythonREPL agent → insight.
|
| 1092 |
+
• Else: call LLM with problem, prior observations, code, and raw output.
|
| 1093 |
+
- Inserts a markdown “Observation” after each code cell.
|
| 1094 |
+
- Appends a final "# Insights and Conclusion" cell.
|
| 1095 |
+
- Sets state['final_notebook'] to the updated notebook JSON (as Python dict).
|
| 1096 |
+
"""
|
| 1097 |
+
# 1) Load executed notebook JSON
|
| 1098 |
+
executed_nb_str = state.get("executed_notebook", "")
|
| 1099 |
+
executed_nb = json.loads(executed_nb_str)
|
| 1100 |
+
|
| 1101 |
+
problem_stmt = state.get("problem_statement", "")
|
| 1102 |
+
dataset_path = state.get("dataset_file_path", "")
|
| 1103 |
+
cells = executed_nb.get("cells", [])
|
| 1104 |
+
|
| 1105 |
+
# 2) Prepare for plot‑REPL (commented out if not needed here)
|
| 1106 |
+
work_dir = "tmp_repl"
|
| 1107 |
+
# stage_dependencies(dataset_path, work_dir)
|
| 1108 |
+
# plot_executor = build_plot_insight_agent_executor(model)
|
| 1109 |
+
|
| 1110 |
+
updated_cells = []
|
| 1111 |
+
context_history = [] # list of past observation strings
|
| 1112 |
+
code_history = [] # list of past code sources
|
| 1113 |
+
|
| 1114 |
+
for cell in cells:
|
| 1115 |
+
updated_cells.append(cell)
|
| 1116 |
+
|
| 1117 |
+
if cell.get("cell_type") == "code" and cell.get("outputs"):
|
| 1118 |
+
code_src = "".join(cell.get("source", []))
|
| 1119 |
+
code_history.append(code_src)
|
| 1120 |
+
|
| 1121 |
+
# aggregate textual outputs
|
| 1122 |
+
raw_out = ""
|
| 1123 |
+
for out in cell["outputs"]:
|
| 1124 |
+
if out.get("output_type") == "stream":
|
| 1125 |
+
raw_out += "".join(out.get("text", []))
|
| 1126 |
+
elif out.get("output_type") == "execute_result":
|
| 1127 |
+
raw_out += "".join(out.get("data", {}).get("text/plain", []))
|
| 1128 |
+
|
| 1129 |
+
if is_plot_code(code_src):
|
| 1130 |
+
# Commented out observation writing for plot cells
|
| 1131 |
+
# combined = "\n".join(code_history)
|
| 1132 |
+
# cwd = os.getcwd()
|
| 1133 |
+
# os.chdir(work_dir)
|
| 1134 |
+
# tool_call = plot_executor.invoke({"input": combined})
|
| 1135 |
+
# obs = tool_call.get("output", "").strip()
|
| 1136 |
+
# os.chdir(cwd)
|
| 1137 |
+
obs = "" # No observation added for plot cells
|
| 1138 |
+
else:
|
| 1139 |
+
prior = "\n".join(f"- {o}" for o in context_history)
|
| 1140 |
+
prompt = f"""
|
| 1141 |
+
You are an AI assistant writing a single one line bullet‑point observation (no code).
|
| 1142 |
+
|
| 1143 |
+
Problem Statement:
|
| 1144 |
+
{problem_stmt}
|
| 1145 |
+
|
| 1146 |
+
Prior Observations:
|
| 1147 |
+
{prior}
|
| 1148 |
+
|
| 1149 |
+
Current Code:
|
| 1150 |
+
{code_src}
|
| 1151 |
+
|
| 1152 |
+
Current Output:
|
| 1153 |
+
{raw_out}
|
| 1154 |
+
|
| 1155 |
+
Write one concise, beginner‑friendly bullet‑point that interprets this output
|
| 1156 |
+
in context of the problem.
|
| 1157 |
+
"""
|
| 1158 |
+
resp = model.invoke([SystemMessage(content=prompt)])
|
| 1159 |
+
obs = resp.content.strip()
|
| 1160 |
+
|
| 1161 |
+
if obs:
|
| 1162 |
+
observation_cell = {
|
| 1163 |
+
"cell_type": "markdown",
|
| 1164 |
+
"metadata": {},
|
| 1165 |
+
"source": [f"**Observation:** {obs}\n"]
|
| 1166 |
+
}
|
| 1167 |
+
updated_cells.append(observation_cell)
|
| 1168 |
+
context_history.append(obs)
|
| 1169 |
+
|
| 1170 |
+
# Final summary section
|
| 1171 |
+
all_obs = "\n".join(f"- {o}" for o in context_history)
|
| 1172 |
+
summary_prompt = f"""
|
| 1173 |
+
You are an AI assistant composing the final **Insights and Conclusion** section.
|
| 1174 |
+
|
| 1175 |
+
Problem Statement:
|
| 1176 |
+
{problem_stmt}
|
| 1177 |
+
|
| 1178 |
+
Observations:
|
| 1179 |
+
{all_obs}
|
| 1180 |
+
|
| 1181 |
+
Provide a short set of business‑focused recommendations and a conclusion.
|
| 1182 |
+
"""
|
| 1183 |
+
summary_resp = model.invoke([SystemMessage(content=summary_prompt)])
|
| 1184 |
+
summary_text = summary_resp.content.strip()
|
| 1185 |
+
summary_cell = {
|
| 1186 |
+
"cell_type": "markdown",
|
| 1187 |
+
"metadata": {},
|
| 1188 |
+
"source": [f"# Insights and Conclusion\n\n{summary_text}\n"]
|
| 1189 |
+
}
|
| 1190 |
+
updated_cells.append(summary_cell)
|
| 1191 |
+
|
| 1192 |
+
executed_nb["cells"] = updated_cells
|
| 1193 |
+
state["final_notebook"] = executed_nb
|
| 1194 |
+
return state
|
| 1195 |
+
|
| 1196 |
+
# ## **Final Workflow**
|
| 1197 |
+
|
| 1198 |
+
# from langgraph.graph import StateGraph, START, END
|
| 1199 |
+
# from IPython.display import Image, display
|
| 1200 |
+
# from langgraph.checkpoint.memory import InMemorySaver
|
| 1201 |
+
|
| 1202 |
+
|
| 1203 |
+
# def create_interactive_workflow() -> StateGraph:
|
| 1204 |
+
# graph = StateGraph(InteractiveCaseStudyState)
|
| 1205 |
+
|
| 1206 |
+
# # Core nodes
|
| 1207 |
+
# graph.add_node("think_sections", think_sections_node)
|
| 1208 |
+
# graph.add_node("review_plan", review_plan_node)
|
| 1209 |
+
# graph.add_node("enhance_plan", enhance_plan_node)
|
| 1210 |
+
# graph.add_node("write_code", write_code_node)
|
| 1211 |
+
# graph.add_node("review_notebook", review_notebook_node)
|
| 1212 |
+
# graph.add_node("modify_notebook", modify_notebook_node)
|
| 1213 |
+
# graph.add_node("execute_notebook",execute_notebook_node)
|
| 1214 |
+
# graph.add_node("review_execution",review_execution_node)
|
| 1215 |
+
# graph.add_node("correct_notebook",correct_notebook_node)
|
| 1216 |
+
# graph.add_node("write_insights", write_insights_node)
|
| 1217 |
+
|
| 1218 |
+
# # START → Think‑Sections → Review Plan
|
| 1219 |
+
# graph.add_edge(START, "think_sections")
|
| 1220 |
+
# graph.add_edge("think_sections", "review_plan")
|
| 1221 |
+
|
| 1222 |
+
# graph.add_conditional_edges(
|
| 1223 |
+
# "review_plan",
|
| 1224 |
+
# lambda s: "true" if s.get("_plan_approved", False) else "false",
|
| 1225 |
+
# {"true": "write_code", "false": "enhance_plan"}
|
| 1226 |
+
# )
|
| 1227 |
+
# graph.add_edge("enhance_plan", "review_plan")
|
| 1228 |
+
|
| 1229 |
+
# # Write‑Code → Review Notebook
|
| 1230 |
+
# graph.add_edge("write_code", "review_notebook")
|
| 1231 |
+
# graph.add_conditional_edges(
|
| 1232 |
+
# "review_notebook",
|
| 1233 |
+
# lambda s: "true" if s.get("_notebook_approved", False) else "false",
|
| 1234 |
+
# {"true": "execute_notebook", "false": "modify_notebook"}
|
| 1235 |
+
# )
|
| 1236 |
+
# graph.add_edge("modify_notebook", "review_notebook")
|
| 1237 |
+
|
| 1238 |
+
# # Execute Notebook → Review Execution
|
| 1239 |
+
# graph.add_edge("execute_notebook", "review_execution")
|
| 1240 |
+
# graph.add_conditional_edges(
|
| 1241 |
+
# "review_execution",
|
| 1242 |
+
# lambda s: "true" if s.get("_execution_approved", False) else "false",
|
| 1243 |
+
# {"true": "write_insights", "false": "correct_notebook"}
|
| 1244 |
+
# )
|
| 1245 |
+
# graph.add_edge("correct_notebook", "execute_notebook")
|
| 1246 |
+
|
| 1247 |
+
# # Final Insights → END
|
| 1248 |
+
# graph.add_edge("write_insights", END)
|
| 1249 |
+
|
| 1250 |
+
# return graph
|
| 1251 |
+
|
| 1252 |
+
# from langgraph.checkpoint.memory import InMemorySaver
|
| 1253 |
+
# checkpointer = InMemorySaver()
|
| 1254 |
+
|
| 1255 |
+
# # ─────────── Compile & Visualize ───────────
|
| 1256 |
+
# case_study_workflow = create_interactive_workflow().compile(checkpointer=checkpointer)
|
| 1257 |
+
# # Show the Mermaid diagram of the workflow
|
| 1258 |
+
# display(Image(case_study_workflow.get_graph().draw_mermaid_png()))
|
| 1259 |
+
|
| 1260 |
+
# thread = {"configurable": {"thread_id": "1"}}
|
| 1261 |
+
|
| 1262 |
+
# case_study_workflow.get_state(thread).next
|
| 1263 |
+
|
| 1264 |
+
# case_study_workflow.get_state({"configurable": {"thread_id": "1"}}).next
|
| 1265 |
+
|
| 1266 |
+
|
src/workflow_utils.py
ADDED
|
@@ -0,0 +1,614 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Standard Libraries
|
| 2 |
+
import os
|
| 3 |
+
import json
|
| 4 |
+
import re
|
| 5 |
+
import shutil
|
| 6 |
+
|
| 7 |
+
from langchain.schema import SystemMessage
|
| 8 |
+
from langchain.tools import Tool
|
| 9 |
+
from langchain.agents import AgentExecutor, create_tool_calling_agent
|
| 10 |
+
from langchain_experimental.utilities import PythonREPL
|
| 11 |
+
|
| 12 |
+
from langchain.prompts import (
|
| 13 |
+
ChatPromptTemplate,
|
| 14 |
+
SystemMessagePromptTemplate,
|
| 15 |
+
HumanMessagePromptTemplate,
|
| 16 |
+
MessagesPlaceholder
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
## Utility Functions
|
| 20 |
+
|
| 21 |
+
def extract_json_from_response(text): # Extract and parse JSON content from a text response
|
| 22 |
+
"""
|
| 23 |
+
Extract JSON content from 'text' using regex and parse it.
|
| 24 |
+
Returns the parsed JSON as a dictionary if successful, otherwise prints an error and returns None.
|
| 25 |
+
"""
|
| 26 |
+
match = re.search(r"\{.*\}", text, re.DOTALL) # Use DOTALL to match multi-line JSON blocks
|
| 27 |
+
if match:
|
| 28 |
+
try:
|
| 29 |
+
return json.loads(match.group()) # Parse and return the JSON object
|
| 30 |
+
except json.JSONDecodeError:
|
| 31 |
+
print("Error: Extracted JSON is invalid.") # Log invalid JSON error
|
| 32 |
+
return None
|
| 33 |
+
else:
|
| 34 |
+
print("Error: No JSON found in response.") # Log if no JSON pattern is found
|
| 35 |
+
return None
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
# def generate_notebook_plan_text(
|
| 39 |
+
# plan: Dict[str, Any],
|
| 40 |
+
# output_txt_file: str = "notebook_plan.txt"
|
| 41 |
+
# ) -> str:
|
| 42 |
+
# """
|
| 43 |
+
# Converts a JSON notebook plan into a human-readable text document using an LLM.
|
| 44 |
+
|
| 45 |
+
# Args:
|
| 46 |
+
# plan (Dict[str, Any]): The JSON notebook plan.
|
| 47 |
+
# output_txt_file (str): The filename to save the generated text document.
|
| 48 |
+
|
| 49 |
+
# Returns:
|
| 50 |
+
# str: The generated text content.
|
| 51 |
+
# """
|
| 52 |
+
|
| 53 |
+
# # Define the system prompt to guide the LLM's behavior
|
| 54 |
+
# system_prompt = """
|
| 55 |
+
# You are an expert AI curriculum designer. Given a structured JSON notebook plan, your task is to convert it into a clear, readable text document. The document should outline the notebook's structure, including sections and subsections, specifying whether each is a markdown or code block. For code blocks, include the 'code_rules' provided. For markdown blocks, include the 'description'. Format the output with appropriate headings and bullet points for clarity.
|
| 56 |
+
# """
|
| 57 |
+
|
| 58 |
+
# # Convert the JSON plan to a formatted string
|
| 59 |
+
# plan_str = json.dumps(plan, indent=2)
|
| 60 |
+
|
| 61 |
+
# # Create the messages for the LLM
|
| 62 |
+
# messages = [
|
| 63 |
+
# SystemMessage(content=system_prompt),
|
| 64 |
+
# HumanMessage(content=f"Here is the JSON notebook plan:\n\n{plan_str}")
|
| 65 |
+
# ]
|
| 66 |
+
|
| 67 |
+
# # Invoke the LLM with the messages
|
| 68 |
+
# response = model.invoke(messages)
|
| 69 |
+
|
| 70 |
+
# # Extract the generated text content
|
| 71 |
+
# text_content = response.content
|
| 72 |
+
|
| 73 |
+
# # Save the generated text to a file
|
| 74 |
+
# with open(output_txt_file, "w", encoding="utf-8") as f:
|
| 75 |
+
# f.write(text_content)
|
| 76 |
+
|
| 77 |
+
# return text_content
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
def get_notebook_blueprint(domain: str) -> str:
|
| 82 |
+
"""
|
| 83 |
+
Returns a detailed notebook blueprint string based on the specified domain.
|
| 84 |
+
"""
|
| 85 |
+
|
| 86 |
+
domain_blueprints = {
|
| 87 |
+
"eda": """
|
| 88 |
+
1. **Problem Statement**
|
| 89 |
+
- Business Context (markdown)
|
| 90 |
+
- Objective of the analysis (markdown)
|
| 91 |
+
- Dataset description and data dictionary overview (markdown)
|
| 92 |
+
|
| 93 |
+
2. **Data Overview**
|
| 94 |
+
- Load dataset and show preview (code)
|
| 95 |
+
- Report rows, columns, and data types (code)
|
| 96 |
+
- Check for missing values and duplicates (code)
|
| 97 |
+
|
| 98 |
+
3. **Univariate Analysis**
|
| 99 |
+
- Compute and plot summary statistics (histograms, box plots) (code)
|
| 100 |
+
|
| 101 |
+
4. **Bivariate Analysis**
|
| 102 |
+
- Compute correlations and display heatmap (code)
|
| 103 |
+
- Scatter plots for key variable pairs (code)
|
| 104 |
+
|
| 105 |
+
5. **Multivariate Analysis**
|
| 106 |
+
- Pair plots or 3D scatter plots for multi-feature interactions (code)
|
| 107 |
+
|
| 108 |
+
6. **Feature Engineering & Transformation**
|
| 109 |
+
- Create new features or bucket continuous variables (code)
|
| 110 |
+
- Handle outliers or perform scaling/normalization (code)
|
| 111 |
+
|
| 112 |
+
7. **Insights & Recommendations**
|
| 113 |
+
- Bullet-point summary of key findings (markdown)
|
| 114 |
+
- Business or analytical recommendations (markdown)
|
| 115 |
+
""",
|
| 116 |
+
"machine_learning": """
|
| 117 |
+
1. **Problem Statement**
|
| 118 |
+
- Business Context (markdown)
|
| 119 |
+
- Problem Objective (markdown)
|
| 120 |
+
- Dataset Summary (markdown)
|
| 121 |
+
- Data Dictionary (markdown)
|
| 122 |
+
|
| 123 |
+
2. **Setup: Imports & Configuration**
|
| 124 |
+
- Install and import required libraries (code)
|
| 125 |
+
- Define global paths or settings (code)
|
| 126 |
+
|
| 127 |
+
3. **Data Overview**
|
| 128 |
+
- Load data and show preview (code)
|
| 129 |
+
- Check for missing values and duplicates (code)
|
| 130 |
+
- Summary statistics and data types (code)
|
| 131 |
+
|
| 132 |
+
4. **Exploratory Data Analysis (EDA)**
|
| 133 |
+
- Univariate: distributions, histograms (code)
|
| 134 |
+
- Bivariate/Multivariate: pair plots, heatmaps, correlation (code)
|
| 135 |
+
|
| 136 |
+
5. **Modeling**
|
| 137 |
+
- Data split (train-test)
|
| 138 |
+
- Model training using selected algorithm(s)
|
| 139 |
+
- Evaluation using sutable metrics (accuracy, recall, precision, F1-score for classification and RMSE, MAE for regression)
|
| 140 |
+
|
| 141 |
+
6. **Insights & Recommendations**
|
| 142 |
+
- Summary of findings
|
| 143 |
+
- Performance interpretation
|
| 144 |
+
- Recommendations for business or deployment
|
| 145 |
+
""",
|
| 146 |
+
|
| 147 |
+
"deep_learning": """
|
| 148 |
+
1. **Problem Statement**
|
| 149 |
+
- Business Context (markdown)
|
| 150 |
+
- Objective of using Deep Learning (markdown)
|
| 151 |
+
- Dataset Summary (markdown)
|
| 152 |
+
- Data Dictionary (markdown)
|
| 153 |
+
|
| 154 |
+
2. **Setup: Imports & Configuration**
|
| 155 |
+
- Install and import required libraries (TensorFlow, Keras, PyTorch, etc.) (code)
|
| 156 |
+
- Set global seeds and GPU configuration (code)
|
| 157 |
+
|
| 158 |
+
3. **Data Overview**
|
| 159 |
+
- Load data and preview (code)
|
| 160 |
+
- Check for class imbalance and missing values (code)
|
| 161 |
+
- Summary statistics and label distribution (code)
|
| 162 |
+
|
| 163 |
+
4. **Preprocessing**
|
| 164 |
+
- Data cleaning and normalization/scaling (code)
|
| 165 |
+
- Label encoding or one-hot encoding (code)
|
| 166 |
+
- Reshaping input for neural networks (code)
|
| 167 |
+
- Train-validation-test split (code)
|
| 168 |
+
|
| 169 |
+
5. **Model Architecture**
|
| 170 |
+
- Define sequential or functional model (code)
|
| 171 |
+
- Configure layers (Dense, Conv2D, LSTM, etc.) and activations (code)
|
| 172 |
+
- Display model summary (code)
|
| 173 |
+
|
| 174 |
+
6. **Model Compilation & Training**
|
| 175 |
+
- Compile model with optimizer, loss, and metrics (code)
|
| 176 |
+
- Fit model with early stopping or callbacks (code)
|
| 177 |
+
- Plot training and validation loss/accuracy (code)
|
| 178 |
+
|
| 179 |
+
7. **Evaluation**
|
| 180 |
+
- Evaluate on test set (code)
|
| 181 |
+
- Confusion matrix, classification report or regression metrics (code)
|
| 182 |
+
- Visualize predictions (code)
|
| 183 |
+
|
| 184 |
+
8. **Insights & Recommendations**
|
| 185 |
+
- Interpretation of model performance (markdown)
|
| 186 |
+
- Overfitting/underfitting analysis (markdown)
|
| 187 |
+
- Suggestions for tuning or deployment (markdown)
|
| 188 |
+
""",
|
| 189 |
+
|
| 190 |
+
"computer_vision": """
|
| 191 |
+
|
| 192 |
+
**NOTE:** This is your high-level reference. You must **ONLY THINK, ANALYZE, AND DECIDE** based on the specific problem statement, dataset information (including its type – tabular, image, text, or mixed), and the user's specific weekly subtopics. Do NOT go beyond the weekly topics. Every decision and every operation performed must be carefully considered and justified in the context of the specific Computer Vision problem given.
|
| 193 |
+
|
| 194 |
+
## 1. Problem Statement (First-level heading markdown)
|
| 195 |
+
## Business Context (Second-level heading markdown)
|
| 196 |
+
* Write the user’s Business context as is in a markdown cell.
|
| 197 |
+
|
| 198 |
+
## Objective (Second-level heading markdown)
|
| 199 |
+
* Write the user's objective as is in a markdown cell.
|
| 200 |
+
|
| 201 |
+
## Data Dictionary (Second-level heading markdown)
|
| 202 |
+
* Write the user's data dictionary as is in a markdown cell.
|
| 203 |
+
* Enhance column/label descriptions with necessary details (e.g., class meanings, annotation formats).
|
| 204 |
+
|
| 205 |
+
*(This section will have 6 markdown cells in total).*
|
| 206 |
+
|
| 207 |
+
---
|
| 208 |
+
|
| 209 |
+
# 2. Install and import the necessary libraries (First-level heading markdown)
|
| 210 |
+
## Install Dependencies (Second-level heading markdown)
|
| 211 |
+
* **Think and decide** on necessary libraries with specific versions (e.g., `tensorflow`/`torch`, `keras`, `opencv-python` (cv2), `scikit-image`, `Pillow`, `tqdm`, `albumentations` for augmentation).
|
| 212 |
+
* **Code Cell:** Install all dependencies in one cell.
|
| 213 |
+
|
| 214 |
+
## Import Libraries (Second-level heading markdown)
|
| 215 |
+
* Import all required libraries.
|
| 216 |
+
* Group imports with clear comments (e.g., "# DL Frameworks", "# Image Processing", "# Visualization").
|
| 217 |
+
* Add comments for less common libraries.
|
| 218 |
+
* **Code Cell:** Import all libraries in one cell.
|
| 219 |
+
|
| 220 |
+
*(This section will have 2 markdown and 2 code cells).*
|
| 221 |
+
|
| 222 |
+
---
|
| 223 |
+
|
| 224 |
+
# 3. Loading the dataset (First-level heading markdown)
|
| 225 |
+
* CV datasets consist of image files (e.g., `.jpg`, `.png`), often with accompanying annotation files (e.g., `.json`, `.xml`, mask images), directory or Zip file of directories structure defining classes.
|
| 226 |
+
* **Think and decide** the best method to load your specific image data and annotations.
|
| 227 |
+
|
| 228 |
+
## Read Data from Path/Source (Second-level heading markdown)
|
| 229 |
+
* Read image data and (if applicable) annotations from the provided path/structure.
|
| 230 |
+
* **Examples of operations:** `tf.keras.utils.image_dataset_from_directory` or `torchvision.datasets.ImageFolder` (classification), custom loops for reading image files and parsing annotation files (detection/segmentation).
|
| 231 |
+
* **Code Cell:** Load data in one cell.
|
| 232 |
+
|
| 233 |
+
## Copy Data to New Variable and Preview (Second-level heading markdown)
|
| 234 |
+
* Copy data to a new variable (if applicable, e.g., list of image paths, dataframe of annotations).
|
| 235 |
+
* Display a preview to confirm data integrity.
|
| 236 |
+
* **Examples of operations:** Displaying a few sample images, showing image paths and corresponding labels/annotations.
|
| 237 |
+
* **Code Cell:** Copy and preview data in one cell.
|
| 238 |
+
|
| 239 |
+
*(This section will have 2 markdown and 2 code cells).*
|
| 240 |
+
|
| 241 |
+
---
|
| 242 |
+
|
| 243 |
+
# 4. Data Overview and Preliminary Checks (First-level heading markdown)
|
| 244 |
+
* Analyze your image dataset: total images, typical dimensions, channels, file formats, class distribution, potential corruption, annotation format.
|
| 245 |
+
* **Think, reason, and decide** on preliminary operations to show data sanity and structure.
|
| 246 |
+
|
| 247 |
+
## Display Basic Dataset Information (Second-level heading markdown)
|
| 248 |
+
* Display initial information about the dataset.
|
| 249 |
+
* **Examples of operations:** Total number of images, number of classes (if classification), paths to a few sample images, basic info on image file types.
|
| 250 |
+
* **Code Cell:** Display info in one cell.
|
| 251 |
+
|
| 252 |
+
## Checking Image Characteristics (Second-level heading markdown)
|
| 253 |
+
* **Think and decide** what basic image characteristics need to be checked based on your problem.
|
| 254 |
+
* **Examples of checks:** Distribution of image dimensions (width/height/aspect ratio), channel distribution (RGB/Grayscale), check for corrupted/unreadable image files, summary statistics of pixel values (e.g., mean/std across channels).
|
| 255 |
+
* **Code Cell(s):** Each distinct check in its own cell.
|
| 256 |
+
|
| 257 |
+
## Preliminary Missing/Corrupt Samples and Duplicates (Second-level heading markdown)
|
| 258 |
+
* **Think and decide** how to check for and handle missing/corrupted image files or duplicate images/annotations.
|
| 259 |
+
* **Examples of checks:** Iterating through file paths to catch read errors, hashing images to find duplicates, checking for empty/malformed annotation files.
|
| 260 |
+
* **Code Cell(s):** Display checks in one cell; handle issues in separate cells (if straightforward).
|
| 261 |
+
|
| 262 |
+
* Identify any data quality issues (e.g., inconsistent image sizes, incorrect labels/annotations, irrelevant images).
|
| 263 |
+
* **Think, analyze, and decide** which initial cleaning/conversion operations are most suitable.
|
| 264 |
+
* Perform all chosen operations in separate cells. Ensure the dataset is preliminarily ready.
|
| 265 |
+
* **Code Cell(s):** Perform initial quality checks and cleanups in separate cells.
|
| 266 |
+
|
| 267 |
+
*(This section will have 5-7 markdown cells and 6-10 code cells. Total: 11-17 cells).*
|
| 268 |
+
* **NOTE:** Focus on image-specific insights relevant to preprocessing and model building.
|
| 269 |
+
|
| 270 |
+
---
|
| 271 |
+
|
| 272 |
+
# 5. Exploratory Data Analysis (EDA) (First-level heading markdown)
|
| 273 |
+
* **NOTE:** Do not introduce analysis beyond weekly topics.
|
| 274 |
+
* Focus on key visualizations to understand image properties, label distributions, and (if applicable) annotation patterns. Aim for 5-8 plots/visualizations.
|
| 275 |
+
|
| 276 |
+
## Univariate Analysis (Second-level heading markdown)
|
| 277 |
+
* **Think and decide** on up to 4 plots/analyses revealing individual image or label characteristics.
|
| 278 |
+
* **Examples of operations:** Bar chart of class distribution, histograms of image dimensions (width, height), pixel intensity histograms for sample images (e.g., mean pixel value distribution).
|
| 279 |
+
* Justify why each visualization helps answer business questions or inform model design.
|
| 280 |
+
* **Code Cell(s):** Each plot/analysis in its own cell.
|
| 281 |
+
|
| 282 |
+
## Bivariate/Multivariate Analysis (Second-level heading markdown)
|
| 283 |
+
* **Think and decide** on up to 4 visualizations showing relationships or patterns across multiple dimensions.
|
| 284 |
+
* **Examples of operations:** Displaying sample images from different classes, visualizing bounding boxes/masks on images (for detection/segmentation), average image per class, relationships between image properties and labels.
|
| 285 |
+
* Justify why each visualization helps uncover deeper insights relevant to the CV task.
|
| 286 |
+
* **Code Cell(s):** Each plot/analysis in its own cell.
|
| 287 |
+
|
| 288 |
+
*(This section will have 2 markdown cells and 8 code cells. Total: 10 cells).*
|
| 289 |
+
|
| 290 |
+
---
|
| 291 |
+
|
| 292 |
+
# 6. Data Preprocessing (First-level heading markdown)
|
| 293 |
+
* Transform raw image data into a format suitable for Deep Learning models.
|
| 294 |
+
* **Think, analyze, and decide** on preprocessing steps based on image data characteristics, CV task, and chosen model requirements.
|
| 295 |
+
* Each distinct operation must be in its own separate code cell.
|
| 296 |
+
|
| 297 |
+
## Image-Specific Transformations (Second-level heading markdown)
|
| 298 |
+
* **Think and decide** on necessary transformations for your specific image data.
|
| 299 |
+
* **Examples of operations:**
|
| 300 |
+
* **Resizing/Rescaling:** Standardizing image dimensions to match model input.
|
| 301 |
+
* **Pixel Normalization:** Scaling pixel values (e.g., to [0, 1] or [-1, 1]).
|
| 302 |
+
* **Channel Manipulation:** Converting to grayscale, ensuring consistent channel count (e.g., 3 channels for RGB).
|
| 303 |
+
* **Data Augmentation:** Applying transformations like rotation, flipping, zooming, brightness/contrast adjustments to increase dataset size and improve model generalization (crucial for DL in CV).
|
| 304 |
+
* **Code Cell(s):** Implement these image preprocessing steps.
|
| 305 |
+
|
| 306 |
+
## Annotation Preprocessing (if applicable) (Second-level heading markdown)
|
| 307 |
+
* For object detection or segmentation, **think and decide** on necessary annotation processing.
|
| 308 |
+
* **Examples of operations:**
|
| 309 |
+
* Parsing raw annotations (XML, JSON) into structured formats.
|
| 310 |
+
* Resizing/scaling bounding box coordinates or mask dimensions to match resized images.
|
| 311 |
+
* Converting mask images to appropriate tensor formats.
|
| 312 |
+
* Handling class IDs and one-hot encoding if needed.
|
| 313 |
+
* **Code Cell(s):** Implement annotation preprocessing steps.
|
| 314 |
+
|
| 315 |
+
## Data Loading Pipelines (Second-level heading markdown)
|
| 316 |
+
* **Think and decide** on the most efficient way to create data pipelines for loading and batching your preprocessed image data for Deep Learning frameworks.
|
| 317 |
+
* **Key Considerations:** Batching, shuffling, caching, prefetching, parallel processing to optimize training speed.
|
| 318 |
+
* **Code Cell(s):** Implement your data loading pipeline (e.g., `tf.data.Dataset`, `torch.utils.data.Dataset` and `DataLoader`).
|
| 319 |
+
|
| 320 |
+
*(This section will have 3-5 markdown cells and 10-15 code cells. Total: 15-20 cells).*
|
| 321 |
+
* **NOTE:** Justify each preprocessing step by explaining how it prepares the data for specific DL architectures and aligns with the problem objective.
|
| 322 |
+
|
| 323 |
+
---
|
| 324 |
+
|
| 325 |
+
# 7. Model Building (First-level heading markdown)
|
| 326 |
+
* Define and train your Computer Vision models.
|
| 327 |
+
* **Think, analyze, and decide** on the most appropriate Deep Learning architecture(s) based on the CV problem, data, and objective.
|
| 328 |
+
* Each distinct operation in its own cell.
|
| 329 |
+
|
| 330 |
+
## Train-Validation-Test Split (Second-level heading markdown)
|
| 331 |
+
* **Think and decide** on the most appropriate strategy for splitting your processed dataset into training, validation, and testing sets.
|
| 332 |
+
* **Key Considerations:** Choose appropriate split ratios, use `random_state` for reproducibility, decide if stratification is necessary (especially for imbalanced classification tasks).
|
| 333 |
+
* **Code Cell:** Implement the data split.
|
| 334 |
+
|
| 335 |
+
## Model Architecture Definition (Second-level heading markdown)
|
| 336 |
+
* **Think and decide** on the most suitable Deep Learning architecture for your CV task.
|
| 337 |
+
* **Examples of architectures:**
|
| 338 |
+
* **Image Classification:** Convolutional Neural Networks (CNNs), pre-trained models from Keras Applications (e.g., ResNet, VGG, EfficientNet) or PyTorch Models.
|
| 339 |
+
* **Object Detection:** Architectures like YOLO, Faster R-CNN (often involve pre-trained backbones).
|
| 340 |
+
* **Image Segmentation:** U-Net, FCN (Fully Convolutional Networks).
|
| 341 |
+
* **Considerations:** Number of layers, activation functions, regularization (Dropout, Batch Normalization), output layer matching the problem type.
|
| 342 |
+
* **Code Cell:** Define your CV model architecture.
|
| 343 |
+
|
| 344 |
+
## Compile Model (Second-level heading markdown)
|
| 345 |
+
* **Think and decide** on the appropriate loss function and optimizer for your specific CV problem.
|
| 346 |
+
* **Loss Functions:** `CategoricalCrossentropy`, `BinaryCrossentropy` (classification); `MeanSquaredError` (regression); `Dice Loss`, `IoU Loss` (segmentation); specialized losses for object detection.
|
| 347 |
+
* **Optimizers:** `Adam`, `SGD` with momentum, `RMSprop`.
|
| 348 |
+
* **Code Cell:** Compile the defined Deep Learning model.
|
| 349 |
+
|
| 350 |
+
## Model Training (Second-level heading markdown)
|
| 351 |
+
* Train your compiled Deep Learning model on the training data, using the validation set for monitoring performance.
|
| 352 |
+
* **Key Training Parameters:** Epochs, Batch Size, Callbacks (e.g., `EarlyStopping`, `ModelCheckpoint`, `ReduceLROnPlateau`).
|
| 353 |
+
* **Code Cell:** Write the code to train your Deep Learning model.
|
| 354 |
+
|
| 355 |
+
*(This section will have 4 markdown cells and 15-20 code cells. Total: 20-25 cells).*
|
| 356 |
+
* **NOTE:** All chosen architectures, splitting strategies, and training parameters must be covered in weekly topics. Justify all decisions based on the specific CV problem and data.
|
| 357 |
+
|
| 358 |
+
---
|
| 359 |
+
|
| 360 |
+
# 8. Model Evaluation (First-level heading markdown)
|
| 361 |
+
* Rigorously evaluate the performance of your trained Computer Vision model(s).
|
| 362 |
+
* It is crucial to **deeply analyze the problem statement and objective** to select the correct evaluation metrics that directly align with the business goal.
|
| 363 |
+
* Each distinct evaluation operation must be performed in its own separate code cell.
|
| 364 |
+
|
| 365 |
+
## Metric Selection and Justification (Second-level heading markdown)
|
| 366 |
+
* **Think and decide** on the most appropriate evaluation metric(s) for your CV task.
|
| 367 |
+
* **Examples of metrics:**
|
| 368 |
+
* **Image Classification:** Accuracy, Precision, Recall, F1-score (macro/micro/weighted), ROC-AUC.
|
| 369 |
+
* **Image Segmentation:** Intersection over Union (IoU), Dice Coefficient.
|
| 370 |
+
* **Object Detection:** Mean Average Precision (mAP), IoU for bounding box accuracy.
|
| 371 |
+
* Justify your choice of metrics.
|
| 372 |
+
* **Code Cell:** Display and explain the chosen metrics.
|
| 373 |
+
|
| 374 |
+
## Model Performance Evaluation (Second-level heading markdown)
|
| 375 |
+
* Evaluate the trained CV model(s) on the unseen test set using the selected metrics.
|
| 376 |
+
* **Examples of visualizations:**
|
| 377 |
+
* Plotting training/validation loss/metric curves over epochs.
|
| 378 |
+
* Displaying confusion matrix for classification.
|
| 379 |
+
* Visualizing sample predictions with overlaid bounding boxes (detection) or masks (segmentation).
|
| 380 |
+
* **Code Cell(s):** Make predictions on the test set, calculate metrics, and include code for any relevant visualizations.
|
| 381 |
+
|
| 382 |
+
## Final Best Model Selection (Second-level heading markdown)
|
| 383 |
+
* If multiple models/configurations were trained, **think, analyze, and decide** on criteria to select the best one.
|
| 384 |
+
* Compare performance across chosen metrics.
|
| 385 |
+
* **Code Cell:** Present comparison results clearly and explicitly identify the final best model (and optionally save it).
|
| 386 |
+
|
| 387 |
+
*(This section will have 4 markdown cells and 3-6 code cells. Total: 7-10 cells).*
|
| 388 |
+
* **NOTE:** All evaluation methods and metrics must be covered in weekly topics.
|
| 389 |
+
""",
|
| 390 |
+
|
| 391 |
+
"natural_language_processing": """
|
| 392 |
+
1. **Problem Statement**
|
| 393 |
+
- Business Context (markdown)
|
| 394 |
+
- Objective of using NLP (e.g., sentiment analysis, text classification) (markdown)
|
| 395 |
+
- Dataset Overview (markdown)
|
| 396 |
+
- Data Dictionary or description of text fields and labels (markdown)
|
| 397 |
+
|
| 398 |
+
2. **Setup: Imports & Configuration**
|
| 399 |
+
- Import libraries (NLTK, spaCy, scikit-learn, Hugging Face Transformers, etc.) (code)
|
| 400 |
+
- Set random seeds and config (code)
|
| 401 |
+
|
| 402 |
+
3. **Data Overview**
|
| 403 |
+
- Load the dataset and preview sample text (code)
|
| 404 |
+
- Check text lengths, class distribution (if classification) (code)
|
| 405 |
+
- Nulls, duplicates, basic stats (code)
|
| 406 |
+
|
| 407 |
+
4. **Text Preprocessing**
|
| 408 |
+
- Clean text: lowercasing, punctuation removal, stopwords removal (code)
|
| 409 |
+
- Tokenization, lemmatization/stemming (code)
|
| 410 |
+
- Optional: POS tagging, Named Entity Recognition (code)
|
| 411 |
+
- Convert to numerical form (TF-IDF, CountVectorizer, or token ids) (code)
|
| 412 |
+
|
| 413 |
+
5. **Modeling**
|
| 414 |
+
- Classical ML pipeline (Logistic Regression, SVM, Naive Bayes with vectorized text) (code)
|
| 415 |
+
- OR Transformer-based modeling (BERT/DistilBERT with Hugging Face) (code)
|
| 416 |
+
- Model training and evaluation with suitable metrics (e.g., F1, accuracy, AUC) (code)
|
| 417 |
+
|
| 418 |
+
6. **Evaluation & Interpretation**
|
| 419 |
+
- Classification report, confusion matrix (code)
|
| 420 |
+
- Visualize word importance or attention weights (code)
|
| 421 |
+
- Analyze misclassifications or hard examples (code)
|
| 422 |
+
|
| 423 |
+
7. **Insights & Recommendations**
|
| 424 |
+
- Summary of results and learnings (markdown)
|
| 425 |
+
- Recommendations for improving NLP pipeline or model (markdown)
|
| 426 |
+
- Optional: discussion of domain-specific language issues or future improvements (markdown)
|
| 427 |
+
""",
|
| 428 |
+
|
| 429 |
+
"generative_ai_prompt_engineering": """
|
| 430 |
+
1. **Problem Statement**
|
| 431 |
+
- Use-case Context (markdown)
|
| 432 |
+
- Goal of prompting (e.g., creative generation, question answering) (markdown)
|
| 433 |
+
- Dataset or API description if relevant (markdown)
|
| 434 |
+
|
| 435 |
+
2. **Setup: Imports & Configuration**
|
| 436 |
+
- Install and import LLM client libraries (OpenAI SDK, LangChain, Transformers) (code)
|
| 437 |
+
- Configure API keys, endpoints, or local model paths (code)
|
| 438 |
+
|
| 439 |
+
3. **Prompt Fundamentals**
|
| 440 |
+
- Explain prompt components: system vs user vs few‐shot examples (markdown)
|
| 441 |
+
- Show simple zero-shot example (code)
|
| 442 |
+
|
| 443 |
+
4. **Prompt Design Patterns**
|
| 444 |
+
- **Few-Shot**: crafting examples in prompt (code + markdown)
|
| 445 |
+
- **Chain-of-Thought**: breaking reasoning steps (code + markdown)
|
| 446 |
+
- **Tree-of-Thought**: exploring multiple reasoning branches (code + markdown)
|
| 447 |
+
- **ReAct**: tool-calling patterns (code + markdown)
|
| 448 |
+
- **Template Engineering**: building reusable prompt templates (code + markdown)
|
| 449 |
+
|
| 450 |
+
5. **Evaluation & Iteration**
|
| 451 |
+
- Metrics: AI-score, human eval, automatic eval (BLEU/ROUGE) (markdown)
|
| 452 |
+
- Test on edge cases and adversarial examples (code + markdown)
|
| 453 |
+
- Automated prompt optimization loops (e.g., prompt tuning via LLM) (code + markdown)
|
| 454 |
+
|
| 455 |
+
6. **Best Practices & Pitfalls**
|
| 456 |
+
- Token budget management and cost awareness (markdown)
|
| 457 |
+
- Hallucination mitigation techniques (markdown)
|
| 458 |
+
- Ethical considerations (bias, privacy) (markdown)
|
| 459 |
+
|
| 460 |
+
7. **Insights & Recommendations**
|
| 461 |
+
- Summary of what worked, what didn’t (markdown)
|
| 462 |
+
- Next steps: chain refinement, retrieval augmentation, or finetuning (markdown)
|
| 463 |
+
""",
|
| 464 |
+
"generative_ai_rag": """
|
| 465 |
+
1. **Problem Statement**
|
| 466 |
+
- Use-case Context (markdown)
|
| 467 |
+
- Objective of RAG solution (e.g., knowledge retrieval + LLM answer) (markdown)
|
| 468 |
+
- Source data description (documents, PDFs, web pages) (markdown)
|
| 469 |
+
|
| 470 |
+
2. **Setup: Imports & Configuration**
|
| 471 |
+
- Install and import libraries (LangChain, OpenAI SDK, FAISS/Chroma, etc.) (code)
|
| 472 |
+
- Configure API keys or local model paths (code)
|
| 473 |
+
|
| 474 |
+
3. **Data Ingestion & Indexing**
|
| 475 |
+
- Load raw documents (text, PDFs, HTML) (code)
|
| 476 |
+
- Chunking & text splitting strategy (code)
|
| 477 |
+
- Compute embeddings (OpenAI, Hugging Face) (code)
|
| 478 |
+
- Build vector store (FAISS, ChromaDB) (code)
|
| 479 |
+
|
| 480 |
+
4. **Retriever Configuration**
|
| 481 |
+
- Define similarity search parameters (k, distance metric) (code)
|
| 482 |
+
- Set up hybrid retrievers (keyword + semantic) if needed (code)
|
| 483 |
+
|
| 484 |
+
5. **LLM & Chain Assembly**
|
| 485 |
+
- Create prompt templates for retrieved context + query (code)
|
| 486 |
+
- Instantiate RetrievalQA or ConversationalRetrievalChain (code)
|
| 487 |
+
- Configure streaming vs. non-streaming responses
|
| 488 |
+
|
| 489 |
+
6. **Query Handling & Response Generation**
|
| 490 |
+
- Test with example queries (code)
|
| 491 |
+
- Display retrieved passages alongside answers (code + markdown)
|
| 492 |
+
- Handle edge cases (no hits, multi-document answers)
|
| 493 |
+
|
| 494 |
+
7. **Evaluation & Metrics**
|
| 495 |
+
- Measure retrieval quality (recall@k, precision@k) (code)
|
| 496 |
+
- Evaluate answer quality (ROUGE, human feedback) (markdown)
|
| 497 |
+
- Logging and traceability of sources
|
| 498 |
+
|
| 499 |
+
8. **Insights & Recommendations**
|
| 500 |
+
- Analysis of retrieval/answer performance (markdown)
|
| 501 |
+
- Suggestions for improving chunk size, embeddings, or model prompts (markdown)
|
| 502 |
+
""",
|
| 503 |
+
|
| 504 |
+
"generative_ai_fine_tuning": """
|
| 505 |
+
1. **Problem Statement**
|
| 506 |
+
- Use-case Context (markdown)
|
| 507 |
+
- Why fine-tuning is needed (markdown)
|
| 508 |
+
- Description of base model and target behavior (markdown)
|
| 509 |
+
|
| 510 |
+
2. **Setup: Imports & Configuration**
|
| 511 |
+
- Install and import fine-tuning libraries (OpenAI CLI/SDK, Hugging Face Transformers, PEFT) (code)
|
| 512 |
+
- Authenticate and configure API keys or model paths (code)
|
| 513 |
+
|
| 514 |
+
3. **Data Preparation**
|
| 515 |
+
- Collect and format training examples (instruction–response pairs) (code)
|
| 516 |
+
- Clean and validate dataset (remove duplicates, ensure token limits) (code)
|
| 517 |
+
- Split into train/validation sets (code)
|
| 518 |
+
|
| 519 |
+
4. **Model & Training Configuration**
|
| 520 |
+
- Choose fine-tuning method (e.g., full-model, LoRA/PEFT, adapter) (markdown)
|
| 521 |
+
- Define hyperparameters: learning rate, batch size, epochs (code or markdown)
|
| 522 |
+
- Set up training callbacks (early stopping, checkpointing) (code)
|
| 523 |
+
|
| 524 |
+
5. **Fine-Tuning Execution**
|
| 525 |
+
- Launch fine-tuning job via API or local script (code)
|
| 526 |
+
- Monitor training logs (loss curves, metrics) (code)
|
| 527 |
+
|
| 528 |
+
6. **Evaluation & Validation**
|
| 529 |
+
- Evaluate on held-out validation data (perplexity, accuracy) (code)
|
| 530 |
+
- Human-in-the-loop sample reviews (markdown)
|
| 531 |
+
- Compare to base model on key tasks (markdown + code)
|
| 532 |
+
|
| 533 |
+
7. **Deployment & Inference**
|
| 534 |
+
- Save and load the fine-tuned model (code)
|
| 535 |
+
- Build simple inference pipeline (API, CLI, or notebook demo) (code)
|
| 536 |
+
|
| 537 |
+
8. **Insights & Recommendations**
|
| 538 |
+
- Summary of fine-tuning performance (markdown)
|
| 539 |
+
- Lessons learned on data quality, hyperparameters (markdown)
|
| 540 |
+
- Next steps: further tuning, active learning, broader use cases (markdown)
|
| 541 |
+
"""
|
| 542 |
+
}
|
| 543 |
+
|
| 544 |
+
domain_key = domain.lower().replace(" ", "_")
|
| 545 |
+
blueprint = domain_blueprints.get(domain_key)
|
| 546 |
+
|
| 547 |
+
if blueprint is None:
|
| 548 |
+
raise ValueError(f"No blueprint found for domain: {domain}")
|
| 549 |
+
|
| 550 |
+
return blueprint.strip()
|
| 551 |
+
|
| 552 |
+
|
| 553 |
+
|
| 554 |
+
# Utility function to classify plot vs. non-plot code
|
| 555 |
+
def is_plot_code(code: str) -> bool:
|
| 556 |
+
"""Return True if code likely generates a plot (matplotlib/seaborn)."""
|
| 557 |
+
return bool(re.search(r"(plt\.|sns\.|figure\()", code))
|
| 558 |
+
|
| 559 |
+
|
| 560 |
+
|
| 561 |
+
|
| 562 |
+
# Utility to stage data/dependency files into a working directory
|
| 563 |
+
def stage_dependencies(dataset_path: str, work_dir: str):
|
| 564 |
+
"""
|
| 565 |
+
Copy the main dataset (and any supporting files) into a clean scratch directory
|
| 566 |
+
so that repl‑based re‑execution (for plot analysis) can import them.
|
| 567 |
+
"""
|
| 568 |
+
os.makedirs(work_dir, exist_ok=True)
|
| 569 |
+
if dataset_path and os.path.isfile(dataset_path):
|
| 570 |
+
shutil.copy(dataset_path, os.path.join(work_dir, os.path.basename(dataset_path)))
|
| 571 |
+
|
| 572 |
+
|
| 573 |
+
|
| 574 |
+
|
| 575 |
+
def build_plot_insight_agent_executor(model) -> AgentExecutor:
|
| 576 |
+
python_repl = PythonREPL()
|
| 577 |
+
repl_tool = Tool(
|
| 578 |
+
name="python_repl",
|
| 579 |
+
description=(
|
| 580 |
+
"Executes Python code (including plotting) and returns either "
|
| 581 |
+
"printed statistics or textual description of the plot object."
|
| 582 |
+
),
|
| 583 |
+
func=python_repl.run,
|
| 584 |
+
)
|
| 585 |
+
|
| 586 |
+
# 1) Wrap your system instructions
|
| 587 |
+
system_template = SystemMessagePromptTemplate.from_template(
|
| 588 |
+
"""You are a Python data visualization specialist. When given Python code
|
| 589 |
+
including data loading and plotting commands, you will:
|
| 590 |
+
|
| 591 |
+
1. Execute the code via the python_repl tool.
|
| 592 |
+
2. Inspect the resulting Matplotlib/Seaborn figure or printed stats.
|
| 593 |
+
3. Return a concise, bullet-point insight describing:
|
| 594 |
+
- What the plot shows (trends, distributions, anomalies).
|
| 595 |
+
- Why those features matter for the business problem.
|
| 596 |
+
"""
|
| 597 |
+
)
|
| 598 |
+
|
| 599 |
+
# 3) Build a ChatPromptTemplate from those
|
| 600 |
+
chat_prompt = ChatPromptTemplate.from_messages([
|
| 601 |
+
system_template,
|
| 602 |
+
("human", "{input}"),
|
| 603 |
+
("placeholder", "{agent_scratchpad}"),
|
| 604 |
+
])
|
| 605 |
+
|
| 606 |
+
# 4) Create the agent with the proper prompt object
|
| 607 |
+
agent = create_tool_calling_agent(
|
| 608 |
+
llm=model,
|
| 609 |
+
tools=[repl_tool],
|
| 610 |
+
prompt=chat_prompt
|
| 611 |
+
)
|
| 612 |
+
|
| 613 |
+
executor = AgentExecutor(agent=agent, tools=[repl_tool], verbose=False)
|
| 614 |
+
return executor
|