Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add --noauth option #28

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,28 @@ For the LLaMA2 license agreement, please check the Meta Platforms, Inc official
- [Optional] Create a virtual python environment with the command `python -m venv .venv` and activate it with `source .venv/bin/activate`
- Install dependencies with `pip install -r requirements.txt`
- Create an account on [Replicate](https://replicate.com/)
- Make your own `.env` file with the command `cp .env_template .env`
- Edit the `.env` file and add your:
- [Replicate API token](https://replicate.com/account) as `REPLICATE_API_TOKEN`

*If you DON'T want to use authentication...*
- Just run the app with the command `streamlit run llama2_chatbot.py -- --noauth`
- (Note the two sets of dashes. This is required for Streamlit to interpret the args correctly, see docs [here](https://docs.streamlit.io/library/advanced-features/cli).)

*If you DO want to use authentication...*
- Create an account on [Auth0 (free)](https://auth0.com/) and configure your application
- Create a Single Page Application
- Navigate to the Settings tab for that application
- If you are running the app locally: set Allowed Web Origins to `http://localhost:8501` and set Allowed Callback URLs to `http://localhost:8501/component/auth0_component.login_button/index.html`
- To run on a remote server: set Allowed Web Origins to `https://<your_domain>` and set Allowed Callback URLs to `http://<your_domain>/component/auth0_component.login_button/index.html`
- Copy Client ID and Domain to use in the next step
- Make your own `.env` file with the command `cp .env_template .env`. Then edit the `.env` file and add your:
- [Replicate API token](https://replicate.com/account) as `REPLICATE_API_TOKEN`
- Edit the `.env` file again and add your:
- [Auth0 Client ID](https://auth0.com/docs/get-started/applications/application-settings) as `AUTH0_CLIENTID`
- [Auth0 Domain](https://auth0.com/docs/get-started/applications/application-settings) as `AUTH0_DOMAIN`
- For your convenience, we include common model endpoints already in the `.env_template` file
- Run the app with `streamlit run llama2_chatbot.py`
- Dockerfile included to [deploy this app](#deploying-on-flyio) in Fly.io

*To deploy to production...*
- `Dockerfile` is included to package the app and [deploy in Fly.io](#deploying-on-flyio) or other hosting providers that support Docker images

(Note: if you are using a Mac, you may need to use the command `python3` instead of `python` and `pip3` instead of `pip`)

Expand Down
31 changes: 23 additions & 8 deletions llama2_chatbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,25 @@
"""
#External libraries:
import streamlit as st
import replicate
from dotenv import load_dotenv
load_dotenv()
import os
import argparse
from utils import debounce_replicate_run
from auth0_component import login_button

# parse comamnd line args
parser = argparse.ArgumentParser()
parser.add_argument('--noauth', action='store_true', help='turns off auth')
try:
args = parser.parse_args()
use_auth = not args.noauth
except SystemExit as e:
# This exception will be raised if --help or invalid command line arguments
# are used. Currently streamlit prevents the program from exiting normally
# so we have to do a hard exit.
os._exit(e.code)

###Global variables:###
REPLICATE_API_TOKEN = os.environ.get('REPLICATE_API_TOKEN', default='')
#Your your (Replicate) models' endpoints:
Expand All @@ -32,8 +44,11 @@
AUTH0_CLIENTID = os.environ.get('AUTH0_CLIENTID', default='')
AUTH0_DOMAIN = os.environ.get('AUTH0_DOMAIN', default='')

if not (REPLICATE_API_TOKEN and REPLICATE_MODEL_ENDPOINT13B and REPLICATE_MODEL_ENDPOINT7B and
AUTH0_CLIENTID and AUTH0_DOMAIN):
if not (
REPLICATE_API_TOKEN and
REPLICATE_MODEL_ENDPOINT7B and REPLICATE_MODEL_ENDPOINT13B and REPLICATE_MODEL_ENDPOINT70B and
((not use_auth) or (AUTH0_CLIENTID and AUTH0_DOMAIN))
):
st.warning("Add a `.env` file to your app directory with the keys specified in `.env_template` to continue.")
st.stop()

Expand Down Expand Up @@ -115,9 +130,10 @@ def clear_history():
# add logout button
def logout():
del st.session_state['user_info']
logout_button = btn_col2.button("Logout",
use_container_width=True,
on_click=logout)
if use_auth:
logout_button = btn_col2.button("Logout",
use_container_width=True,
on_click=logout)

# add links to relevant resources for users to select
st.sidebar.write(" ")
Expand Down Expand Up @@ -175,8 +191,7 @@ def logout():
st.session_state.chat_dialogue.append({"role": "assistant", "content": full_response})


if 'user_info' in st.session_state:
# if user_info:
if 'user_info' in st.session_state or (not use_auth):
render_app()
else:
st.write("Please login to use the app. This is just to prevent abuse, we're not charging for usage.")
Expand Down