|
3 | 3 | from database import create_connection, create_table, create_prompts_table, get_user, insert_user |
4 | 4 | from homepage import gethomepage |
5 | 5 | import os |
| 6 | +import sys |
6 | 7 | from dotenv import load_dotenv, find_dotenv |
7 | 8 |
|
8 | 9 | # Load environment variables FIRST, before any imports |
|
15 | 16 | else: |
16 | 17 | print("WARNING: HF_TOKEN not found in environment variables") |
17 | 18 |
|
| 19 | +# Check command line arguments for mode |
| 20 | +def get_operation_mode(): |
| 21 | + """Determine operation mode from command line arguments or session state override""" |
| 22 | + # Check if user switched to dev mode via button |
| 23 | + if st.session_state.get("switch_to_dev", False): |
| 24 | + return "dev" |
| 25 | + |
| 26 | + # Check command line arguments |
| 27 | + if len(sys.argv) > 1 and sys.argv[1] == "dev": |
| 28 | + return "dev" |
| 29 | + return "production" |
| 30 | + |
18 | 31 | # Define the mode of operation |
19 | | -operation_mode = "dev" # Change to "production" for production mode |
| 32 | +operation_mode = get_operation_mode() |
| 33 | + |
| 34 | +# Function to check if production credentials are configured |
| 35 | +def check_production_credentials(): |
| 36 | + """Check if required credentials are configured in secrets.toml""" |
| 37 | + try: |
| 38 | + # Check if auth0 credentials are configured |
| 39 | + auth0_domain = st.secrets.get("auth.auth0.domain", "") |
| 40 | + auth0_client_id = st.secrets.get("auth.auth0.client_id", "") |
| 41 | + auth0_client_secret = st.secrets.get("auth.auth0.client_secret", "") |
| 42 | + |
| 43 | + # Check if google credentials are configured |
| 44 | + google_client_id = st.secrets.get("auth.google.client_id", "") |
| 45 | + google_client_secret = st.secrets.get("auth.google.client_secret", "") |
| 46 | + |
| 47 | + # Return True if at least one authentication method is configured |
| 48 | + return bool(auth0_domain and auth0_client_id and auth0_client_secret) or \ |
| 49 | + bool(google_client_id and google_client_secret) |
| 50 | + except Exception: |
| 51 | + return False |
| 52 | + |
| 53 | +# Function to display error message |
| 54 | +def show_credentials_error(): |
| 55 | + """Display error message for missing credentials""" |
| 56 | + st.error("⚠️ Production Mode Error: Credentials not configured") |
| 57 | + |
| 58 | + 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.") |
| 59 | + |
| 60 | + st.subheader("Solutions:") |
| 61 | + |
| 62 | + st.write("**Option 1: Use Development Mode**") |
| 63 | + st.code("streamlit run app.py dev") |
| 64 | + |
| 65 | + st.write("**Option 2: Fill up credentials in secrets.toml for Production**") |
| 66 | + st.write("For production mode, you need to fill up the credentials in your `.streamlit/secrets.toml` file:") |
| 67 | + |
| 68 | + st.code(""" |
| 69 | +# For Auth0 authentication |
| 70 | +[auth.auth0] |
| 71 | +domain = "your-domain.auth0.com" |
| 72 | +client_id = "your-client-id" |
| 73 | +client_secret = "your-client-secret" |
| 74 | +server_metadata_url = "https://your-domain.auth0.com/.well-known/openid_configuration" |
| 75 | +client_kwargs = { "prompt" = "login"} |
| 76 | +
|
| 77 | +# OR for Google OAuth |
| 78 | +[auth.google] |
| 79 | +client_id = "your-google-client-id" |
| 80 | +client_secret = "your-google-client-secret" |
| 81 | +server_metadata_url = "https://accounts.google.com/.well-known/openid_configuration" |
| 82 | +
|
| 83 | +# General auth settings |
| 84 | +[auth] |
| 85 | +redirect_uri = "your-redirect-uri" |
| 86 | +cookie_secret = "your-cookie-secret" |
| 87 | +""") |
| 88 | + |
| 89 | + st.warning("⚠️ **For Production Mode:** Either fill up the credentials in `secrets.toml` or use dev mode by running `streamlit run app.py dev`") |
| 90 | + |
| 91 | +# Check if we're in production mode and credentials are missing |
| 92 | +if operation_mode == "production" and not check_production_credentials(): |
| 93 | + show_credentials_error() |
| 94 | + # Don't stop here, let the button work |
| 95 | + if st.button("🔄 Switch to Development Mode", type="primary"): |
| 96 | + # Set a session state to indicate dev mode switch |
| 97 | + st.session_state.switch_to_dev = True |
| 98 | + st.rerun() |
| 99 | + |
| 100 | + # Only stop if user hasn't clicked the switch button |
| 101 | + if not st.session_state.get("switch_to_dev", False): |
| 102 | + st.stop() |
20 | 103 |
|
21 | 104 | # Get markdown homepage |
22 | 105 | st.markdown(body=gethomepage(), unsafe_allow_html=True) |
23 | 106 |
|
| 107 | +# Display current mode indicator |
| 108 | +if operation_mode == "dev": |
| 109 | + st.sidebar.info("Development Mode") |
| 110 | +else: |
| 111 | + st.sidebar.success("Production Mode") |
| 112 | + |
24 | 113 | if (operation_mode == "dev"): |
25 | 114 | # Development mode: no authentication required |
26 | 115 | # Initialize all necessary session state variables for full functionality |
|
0 commit comments