From 73fafc82c5e0f231ac3c8a8dd8e4eb8ae9eef606 Mon Sep 17 00:00:00 2001 From: RAWx18 Date: Thu, 17 Jul 2025 11:22:04 +0530 Subject: [PATCH] fixed bug regarding production and development mode and updated Readme Signed-off-by: RAWx18 --- README.md | 39 ++++++++++++++++++++++ src/mvt/app.py | 91 +++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 129 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 0be1583..bd34f81 100644 --- a/README.md +++ b/README.md @@ -85,10 +85,49 @@ HF_TOKEN= ### Launch +The application supports two modes of operation: + +#### Production Mode (Default) ```bash streamlit run app.py ``` +**Note:** Production mode requires authentication credentials to be configured in `.streamlit/secrets.toml`. If credentials are not configured, the application will show an error message with options to either: +- Switch to development mode +- Configure the required credentials + +#### Development Mode +```bash +streamlit run app.py dev +``` + +**Note:** Development mode provides full functionality without authentication requirements, making it ideal for testing and development. + +### Authentication Configuration (Production Mode) + +For production mode, you need to configure authentication credentials in `.streamlit/secrets.toml`: + +```toml +# For Auth0 authentication +[auth.auth0] +domain = "your-domain.auth0.com" +client_id = "your-client-id" +client_secret = "your-client-secret" +server_metadata_url = "https://your-domain.auth0.com/.well-known/openid_configuration" +client_kwargs = { "prompt" = "login"} + +# OR for Google OAuth +[auth.google] +client_id = "your-google-client-id" +client_secret = "your-google-client-secret" +server_metadata_url = "https://accounts.google.com/.well-known/openid_configuration" + +# General auth settings +[auth] +redirect_uri = "your-redirect-uri" +cookie_secret = "your-cookie-secret" +``` + HF Access Token HF Access Token diff --git a/src/mvt/app.py b/src/mvt/app.py index 29475fe..01a116e 100644 --- a/src/mvt/app.py +++ b/src/mvt/app.py @@ -3,6 +3,7 @@ from database import create_connection, create_table, create_prompts_table, get_user, insert_user from homepage import gethomepage import os +import sys from dotenv import load_dotenv, find_dotenv # Load environment variables FIRST, before any imports @@ -15,12 +16,100 @@ else: print("WARNING: HF_TOKEN not found in environment variables") +# Check command line arguments for mode +def get_operation_mode(): + """Determine operation mode from command line arguments or session state override""" + # Check if user switched to dev mode via button + if st.session_state.get("switch_to_dev", False): + return "dev" + + # Check command line arguments + if len(sys.argv) > 1 and sys.argv[1] == "dev": + return "dev" + return "production" + # Define the mode of operation -operation_mode = "dev" # Change to "production" for production mode +operation_mode = get_operation_mode() + +# Function to check if production credentials are configured +def check_production_credentials(): + """Check if required credentials are configured in secrets.toml""" + try: + # Check if auth0 credentials are configured + auth0_domain = st.secrets.get("auth.auth0.domain", "") + auth0_client_id = st.secrets.get("auth.auth0.client_id", "") + auth0_client_secret = st.secrets.get("auth.auth0.client_secret", "") + + # Check if google credentials are configured + google_client_id = st.secrets.get("auth.google.client_id", "") + google_client_secret = st.secrets.get("auth.google.client_secret", "") + + # Return True if at least one authentication method is configured + return bool(auth0_domain and auth0_client_id and auth0_client_secret) or \ + bool(google_client_id and google_client_secret) + except Exception: + return False + +# Function to display error message +def show_credentials_error(): + """Display error message for missing credentials""" + st.error("⚠️ Production Mode Error: Credentials not configured") + + st.write("You're trying to run the application in **production mode**, but the required authentication credentials are not configured in your `.streamlit/secrets.toml` file.") + + st.subheader("Solutions:") + + st.write("**Option 1: Use Development Mode**") + st.code("streamlit run app.py dev") + + st.write("**Option 2: Fill up credentials in secrets.toml for Production**") + st.write("For production mode, you need to fill up the credentials in your `.streamlit/secrets.toml` file:") + + st.code(""" +# For Auth0 authentication +[auth.auth0] +domain = "your-domain.auth0.com" +client_id = "your-client-id" +client_secret = "your-client-secret" +server_metadata_url = "https://your-domain.auth0.com/.well-known/openid_configuration" +client_kwargs = { "prompt" = "login"} + +# OR for Google OAuth +[auth.google] +client_id = "your-google-client-id" +client_secret = "your-google-client-secret" +server_metadata_url = "https://accounts.google.com/.well-known/openid_configuration" + +# General auth settings +[auth] +redirect_uri = "your-redirect-uri" +cookie_secret = "your-cookie-secret" +""") + + st.warning("⚠️ **For Production Mode:** Either fill up the credentials in `secrets.toml` or use dev mode by running `streamlit run app.py dev`") + +# Check if we're in production mode and credentials are missing +if operation_mode == "production" and not check_production_credentials(): + show_credentials_error() + # Don't stop here, let the button work + if st.button("🔄 Switch to Development Mode", type="primary"): + # Set a session state to indicate dev mode switch + st.session_state.switch_to_dev = True + st.rerun() + + # Only stop if user hasn't clicked the switch button + if not st.session_state.get("switch_to_dev", False): + st.stop() # Get markdown homepage st.markdown(body=gethomepage(), unsafe_allow_html=True) +# Display current mode indicator +if operation_mode == "dev": + st.sidebar.info("Development Mode") +else: + st.sidebar.success("Production Mode") + if (operation_mode == "dev"): # Development mode: no authentication required # Initialize all necessary session state variables for full functionality