#!/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 # ---------------------------- # Configurable BASE_URL # ---------------------------- BASE_URL = "https://huggingface.co/spaces/awacke1/MermaidMarkdownDiagramEditor" # Example placeholders for prompt prefixes PromptPrefix = "AI-Search: " PromptPrefix2 = "AI-Refine: " PromptPrefix3 = "AI-JS: " # Example roleplaying glossary 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"] } } # Example transhuman glossary transhuman_glossary = { "Neural Interfaces": ["Cortex Jack", "Mind-Machine Fusion"], "Cybernetics": ["Robotic Limbs", "Augmented Eyes"], } # Simple function stubs def process_text(text): st.write(f"process_text called with: {text}") def search_arxiv(text): st.write(f"search_arxiv called with: {text}") def SpeechSynthesis(text): st.write(f"SpeechSynthesis called with: {text}") def process_image(image_file, prompt): return f"[process_image placeholder] Processing {image_file} with prompt: {prompt}" def process_video(video_file, seconds_per_frame): st.write(f"[process_video placeholder] Video: {video_file}, seconds/frame: {seconds_per_frame}") # Stub if you have a Hugging Face endpoint API_URL = "https://huggingface-inference-endpoint-placeholder" API_KEY = "hf_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" @st.cache_resource def InferenceLLM(prompt): return f"[InferenceLLM placeholder response to prompt: {prompt}]" # ------------------------------------------ # Glossary & File Utility # ------------------------------------------ @st.cache_resource def display_glossary_entity(k): """ Creates multiple link emojis for a single entity. """ 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 a query matches something in transhuman_glossary or a local image, show it. """ 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 clearing URL params, you'd typically use a new link or st.experimental_set_query_params(). Here, we just warn the user. """ st.warning("Define a redirect or link without query params if you want to truly clear them.") # ----------------------- # File Handling # ----------------------- def load_file(file_path): 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): 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): 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. """ 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): return os.path.getsize(file_path) def FileSidebar(): """ Renders .md files, providing open/view/delete/run logic in the sidebar. """ all_files = glob.glob("*.md") # Exclude short-named or special files if needed 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 we loaded a file 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...") # --------------------------- # 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): """ Track a 'score' for each glossary item or term, saved in JSON per key. """ 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): 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 that increment 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}") # ------------------------------- # Image & Video # ------------------------------- def display_images_and_wikipedia_summaries(num_columns=4): 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): 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 # -------------------------------- # MERMAID DIAGRAM # -------------------------------- def generate_mermaid_html(mermaid_code: str) -> str: """ Returns HTML that centers the Mermaid diagram, loading from a CDN. """ return f"""