Skip to content

Commit

Permalink
Merge pull request #7 from robertmcleod2/add-auth
Browse files Browse the repository at this point in the history
Add auth
  • Loading branch information
robertmcleod2 authored Oct 4, 2024
2 parents 7a615bb + 7de971d commit 652b93b
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 9 deletions.
2 changes: 2 additions & 0 deletions .env_template
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
OPENAI_API_KEY=<OPENAI_API_KEY>
STREAMLIT_PASSWORD=1234
3 changes: 2 additions & 1 deletion .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ jobs:
# App env variables
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
LANGCHAIN_API_KEY: ${{ secrets.LANGCHAIN_API_KEY }}
STREAMLIT_PASSWORD: ${{ secrets.STREAMLIT_PASSWORD }}

steps:
- name: Checkout
uses: actions/checkout@v3
- name: Login to Azure and run deploy.sh
- name: run deploy.sh
shell: bash
run: |
echo "$AZURE_CLIENT_CERT" > ${{ vars.AZURE_CLIENT_CERT_NAME }}
Expand Down
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
# energy-usage-anomaly-detection-assistant
The energy usage anomaly detection assistant is an LLM based agentic assistant that uses tools to detect anomalies in smart meter data and provide human readable alerts to the customer.
The energy usage anomaly detection assistant is an LLM based agentic assistant that uses tools to detect anomalies in smart meter data and provide human readable alerts to the customer.

## Setup
The customer interacts with the assistant through a streamlit application. The application is deployed to Azure, and can be accessed [here](https://anomalydetectionprodapp.azurewebsites.net/). Please reach out to Robert Mcleod for the password to access the application.

## Local Setup

To run the application locally, follow the steps below:

1. Clone the repository

Expand All @@ -23,7 +27,10 @@ conda activate energy-usage-anomaly-detection-assistant
pip install -r requirements_dev.txt
```

4. Run the streamlit application
4. Set up your local environment variables. Create a `.env` file in the root directory of the project, following the template in the .env_template file. The streamlit password can be set to any value. The `OPENAI_API_KEY` can be obtained from [OpenAI](https://platform.openai.com/api-keys), or reach out to Robert Mcleod for the key.


5. Run the streamlit application

```bash
streamlit run src/app.py
Expand Down
9 changes: 5 additions & 4 deletions deployment/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,18 @@ password=$(az acr credential show -n ${AZURE_RESOURCE_GROUP} --query passwords[0

# build and push docker image
docker build -t streamlit .
docker image tag streamlit ${AZURE_RESOURCE_GROUP}.azurecr.io/dashboard
docker push ${AZURE_RESOURCE_GROUP}.azurecr.io/dashboard
docker image tag streamlit ${AZURE_RESOURCE_GROUP}.azurecr.io/dashboard:latest
docker push ${AZURE_RESOURCE_GROUP}.azurecr.io/dashboard:latest

# create web app
az webapp create --name ${AZURE_RESOURCE_GROUP}app --plan ${AZURE_RESOURCE_GROUP} --resource-group ${AZURE_RESOURCE_GROUP} \
--container-image-name ${AZURE_RESOURCE_GROUP}.azurecr.io/dashboard \
--container-image-name ${AZURE_RESOURCE_GROUP}.azurecr.io/dashboard:latest \
--container-registry-user ${username} --container-registry-password ${password} --https-only true

# set env variables
az webapp config appsettings set --resource-group ${AZURE_RESOURCE_GROUP} --name ${AZURE_RESOURCE_GROUP}app \
--settings OPENAI_API_KEY=${OPENAI_API_KEY} LANGCHAIN_TRACING_V2="true" LANGCHAIN_API_KEY=${LANGCHAIN_API_KEY}
--settings OPENAI_API_KEY=${OPENAI_API_KEY} LANGCHAIN_TRACING_V2="true" LANGCHAIN_API_KEY=${LANGCHAIN_API_KEY} \
STREAMLIT_PASSWORD=${STREAMLIT_PASSWORD}

az webapp restart --resource-group ${AZURE_RESOURCE_GROUP} --name ${AZURE_RESOURCE_GROUP}app

6 changes: 5 additions & 1 deletion src/app.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import streamlit as st
from basic_chatbot import setup_chain
from dotenv import load_dotenv
from utils import check_password

load_dotenv()

if not check_password():
st.stop() # Do not continue if check_password is not True.

st.title("Energy Usage Anomaly Detection Assistant")

if "chain" not in st.session_state:
Expand All @@ -16,7 +20,7 @@
with st.chat_message(message["role"]):
st.markdown(message["content"])

if prompt := st.chat_input("What is up?"):
if prompt := st.chat_input("Please describe your energy usage anomaly."):
st.session_state.messages.append({"role": "user", "content": prompt})
with st.chat_message("user"):
st.markdown(prompt)
Expand Down
38 changes: 38 additions & 0 deletions src/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import os
import time

import streamlit as st
import logging


def check_password():
"""Returns `True` if the user had the correct password."""

def password_entered():
"""Checks whether a password entered by the user is correct."""
password = os.getenv("STREAMLIT_PASSWORD")
if not password:
st.error("😕 Password not set")
return False
if st.session_state["password"] == password:
st.session_state["password_correct"] = True
st.session_state["show_success"] = True
del st.session_state["password"] # Don't store the password.
else:
st.session_state["password_correct"] = False

# Return True if the password is validated.
if st.session_state.get("password_correct", False):
if st.session_state.get("show_success", False):
placeholder = st.empty()
placeholder.success("🎉 Password correct")
time.sleep(1.5)
placeholder.empty()
st.session_state["show_success"] = False
return True

# Show input for password.
st.text_input("Password", type="password", on_change=password_entered, key="password")
if "password_correct" in st.session_state:
st.error("😕 Password incorrect")
return False

0 comments on commit 652b93b

Please sign in to comment.