Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,16 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
import os
|
| 4 |
-
import glob
|
| 5 |
import re
|
| 6 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
from datetime import datetime
|
| 8 |
-
import
|
| 9 |
|
| 10 |
-
#
|
| 11 |
st.set_page_config(
|
| 12 |
page_title="AI Knowledge Tree Builder ππΏ",
|
| 13 |
page_icon="π³β¨",
|
|
@@ -16,7 +19,8 @@ st.set_page_config(
|
|
| 16 |
)
|
| 17 |
|
| 18 |
# Predefined Knowledge Trees
|
| 19 |
-
|
|
|
|
| 20 |
0. Biology Core Rules and Future Exceptions
|
| 21 |
1. Central Dogma DNA RNA Protein
|
| 22 |
- Current CRISPR RNA editing π§ͺ
|
|
@@ -26,42 +30,32 @@ BiologyAndLevel36MagicUsers = """
|
|
| 26 |
- Current iPSCs organoids π¦
|
| 27 |
- Research Synthetic cells π¬
|
| 28 |
- Future De novo cell creation π
|
| 29 |
-
"""
|
| 30 |
-
|
| 31 |
-
AITopicsToInnovate1 = """
|
| 32 |
1. Major AI Industry Players π
|
| 33 |
1. Research Leaders π―
|
| 34 |
- OpenAI: GPT-4 DALL-E Foundation Models π΅
|
| 35 |
- Google: PaLM Gemini LLMs π¦
|
| 36 |
- Anthropic: Claude Constitutional AI β‘
|
| 37 |
-
"""
|
| 38 |
-
|
| 39 |
-
MultiplayerGames = """
|
| 40 |
0. Fantasy Domain Introduction
|
| 41 |
1. Setting the Scene
|
| 42 |
- Current Create a high-fantasy realm ποΈ
|
| 43 |
- Research Add domain-specific entities π§ββοΈ
|
| 44 |
- Future AI-generated worldbuilding π
|
| 45 |
-
"""
|
| 46 |
-
|
| 47 |
-
# Root Node with URLs
|
| 48 |
-
RootNode = """
|
| 49 |
-
0. Research Hub π
|
| 50 |
-
1. Awacke1 Profile
|
| 51 |
-
- Link: [Hugging Face Profile](https://huggingface.co/awacke1) π
|
| 52 |
-
2. TeachingCV App
|
| 53 |
-
- Link: [TeachingCV](https://huggingface.co/spaces/awacke1/TeachingCV) π₯οΈ
|
| 54 |
-
3. DeepResearchEvaluator App
|
| 55 |
-
- Link: [DeepResearchEvaluator](https://huggingface.co/spaces/awacke1/DeepResearchEvaluator) π
|
| 56 |
-
"""
|
| 57 |
|
| 58 |
# Utility Functions
|
| 59 |
def sanitize_filename(text):
|
|
|
|
| 60 |
safe_text = re.sub(r'[^\w\s-]', ' ', text)
|
| 61 |
safe_text = re.sub(r'\s+', ' ', safe_text)
|
| 62 |
return safe_text.strip()[:50]
|
| 63 |
|
| 64 |
def generate_timestamp_filename(query):
|
|
|
|
| 65 |
central = pytz.timezone('US/Central')
|
| 66 |
current_time = datetime.now(central)
|
| 67 |
time_str = current_time.strftime("%I%M%p")
|
|
@@ -70,9 +64,11 @@ def generate_timestamp_filename(query):
|
|
| 70 |
return f"{time_str} {date_str} ({safe_query}).md"
|
| 71 |
|
| 72 |
def parse_outline_to_mermaid(outline_text):
|
|
|
|
| 73 |
lines = outline_text.strip().split('\n')
|
| 74 |
nodes = []
|
| 75 |
edges = []
|
|
|
|
| 76 |
stack = []
|
| 77 |
for line in lines:
|
| 78 |
indent = len(line) - len(line.lstrip())
|
|
@@ -82,6 +78,7 @@ def parse_outline_to_mermaid(outline_text):
|
|
| 82 |
if label:
|
| 83 |
node_id = f"N{len(nodes)}"
|
| 84 |
nodes.append(f'{node_id}["{label}"]')
|
|
|
|
| 85 |
if stack:
|
| 86 |
parent_level = stack[-1][0]
|
| 87 |
if level > parent_level:
|
|
@@ -97,9 +94,38 @@ def parse_outline_to_mermaid(outline_text):
|
|
| 97 |
stack.append((level, node_id))
|
| 98 |
else:
|
| 99 |
stack.append((level, node_id))
|
| 100 |
-
return "graph TD\n" + "\n".join(nodes + edges)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
def grow_tree(base_tree, new_node_name, parent_node):
|
|
|
|
| 103 |
lines = base_tree.strip().split('\n')
|
| 104 |
new_lines = []
|
| 105 |
added = False
|
|
@@ -111,89 +137,76 @@ def grow_tree(base_tree, new_node_name, parent_node):
|
|
| 111 |
added = True
|
| 112 |
return "\n".join(new_lines)
|
| 113 |
|
| 114 |
-
def breed_trees(tree1, tree2, intersect_node):
|
| 115 |
-
lines1 = tree1.strip().split('\n')
|
| 116 |
-
lines2 = tree2.strip().split('\n')
|
| 117 |
-
new_lines = lines1.copy()
|
| 118 |
-
for line in lines2:
|
| 119 |
-
if intersect_node not in line and not any(line.strip() in l for l in lines1):
|
| 120 |
-
new_lines.append(line)
|
| 121 |
-
return "\n".join(new_lines)
|
| 122 |
-
|
| 123 |
-
# Model Building Process
|
| 124 |
-
def generate_model_pipeline():
|
| 125 |
-
return """
|
| 126 |
-
graph TD
|
| 127 |
-
A[Load Data π] --> B[Preprocess Data π οΈ]
|
| 128 |
-
B --> C[Train Model π€]
|
| 129 |
-
C --> D[Evaluate Model π]
|
| 130 |
-
D --> E[Deploy Model π]
|
| 131 |
-
"""
|
| 132 |
-
|
| 133 |
# AI Lookup
|
| 134 |
@st.cache_resource
|
| 135 |
def load_generator():
|
|
|
|
| 136 |
return pipeline("text-generation", model="distilgpt2")
|
| 137 |
|
| 138 |
-
#
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
|
| 154 |
-
# Main App
|
| 155 |
st.title("π³ AI Knowledge Tree Builder π±")
|
| 156 |
-
st.markdown("Grow and
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
else:
|
| 163 |
-
#
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
selected_tree
|
| 171 |
-
|
|
|
|
| 172 |
|
| 173 |
# Tree Growth
|
| 174 |
new_node = st.text_input("Add New Node (e.g., 'ML Pipeline')")
|
| 175 |
-
parent_node = st.text_input("Parent Node to Attach To (e.g., '
|
| 176 |
if st.button("Grow Tree π±") and new_node and parent_node:
|
| 177 |
-
current_tree = grow_tree(current_tree, new_node, parent_node)
|
| 178 |
-
trees[selected_tree] = current_tree
|
| 179 |
st.success(f"Added '{new_node}' under '{parent_node}'!")
|
| 180 |
|
| 181 |
-
#
|
| 182 |
-
breed_with = st.selectbox("Breed With Another Tree", [t for t in trees.keys() if t != selected_tree])
|
| 183 |
-
intersect_node = st.text_input("Common Node for Breeding (e.g., 'Research')")
|
| 184 |
-
if st.button("Breed Trees π³"):
|
| 185 |
-
new_tree = breed_trees(current_tree, trees[breed_with], intersect_node)
|
| 186 |
-
trees[f"{selected_tree} + {breed_with}"] = new_tree
|
| 187 |
-
st.success(f"Created new tree: {selected_tree} + {breed_with}")
|
| 188 |
-
|
| 189 |
-
# Display Tree
|
| 190 |
-
mermaid_code = parse_outline_to_mermaid(current_tree)
|
| 191 |
st.markdown("### Knowledge Tree Visualization")
|
| 192 |
-
st.
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
st.markdown("### ML Model Building Pipeline")
|
| 196 |
-
st.mermaid(generate_model_pipeline())
|
| 197 |
|
| 198 |
# AI Lookup
|
| 199 |
query = st.text_input("Enter Query for AI Lookup")
|
|
@@ -206,5 +219,13 @@ else:
|
|
| 206 |
f.write(f"# Query: {query}\n\n## AI Response\n{response}")
|
| 207 |
st.success(f"Saved to {filename}")
|
| 208 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 209 |
if __name__ == "__main__":
|
| 210 |
-
st.sidebar.markdown("Explore
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
|
| 3 |
import os
|
|
|
|
| 4 |
import re
|
| 5 |
+
import glob
|
| 6 |
+
import streamlit as st
|
| 7 |
+
import streamlit.components.v1 as components
|
| 8 |
+
from transformers import pipeline
|
| 9 |
+
from urllib.parse import quote
|
| 10 |
from datetime import datetime
|
| 11 |
+
import pytz
|
| 12 |
|
| 13 |
+
# Page Configuration
|
| 14 |
st.set_page_config(
|
| 15 |
page_title="AI Knowledge Tree Builder ππΏ",
|
| 16 |
page_icon="π³β¨",
|
|
|
|
| 19 |
)
|
| 20 |
|
| 21 |
# Predefined Knowledge Trees
|
| 22 |
+
trees = {
|
| 23 |
+
"Biology": """
|
| 24 |
0. Biology Core Rules and Future Exceptions
|
| 25 |
1. Central Dogma DNA RNA Protein
|
| 26 |
- Current CRISPR RNA editing π§ͺ
|
|
|
|
| 30 |
- Current iPSCs organoids π¦
|
| 31 |
- Research Synthetic cells π¬
|
| 32 |
- Future De novo cell creation π
|
| 33 |
+
""",
|
| 34 |
+
"AI Topics": """
|
|
|
|
| 35 |
1. Major AI Industry Players π
|
| 36 |
1. Research Leaders π―
|
| 37 |
- OpenAI: GPT-4 DALL-E Foundation Models π΅
|
| 38 |
- Google: PaLM Gemini LLMs π¦
|
| 39 |
- Anthropic: Claude Constitutional AI β‘
|
| 40 |
+
""",
|
| 41 |
+
"Multiplayer Games": """
|
|
|
|
| 42 |
0. Fantasy Domain Introduction
|
| 43 |
1. Setting the Scene
|
| 44 |
- Current Create a high-fantasy realm ποΈ
|
| 45 |
- Research Add domain-specific entities π§ββοΈ
|
| 46 |
- Future AI-generated worldbuilding π
|
| 47 |
+
"""
|
| 48 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
# Utility Functions
|
| 51 |
def sanitize_filename(text):
|
| 52 |
+
"""Sanitize text for use in filenames."""
|
| 53 |
safe_text = re.sub(r'[^\w\s-]', ' ', text)
|
| 54 |
safe_text = re.sub(r'\s+', ' ', safe_text)
|
| 55 |
return safe_text.strip()[:50]
|
| 56 |
|
| 57 |
def generate_timestamp_filename(query):
|
| 58 |
+
"""Generate a timestamped filename for saving files."""
|
| 59 |
central = pytz.timezone('US/Central')
|
| 60 |
current_time = datetime.now(central)
|
| 61 |
time_str = current_time.strftime("%I%M%p")
|
|
|
|
| 64 |
return f"{time_str} {date_str} ({safe_query}).md"
|
| 65 |
|
| 66 |
def parse_outline_to_mermaid(outline_text):
|
| 67 |
+
"""Convert a tree outline to Mermaid syntax with clickable nodes."""
|
| 68 |
lines = outline_text.strip().split('\n')
|
| 69 |
nodes = []
|
| 70 |
edges = []
|
| 71 |
+
clicks = []
|
| 72 |
stack = []
|
| 73 |
for line in lines:
|
| 74 |
indent = len(line) - len(line.lstrip())
|
|
|
|
| 78 |
if label:
|
| 79 |
node_id = f"N{len(nodes)}"
|
| 80 |
nodes.append(f'{node_id}["{label}"]')
|
| 81 |
+
clicks.append(f'click {node_id} "?q={quote(label)}" _blank')
|
| 82 |
if stack:
|
| 83 |
parent_level = stack[-1][0]
|
| 84 |
if level > parent_level:
|
|
|
|
| 94 |
stack.append((level, node_id))
|
| 95 |
else:
|
| 96 |
stack.append((level, node_id))
|
| 97 |
+
return "graph TD\n" + "\n".join(nodes + edges + clicks)
|
| 98 |
+
|
| 99 |
+
def generate_mermaid_html(mermaid_code: str) -> str:
|
| 100 |
+
"""Generate HTML to center and display a Mermaid diagram."""
|
| 101 |
+
return f"""
|
| 102 |
+
<html>
|
| 103 |
+
<head>
|
| 104 |
+
<script src="https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js"></script>
|
| 105 |
+
<style>
|
| 106 |
+
.centered-mermaid {{
|
| 107 |
+
display: flex;
|
| 108 |
+
justify-content: center;
|
| 109 |
+
margin: 20px auto;
|
| 110 |
+
}}
|
| 111 |
+
.mermaid {{
|
| 112 |
+
max-width: 800px;
|
| 113 |
+
}}
|
| 114 |
+
</style>
|
| 115 |
+
</head>
|
| 116 |
+
<body>
|
| 117 |
+
<div class="mermaid centered-mermaid">
|
| 118 |
+
{mermaid_code}
|
| 119 |
+
</div>
|
| 120 |
+
<script>
|
| 121 |
+
mermaid.initialize({{ startOnLoad: true }});
|
| 122 |
+
</script>
|
| 123 |
+
</body>
|
| 124 |
+
</html>
|
| 125 |
+
"""
|
| 126 |
|
| 127 |
def grow_tree(base_tree, new_node_name, parent_node):
|
| 128 |
+
"""Add a new node to the tree under a specified parent."""
|
| 129 |
lines = base_tree.strip().split('\n')
|
| 130 |
new_lines = []
|
| 131 |
added = False
|
|
|
|
| 137 |
added = True
|
| 138 |
return "\n".join(new_lines)
|
| 139 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
# AI Lookup
|
| 141 |
@st.cache_resource
|
| 142 |
def load_generator():
|
| 143 |
+
"""Load a text generation model for AI lookups."""
|
| 144 |
return pipeline("text-generation", model="distilgpt2")
|
| 145 |
|
| 146 |
+
# File Management
|
| 147 |
+
def FileSidebar():
|
| 148 |
+
"""Render a sidebar for managing Markdown files."""
|
| 149 |
+
st.sidebar.title("π Saved Interactions")
|
| 150 |
+
md_files = glob.glob("*.md")
|
| 151 |
+
for file in md_files:
|
| 152 |
+
col1, col2, col3 = st.sidebar.columns([1, 3, 1])
|
| 153 |
+
with col1:
|
| 154 |
+
if st.button("π", key=f"open_{file}"):
|
| 155 |
+
with open(file, 'r') as f:
|
| 156 |
+
st.session_state['file_content'] = f.read()
|
| 157 |
+
st.session_state['selected_file'] = file
|
| 158 |
+
with col2:
|
| 159 |
+
st.write(file)
|
| 160 |
+
with col3:
|
| 161 |
+
if st.button("π", key=f"delete_{file}"):
|
| 162 |
+
os.remove(file)
|
| 163 |
+
st.rerun()
|
| 164 |
+
if st.sidebar.button("Create New Note"):
|
| 165 |
+
filename = generate_timestamp_filename("New Note")
|
| 166 |
+
with open(filename, 'w') as f:
|
| 167 |
+
f.write("# New Note\n")
|
| 168 |
+
st.sidebar.success(f"Created {filename}")
|
| 169 |
|
| 170 |
+
# Main App Logic
|
| 171 |
st.title("π³ AI Knowledge Tree Builder π±")
|
| 172 |
+
st.markdown("Grow, visualize, and explore knowledge trees with AI!")
|
| 173 |
+
|
| 174 |
+
# Handle Query Parameters
|
| 175 |
+
query_params = st.query_params
|
| 176 |
+
if 'q' in query_params:
|
| 177 |
+
query = query_params['q']
|
| 178 |
+
st.subheader(f"AI Lookup for: {query}")
|
| 179 |
+
generator = load_generator()
|
| 180 |
+
response = generator(query, max_length=50)[0]['generated_text']
|
| 181 |
+
st.write(f"**Response:** {response}")
|
| 182 |
+
filename = generate_timestamp_filename(query)
|
| 183 |
+
with open(filename, 'w') as f:
|
| 184 |
+
f.write(f"# Query: {query}\n\n## AI Response\n{response}")
|
| 185 |
+
st.success(f"Saved to {filename}")
|
| 186 |
else:
|
| 187 |
+
# Tree Selection and Interaction
|
| 188 |
+
if 'current_tree' not in st.session_state:
|
| 189 |
+
st.session_state['current_tree'] = trees["Biology"]
|
| 190 |
+
if 'selected_tree_name' not in st.session_state:
|
| 191 |
+
st.session_state['selected_tree_name'] = "Biology"
|
| 192 |
+
|
| 193 |
+
selected_tree = st.selectbox("Select Knowledge Tree", list(trees.keys()), key="tree_select")
|
| 194 |
+
if selected_tree != st.session_state['selected_tree_name']:
|
| 195 |
+
st.session_state['current_tree'] = trees[selected_tree]
|
| 196 |
+
st.session_state['selected_tree_name'] = selected_tree
|
| 197 |
|
| 198 |
# Tree Growth
|
| 199 |
new_node = st.text_input("Add New Node (e.g., 'ML Pipeline')")
|
| 200 |
+
parent_node = st.text_input("Parent Node to Attach To (e.g., 'Central Dogma')")
|
| 201 |
if st.button("Grow Tree π±") and new_node and parent_node:
|
| 202 |
+
st.session_state['current_tree'] = grow_tree(st.session_state['current_tree'], new_node, parent_node)
|
|
|
|
| 203 |
st.success(f"Added '{new_node}' under '{parent_node}'!")
|
| 204 |
|
| 205 |
+
# Display Mermaid Diagram
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 206 |
st.markdown("### Knowledge Tree Visualization")
|
| 207 |
+
mermaid_code = parse_outline_to_mermaid(st.session_state['current_tree'])
|
| 208 |
+
mermaid_html = generate_mermaid_html(mermaid_code)
|
| 209 |
+
components.html(mermaid_html, height=600)
|
|
|
|
|
|
|
| 210 |
|
| 211 |
# AI Lookup
|
| 212 |
query = st.text_input("Enter Query for AI Lookup")
|
|
|
|
| 219 |
f.write(f"# Query: {query}\n\n## AI Response\n{response}")
|
| 220 |
st.success(f"Saved to {filename}")
|
| 221 |
|
| 222 |
+
# File Viewer
|
| 223 |
+
if 'file_content' in st.session_state:
|
| 224 |
+
st.markdown("### Selected File")
|
| 225 |
+
st.markdown(st.session_state['file_content'])
|
| 226 |
+
|
| 227 |
+
# Sidebar
|
| 228 |
+
FileSidebar()
|
| 229 |
+
|
| 230 |
if __name__ == "__main__":
|
| 231 |
+
st.sidebar.markdown("Explore and grow your knowledge trees!")
|