File size: 8,915 Bytes
912ec24 ac04dd1 912ec24 ac04dd1 912ec24 ac04dd1 912ec24 ac04dd1 912ec24 ac04dd1 912ec24 ac04dd1 912ec24 ac04dd1 912ec24 ac04dd1 912ec24 ac04dd1 912ec24 ac04dd1 912ec24 ac04dd1 912ec24 ac04dd1 912ec24 ac04dd1 912ec24 ac04dd1 912ec24 ac04dd1 912ec24 ac04dd1 912ec24 ac04dd1 912ec24 ac04dd1 912ec24 ac04dd1 912ec24 ac04dd1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
#!/usr/bin/env python3
import os
import re
import streamlit as st
import streamlit.components.v1 as components
from urllib.parse import quote
import pandas as pd
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader, TensorDataset
import base64
# Page Configuration
st.set_page_config(
page_title="AI Knowledge Tree Builder ๐๐ฟ",
page_icon="๐ณโจ",
layout="wide",
initial_sidebar_state="auto",
)
# Predefined Knowledge Trees
trees = {
"ML Engineering": """
0. ML Engineering ๐
1. Data Preparation
- Load Data ๐
- Preprocess Data ๐ ๏ธ
2. Model Building
- Train Model ๐ค
- Evaluate Model ๐
3. Deployment
- Deploy Model ๐
""",
"Health": """
0. Health and Wellness ๐ฟ
1. Physical Health
- Exercise ๐๏ธ
- Nutrition ๐
2. Mental Health
- Meditation ๐ง
- Therapy ๐๏ธ
""",
}
# Project Seeds
project_seeds = {
"Code Project": """
0. Code Project ๐
1. app.py ๐
2. requirements.txt ๐ฆ
3. README.md ๐
""",
"Papers Project": """
0. Papers Project ๐
1. markdown ๐
2. mermaid ๐ผ๏ธ
3. huggingface.co ๐ค
""",
"AI Project": """
0. AI Project ๐ค
1. Streamlit Torch Transformers
- Streamlit ๐
- Torch ๐ฅ
- Transformers ๐ค
2. DistillKit MergeKit Spectrum
- DistillKit ๐งช
- MergeKit ๐
- Spectrum ๐
3. Transformers Diffusers Datasets
- Transformers ๐ค
- Diffusers ๐จ
- Datasets ๐
""",
}
# Utility Functions
def sanitize_label(label):
"""Remove invalid characters for Mermaid labels."""
return re.sub(r'[^\w\s-]', '', label).replace(' ', '_')
def parse_outline_to_mermaid(outline_text, search_agent):
"""Convert tree outline to Mermaid syntax with clickable nodes."""
lines = outline_text.strip().split('\n')
nodes, edges, clicks, stack = [], [], [], []
for line in lines:
indent = len(line) - len(line.lstrip())
level = indent // 4
label = re.sub(r'^[#*\->\d\.\s]+', '', line.strip())
if label:
node_id = f"N{len(nodes)}"
sanitized_label = sanitize_label(label)
nodes.append(f'{node_id}["{label}"]')
search_url = search_urls[search_agent](label)
clicks.append(f'click {node_id} "{search_url}" _blank')
if stack:
parent_level = stack[-1][0]
if level > parent_level:
edges.append(f"{stack[-1][1]} --> {node_id}")
stack.append((level, node_id))
else:
while stack and stack[-1][0] >= level:
stack.pop()
if stack:
edges.append(f"{stack[-1][1]} --> {node_id}")
stack.append((level, node_id))
else:
stack.append((level, node_id))
return "%%{init: {'themeVariables': {'fontSize': '18px'}}}%%\nflowchart LR\n" + "\n".join(nodes + edges + clicks)
def generate_mermaid_html(mermaid_code):
"""Generate HTML to display Mermaid diagram."""
return f"""
<html><head><script src="https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js"></script>
<style>.centered-mermaid{{display:flex;justify-content:center;margin:20px auto;}}</style></head>
<body><div class="mermaid centered-mermaid">{mermaid_code}</div>
<script>mermaid.initialize({{startOnLoad:true}});</script></body></html>
"""
def grow_tree(base_tree, new_node_name, parent_node):
"""Add a new node to the tree under a specified parent."""
lines = base_tree.strip().split('\n')
new_lines = []
added = False
for line in lines:
new_lines.append(line)
if parent_node in line and not added:
indent = len(line) - len(line.lstrip())
new_lines.append(f"{' ' * (indent + 4)}- {new_node_name} ๐ฑ")
added = True
return "\n".join(new_lines)
def get_download_link(file_path, mime_type="text/plain"):
"""Generate a download link for a file."""
with open(file_path, 'rb') as f:
data = f.read()
b64 = base64.b64encode(data).decode()
return f'<a href="data:{mime_type};base64,{b64}" download="{file_path}">Download {file_path}</a>'
# Search Agents (Highest resolution social network default: X)
search_urls = {
"๐๐ArXiv": lambda k: f"/?q={quote(k)}",
"๐ฎGoogle": lambda k: f"https://www.google.com/search?q={quote(k)}",
"๐บYoutube": lambda k: f"https://www.youtube.com/results?search_query={quote(k)}",
"๐ญBing": lambda k: f"https://www.bing.com/search?q={quote(k)}",
"๐กTruth": lambda k: f"https://truthsocial.com/search?q={quote(k)}",
"๐ฑX": lambda k: f"https://twitter.com/search?q={quote(k)}",
}
# Main App
st.title("๐ณ AI Knowledge Tree Builder ๐ฑ")
# Select Project Type
project_type = st.selectbox("Select Project Type", ["Code Project", "Papers Project", "AI Project"])
if 'current_tree' not in st.session_state:
st.session_state['current_tree'] = trees.get("ML Engineering", project_seeds[project_type])
# Select Search Agent for Node Links
search_agent = st.selectbox("Select Search Agent for Node Links", list(search_urls.keys()), index=5) # Default to X
# Tree Growth
new_node = st.text_input("Add New Node")
parent_node = st.text_input("Parent Node")
if st.button("Grow Tree ๐ฑ") and new_node and parent_node:
st.session_state['current_tree'] = grow_tree(st.session_state['current_tree'], new_node, parent_node)
with open("current_tree.md", "w") as f:
f.write(st.session_state['current_tree'])
st.success(f"Added '{new_node}' under '{parent_node}'!")
# Display Mermaid Diagram
st.markdown("### Knowledge Tree Visualization")
mermaid_code = parse_outline_to_mermaid(st.session_state['current_tree'], search_agent)
components.html(generate_mermaid_html(mermaid_code), height=600)
# Export Tree
if st.button("Export Tree as Markdown"):
export_md = f"# Knowledge Tree\n\n## Outline\n{st.session_state['current_tree']}\n\n## Mermaid Diagram\n```mermaid\n{mermaid_code}\n```"
with open("knowledge_tree.md", "w") as f:
f.write(export_md)
st.markdown(get_download_link("knowledge_tree.md", "text/markdown"), unsafe_allow_html=True)
# AI Project: Minimal ML Model Building
if project_type == "AI Project":
st.subheader("Build Minimal ML Model from CSV")
uploaded_file = st.file_uploader("Upload CSV", type="csv")
if uploaded_file:
df = pd.read_csv(uploaded_file)
st.write("Columns:", df.columns.tolist())
feature_cols = st.multiselect("Select feature columns", df.columns)
target_col = st.selectbox("Select target column", df.columns)
if st.button("Train Model"):
X = df[feature_cols].values
y = df[target_col].values
X_tensor = torch.tensor(X, dtype=torch.float32)
y_tensor = torch.tensor(y, dtype=torch.float32).view(-1, 1)
dataset = TensorDataset(X_tensor, y_tensor)
loader = DataLoader(dataset, batch_size=32, shuffle=True)
model = nn.Linear(X.shape[1], 1)
criterion = nn.MSELoss()
optimizer = optim.Adam(model.parameters(), lr=0.01)
for epoch in range(10):
for batch_X, batch_y in loader:
optimizer.zero_grad()
outputs = model(batch_X)
loss = criterion(outputs, batch_y)
loss.backward()
optimizer.step()
torch.save(model.state_dict(), "model.pth")
app_code = f"""
import streamlit as st
import torch
import torch.nn as nn
model = nn.Linear({len(feature_cols)}, 1)
model.load_state_dict(torch.load("model.pth"))
model.eval()
st.title("ML Model Demo")
inputs = []
for col in {feature_cols}:
inputs.append(st.number_input(col))
if st.button("Predict"):
input_tensor = torch.tensor([inputs], dtype=torch.float32)
prediction = model(input_tensor).item()
st.write(f"Predicted {target_col}: {{prediction}}")
"""
with open("app.py", "w") as f:
f.write(app_code)
reqs = "streamlit\ntorch\npandas\n"
with open("requirements.txt", "w") as f:
f.write(reqs)
readme = """
# ML Model Demo
## How to run
1. Install requirements: `pip install -r requirements.txt`
2. Run the app: `streamlit run app.py`
3. Input feature values and click "Predict".
"""
with open("README.md", "w") as f:
f.write(readme)
st.markdown(get_download_link("model.pth", "application/octet-stream"), unsafe_allow_html=True)
st.markdown(get_download_link("app.py", "text/plain"), unsafe_allow_html=True)
st.markdown(get_download_link("requirements.txt", "text/plain"), unsafe_allow_html=True)
st.markdown(get_download_link("README.md", "text/markdown"), unsafe_allow_html=True) |