import streamlit as st import anthropic, openai, base64, cv2, glob, json, math, os, pytz, random, re, requests, textract, time, zipfile import plotly.graph_objects as go import streamlit.components.v1 as components from datetime import datetime from audio_recorder_streamlit import audio_recorder from bs4 import BeautifulSoup from collections import defaultdict, deque from dotenv import load_dotenv from gradio_client import Client from huggingface_hub import InferenceClient from io import BytesIO from PIL import Image from PyPDF2 import PdfReader from urllib.parse import quote from xml.etree import ElementTree as ET from openai import OpenAI import extra_streamlit_components as stx from streamlit.runtime.scriptrunner import get_script_run_ctx import asyncio import edge_tts import io import sys import subprocess # 1. Core Configuration & Setup st.set_page_config( page_title="🚲BikeAIπŸ† Claude/GPT Research", page_icon="πŸš²πŸ†", layout="wide", initial_sidebar_state="auto", menu_items={ 'Get Help': 'https://huggingface.co/awacke1', 'Report a bug': 'https://huggingface.co/spaces/awacke1', 'About': "🚲BikeAIπŸ† Claude/GPT Research AI" } ) load_dotenv() # 2. API Setup & Clients openai_api_key = os.getenv('OPENAI_API_KEY', "") anthropic_key = os.getenv('ANTHROPIC_API_KEY_3', "") if 'OPENAI_API_KEY' in st.secrets: openai_api_key = st.secrets['OPENAI_API_KEY'] if 'ANTHROPIC_API_KEY' in st.secrets: anthropic_key = st.secrets["ANTHROPIC_API_KEY"] openai.api_key = openai_api_key claude_client = anthropic.Anthropic(api_key=anthropic_key) openai_client = OpenAI(api_key=openai.api_key, organization=os.getenv('OPENAI_ORG_ID')) HF_KEY = os.getenv('HF_KEY') API_URL = os.getenv('API_URL') # 3. Session State Management if 'transcript_history' not in st.session_state: st.session_state['transcript_history'] = [] if 'chat_history' not in st.session_state: st.session_state['chat_history'] = [] if 'openai_model' not in st.session_state: st.session_state['openai_model'] = "gpt-4o-2024-05-13" if 'messages' not in st.session_state: st.session_state['messages'] = [] if 'last_voice_input' not in st.session_state: st.session_state['last_voice_input'] = "" if 'editing_file' not in st.session_state: st.session_state['editing_file'] = None if 'edit_new_name' not in st.session_state: st.session_state['edit_new_name'] = "" if 'edit_new_content' not in st.session_state: st.session_state['edit_new_content'] = "" if 'viewing_prefix' not in st.session_state: st.session_state['viewing_prefix'] = None if 'should_rerun' not in st.session_state: st.session_state['should_rerun'] = False if 'old_val' not in st.session_state: st.session_state['old_val'] = None # 4. Custom CSS st.markdown(""" """, unsafe_allow_html=True) FILE_EMOJIS = { "md": "πŸ“", "mp3": "🎡", } def get_high_info_terms(text: str, prioritize_start=True) -> list: """🧠 #1 - The Neural Network for Filenames (but way simpler and probably underpaid) Scans text like a caffeinated librarian on a mission, hunting for words that actually mean something. Filters out boring words like 'the' and 'and' (sorry old friends), while preserving the good stuff like 'quantum' and 'neural' (party time! πŸŽ‰). Think of it as a bouncer for your filenames - if a word isn't cool enough, it's not getting in. But key phrases? VIP access, baby! 🎭 Args: text (str): The text to strip mine for linguistic gold prioritize_start (bool): If True, treats the start like the cool kids' table (default behavior because we're not monsters) Returns: list: The VIP list of words that made the cut. Maximum of 8 terms if we're prioritizing the start (because YOLO), 5 otherwise (because sanity). Warning: May occasionally let through a word that sounds smart but is actually just showing off. We're working on its ego. 🎭 """ stop_words = set([ 'the', 'a', 'an', 'and', 'or', 'but', 'in', 'on', 'at', 'to', 'for', 'of', 'with', 'by', 'from', 'up', 'about', 'into', 'over', 'after', 'is', 'are', 'was', 'were', 'be', 'been', 'being', 'have', 'has', 'had', 'do', 'does', 'did', 'will', 'would', 'should', 'could', 'might', 'must', 'shall', 'can', 'may', 'this', 'that', 'these', 'those', 'i', 'you', 'he', 'she', 'it', 'we', 'they', 'what', 'which', 'who', 'when', 'where', 'why', 'how', 'all', 'any', 'both', 'each', 'few', 'more', 'most', 'other', 'some', 'such', 'than', 'too', 'very', 'just', 'there', 'please', 'tell', 'explain', 'show', 'give', 'write', 'provide', 'need', 'want', 'would', 'could', 'lets', 'let', 'try', 'use', 'make', 'help' ]) key_phrases = [ 'artificial intelligence', 'machine learning', 'deep learning', 'neural network', 'personal assistant', 'natural language', 'computer vision', 'data science', 'reinforcement learning', 'knowledge graph', 'semantic search', 'time series', 'large language model', 'transformer model', 'attention mechanism', 'autonomous system', 'edge computing', 'quantum computing', 'blockchain technology', 'cognitive science', 'human computer', 'decision making', 'arxiv search', 'research paper', 'scientific study', 'empirical analysis' ] # First check for key phrases at the start of the text start_phrases = [] lower_text = text.lower() text_start = lower_text[:100] # Look at first 100 chars for starting phrases for phrase in key_phrases: if text_start.startswith(phrase) or text_start.find(f" {phrase}") >= 0: start_phrases.append(phrase) text = text.replace(phrase, '') # Then check for key phrases in the rest of the text preserved_phrases = [] for phrase in key_phrases: if phrase in lower_text and phrase not in start_phrases: preserved_phrases.append(phrase) text = text.replace(phrase, '') # Get the first ~50 words to analyze the start more carefully start_words = text.split()[:50] words_with_pos = [] for pos, word in enumerate(start_words): word = re.sub(r'[^\w\s-]', '', word.lower()) if (len(word) > 3 and word not in stop_words and not word.isdigit() and any(c.isalpha() for c in word)): words_with_pos.append((pos, word)) # Get remaining high-info words from the rest of the text remaining_words = re.findall(r'\b\w+(?:-\w+)*\b', text[100:]) high_info_words = [ word.lower() for word in remaining_words if len(word) > 3 and word.lower() not in stop_words and not word.isdigit() and any(c.isalpha() for c in word) ] # Combine terms prioritizing start content start_terms = [word for _, word in sorted(words_with_pos, key=lambda x: x[0])][:3] all_terms = (start_phrases + start_terms + preserved_phrases + high_info_words) # Remove duplicates while preserving order seen = set() unique_terms = [] for term in all_terms: if term not in seen: seen.add(term) unique_terms.append(term) max_terms = 8 if prioritize_start else 5 return unique_terms[:max_terms] def generate_filename(content, file_type="md"): """🎯 #2 - The File Naming Sommelier (pairs well with frustrated developers) Takes your content and turns it into a filename that's actually readable by humans! A revolutionary concept, we know. Combines timestamps with meaningful words, because '20231218_quantum_research' beats 'asdfg123.md' any day of the week. Think of it as your personal file naming barista - takes your raw content beans and turns them into a smooth, well-crafted filename. No foam art though, sorry! β˜• Args: content (str): Your beautiful text that needs a home(name) file_type (str): The file extension (defaults to "md" because we're markdown hipsters at heart) Returns: str: A filename that won't make you question your life choices when you see it in 6 months. Limited to 120 chars because we're not writing a novel here. Pro Tip: If your filename ends up being just 'file.md', either your content was empty or we've failed spectacularly. Please file a bug report or just laugh it off. πŸŽͺ """ prefix = datetime.now().strftime("%y%m_%H%M") + "_" # Get high-info terms with start prioritization info_terms = get_high_info_terms(content, prioritize_start=True) # Create filename with terms name_text = '_'.join(term.replace(' ', '-') for term in info_terms) if info_terms else 'file' # Ensure reasonable length max_length = 120 # Increased to allow more meaningful content if len(name_text) > max_length: name_text = name_text[:max_length] filename = f"{prefix}{name_text}.{file_type}" return filename # 7. Audio Processing def clean_for_speech(text: str) -> str: text = text.replace("\n", " ") text = text.replace("", " ") text = text.replace("#", "") text = re.sub(r"\(https?:\/\/[^\)]+\)", "", text) text = re.sub(r"\s+", " ", text).strip() return text @st.cache_resource def speech_synthesis_html(result): html_code = f""" """ components.html(html_code, height=0) async def edge_tts_generate_audio(text, voice="en-US-AriaNeural", rate=0, pitch=0): text = clean_for_speech(text) if not text.strip(): return None rate_str = f"{rate:+d}%" pitch_str = f"{pitch:+d}Hz" communicate = edge_tts.Communicate(text, voice, rate=rate_str, pitch=pitch_str) out_fn = generate_filename(text, "mp3") await communicate.save(out_fn) return out_fn def speak_with_edge_tts(text, voice="en-US-AriaNeural", rate=0, pitch=0): return asyncio.run(edge_tts_generate_audio(text, voice, rate, pitch)) def play_and_download_audio(file_path): if file_path and os.path.exists(file_path): st.audio(file_path) dl_link = f'Download {os.path.basename(file_path)}' st.markdown(dl_link, unsafe_allow_html=True) # 8. Media Processing def process_image(image_path, user_prompt): with open(image_path, "rb") as imgf: image_data = imgf.read() b64img = base64.b64encode(image_data).decode("utf-8") resp = openai_client.chat.completions.create( model=st.session_state["openai_model"], messages=[ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": [ {"type": "text", "text": user_prompt}, {"type": "image_url", "image_url": {"url": f"data:image/png;base64,{b64img}"}} ]} ], temperature=0.0, ) return resp.choices[0].message.content def process_audio(audio_path): with open(audio_path, "rb") as f: transcription = openai_client.audio.transcriptions.create(model="whisper-1", file=f) st.session_state.messages.append({"role":"user","content":transcription.text}) return transcription.text def process_video(video_path, seconds_per_frame=1): vid = cv2.VideoCapture(video_path) total = int(vid.get(cv2.CAP_PROP_FRAME_COUNT)) fps = vid.get(cv2.CAP_PROP_FPS) skip = int(fps*seconds_per_frame) frames_b64 = [] for i in range(0, total, skip): vid.set(cv2.CAP_PROP_POS_FRAMES, i) ret, frame = vid.read() if not ret: break _, buf = cv2.imencode(".jpg", frame) frames_b64.append(base64.b64encode(buf).decode("utf-8")) vid.release() return frames_b64 def process_video_with_gpt(video_path, prompt): frames = process_video(video_path) resp = openai_client.chat.completions.create( model=st.session_state["openai_model"], messages=[ {"role":"system","content":"Analyze video frames."}, {"role":"user","content":[ {"type":"text","text":prompt}, *[{"type":"image_url","image_url":{"url":f"data:image/jpeg;base64,{fr}"}} for fr in frames] ]} ] ) return resp.choices[0].message.content # Execution context for code blocks context = {} # 9. Updated create_file function with error handling def create_file(filename, prompt, response, should_save=True): if not should_save: return base_filename, ext = os.path.splitext(filename) combined_content = "" combined_content += "# Prompt πŸ“\n" + prompt + "\n\n" combined_content += "# Response πŸ’¬\n" + response + "\n\n" resources = re.findall(r"```([\s\S]*?)```", response) for resource in resources: if "python" in resource.lower(): cleaned_code = re.sub(r'^\s*python', '', resource, flags=re.IGNORECASE | re.MULTILINE) combined_content += "# Code Results πŸš€\n" original_stdout = sys.stdout sys.stdout = io.StringIO() try: exec(cleaned_code, context) code_output = sys.stdout.getvalue() combined_content += f"```\n{code_output}\n```\n\n" realtimeEvalResponse = "# Code Results πŸš€\n" + "```" + code_output + "```\n\n" st.code(realtimeEvalResponse) except Exception as e: combined_content += f"```python\nError executing Python code: {e}\n```\n\n" sys.stdout = original_stdout else: combined_content += "# Resource πŸ› οΈ\n" + "```" + resource + "```\n\n" if should_save: with open(f"{base_filename}.md", 'w') as file: file.write(combined_content) st.code(combined_content) with open(f"{base_filename}.md", 'rb') as file: encoded_file = base64.b64encode(file.read()).decode() href = f'Download File πŸ“„' st.markdown(href, unsafe_allow_html=True) def generate_code_from_paper(title, summary, instructions): code_prompt = f""" You are a coding assistant. Given the paper titled: "{title}" Summary: "{summary}" The user wants to implement the following steps in Python code. Provide a minimal, self-contained Python code snippet that: 1. Uses only standard libraries if possible. If a library is required, include a code snippet that uses subprocess to install it (like `subprocess.run(['pip','install','somepackage'])`). 2. Implement the requested functionality as simple functions and variables, minimal code. 3. Include error handling: if a file is missing, print an error message. Wrap code in a `try/except` block. 4. Output should be minimal, just the code block (no extra explanations), enclosed in triple backticks. User instructions: "{instructions}" """ try: completion = openai_client.chat.completions.create( model=st.session_state["openai_model"], messages=[ {"role": "system", "content": "You are a helpful coding assistant."}, {"role": "user", "content": code_prompt} ], temperature=0.0 ) generated_code = completion.choices[0].message.content return generated_code except Exception as e: st.error(f"Error generating code: {e}") return "" # 10. AI Model Integration def perform_ai_lookup(q, vocal_summary=True, extended_refs=False, titles_summary=True, full_audio=False): start = time.time() client = Client("awacke1/Arxiv-Paper-Search-And-QA-RAG-Pattern") r = client.predict(q,20,"Semantic Search","mistralai/Mixtral-8x7B-Instruct-v0.1",api_name="/update_with_rag_md") refs = r[0] r2 = client.predict(q,"mistralai/Mixtral-8x7B-Instruct-v0.1",True,api_name="/ask_llm") result = f"### πŸ”Ž {q}\n\n{r2}\n\n{refs}" st.markdown(result) if full_audio: complete_text = f"Complete response for query: {q}. {clean_for_speech(r2)} {clean_for_speech(refs)}" audio_file_full = speak_with_edge_tts(complete_text) st.write("### πŸ“š Complete Audio Response") play_and_download_audio(audio_file_full) if vocal_summary: main_text = clean_for_speech(r2) audio_file_main = speak_with_edge_tts(main_text) st.write("### πŸŽ™οΈ Vocal Summary (Short Answer)") play_and_download_audio(audio_file_main) if extended_refs: summaries_text = "Here are the summaries from the references: " + refs.replace('"','') summaries_text = clean_for_speech(summaries_text) audio_file_refs = speak_with_edge_tts(summaries_text) st.write("### πŸ“œ Extended References & Summaries") play_and_download_audio(audio_file_refs) if titles_summary: titles = [] for line in refs.split('\n'): m = re.search(r"\[([^\]]+)\]", line) if m: titles.append(m.group(1)) if titles: titles_text = "Here are the titles of the papers: " + ", ".join(titles) titles_text = clean_for_speech(titles_text) audio_file_titles = speak_with_edge_tts(titles_text) st.write("### πŸ”– Paper Titles") play_and_download_audio(audio_file_titles) elapsed = time.time()-start st.write(f"**Total Elapsed:** {elapsed:.2f} s") filename = generate_filename(result, "md") create_file(filename, q, result, should_save=True) # Parse out papers papers_raw = refs.strip().split("[Title]") papers = [] for p in papers_raw: p = p.strip() if not p: continue lines = p.split("\n") title_line = lines[0].strip() if lines else "" summary_line = "" link_line = "" pdf_line = "" for line in lines[1:]: line = line.strip() if line.startswith("Summary:"): summary_line = line.replace("Summary:", "").strip() elif line.startswith("Link:"): link_line = line.replace("Link:", "").strip() elif line.startswith("PDF:"): pdf_line = line.replace("PDF:", "").strip() if title_line and summary_line: papers.append({ "title": title_line, "summary": summary_line, "link": link_line, "pdf": pdf_line }) st.write("## Code Interpreter Options for Each Paper") for i, paper in enumerate(papers): st.write(f"**Paper {i+1}:** {paper['title']}") st.write(f"**Summary:** {paper['summary']}") if paper['link']: st.write(f"[Arxiv Link]({paper['link']})") if paper['pdf']: st.write(f"[PDF]({paper['pdf']})") # UI for generating code steps with st.expander("Generate Python Code Steps"): instructions = st.text_area( f"Enter instructions for Python code implementation for this paper:", height=100, key=f"code_task_{i}" ) if st.button(f"Generate Python Code Steps for Paper {i+1}", key=f"gen_code_{i}"): if instructions.strip(): generated_code = generate_code_from_paper(paper['title'], paper['summary'], instructions) if generated_code.strip(): st.write("### Generated Code") st.code(generated_code, language="python") # Attempt to run the generated code if '```' in generated_code: # Extract code blocks code_blocks = re.findall(r"```([\s\S]*?)```", generated_code) for cb in code_blocks: # Try executing cb original_stdout = sys.stdout sys.stdout = io.StringIO() try: exec(cb, {}) exec_output = sys.stdout.getvalue() if exec_output.strip(): st.write("### Code Output") st.write(exec_output) # TTS on code output audio_file = speak_with_edge_tts(exec_output) if audio_file: play_and_download_audio(audio_file) except Exception as e: st.error(f"Error executing code: {e}") finally: sys.stdout = original_stdout else: st.error("No code was generated.") else: st.warning("Please provide instructions before generating code.") return result def process_with_gpt(text): if not text: return st.session_state.messages.append({"role":"user","content":text}) with st.chat_message("user"): st.markdown(text) with st.chat_message("assistant"): try: c = openai_client.chat.completions.create( model=st.session_state["openai_model"], messages=st.session_state.messages, stream=False ) ans = c.choices[0].message.content except Exception as e: ans = f"Error calling GPT-4 API: {e}" st.write("GPT-4o: " + ans) filename = generate_filename(ans.strip() if ans.strip() else text.strip(), "md") create_file(filename, text, ans, should_save=True) st.session_state.messages.append({"role":"assistant","content":ans}) return ans def process_with_claude(text): if not text: return with st.chat_message("user"): st.markdown(text) with st.chat_message("assistant"): try: r = claude_client.messages.create( model="claude-3-sonnet-20240229", max_tokens=1000, messages=[{"role":"user","content":text}] ) ans = r.content[0].text except Exception as e: ans = f"Error calling Claude API: {e}" st.write("Claude-3.5: " + ans) filename = generate_filename(ans.strip() if ans.strip() else text.strip(), "md") create_file(filename, text, ans, should_save=True) st.session_state.chat_history.append({"user":text,"claude":ans}) return ans # 11. File Management def create_zip_of_files(md_files, mp3_files): md_files = [f for f in md_files if os.path.basename(f).lower() != 'readme.md'] all_files = md_files + mp3_files if not all_files: return None all_content = [] for f in all_files: if f.endswith('.md'): with open(f, 'r', encoding='utf-8') as file: all_content.append(file.read()) elif f.endswith('.mp3'): all_content.append(os.path.basename(f)) combined_content = " ".join(all_content) info_terms = get_high_info_terms(combined_content) timestamp = datetime.now().strftime("%y%m_%H%M") name_text = '_'.join(term.replace(' ', '-') for term in info_terms[:3]) zip_name = f"{timestamp}_{name_text}.zip" with zipfile.ZipFile(zip_name,'w') as z: for f in all_files: z.write(f) return zip_name def load_files_for_sidebar(): md_files = glob.glob("*.md") mp3_files = glob.glob("*.mp3") md_files = [f for f in md_files if os.path.basename(f).lower() != 'readme.md'] all_files = md_files + mp3_files groups = defaultdict(list) for f in all_files: fname = os.path.basename(f) prefix = fname[:10] groups[prefix].append(f) for prefix in groups: groups[prefix].sort(key=lambda x: os.path.getmtime(x), reverse=True) sorted_prefixes = sorted(groups.keys(), key=lambda pre: max(os.path.getmtime(x) for x in groups[pre]), reverse=True) return groups, sorted_prefixes def extract_keywords_from_md(files): text = "" for f in files: if f.endswith(".md"): c = open(f,'r',encoding='utf-8').read() text += " " + c return get_high_info_terms(text) def display_file_manager_sidebar(groups, sorted_prefixes): st.sidebar.title("🎡 Audio & Document Manager") all_md = [] all_mp3 = [] for prefix in groups: for f in groups[prefix]: if f.endswith(".md"): all_md.append(f) elif f.endswith(".mp3"): all_mp3.append(f) top_bar = st.sidebar.columns(3) with top_bar[0]: if st.button("πŸ—‘ Del All MD"): for f in all_md: os.remove(f) st.session_state.should_rerun = True with top_bar[1]: if st.button("πŸ—‘ Del All MP3"): for f in all_mp3: os.remove(f) st.session_state.should_rerun = True with top_bar[2]: if st.button("⬇️ Zip All"): z = create_zip_of_files(all_md, all_mp3) if z: with open(z, "rb") as f: b64 = base64.b64encode(f.read()).decode() dl_link = f'πŸ“‚ Download {os.path.basename(z)}' st.sidebar.markdown(dl_link,unsafe_allow_html=True) for prefix in sorted_prefixes: files = groups[prefix] kw = extract_keywords_from_md(files) keywords_str = " ".join(kw) if kw else "No Keywords" with st.sidebar.expander(f"{prefix} Files ({len(files)}) - Keywords: {keywords_str}", expanded=True): c1,c2 = st.columns(2) with c1: if st.button("πŸ‘€View Group", key="view_group_"+prefix): st.session_state.viewing_prefix = prefix with c2: if st.button("πŸ—‘Del Group", key="del_group_"+prefix): for f in files: os.remove(f) st.success(f"Deleted all files in group {prefix} successfully!") st.session_state.should_rerun = True for f in files: fname = os.path.basename(f) ctime = datetime.fromtimestamp(os.path.getmtime(f)).strftime("%Y-%m-%d %H:%M:%S") st.write(f"**{fname}** - {ctime}") # 12. Main Application def main(): st.sidebar.markdown("### 🚲BikeAIπŸ† Multi-Agent Research AI") tab_main = st.radio("Action:",["🎀 Voice Input","πŸ“Έ Media Gallery","πŸ” Search ArXiv","πŸ“ File Editor"],horizontal=True) mycomponent = components.declare_component("mycomponent", path="mycomponent") val = mycomponent(my_input_value="Hello") # Show input in a text box for editing if detected if val: val_stripped = val.replace('\n', ' ') edited_input = st.text_area("Edit your detected input:", value=val_stripped, height=100) run_option = st.selectbox("Select AI Model:", ["Arxiv", "GPT-4o", "Claude-3.5"]) col1, col2 = st.columns(2) with col1: autorun = st.checkbox("AutoRun on input change", value=False) with col2: full_audio = st.checkbox("Generate Complete Audio", value=False, help="Generate audio for the complete response including all papers and summaries") input_changed = (val != st.session_state.old_val) if autorun and input_changed: st.session_state.old_val = val if run_option == "Arxiv": perform_ai_lookup(edited_input, vocal_summary=True, extended_refs=False, titles_summary=True, full_audio=full_audio) else: if run_option == "GPT-4o": process_with_gpt(edited_input) elif run_option == "Claude-3.5": process_with_claude(edited_input) else: if st.button("Process Input"): st.session_state.old_val = val if run_option == "Arxiv": perform_ai_lookup(edited_input, vocal_summary=True, extended_refs=False, titles_summary=True, full_audio=full_audio) else: if run_option == "GPT-4o": process_with_gpt(edited_input) elif run_option == "Claude-3.5": process_with_claude(edited_input) if tab_main == "πŸ” Search ArXiv": st.subheader("πŸ” Search ArXiv") q = st.text_input("Research query:") st.markdown("### πŸŽ›οΈ Audio Generation Options") vocal_summary = st.checkbox("πŸŽ™οΈ Vocal Summary (Short Answer)", value=True) extended_refs = st.checkbox("πŸ“œ Extended References & Summaries (Long)", value=False) titles_summary = st.checkbox("πŸ”– Paper Titles Only", value=True) full_audio = st.checkbox("πŸ“š Generate Complete Audio Response", value=False, help="Generate audio for the complete response including all papers and summaries") if q and st.button("Run ArXiv Query"): perform_ai_lookup(q, vocal_summary=vocal_summary, extended_refs=extended_refs, titles_summary=titles_summary, full_audio=full_audio) elif tab_main == "🎀 Voice Input": st.subheader("🎀 Voice Recognition") user_text = st.text_area("Message:", height=100) user_text = user_text.strip().replace('\n', ' ') if st.button("Send πŸ“¨"): process_with_gpt(user_text) st.subheader("πŸ“œ Chat History") t1,t2=st.tabs(["Claude History","GPT-4o History"]) with t1: for c in st.session_state.chat_history: st.write("**You:**", c["user"]) st.write("**Claude:**", c["claude"]) with t2: for m in st.session_state.messages: with st.chat_message(m["role"]): st.markdown(m["content"]) elif tab_main == "πŸ“Έ Media Gallery": st.header("🎬 Media Gallery - Images and Videos") tabs = st.tabs(["πŸ–ΌοΈ Images", "πŸŽ₯ Video"]) with tabs[0]: imgs = glob.glob("*.png")+glob.glob("*.jpg") if imgs: c = st.slider("Cols",1,5,3) cols = st.columns(c) for i,f in enumerate(imgs): with cols[i%c]: st.image(Image.open(f),use_container_width=True) if st.button(f"πŸ‘€ Analyze {os.path.basename(f)}", key=f"analyze_{f}"): a = process_image(f,"Describe this image.") st.markdown(a) else: st.write("No images found.") with tabs[1]: vids = glob.glob("*.mp4") if vids: for v in vids: with st.expander(f"πŸŽ₯ {os.path.basename(v)}"): st.video(v) if st.button(f"Analyze {os.path.basename(v)}", key=f"analyze_{v}"): a = process_video_with_gpt(v,"Describe video.") st.markdown(a) else: st.write("No videos found.") elif tab_main == "πŸ“ File Editor": if getattr(st.session_state,'current_file',None): st.subheader(f"Editing: {st.session_state.current_file}") new_text = st.text_area("Content:", st.session_state.file_content, height=300) if st.button("Save"): with open(st.session_state.current_file,'w',encoding='utf-8') as f: f.write(new_text) st.success("Updated!") st.session_state.should_rerun = True else: st.write("Select a file from the sidebar to edit.") groups, sorted_prefixes = load_files_for_sidebar() display_file_manager_sidebar(groups, sorted_prefixes) if st.session_state.viewing_prefix and st.session_state.viewing_prefix in groups: st.write("---") st.write(f"**Viewing Group:** {st.session_state.viewing_prefix}") for f in groups[st.session_state.viewing_prefix]: fname = os.path.basename(f) ext = os.path.splitext(fname)[1].lower().strip('.') st.write(f"### {fname}") if ext == "md": content = open(f,'r',encoding='utf-8').read() st.markdown(content) elif ext == "mp3": st.audio(f) else: with open(f, "rb") as file: b64 = base64.b64encode(file.read()).decode() dl_link = f'Download {fname}' st.markdown(dl_link, unsafe_allow_html=True) if st.button("Close Group View"): st.session_state.viewing_prefix = None if st.session_state.should_rerun: st.session_state.should_rerun = False st.rerun() if __name__=="__main__": main()