diff --git a/README.md b/README.md index 8717a28..db1ae02 100644 --- a/README.md +++ b/README.md @@ -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://` and set Allowed Callback URLs to `http:///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`) diff --git a/llama2_chatbot.py b/llama2_chatbot.py index d2fc046..53cad67 100644 --- a/llama2_chatbot.py +++ b/llama2_chatbot.py @@ -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: @@ -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() @@ -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(" ") @@ -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.")