#!/usr/bin/env python3 import os import re import glob import json import base64 import zipfile import random import requests import openai from PIL import Image from urllib.parse import quote import streamlit as st import streamlit.components.v1 as components # ๐Ÿฐ If you do model inference via huggingface_hub # from huggingface_hub import InferenceClient # ===================================================================================== # 1) GLOBAL CONFIG & PLACEHOLDERS # ===================================================================================== BASE_URL = "https://huggingface.co/spaces/awacke1/MermaidMarkdownDiagramEditor" PromptPrefix = "AI-Search: " PromptPrefix2 = "AI-Refine: " PromptPrefix3 = "AI-JS: " roleplaying_glossary = { "Core Rulebooks": { "Dungeons and Dragons": ["Player's Handbook", "Dungeon Master's Guide", "Monster Manual"], "GURPS": ["Basic Set Characters", "Basic Set Campaigns"] }, "Campaigns & Adventures": { "Pathfinder": ["Rise of the Runelords", "Curse of the Crimson Throne"] } } transhuman_glossary = { "Neural Interfaces": ["Cortex Jack", "Mind-Machine Fusion"], "Cybernetics": ["Robotic Limbs", "Augmented Eyes"], } def process_text(text): """๐Ÿ•ต๏ธ process_text: detective styleโ€”prints lines to Streamlit for debugging.""" st.write(f"process_text called with: {text}") def search_arxiv(text): """๐Ÿ”ญ search_arxiv: pretend to search ArXiv, just prints debug for now.""" st.write(f"search_arxiv called with: {text}") def SpeechSynthesis(text): """๐Ÿ—ฃ SpeechSynthesis: read lines out loud? Here, we log them for demonstration.""" st.write(f"SpeechSynthesis called with: {text}") def process_image(image_file, prompt): """๐Ÿ“ท process_image: imagine an AI pipeline for images, here we just log.""" return f"[process_image placeholder] {image_file} => {prompt}" def process_video(video_file, seconds_per_frame): """๐ŸŽž process_video: placeholder for video tasks, logs to Streamlit.""" st.write(f"[process_video placeholder] {video_file}, {seconds_per_frame} sec/frame") API_URL = "https://huggingface-inference-endpoint-placeholder" API_KEY = "hf_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" @st.cache_resource def InferenceLLM(prompt): """๐Ÿ”ฎ InferenceLLM: a stub returning a mock response for 'prompt'.""" return f"[InferenceLLM placeholder response to prompt: {prompt}]" # ===================================================================================== # 2) GLOSSARY & FILE UTILITY # ===================================================================================== @st.cache_resource def display_glossary_entity(k): """ Creates multiple link emojis for a single entity. Each link might point to /?q=..., /?q=..., or external sites. """ search_urls = { "๐Ÿš€๐ŸŒŒArXiv": lambda x: f"/?q={quote(x)}", "๐ŸƒAnalyst": lambda x: f"/?q={quote(x)}-{quote(PromptPrefix)}", "๐Ÿ“šPyCoder": lambda x: f"/?q={quote(x)}-{quote(PromptPrefix2)}", "๐Ÿ”ฌJSCoder": lambda x: f"/?q={quote(x)}-{quote(PromptPrefix3)}", "๐Ÿ“–": lambda x: f"https://en.wikipedia.org/wiki/{quote(x)}", "๐Ÿ”": lambda x: f"https://www.google.com/search?q={quote(x)}", "๐Ÿ”Ž": lambda x: f"https://www.bing.com/search?q={quote(x)}", "๐ŸŽฅ": lambda x: f"https://www.youtube.com/results?search_query={quote(x)}", "๐Ÿฆ": lambda x: f"https://twitter.com/search?q={quote(x)}", } links_md = ' '.join([f"[{emoji}]({url(k)})" for emoji, url in search_urls.items()]) st.markdown(f"**{k}** {links_md}", unsafe_allow_html=True) def display_content_or_image(query): """ If 'query' is in transhuman_glossary or there's an image matching 'images/.png', we show it. Otherwise warn. """ for category, term_list in transhuman_glossary.items(): for term in term_list: if query.lower() in term.lower(): st.subheader(f"Found in {category}:") st.write(term) return True image_path = f"images/{query}.png" if os.path.exists(image_path): st.image(image_path, caption=f"Image for {query}") return True st.warning("No matching content or image found.") return False def clear_query_params(): """For fully clearing, you'd do a redirect or st.experimental_set_query_params().""" st.warning("Define a redirect or link without query params if you want to truly clear them.") # ===================================================================================== # 3) FILE-HANDLING (MD files, etc.) # ===================================================================================== def load_file(file_path): """Load file contents as UTF-8 text, or return empty on error.""" try: with open(file_path, "r", encoding='utf-8') as f: return f.read() except: return "" @st.cache_resource def create_zip_of_files(files): """Combine multiple local files into a single .zip for user to download.""" zip_name = "Arxiv-Paper-Search-QA-RAG-Streamlit-Gradio-AP.zip" with zipfile.ZipFile(zip_name, 'w') as zipf: for file in files: zipf.write(file) return zip_name @st.cache_resource def get_zip_download_link(zip_file): """Return an link to download the given zip_file (base64-encoded).""" with open(zip_file, 'rb') as f: data = f.read() b64 = base64.b64encode(data).decode() return f'Download All' def get_table_download_link(file_path): """ Creates a download link for a single file from your snippet. Encodes it as base64 data. """ try: with open(file_path, 'r', encoding='utf-8') as file: data = file.read() b64 = base64.b64encode(data.encode()).decode() file_name = os.path.basename(file_path) ext = os.path.splitext(file_name)[1] mime_map = { '.txt': 'text/plain', '.py': 'text/plain', '.xlsx': 'text/plain', '.csv': 'text/plain', '.htm': 'text/html', '.md': 'text/markdown', '.wav': 'audio/wav' } mime_type = mime_map.get(ext, 'application/octet-stream') return f'{file_name}' except: return '' def get_file_size(file_path): """Get file size in bytes.""" return os.path.getsize(file_path) def FileSidebar(): """ Renders .md files in the sidebar with open/view/run/delete logic. """ all_files = glob.glob("*.md") # If you want to filter out short-named or special files: all_files = [f for f in all_files if len(os.path.splitext(f)[0]) >= 5] all_files.sort(key=lambda x: (os.path.splitext(x)[1], x), reverse=True) Files1, Files2 = st.sidebar.columns(2) with Files1: if st.button("๐Ÿ—‘ Delete All"): for file in all_files: os.remove(file) st.rerun() with Files2: if st.button("โฌ‡๏ธ Download"): zip_file = create_zip_of_files(all_files) st.sidebar.markdown(get_zip_download_link(zip_file), unsafe_allow_html=True) file_contents = '' file_name = '' next_action = '' for file in all_files: col1, col2, col3, col4, col5 = st.sidebar.columns([1, 6, 1, 1, 1]) with col1: if st.button("๐ŸŒ", key="md_" + file): file_contents = load_file(file) file_name = file next_action = 'md' st.session_state['next_action'] = next_action with col2: st.markdown(get_table_download_link(file), unsafe_allow_html=True) with col3: if st.button("๐Ÿ“‚", key="open_" + file): file_contents = load_file(file) file_name = file next_action = 'open' st.session_state['lastfilename'] = file st.session_state['filename'] = file st.session_state['filetext'] = file_contents st.session_state['next_action'] = next_action with col4: if st.button("โ–ถ๏ธ", key="read_" + file): file_contents = load_file(file) file_name = file next_action = 'search' st.session_state['next_action'] = next_action with col5: if st.button("๐Ÿ—‘", key="delete_" + file): os.remove(file) st.rerun() if file_contents: if next_action == 'open': open1, open2 = st.columns([0.8, 0.2]) with open1: file_name_input = st.text_input('File Name:', file_name, key='file_name_input') file_content_area = st.text_area('File Contents:', file_contents, height=300, key='file_content_area') if st.button('๐Ÿ’พ Save File'): with open(file_name_input, 'w', encoding='utf-8') as f: f.write(file_content_area) st.markdown(f'Saved {file_name_input} successfully.') elif next_action == 'search': file_content_area = st.text_area("File Contents:", file_contents, height=500) user_prompt = PromptPrefix2 + file_contents st.markdown(user_prompt) if st.button('๐Ÿ”Re-Code'): search_arxiv(file_contents) elif next_action == 'md': st.markdown(file_contents) SpeechSynthesis(file_contents) if st.button("๐Ÿ”Run"): st.write("Running GPT logic placeholder...") # ===================================================================================== # 4) SCORING / GLOSSARIES # ===================================================================================== score_dir = "scores" os.makedirs(score_dir, exist_ok=True) def generate_key(label, header, idx): return f"{header}_{label}_{idx}_key" def update_score(key, increment=1): """Increment the 'score' for a glossary item in JSON storage.""" score_file = os.path.join(score_dir, f"{key}.json") if os.path.exists(score_file): with open(score_file, "r") as file: score_data = json.load(file) else: score_data = {"clicks": 0, "score": 0} score_data["clicks"] += increment score_data["score"] += increment with open(score_file, "w") as file: json.dump(score_data, file) return score_data["score"] def load_score(key): """Load the stored score from .json if it exists, else 0.""" file_path = os.path.join(score_dir, f"{key}.json") if os.path.exists(file_path): with open(file_path, "r") as file: score_data = json.load(file) return score_data["score"] return 0 def display_buttons_with_scores(num_columns_text): """ Show glossary items as clickable buttons, each increments a 'score'. """ game_emojis = { "Dungeons and Dragons": "๐Ÿ‰", "Call of Cthulhu": "๐Ÿ™", "GURPS": "๐ŸŽฒ", "Pathfinder": "๐Ÿ—บ๏ธ", "Kindred of the East": "๐ŸŒ…", "Changeling": "๐Ÿƒ", } topic_emojis = { "Core Rulebooks": "๐Ÿ“š", "Maps & Settings": "๐Ÿ—บ๏ธ", "Game Mechanics & Tools": "โš™๏ธ", "Monsters & Adversaries": "๐Ÿ‘น", "Campaigns & Adventures": "๐Ÿ“œ", "Creatives & Assets": "๐ŸŽจ", "Game Master Resources": "๐Ÿ› ๏ธ", "Lore & Background": "๐Ÿ“–", "Character Development": "๐Ÿง", "Homebrew Content": "๐Ÿ”ง", "General Topics": "๐ŸŒ", } for category, games in roleplaying_glossary.items(): category_emoji = topic_emojis.get(category, "๐Ÿ”") st.markdown(f"## {category_emoji} {category}") for game, terms in games.items(): game_emoji = game_emojis.get(game, "๐ŸŽฎ") for term in terms: key = f"{category}_{game}_{term}".replace(' ', '_').lower() score_val = load_score(key) if st.button(f"{game_emoji} {category} {game} {term} {score_val}", key=key): newscore = update_score(key.replace('?', '')) st.markdown(f"Scored **{category} - {game} - {term}** -> {newscore}") # ===================================================================================== # 5) IMAGES & VIDEOS # ===================================================================================== def display_images_and_wikipedia_summaries(num_columns=4): """Display .png images in a grid, referencing the name as a 'keyword'.""" image_files = [f for f in os.listdir('.') if f.endswith('.png')] if not image_files: st.write("No PNG images found in the current directory.") return image_files_sorted = sorted(image_files, key=lambda x: len(x.split('.')[0])) cols = st.columns(num_columns) col_index = 0 for image_file in image_files_sorted: with cols[col_index % num_columns]: try: image = Image.open(image_file) st.image(image, use_column_width=True) k = image_file.split('.')[0] display_glossary_entity(k) image_text_input = st.text_input(f"Prompt for {image_file}", key=f"image_prompt_{image_file}") if image_text_input: response = process_image(image_file, image_text_input) st.markdown(response) except: st.write(f"Could not open {image_file}") col_index += 1 def display_videos_and_links(num_columns=4): """Displays all .mp4/.webm in a grid, plus text input for prompts.""" video_files = [f for f in os.listdir('.') if f.endswith(('.mp4', '.webm'))] if not video_files: st.write("No MP4 or WEBM videos found in the current directory.") return video_files_sorted = sorted(video_files, key=lambda x: len(x.split('.')[0])) cols = st.columns(num_columns) col_index = 0 for video_file in video_files_sorted: with cols[col_index % num_columns]: k = video_file.split('.')[0] st.video(video_file, format='video/mp4', start_time=0) display_glossary_entity(k) video_text_input = st.text_input(f"Video Prompt for {video_file}", key=f"video_prompt_{video_file}") if video_text_input: try: seconds_per_frame = 10 process_video(video_file, seconds_per_frame) except ValueError: st.error("Invalid input for seconds per frame!") col_index += 1 # ===================================================================================== # 6) MERMAID & PARTIAL SUBGRAPH LOGIC # ===================================================================================== def generate_mermaid_html(mermaid_code: str) -> str: """Embed mermaid_code in a minimal HTML snippet, centered.""" return f"""
{mermaid_code}
""" def append_model_param(url: str, model_selected: bool) -> str: """If user selects 'model=1', we append &model=1 or ?model=1 if not present.""" if not model_selected: return url delimiter = "&" if "?" in url else "?" return f"{url}{delimiter}model=1" def inject_base_url(url: str) -> str: """If link doesn't start with 'http', prepend BASE_URL so it's absolute.""" if url.startswith("http"): return url return f"{BASE_URL}{url}" # Our default diagram, containing the "click" lines with /?q=... DEFAULT_MERMAID = r""" flowchart LR U((User ๐Ÿ˜Ž)) -- "Talk ๐Ÿ—ฃ๏ธ" --> LLM[LLM Agent ๐Ÿค–\nExtract Info] click U "/?q=User%20๐Ÿ˜Ž" "Open 'User ๐Ÿ˜Ž'" "_blank" click LLM "/?q=LLM%20Agent%20Extract%20Info" "Open LLM" "_blank" LLM -- "Query ๐Ÿ”" --> HS[Hybrid Search ๐Ÿ”Ž\nVector+NER+Lexical] click HS "/?q=Hybrid%20Search%20Vector+NER+Lexical" "Open HS" "_blank" HS -- "Reason ๐Ÿค”" --> RE[Reasoning Engine ๐Ÿ› ๏ธ\nNeuralNetwork+Medical] click RE "/?q=Reasoning%20Engine%20NeuralNetwork+Medical" "Open RE" "_blank" RE -- "Link ๐Ÿ“ก" --> KG((Knowledge Graph ๐Ÿ“š\nOntology+GAR+RAG)) click KG "/?q=Knowledge%20Graph%20Ontology+GAR+RAG" "Open KG" "_blank" """ # BFS subgraph: we parse lines like A -- "Label" --> B def parse_mermaid_edges(mermaid_text: str): """ ๐Ÿฟ parse_mermaid_edges: - Find lines like: A -- "Label" --> B - Return adjacency dict: edges[A] = [(label, B), ...] """ adjacency = {} # e.g. U((User ๐Ÿ˜Ž)) -- "Talk ๐Ÿ—ฃ๏ธ" --> LLM[LLM Agent ๐Ÿค–\nExtract Info] edge_pattern = re.compile(r'(\S+)\s*--\s*"([^"]*)"\s*-->\s*(\S+)') for line in mermaid_text.split('\n'): match = edge_pattern.search(line.strip()) if match: nodeA, label, nodeB = match.groups() if nodeA not in adjacency: adjacency[nodeA] = [] adjacency[nodeA].append((label, nodeB)) return adjacency def bfs_subgraph(adjacency, start_node, depth=1): """ ๐ŸŽ bfs_subgraph: - Gather edges up to 'depth' levels from start_node - If depth=1, only direct edges from node """ from collections import deque visited = set() queue = deque([(start_node, 0)]) edges = [] while queue: current, lvl = queue.popleft() if current in visited: continue visited.add(current) if current in adjacency and lvl < depth: for (label, child) in adjacency[current]: edges.append((current, label, child)) queue.append((child, lvl + 1)) return edges def create_subgraph_mermaid(sub_edges, start_node): """ ๐Ÿ„ create_subgraph_mermaid: - build a smaller flowchart snippet with edges from BFS """ sub_mermaid = "flowchart LR\n" sub_mermaid += f" %% Subgraph for {start_node}\n" if not sub_edges: sub_mermaid += f" {start_node}\n" sub_mermaid += " %% End of partial subgraph\n" return sub_mermaid for (A, label, B) in sub_edges: sub_mermaid += f' {A} -- "{label}" --> {B}\n' sub_mermaid += " %% End of partial subgraph\n" return sub_mermaid # ===================================================================================== # 7) MAIN APP # ===================================================================================== def main(): st.set_page_config(page_title="Mermaid + BFS Subgraph + Full Logic", layout="wide") # 1) Query param parsing query_params = st.query_params query_list = (query_params.get('q') or query_params.get('query') or ['']) q_or_query = query_list[0].strip() if len(query_list) > 0 else "" # If 'action' param is present if 'action' in query_params: action_list = query_params['action'] if action_list: action = action_list[0] if action == 'show_message': st.success("Showing a message because 'action=show_message' was found in the URL.") elif action == 'clear': clear_query_params() # If there's a 'query=' param, display content or image if 'query' in query_params: query_val = query_params['query'][0] display_content_or_image(query_val) # 2) Let user pick ?model=1 st.sidebar.write("## Diagram Link Settings") model_selected = st.sidebar.checkbox("Append ?model=1 to each link?") # 3) We'll parse adjacency from DEFAULT_MERMAID, then do the injection for base URL # and possible model param. We'll store the final mermaid code in session. lines = DEFAULT_MERMAID.strip().split("\n") new_lines = [] for line in lines: if "click " in line and '"/?' in line: # Try to parse out the URL via a simpler pattern # e.g. click U "/?q=User%20๐Ÿ˜Ž" "Open 'User ๐Ÿ˜Ž'" "_blank" # We'll do a quick re.split capturing 4 groups # Example: [prefix, '/?q=User%20๐Ÿ˜Ž', "Open 'User ๐Ÿ˜Ž'", '_blank', remainder?] pattern = r'(click\s+\S+\s+)"([^"]+)"\s+"([^"]+)"\s+"([^"]+)"' match = re.match(pattern, line.strip()) if match: prefix_part = match.group(1) # e.g. "click U " old_url = match.group(2) # e.g. /?q=User%20๐Ÿ˜Ž tooltip = match.group(3) # e.g. Open 'User ๐Ÿ˜Ž' target = match.group(4) # e.g. _blank # 1) base new_url = inject_base_url(old_url) # 2) model param new_url = append_model_param(new_url, model_selected) new_line = f'{prefix_part}"{new_url}" "{tooltip}" "{target}"' new_lines.append(new_line) else: new_lines.append(line) else: new_lines.append(line) final_mermaid = "\n".join(new_lines) adjacency = parse_mermaid_edges(final_mermaid) # 4) If user clicked a shape => we show a partial subgraph as "SearchResult" partial_subgraph_html = "" if q_or_query: st.info(f"process_text called with: {PromptPrefix}{q_or_query}") # Attempt to find a node whose ID or label includes q_or_query: # We'll do a naive approach: if q_or_query is substring ignoring spaces possible_keys = [] for nodeKey in adjacency.keys(): # e.g. nodeKey might be 'U((User ๐Ÿ˜Ž))' simplified_key = nodeKey.replace("\\n", " ").replace("[", "").replace("]", "").lower() simplified_query = q_or_query.lower().replace("%20", " ") if simplified_query in simplified_key: possible_keys.append(nodeKey) chosen_node = None if possible_keys: chosen_node = possible_keys[0] else: st.warning("No adjacency node matched the query param's text. Subgraph is empty.") if chosen_node: sub_edges = bfs_subgraph(adjacency, chosen_node, depth=1) sub_mermaid = create_subgraph_mermaid(sub_edges, chosen_node) partial_subgraph_html = generate_mermaid_html(sub_mermaid) # 5) Show partial subgraph top-center if we have any if partial_subgraph_html: st.subheader("SearchResult Subgraph") components.html(partial_subgraph_html, height=300, scrolling=False) # 6) Render the top-centered *full* diagram st.title("Full Mermaid Diagram (with Base URL + BFS partial subgraphs)") diagram_html = generate_mermaid_html(final_mermaid) components.html(diagram_html, height=400, scrolling=True) # 7) Editor columns: Markdown & Mermaid left_col, right_col = st.columns(2) with left_col: st.subheader("Markdown Side ๐Ÿ“") if "markdown_text" not in st.session_state: st.session_state["markdown_text"] = "## Hello!\nYou can type some *Markdown* here.\n" markdown_text = st.text_area( "Edit Markdown:", value=st.session_state["markdown_text"], height=300 ) st.session_state["markdown_text"] = markdown_text # Buttons colA, colB = st.columns(2) with colA: if st.button("๐Ÿ”„ Refresh Markdown"): st.write("**Markdown** content refreshed! ๐Ÿฟ") with colB: if st.button("โŒ Clear Markdown"): st.session_state["markdown_text"] = "" st.rerun() st.markdown("---") st.markdown("**Preview:**") st.markdown(markdown_text) with right_col: st.subheader("Mermaid Side ๐Ÿงœโ€โ™‚๏ธ") if "current_mermaid" not in st.session_state: st.session_state["current_mermaid"] = final_mermaid mermaid_input = st.text_area( "Edit Mermaid Code:", value=st.session_state["current_mermaid"], height=300 ) colC, colD = st.columns(2) with colC: if st.button("๐ŸŽจ Refresh Diagram"): st.session_state["current_mermaid"] = mermaid_input st.write("**Mermaid** diagram refreshed! ๐ŸŒˆ") st.rerun() with colD: if st.button("โŒ Clear Mermaid"): st.session_state["current_mermaid"] = "" st.rerun() st.markdown("---") st.markdown("**Mermaid Source:**") st.code(mermaid_input, language="python", line_numbers=True) # 8) Show the galleries st.markdown("---") st.header("Media Galleries") num_columns_images = st.slider("Choose Number of Image Columns", 1, 15, 5, key="num_columns_images") display_images_and_wikipedia_summaries(num_columns_images) num_columns_video = st.slider("Choose Number of Video Columns", 1, 15, 5, key="num_columns_video") display_videos_and_links(num_columns_video) # 9) Possibly show extended text interface showExtendedTextInterface = False if showExtendedTextInterface: # e.g. display_glossary_grid(roleplaying_glossary) # num_columns_text = st.slider("Choose Number of Text Columns", 1, 15, 4) # display_buttons_with_scores(num_columns_text) pass # 10) Render the file sidebar FileSidebar() # 11) Random title at bottom titles = [ "๐Ÿง ๐ŸŽญ Semantic Symphonies & Episodic Encores", "๐ŸŒŒ๐ŸŽผ AI Rhythms of Memory Lane", "๐ŸŽญ๐ŸŽ‰ Cognitive Crescendos & Neural Harmonies", "๐Ÿง ๐ŸŽบ Mnemonic Melodies & Synaptic Grooves", "๐ŸŽผ๐ŸŽธ Straight Outta Cognition", "๐Ÿฅ๐ŸŽป Jazzy Jambalaya of AI Memories", "๐Ÿฐ Semantic Soul & Episodic Essence", "๐Ÿฅ๐ŸŽป The Music Of AI's Mind" ] st.markdown(f"**{random.choice(titles)}**") if __name__ == "__main__": main()