awacke1's picture
Update app.py
39fa99f verified
raw
history blame
2.71 kB
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()