Skip to content

Commit

Permalink
fix secrets and installer name
Browse files Browse the repository at this point in the history
  • Loading branch information
dayesouza committed Apr 10, 2024
1 parent dc1c654 commit e185e2d
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 31 deletions.
1 change: 0 additions & 1 deletion .streamlit/secrets.toml

This file was deleted.

2 changes: 1 addition & 1 deletion .vsts-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ stages:
script: '.\installer_script.ps1'
- task: PublishPipelineArtifact@1
inputs:
targetPath: '$(System.DefaultWorkingDirectory)/build/nsis/Intelligence Toolkit.exe'
targetPath: '$(System.DefaultWorkingDirectory)/build/nsis/Intelligence_toolkit_installer.exe'
artifact: 'executable' # Name of the artifact


3 changes: 1 addition & 2 deletions app/components/app_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,5 @@ def load_multipage_app():
app_openai = ao.app_openai()
app_openai.api_info()

#load css
# add_styles()
add_styles()

1 change: 0 additions & 1 deletion app/components/app_terminator.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ def click():
return click

def terminate_app_btn(self):
#TODO: Use constants with mode
if self.sv.mode.value == 'exe':
exit_app = st.sidebar.button("🔴 Terminate application", disabled=st.session_state.off_btn_disabled, on_click=self._on_click)
if exit_app:
Expand Down
17 changes: 8 additions & 9 deletions app/pages/Settings.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
from components.app_loader import load_multipage_app
from util.openai_instance import get_key_env
from util.SecretsHandler import SecretsHandler
import streamlit as st
Expand All @@ -8,35 +9,33 @@
key = 'openaikey'
def on_change(handler, key = None, value = None):
def change():
handler.write_secret('api_key', st.session_state[key] if key else value)
handler.write_secret(key, value)
return change

def main():
st.set_page_config(layout="wide", initial_sidebar_state="collapsed", page_icon="app/myapp.ico", page_title='Intelligence Toolkit | Settings')
load_multipage_app()
st.header("Settings")
sv = SessionVariables('home')

if key not in st.session_state:
st.session_state[key] = ''

secrets_handler = SecretsHandler()
placeholder = "Enter key here..."
secret = secrets_handler.get_secret("api_key")

secret = secrets_handler.get_secret(key)
is_mode_cloud = sv.mode.value == 'cloud'

secret_input = st.text_input('Enter your OpenAI key', key=key, type="password", disabled=is_mode_cloud, placeholder=placeholder, value=secret, on_change=on_change(secrets_handler, key))
secret_input = st.text_input('Enter your OpenAI key', type="password", disabled=is_mode_cloud, placeholder=placeholder, value=secret)

if secret and len(secret):
if secret and len(secret) > 0:
st.info("Your key is saved securely.")
clear_btn = st.button("Clear local key")

if clear_btn:
on_change(secrets_handler, value='')()
on_change(secrets_handler, key, value='')()
time.sleep(0.3)
st.rerun()

if secret_input and secret_input != secret:
secrets_handler.write_secret(key, secret_input)
st.rerun()
elif get_key_env() == '':
st.warning("No OpenAI key found in the environment. Please insert one above.")
Expand Down
30 changes: 14 additions & 16 deletions app/util/SecretsHandler.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
import os
import streamlit as st

class SecretsHandler:
_instance = None
_directory = "/.streamlit"
_directory = ".streamlit"
_file_name = "app_secrets.toml"
_file_path = os.path.join(_directory, _file_name)

def __init__(self):
if not os.path.exists(self._directory):
os.makedirs(self._directory)
with(open(os.path.join(self._directory, "secrets.toml"), "w")) as f:
with(open(self._file_path, "w+")) as f:
f.write("")

def __new__(cls):
if cls._instance is None:
cls._instance = super().__new__(cls)

return cls._instance

def write_secret(self, key, value):
with(open(os.path.join(self._directory, "secrets.toml"), "w")) as f:
f.write(f"{key} = '{value}'")
with(open(self._file_path, "w")) as f:
f.write(f"{key}:{value};")

def get_secret(self, key) -> str:
if st.secrets and key in st.secrets:
return st.secrets[key]
return ''

with(open(self._file_path, "r")) as f:
secrets = f.read()
secret_key = secrets.split(";")
for key_value in secret_key:
secret_key = key_value.split(":")[0]
if key == secret_key:
return key_value.split(":")[1]
return ''
2 changes: 1 addition & 1 deletion installer.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ icon=app/myapp.ico

[Build]
nsi_template=installer.nsi
installer_name=Intelligence Toolkit.exe
installer_name=Intelligence_toolkit_installer.exe

[Python]
version=3.10.11
Expand Down

0 comments on commit e185e2d

Please sign in to comment.