Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,7 +6,6 @@
|
|
| 6 |
############################################################################################################
|
| 7 |
|
| 8 |
import streamlit as st # Web app framework
|
| 9 |
-
import hmac # Secure password validation
|
| 10 |
import pandas as pd # Data handling
|
| 11 |
import os # File operations
|
| 12 |
import logging # Logging functionality
|
|
@@ -162,6 +161,21 @@ st.markdown("""
|
|
| 162 |
</style>
|
| 163 |
""", unsafe_allow_html=True)
|
| 164 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
############################################################################################################
|
| 166 |
# Initialize all session state variables - Persistent data between app reruns
|
| 167 |
############################################################################################################
|
|
@@ -184,47 +198,6 @@ if 'seen_terms' not in st.session_state:
|
|
| 184 |
if 'openai_model' not in st.session_state:
|
| 185 |
st.session_state.openai_model = config.ai_model # AI model being used
|
| 186 |
|
| 187 |
-
############################################################################################################
|
| 188 |
-
# Password protection - User authentication functionality
|
| 189 |
-
############################################################################################################
|
| 190 |
-
|
| 191 |
-
def check_credentials():
|
| 192 |
-
"""Verifies username and password against stored credentials.
|
| 193 |
-
|
| 194 |
-
Returns:
|
| 195 |
-
bool: True if credentials are valid, False otherwise.
|
| 196 |
-
"""
|
| 197 |
-
def credentials_entered():
|
| 198 |
-
"""Callback for when credentials are entered by the user."""
|
| 199 |
-
if (
|
| 200 |
-
hmac.compare_digest(st.session_state["username"], st.secrets["username"]) and
|
| 201 |
-
hmac.compare_digest(st.session_state["password"], st.secrets["password"])
|
| 202 |
-
):
|
| 203 |
-
st.session_state["credentials_correct"] = True
|
| 204 |
-
del st.session_state["username"] # Remove for security
|
| 205 |
-
del st.session_state["password"] # Remove for security
|
| 206 |
-
else:
|
| 207 |
-
st.session_state["credentials_correct"] = False
|
| 208 |
-
|
| 209 |
-
# Return True if already validated in this session
|
| 210 |
-
if st.session_state.get("credentials_correct", False):
|
| 211 |
-
return True
|
| 212 |
-
|
| 213 |
-
# Display login form
|
| 214 |
-
st.text_input("Username", on_change=credentials_entered, key="username")
|
| 215 |
-
st.text_input("Password", type="password", on_change=credentials_entered, key="password")
|
| 216 |
-
|
| 217 |
-
# Log invalid login attempts for security monitoring
|
| 218 |
-
if "credentials_correct" in st.session_state:
|
| 219 |
-
logging.warning("Invalid login attempt")
|
| 220 |
-
return False
|
| 221 |
-
|
| 222 |
-
# Validate user credentials before proceeding
|
| 223 |
-
if not check_credentials():
|
| 224 |
-
st.stop() # Stop execution if authentication fails
|
| 225 |
-
else:
|
| 226 |
-
logging.info("Successful login") # Log successful logins
|
| 227 |
-
|
| 228 |
############################################################################################################
|
| 229 |
# Loading Terms - Data loading and preparation functions
|
| 230 |
############################################################################################################
|
|
@@ -364,9 +337,14 @@ with right_col:
|
|
| 364 |
# Generate and display AI response when triggered
|
| 365 |
if st.session_state.get("trigger_llm", False):
|
| 366 |
try:
|
| 367 |
-
# Initialize OpenAI client
|
| 368 |
-
|
| 369 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 370 |
# Generate system message with term context
|
| 371 |
system_message = config.term_prompt(
|
| 372 |
selected_term=st.session_state.selected_term,
|
|
|
|
| 6 |
############################################################################################################
|
| 7 |
|
| 8 |
import streamlit as st # Web app framework
|
|
|
|
| 9 |
import pandas as pd # Data handling
|
| 10 |
import os # File operations
|
| 11 |
import logging # Logging functionality
|
|
|
|
| 161 |
</style>
|
| 162 |
""", unsafe_allow_html=True)
|
| 163 |
|
| 164 |
+
with st.sidebar:
|
| 165 |
+
st.header("Configuration")
|
| 166 |
+
|
| 167 |
+
# API Key Input Field
|
| 168 |
+
api_key = st.text_input(
|
| 169 |
+
"OpenAI API Key",
|
| 170 |
+
type="password",
|
| 171 |
+
key="api_key",
|
| 172 |
+
help="This is the API key for your OpenAI account. You can find it [here](https://platform.openai.com/api-keys)."
|
| 173 |
+
)
|
| 174 |
+
if api_key:
|
| 175 |
+
st.session_state["OPENAI_API_KEY"] = api_key
|
| 176 |
+
else:
|
| 177 |
+
st.warning("Please provide your OpenAI API key to enable chat functionality.")
|
| 178 |
+
|
| 179 |
############################################################################################################
|
| 180 |
# Initialize all session state variables - Persistent data between app reruns
|
| 181 |
############################################################################################################
|
|
|
|
| 198 |
if 'openai_model' not in st.session_state:
|
| 199 |
st.session_state.openai_model = config.ai_model # AI model being used
|
| 200 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 201 |
############################################################################################################
|
| 202 |
# Loading Terms - Data loading and preparation functions
|
| 203 |
############################################################################################################
|
|
|
|
| 337 |
# Generate and display AI response when triggered
|
| 338 |
if st.session_state.get("trigger_llm", False):
|
| 339 |
try:
|
| 340 |
+
# Initialize OpenAI client from session state
|
| 341 |
+
api_key = st.session_state.get("OPENAI_API_KEY", "")
|
| 342 |
+
if api_key:
|
| 343 |
+
client = OpenAI(api_key=api_key)
|
| 344 |
+
else:
|
| 345 |
+
client = None
|
| 346 |
+
st.error("🔒 Missing API key—please enter it in the sidebar above.")
|
| 347 |
+
|
| 348 |
# Generate system message with term context
|
| 349 |
system_message = config.term_prompt(
|
| 350 |
selected_term=st.session_state.selected_term,
|