Spaces:
Sleeping
Sleeping
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() |