HackGPT icon indicating copy to clipboard operation
HackGPT copied to clipboard

Saving / Loading of Conversations

Open ricardobalk opened this issue 1 year ago • 0 comments

As part of enhancing our application's user experience and offering a basic solution for data persistence, we propose implementing the feature to save and load conversations using Streamlit's st.file_uploader and st.download_button functionalities. This will enable users to save ongoing conversations and reload them later for review, sharing, manipulation or continuation.


Feature Specification

  1. Save Conversation to JSON (Must Have): Introduce a "Save Conversation" option in the user interface that allows users to save the current conversation's raw JSON-based request data as a JSON file.

  2. Load Conversation from JSON (Must Have): Implement a "Load Conversation" feature that enables users to upload a previously saved JSON file containing conversation data. The application will parse and load the conversation for review, sharing, or continuation.

  3. Error Handling and Validation (Should Have): To ensure data integrity, the application will validate the loaded JSON files. Proper error handling mechanisms will be implemented to notify users of any issues with the loaded data, such as incorrect format or corrupted data.

  4. Conversations Management (Could Have): Add a convenient way for users to manage their saved conversations. This includes providing access to a list of saved conversations where users can view, delete, or load any conversation from the list.


Sample Code

Below is some sample code that shows how to download and upload JSON files to a Streamlit app.

import streamlit as st
import json

# Function to save conversation to JSON
def save_conversation_to_json(conversation_data):
    # Implementation to save the conversation data as a JSON file
    ...

# Function to load conversation from JSON
def load_conversation_from_json(file):
    # Implementation to load conversation data from a JSON file and validate the format
    conversation_data = None
    try:
        with open(file.name, 'r') as f:
            conversation_data = json.load(f)
    except:
        st.error("Error loading conversation. Please ensure the file is in valid JSON format.")
    return conversation_data

# Streamlit App
def main():
    st.title("Conversation Saving and Loading")
    
    # Display current conversation or load from file
    uploaded_file = st.file_uploader("Load Conversation from JSON file", type="json")
    
    if uploaded_file:
        conversation_data = load_conversation_from_json(uploaded_file)
        if conversation_data:
            st.success("Conversation loaded successfully!")
            st.json(conversation_data)
    
    st.header("Current Conversation")
    st.json(sample_conversation_data)  # Replace with your actual conversation data
    
    # Save conversation to JSON
    if st.button("Save Conversation to JSON"):
        save_conversation_to_json(sample_conversation_data)  # Replace with your actual conversation data
        st.success("Conversation saved successfully!")

    # Download button for the saved JSON file
    if st.button("Download Saved Conversation"):
        with open('saved_conversation.json', 'r') as f:
            file_content = f.read()
        st.download_button(label="Download Conversation", data=file_content, file_name='saved_conversation.json', mime='application/json')

if __name__ == "__main__":
    main()

ricardobalk avatar Jul 27 '23 19:07 ricardobalk