Spaces:
Sleeping
Sleeping
import streamlit as st | |
import os | |
import glob | |
import re | |
import base64 | |
import pytz | |
from datetime import datetime | |
from transformers.agents import CodeAgent, ReactCodeAgent, ReactJsonAgent, load_tool | |
# Page config | |
st.set_page_config(page_title="π³β¨ AI Knowledge Tree Builder π οΈπ€", page_icon="π³", layout="wide") | |
st.sidebar.markdown(""" | |
π³π€ **AI Knowledge Tree Builder** | |
1. π± Universal access | |
2. β‘ Rapid builds (<2min) | |
3. π Linked AI sources | |
4. π― Lean core | |
5. π§ Shareable memory | |
6. π€ Personalized | |
7. π¦ Cloneable brevity | |
""") | |
# Initialize Agents | |
tools = [load_tool("text-to-speech"), load_tool("image_question_answering")] | |
agents = { | |
"CodeCrafter": CodeAgent(tools=tools, system_prompt="Craft code like a pro! π₯οΈ"), | |
"StepSage": ReactCodeAgent(tools=tools, system_prompt="Step-by-step wisdom! π§ "), | |
"JsonJugger": ReactJsonAgent(tools=tools, system_prompt="JSON-powered antics! π€‘"), | |
"OutlineOracle": ReactCodeAgent(tools=tools, system_prompt="Outline everything! π"), | |
"ToolTitan": CodeAgent(tools=tools, system_prompt="List tools with swagger! π§"), | |
"SpecSpinner": ReactJsonAgent(tools=tools, system_prompt="Spin specs with style! π"), | |
"ImageImp": CodeAgent(tools=tools, system_prompt="Dream up image prompts! π¨"), | |
"VisualVortex": ReactCodeAgent(tools=tools, system_prompt="Generate visuals! πΌοΈ"), | |
"GlossGuru": ReactJsonAgent(tools=tools, system_prompt="Define terms with pizzazz! π"), | |
} | |
# MoE Prompts mapped to agents | |
moe_prompts = { | |
"Create a python streamlit app.py...": "CodeCrafter", | |
"Create a python gradio app.py...": "CodeCrafter", | |
"Create a mermaid model...": "OutlineOracle", | |
"Create a top three list of tools...": "ToolTitan", | |
"Create a specification in markdown...": "SpecSpinner", | |
"Create an image generation prompt...": "ImageImp", | |
"Generate an image which describes...": "VisualVortex", | |
"List top ten glossary terms...": "GlossGuru", | |
"": "StepSage" # Default agent for blank selection | |
} | |
# Session state | |
if 'selected_file' not in st.session_state: | |
st.session_state.selected_file = None | |
if 'view_mode' not in st.session_state: | |
st.session_state.view_mode = 'view' | |
if 'files' not in st.session_state: | |
st.session_state.files = [] | |
# MoE Selection | |
moe_options = list(moe_prompts.keys()) | |
selected_moe = st.selectbox("Pick an MoE Adventure! π²", moe_options, index=0, key="selected_moe") | |
# Utility Functions (simplified for brevity, reusing yours where unchanged) | |
def sanitize_filename(text): | |
return re.sub(r'[^\w\s-]', ' ', text.strip())[:50] | |
def generate_timestamp_filename(query): | |
central = pytz.timezone('US/Central') | |
now = datetime.now(central) | |
return f"{now.strftime('%I%M%p %m%d%Y')} ({sanitize_filename(query)}).md" | |
def save_ai_interaction(query, result, is_rerun=False): | |
filename = generate_timestamp_filename(query) | |
content = f"# {'Rerun' if is_rerun else 'Query'}: {query}\n\n## AI Response\n{result}" | |
with open(filename, 'w', encoding='utf-8') as f: | |
f.write(content) | |
return filename | |
def run_agent(task, agent_name): | |
agent = agents[agent_name] | |
if isinstance(agent, CodeAgent): | |
return agent.run(task, return_generated_code=True) | |
return agent.run(task) | |
# Main App | |
def main(): | |
st.markdown("### π³ AI Knowledge Tree Builder π§ π± Letβs Grow Some Smarts!") | |
query = st.text_input("Ask Away! π€", placeholder="E.g., 'Explain transformers!'") | |
if query: | |
agent_name = moe_prompts[selected_moe] | |
try: | |
result = run_agent(f"{selected_moe} {query}" if selected_moe else query, agent_name) | |
st.markdown(f"π {agent_name} says: {result}") | |
saved_file = save_ai_interaction(query, result) | |
st.success(f"Saved to {saved_file}") | |
st.session_state.selected_file = saved_file | |
except Exception as e: | |
st.error(f"π± Oops! {e}") | |
# File Management (simplified) | |
st.sidebar.title("π Files") | |
md_files = sorted([f for f in glob.glob("*.md") if f.lower() != 'readme.md']) | |
for file in md_files: | |
st.sidebar.write(file) | |
if st.sidebar.button(f"View {file}", key=f"v_{file}"): | |
st.session_state.selected_file = file | |
if st.session_state.selected_file: | |
with open(st.session_state.selected_file, 'r') as f: | |
st.markdown(f.read()) | |
if __name__ == "__main__": | |
main() |