Spaces:
Sleeping
Sleeping
File size: 2,707 Bytes
9d6ac78 39fa99f b99f72a 39fa99f b99f72a 39fa99f b99f72a 39fa99f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
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() |