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()