Skip to content

Commit

Permalink
add security promps into Generative AI
Browse files Browse the repository at this point in the history
  • Loading branch information
dayesouza committed Apr 12, 2024
1 parent 37862ab commit 4835033
Show file tree
Hide file tree
Showing 34 changed files with 340 additions and 133 deletions.
3 changes: 2 additions & 1 deletion app/components/app_terminator.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Copyright (c) 2024 Microsoft Corporation. All rights reserved.
import streamlit as st
from util.enums import Mode
from util.session_variables import SessionVariables
import psutil
import keyboard
Expand All @@ -24,7 +25,7 @@ def click():
return click

def terminate_app_btn(self):
if self.sv.mode.value == 'exe':
if self.sv.mode.value == Mode.EXE.value:
exit_app = st.sidebar.button("🔴 Terminate application", disabled=st.session_state.off_btn_disabled, on_click=self._on_click)
if exit_app:
st.text("Shutting down application...")
Expand Down
3 changes: 2 additions & 1 deletion app/components/app_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import streamlit as st
from javascript.scripts import get_auth_user
from util.session_variables import SessionVariables
from util.enums import Mode

class app_user:

Expand Down Expand Up @@ -29,7 +30,7 @@ def _view_error_info(self, return_value):

def login(self):
return
if self.sv.mode.value != 'cloud':
if self.sv.mode.value != Mode.CLOUD.value:
return
return_value = get_auth_user()
username = None
Expand Down
12 changes: 11 additions & 1 deletion app/pages/Attribute_Patterns.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
# Copyright (c) 2024 Microsoft Corporation. All rights reserved.
from util.session_variables import SessionVariables
import workflows.attribute_patterns.workflow
from components.app_loader import load_multipage_app
import streamlit as st
from util.enums import Mode

def main():
st.set_page_config(layout="wide", initial_sidebar_state="collapsed", page_icon="app/myapp.ico", page_title='Intelligence Toolkit | Attribute Patterns')
load_multipage_app()
workflows.attribute_patterns.workflow.create()
sv_home = SessionVariables('home')

try:
workflows.attribute_patterns.workflow.create()
except Exception as e:
if sv_home.mode.value == Mode.DEV.value:
st.exception(e)
else:
st.error(f"An error occurred: {e}")

if __name__ == '__main__':
main()
12 changes: 11 additions & 1 deletion app/pages/Data_Synthesis.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
# Copyright (c) 2024 Microsoft Corporation. All rights reserved.
from util.session_variables import SessionVariables
import workflows.data_synthesis.workflow
from components.app_loader import load_multipage_app
import streamlit as st
from util.enums import Mode


def main():
st.set_page_config(layout="wide", initial_sidebar_state="collapsed", page_icon="app/myapp.ico", page_title='Intelligence Toolkit | Data Synthesis')
load_multipage_app()
workflows.data_synthesis.workflow.create()
sv_home = SessionVariables('home')
try:
workflows.data_synthesis.workflow.create()
except Exception as e:
if sv_home.mode.value == Mode.DEV.value:
st.exception(e)
else:
st.error(f"An error occurred: {e}")

if __name__ == '__main__':
main()
11 changes: 10 additions & 1 deletion app/pages/Group_Narratives.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
# Copyright (c) 2024 Microsoft Corporation. All rights reserved.
from util.session_variables import SessionVariables
import workflows.group_narratives.workflow
import streamlit as st
from components.app_loader import load_multipage_app
from util.enums import Mode

def main():
st.set_page_config(layout="wide", initial_sidebar_state="collapsed", page_icon="app/myapp.ico", page_title='Intelligence Toolkit | Group Narratives')
sv_home = SessionVariables('home')
load_multipage_app()
workflows.group_narratives.workflow.create()
try:
workflows.group_narratives.workflow.create()
except Exception as e:
if sv_home.mode.value == Mode.DEV.value:
st.exception(e)
else:
st.error(f"An error occurred: {e}")

if __name__ == '__main__':
main()
14 changes: 11 additions & 3 deletions app/pages/Question_Answering.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
# Copyright (c) 2024 Microsoft Corporation. All rights reserved.
from util.session_variables import SessionVariables
import workflows.question_answering.workflow
from components.app_loader import load_multipage_app
import streamlit as st

from util.enums import Mode

def main():
st.set_page_config(layout="wide", initial_sidebar_state="collapsed", page_icon="app/myapp.ico", page_title='Intelligence Toolkit | Question Answering')
load_multipage_app()
workflows.question_answering.workflow.create()

sv_home = SessionVariables('home')
try:
workflows.question_answering.workflow.create()
except Exception as e:
if sv_home.mode.value == Mode.DEV.value:
st.exception(e)
else:
st.error(f"An error occurred: {e}")

if __name__ == '__main__':
main()
12 changes: 11 additions & 1 deletion app/pages/Record_Matching.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
# Copyright (c) 2024 Microsoft Corporation. All rights reserved.
from util.session_variables import SessionVariables
import workflows.record_matching.workflow
from components.app_loader import load_multipage_app
import streamlit as st
from util.enums import Mode

def main():
st.set_page_config(layout="wide", initial_sidebar_state="collapsed", page_icon="app/myapp.ico", page_title='Intelligence Toolkit | Record Matching')
load_multipage_app()
workflows.record_matching.workflow.create()
sv_home = SessionVariables('home')

try:
workflows.record_matching.workflow.create()
except Exception as e:
if sv_home.mode.value == Mode.DEV.value:
st.exception(e)
else:
st.error(f"An error occurred: {e}")

if __name__ == '__main__':
main()
12 changes: 11 additions & 1 deletion app/pages/Risk_Networks.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
# Copyright (c) 2024 Microsoft Corporation. All rights reserved.
from util.session_variables import SessionVariables
import workflows.risk_networks.workflow
from components.app_loader import load_multipage_app
import streamlit as st
from util.enums import Mode

def main():
st.set_page_config(layout="wide", initial_sidebar_state="collapsed", page_icon="app/myapp.ico", page_title='Intelligence Toolkit | Risk Networks')
load_multipage_app()
workflows.risk_networks.workflow.create()
sv_home = SessionVariables('home')

try:
workflows.risk_networks.workflow.create()
except Exception as e:
if sv_home.mode.value == Mode.DEV.value:
st.exception(e)
else:
st.error(f"An error occurred: {e}")

if __name__ == '__main__':
main()
3 changes: 2 additions & 1 deletion app/pages/Settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import streamlit as st
import time
from util.session_variables import SessionVariables
from util.enums import Mode

def on_change(handler, key = None, value = None):
def change():
Expand All @@ -20,7 +21,7 @@ def main():
secrets_handler = SecretsHandler()
placeholder = "Enter key here..."
secret = secrets_handler.get_secret(key)
is_mode_cloud = sv.mode.value == 'cloud'
is_mode_cloud = sv.mode.value == Mode.CLOUD.value

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

Expand Down
9 changes: 5 additions & 4 deletions app/util/AI_API.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Copyright (c) 2024 Microsoft Corporation. All rights reserved.
import tiktoken
import json
from util.openai_instance import _OpenAI
Expand Down Expand Up @@ -64,9 +65,9 @@ def generate_text_from_message_list(messages, placeholder=None, prefix='', model
except Exception as e:
print(f'Error generating from message list: {e}')
if '401' and 'invalid_api_key' in str(e):
if placeholder is not None:
placeholder.error(f'Error generating OpenAI response. Your key is invalid.')
raise Exception('Your OpenAI key is invalid.')
elif 'rate_limit_exceeded' in str(e):
raise Exception('Rate limit exceeded when generating OpenAI response. Try again in a few seconds.')
else:
if placeholder is not None:
placeholder.error(f'Error generating OpenAI response.')
raise Exception('Problem in OpenAI response.')
return response
31 changes: 18 additions & 13 deletions app/util/Embedder.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,19 @@ def encode_all(self, texts):
list_all_embeddings = []
try:
embeddings = [x.embedding for x in openai.client().embeddings.create(input = batch_texts, model=self.model).data]
for j, (ix, text) in enumerate(batch):
hsh = hash(text)
list_all_embeddings.append((hsh, embeddings[j]))
final_embeddings[ix] = np.array(embeddings[j])
self.connection.insert_multiple_into_embeddings(list_all_embeddings)
except Exception as e:
if '401' and 'invalid_api_key' in str(e):
raise Exception('Error generating OpenAI response. Your key is invalid.')
raise Exception('Your OpenAI key is invalid.')
elif 'rate_limit_exceeded' in str(e):
raise Exception('Rate limit exceeded when generating OpenAI response. Try again in a few seconds.')
else:
raise Exception('Error generating OpenAI response.')

raise Exception('Problem in OpenAI response.')

for j, (ix, text) in enumerate(batch):
hsh = hash(text)
list_all_embeddings.append((hsh, embeddings[j]))
final_embeddings[ix] = np.array(embeddings[j])
self.connection.insert_multiple_into_embeddings(list_all_embeddings)
pb.empty()
return np.array(final_embeddings)

Expand All @@ -79,14 +81,17 @@ def encode(self, text, auto_save = True):
print('Truncated text to max tokens')
try:
embedding = openai.client().embeddings.create(input = [text], model=self.model).data[0].embedding
if auto_save:
self.connection.insert_into_embeddings(hsh, embedding, self.username)
return embedding
except Exception as e:
if '401' and 'invalid_api_key' in str(e):
raise Exception('Error generating OpenAI response. Your key is invalid.')
raise Exception('Your OpenAI key is invalid.')
elif 'rate_limit_exceeded' in str(e):
raise Exception('Rate limit exceeded when generating OpenAI response. Try again in a few seconds.')
else:
raise Exception('Error generating OpenAI response.')
raise Exception('Problem in OpenAI response.')

if auto_save:
self.connection.insert_into_embeddings(hsh, embedding, self.username)
return embedding

def create_embedder(cache, model=embed_model, encoder=text_encoder, max_tokens=max_embed_tokens):
return Embedder(cache, model, encoder, max_tokens)
14 changes: 8 additions & 6 deletions app/util/download_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ def add_download_pdf(name, text, button_text='Download PDF', is_markdown=True, d

# Generate PDF from HTML string
config_pdf = config_pdfkit()
with tempfile.NamedTemporaryFile(suffix='.pdf', delete=False) as temp_file:
pdfkit.from_string(text, temp_file.name, options=pdfkit_options, configuration=config_pdf)

# Provide download button for the generated PDF
with open(temp_file.name, 'rb') as f:
st.download_button(button_text, f, file_name=name, mime='application/pdf', disabled=disabled)

with st.spinner('Preparing PDF download...'):
with tempfile.NamedTemporaryFile(suffix='.pdf', delete=False) as temp_file:
pdfkit.from_string(text, temp_file.name, options=pdfkit_options, configuration=config_pdf)

# Provide download button for the generated PDF
with open(temp_file.name, 'rb') as f:
st.download_button(button_text, f, file_name=name, mime='application/pdf', disabled=disabled)
8 changes: 8 additions & 0 deletions app/util/enums.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Copyright (c) 2024 Microsoft Corporation. All rights reserved.

from enum import Enum

class Mode(Enum):
DEV = 'dev'
CLOUD = 'cloud'
EXE = 'exe'
3 changes: 2 additions & 1 deletion app/util/openai_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ def client(self):
try:
self._instance = OpenAI(api_key=self._key)
except Exception as e:
print(f'Error creating OpenAI client: {e}')
raise Exception(f'OpenAI client not created: {e}')


return self._instance

Expand Down
7 changes: 2 additions & 5 deletions app/util/session_variables.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from util.enums import Mode
from util.session_variable import SessionVariable
import pandas as pd
import util.session_variable as sv
Expand All @@ -7,9 +8,5 @@ class SessionVariables:

def __init__(self, prefix = ''):
self.narrative_input_df = SessionVariable(pd.DataFrame(), prefix)
self.mode = sv.SessionVariable(os.environ.get("MODE", "dev"))
self.mode = sv.SessionVariable(os.environ.get("MODE", Mode.DEV.value))
self.username = sv.SessionVariable('')




Loading

0 comments on commit 4835033

Please sign in to comment.