import streamlit as st from pathlib import Path import json # 🦄 Magical state initialization if 'file_data' not in st.session_state: st.session_state.file_data = {} def save_to_file(data, filename): """🌈 Rainbow file saver - spreading joy through data persistence!""" try: with open(filename, 'w') as f: json.dump(data, f) return True except Exception as e: st.error(f"Oopsie! 🙈 Couldn't save to {filename}: {str(e)}") return False def load_from_file(filename): """🎀 Pretty file loader - bringing your data back with style!""" try: with open(filename, 'r') as f: return json.load(f) except FileNotFoundError: st.info(f"✨ New adventure! No previous data found in {filename}") return {} except json.JSONDecodeError: st.warning(f"🎭 Hmm, the file {filename} contains invalid JSON. Starting fresh!") return {} def main(): st.title("🎉 Super Fun File Handler!") # 🎨 Pretty file upload section uploaded_file = st.file_uploader("Upload your happy little file!", type=['txt', 'json']) if uploaded_file: # 🎪 Process the uploaded file with circus-level excitement! content = uploaded_file.getvalue().decode() st.session_state.file_data[uploaded_file.name] = content st.success(f"🌟 Woohoo! Successfully loaded {uploaded_file.name}") # 🎠 Display our magical collection of files if st.session_state.file_data: st.subheader("🗂️ Your Wonderful Files:") for filename, content in st.session_state.file_data.items(): with st.expander(f"📁 {filename}"): st.text_area("Content", content, height=150, key=filename) if st.button(f"💾 Save changes to {filename}"): updated_content = st.session_state[filename] if save_to_file(updated_content, filename): st.success(f"🎉 Yay! Saved {filename} successfully!") # 🎪 System prompt section st.subheader("🤖 Super Smart System Prompt Generator") if st.session_state.file_data: selected_files = st.multiselect( "Select files for your prompt", list(st.session_state.file_data.keys()) ) if selected_files: prompt = "Here are the contents of the selected files:\n\n" for file in selected_files: prompt += f"File: {file}\n```\n{st.session_state.file_data[file]}\n```\n\n" st.text_area("Generated Prompt", prompt, height=300) if __name__ == "__main__": main()