Skip to content

Commit ddfdffc

Browse files
authored
fixed bug regarding production and development mode and updated Readme (#144)
Signed-off-by: RAWx18 <[email protected]>
1 parent 0c1fa80 commit ddfdffc

File tree

2 files changed

+129
-1
lines changed

2 files changed

+129
-1
lines changed

README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,49 @@ HF_TOKEN=<required - get from https://huggingface.co/settings/tokens>
8585

8686
### Launch
8787

88+
The application supports two modes of operation:
89+
90+
#### Production Mode (Default)
8891
```bash
8992
streamlit run app.py
9093
```
9194

95+
**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:
96+
- Switch to development mode
97+
- Configure the required credentials
98+
99+
#### Development Mode
100+
```bash
101+
streamlit run app.py dev
102+
```
103+
104+
**Note:** Development mode provides full functionality without authentication requirements, making it ideal for testing and development.
105+
106+
### Authentication Configuration (Production Mode)
107+
108+
For production mode, you need to configure authentication credentials in `.streamlit/secrets.toml`:
109+
110+
```toml
111+
# For Auth0 authentication
112+
[auth.auth0]
113+
domain = "your-domain.auth0.com"
114+
client_id = "your-client-id"
115+
client_secret = "your-client-secret"
116+
server_metadata_url = "https://your-domain.auth0.com/.well-known/openid_configuration"
117+
client_kwargs = { "prompt" = "login"}
118+
119+
# OR for Google OAuth
120+
[auth.google]
121+
client_id = "your-google-client-id"
122+
client_secret = "your-google-client-secret"
123+
server_metadata_url = "https://accounts.google.com/.well-known/openid_configuration"
124+
125+
# General auth settings
126+
[auth]
127+
redirect_uri = "your-redirect-uri"
128+
cookie_secret = "your-cookie-secret"
129+
```
130+
92131
<img src="./images/add_knowledge.png" alt="HF Access Token" width="500" height="300"/>
93132
<img src="./images/update_knowledge.png" alt="HF Access Token" width="500" height="300"/>
94133

src/mvt/app.py

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from database import create_connection, create_table, create_prompts_table, get_user, insert_user
44
from homepage import gethomepage
55
import os
6+
import sys
67
from dotenv import load_dotenv, find_dotenv
78

89
# Load environment variables FIRST, before any imports
@@ -15,12 +16,100 @@
1516
else:
1617
print("WARNING: HF_TOKEN not found in environment variables")
1718

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+
1831
# 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()
20103

21104
# Get markdown homepage
22105
st.markdown(body=gethomepage(), unsafe_allow_html=True)
23106

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+
24113
if (operation_mode == "dev"):
25114
# Development mode: no authentication required
26115
# Initialize all necessary session state variables for full functionality

0 commit comments

Comments
 (0)