You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
GPT-Brain/web_ui/pages/1_Configs.py

114 lines
4.3 KiB
Python

import streamlit as st
import os
from modules import utilities as util
import tkinter as tk
from tkinter import filedialog
st.set_page_config(
page_title='Configs'
)
body = st.container()
user_dir = '.user/'
prompt_dir = f'{user_dir}prompt/'
brain_memo = f'{user_dir}brain_memo.json'
def save(content, path, page=''):
save_but = st.button('💾Save')
if save_but:
util.write_file(content, path)
st.success(f'✅File saved!')
# write to json file
if page == '💽Brain Memory':
util.update_json(brain_memo, 'delimiter', delimiter)
util.update_json(brain_memo, 'append_mode', append_mode)
util.update_json(brain_memo, 'force_mode', force_delimiter)
def select_directory():
root = tk.Tk()
root.withdraw()
# make sure the dialoge is on top of the main window
root.attributes('-topmost', True)
directory = filedialog.askdirectory(initialdir=os.getcwd(), title='Select Note Directory', master=root)
return directory
with st.sidebar:
st.title('Settings')
menu = st.radio('Menu', [
'📝Prompts',
'💽Brain Memory',
'🔑API Keys'
])
with body:
match menu:
case '📝Prompts':
st.title('📝Prompts')
st.text('Configuration of prompts.')
selected_file = st.selectbox('Prompt File', os.listdir(prompt_dir))
selected_path = prompt_dir + selected_file
mod_text = st.text_area('Prompts',value=util.read_file(selected_path), height=500)
save(mod_text, selected_path)
case '💽Brain Memory':
st.title('💽Brain Memory')
st.text('Modify your brain knowledge base.')
memory_data = util.read_file(f'{user_dir}input.txt')
note_dir = ''
util.create_json_not_exist(brain_memo, {'note_dir': '', 'delimiter': '', 'append_mode': 'False', 'force_mode': 'False'})
util.create_file_not_exist(f'{user_dir}note_dir_info.txt')
util.create_file_not_exist(f'{user_dir}brain_mem_sig.temp')
col1, col2 = st.columns(2)
with col1:
st.button('🔄Refresh')
with col2:
if st.button('📁Select Note Directory'):
note_dir = select_directory()
util.update_json(brain_memo, 'note_dir', note_dir)
note_dir = st.text_input('Note Directory', value=util.read_json_at(brain_memo, 'note_dir'), placeholder='Select Note Directory', key='note_dir')
col1, col2 = st.columns(2)
with col1:
delimiter_memo = util.read_json_at(brain_memo, 'delimiter')
delimiter = st.text_input('Delimiter', delimiter_memo, placeholder='e.g. +++')
with col2:
append_mode = st.checkbox('Append Mode', value=util.read_json_at(brain_memo, 'append_mode'))
force_delimiter = st.checkbox('Force Delimiter', value=util.read_json_at(brain_memo, 'force_mode'))
# if note directory is selected
if note_dir != '':
note_data = util.read_files(note_dir, delimiter, force_delimiter)
if append_mode:
memory_data += note_data
else:
memory_data = note_data
# if st.button('📝Update Brain Memory') or util.read_json_at(brain_memo, 'save') == 'true':
# util.update_json(brain_memo, 'save', 'true')
# util.update_json(brain_memo, 'delimiter', delimiter)
# note_data = util.read_files(note_dir, delimiter, force_delimiter)
# if append_mode:
# memory_data += note_data
# else:
# memory_data = note_data
mod_text = st.text_area('Raw Memory Inputs', value=memory_data, height=500)
save(mod_text, f'{user_dir}input.txt', '💽Brain Memory')
case '🔑API Keys':
st.title('🔑API Keys')
st.text('Configure your OpenAI API keys.')
mod_text = st.text_input('API Keys', value=util.read_file(f'{user_dir}API-KEYS.txt'))
save(mod_text, f'{user_dir}API-KEYS.txt')