Spaces:
Sleeping
Sleeping
Rupali Ravindra Shetye commited on
Commit Β·
72f30cd
1
Parent(s): 240ad9a
SECURITY: Hide API key and disable sensitive actions in live demo
Browse files- app/ui/pages/settings.py +77 -56
app/ui/pages/settings.py
CHANGED
|
@@ -8,6 +8,12 @@ def render_settings():
|
|
| 8 |
"""Render the settings page."""
|
| 9 |
st.title("βοΈ Settings")
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
st.markdown("Configure DocIntel settings and preferences.")
|
| 12 |
|
| 13 |
# General Settings
|
|
@@ -22,7 +28,8 @@ def render_settings():
|
|
| 22 |
max_value=4000,
|
| 23 |
value=settings.CHUNK_SIZE,
|
| 24 |
step=100,
|
| 25 |
-
help="Size of document chunks for processing"
|
|
|
|
| 26 |
)
|
| 27 |
|
| 28 |
with col2:
|
|
@@ -32,7 +39,8 @@ def render_settings():
|
|
| 32 |
max_value=500,
|
| 33 |
value=settings.CHUNK_OVERLAP,
|
| 34 |
step=50,
|
| 35 |
-
help="Overlap between chunks for context preservation"
|
|
|
|
| 36 |
)
|
| 37 |
|
| 38 |
top_k = st.number_input(
|
|
@@ -41,7 +49,8 @@ def render_settings():
|
|
| 41 |
max_value=20,
|
| 42 |
value=settings.TOP_K,
|
| 43 |
step=1,
|
| 44 |
-
help="Number of documents to retrieve per query"
|
|
|
|
| 45 |
)
|
| 46 |
|
| 47 |
# Model Settings
|
|
@@ -54,7 +63,8 @@ def render_settings():
|
|
| 54 |
"Embedding Model",
|
| 55 |
["all-MiniLM-L6-v2", "all-mpnet-base-v2", "all-distilroberta-v1"],
|
| 56 |
index=0,
|
| 57 |
-
help="Model for generating document embeddings (requires restart)"
|
|
|
|
| 58 |
)
|
| 59 |
|
| 60 |
with col2:
|
|
@@ -62,68 +72,78 @@ def render_settings():
|
|
| 62 |
"LLM Model",
|
| 63 |
["mixtral-8x7b-32768", "llama2-70b-4096", "gemma-7b-it"],
|
| 64 |
index=0,
|
| 65 |
-
help="Model for generating responses"
|
|
|
|
| 66 |
)
|
| 67 |
|
| 68 |
-
# API Settings
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
groq_api_key = st.text_input(
|
| 72 |
-
"Groq API Key",
|
| 73 |
-
value=settings.GROQ_API_KEY,
|
| 74 |
-
type="password",
|
| 75 |
-
help="Get your API key from console.groq.com"
|
| 76 |
-
)
|
| 77 |
-
|
| 78 |
-
if st.button("πΎ Save Settings", type="primary"):
|
| 79 |
-
# Save settings to .env
|
| 80 |
-
env_path = ".env"
|
| 81 |
-
env_content = []
|
| 82 |
-
|
| 83 |
-
# Read existing .env
|
| 84 |
-
if os.path.exists(env_path):
|
| 85 |
-
with open(env_path, "r") as f:
|
| 86 |
-
env_content = f.readlines()
|
| 87 |
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
"
|
| 92 |
-
"
|
| 93 |
-
|
| 94 |
-
"GROQ_MODEL": llm_model,
|
| 95 |
-
"GROQ_API_KEY": groq_api_key
|
| 96 |
-
}
|
| 97 |
-
|
| 98 |
-
updated_lines = []
|
| 99 |
-
for line in env_content:
|
| 100 |
-
key = line.split("=")[0].strip() if "=" in line else None
|
| 101 |
-
if key in updates:
|
| 102 |
-
updated_lines.append(f"{key}={updates[key]}\n")
|
| 103 |
-
del updates[key]
|
| 104 |
-
else:
|
| 105 |
-
updated_lines.append(line)
|
| 106 |
-
|
| 107 |
-
# Add new values
|
| 108 |
-
for key, value in updates.items():
|
| 109 |
-
updated_lines.append(f"{key}={value}\n")
|
| 110 |
-
|
| 111 |
-
# Write back
|
| 112 |
-
with open(env_path, "w") as f:
|
| 113 |
-
f.writelines(updated_lines)
|
| 114 |
|
| 115 |
-
st.
|
| 116 |
-
|
| 117 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
st.divider()
|
| 119 |
st.subheader("ποΈ Database Management")
|
| 120 |
|
| 121 |
col1, col2 = st.columns(2)
|
| 122 |
|
| 123 |
with col1:
|
| 124 |
-
if
|
| 125 |
-
|
| 126 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
|
| 128 |
with col2:
|
| 129 |
if st.button("π Database Stats", use_container_width=True):
|
|
@@ -155,6 +175,7 @@ def render_settings():
|
|
| 155 |
- **Platform**: {os.sys.platform}
|
| 156 |
- **Working Directory**: {os.getcwd()}
|
| 157 |
- **Environment**: {'π³ Docker' if os.path.exists('/.dockerenv') else 'π₯οΈ Local'}
|
|
|
|
| 158 |
""")
|
| 159 |
|
| 160 |
# About
|
|
|
|
| 8 |
"""Render the settings page."""
|
| 9 |
st.title("βοΈ Settings")
|
| 10 |
|
| 11 |
+
# Check if running on Hugging Face (live demo)
|
| 12 |
+
is_demo = os.path.exists('/.dockerenv') or 'HF_TOKEN' in os.environ
|
| 13 |
+
|
| 14 |
+
if is_demo:
|
| 15 |
+
st.info("π **Live Demo Mode** - Settings are read-only for security. API keys and sensitive actions are hidden.")
|
| 16 |
+
|
| 17 |
st.markdown("Configure DocIntel settings and preferences.")
|
| 18 |
|
| 19 |
# General Settings
|
|
|
|
| 28 |
max_value=4000,
|
| 29 |
value=settings.CHUNK_SIZE,
|
| 30 |
step=100,
|
| 31 |
+
help="Size of document chunks for processing",
|
| 32 |
+
disabled=is_demo # Disable in demo
|
| 33 |
)
|
| 34 |
|
| 35 |
with col2:
|
|
|
|
| 39 |
max_value=500,
|
| 40 |
value=settings.CHUNK_OVERLAP,
|
| 41 |
step=50,
|
| 42 |
+
help="Overlap between chunks for context preservation",
|
| 43 |
+
disabled=is_demo # Disable in demo
|
| 44 |
)
|
| 45 |
|
| 46 |
top_k = st.number_input(
|
|
|
|
| 49 |
max_value=20,
|
| 50 |
value=settings.TOP_K,
|
| 51 |
step=1,
|
| 52 |
+
help="Number of documents to retrieve per query",
|
| 53 |
+
disabled=is_demo # Disable in demo
|
| 54 |
)
|
| 55 |
|
| 56 |
# Model Settings
|
|
|
|
| 63 |
"Embedding Model",
|
| 64 |
["all-MiniLM-L6-v2", "all-mpnet-base-v2", "all-distilroberta-v1"],
|
| 65 |
index=0,
|
| 66 |
+
help="Model for generating document embeddings (requires restart)",
|
| 67 |
+
disabled=is_demo # Disable in demo
|
| 68 |
)
|
| 69 |
|
| 70 |
with col2:
|
|
|
|
| 72 |
"LLM Model",
|
| 73 |
["mixtral-8x7b-32768", "llama2-70b-4096", "gemma-7b-it"],
|
| 74 |
index=0,
|
| 75 |
+
help="Model for generating responses",
|
| 76 |
+
disabled=is_demo # Disable in demo
|
| 77 |
)
|
| 78 |
|
| 79 |
+
# API Settings - Hidden completely in demo mode
|
| 80 |
+
if not is_demo:
|
| 81 |
+
st.subheader("π API Settings")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
+
groq_api_key = st.text_input(
|
| 84 |
+
"Groq API Key",
|
| 85 |
+
value=settings.GROQ_API_KEY,
|
| 86 |
+
type="password",
|
| 87 |
+
help="Get your API key from console.groq.com"
|
| 88 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
+
if st.button("πΎ Save Settings", type="primary"):
|
| 91 |
+
# Save settings to .env
|
| 92 |
+
env_path = ".env"
|
| 93 |
+
env_content = []
|
| 94 |
+
|
| 95 |
+
# Read existing .env
|
| 96 |
+
if os.path.exists(env_path):
|
| 97 |
+
with open(env_path, "r") as f:
|
| 98 |
+
env_content = f.readlines()
|
| 99 |
+
|
| 100 |
+
# Update values
|
| 101 |
+
updates = {
|
| 102 |
+
"CHUNK_SIZE": str(chunk_size),
|
| 103 |
+
"CHUNK_OVERLAP": str(chunk_overlap),
|
| 104 |
+
"TOP_K": str(top_k),
|
| 105 |
+
"EMBEDDING_MODEL": embedding_model,
|
| 106 |
+
"GROQ_MODEL": llm_model,
|
| 107 |
+
"GROQ_API_KEY": groq_api_key
|
| 108 |
+
}
|
| 109 |
+
|
| 110 |
+
updated_lines = []
|
| 111 |
+
for line in env_content:
|
| 112 |
+
key = line.split("=")[0].strip() if "=" in line else None
|
| 113 |
+
if key in updates:
|
| 114 |
+
updated_lines.append(f"{key}={updates[key]}\n")
|
| 115 |
+
del updates[key]
|
| 116 |
+
else:
|
| 117 |
+
updated_lines.append(line)
|
| 118 |
+
|
| 119 |
+
# Add new values
|
| 120 |
+
for key, value in updates.items():
|
| 121 |
+
updated_lines.append(f"{key}={value}\n")
|
| 122 |
+
|
| 123 |
+
# Write back
|
| 124 |
+
with open(env_path, "w") as f:
|
| 125 |
+
f.writelines(updated_lines)
|
| 126 |
+
|
| 127 |
+
st.success("β
Settings saved! Restart DocIntel for changes to take effect.")
|
| 128 |
+
else:
|
| 129 |
+
# Show masked API status in demo
|
| 130 |
+
st.subheader("π API Status")
|
| 131 |
+
st.success("β
Groq API: **Connected** (Key hidden for security)")
|
| 132 |
+
|
| 133 |
+
# Database Management - Disabled in demo
|
| 134 |
st.divider()
|
| 135 |
st.subheader("ποΈ Database Management")
|
| 136 |
|
| 137 |
col1, col2 = st.columns(2)
|
| 138 |
|
| 139 |
with col1:
|
| 140 |
+
if is_demo:
|
| 141 |
+
st.button("π§Ή Clear Vector Database", use_container_width=True, disabled=True)
|
| 142 |
+
st.caption("π Disabled in live demo")
|
| 143 |
+
else:
|
| 144 |
+
if st.button("π§Ή Clear Vector Database", use_container_width=True):
|
| 145 |
+
ChromaClient().delete_all()
|
| 146 |
+
st.success("Vector database cleared!")
|
| 147 |
|
| 148 |
with col2:
|
| 149 |
if st.button("π Database Stats", use_container_width=True):
|
|
|
|
| 175 |
- **Platform**: {os.sys.platform}
|
| 176 |
- **Working Directory**: {os.getcwd()}
|
| 177 |
- **Environment**: {'π³ Docker' if os.path.exists('/.dockerenv') else 'π₯οΈ Local'}
|
| 178 |
+
- **Mode**: {'π Live Demo (Read-Only)' if is_demo else 'π§ Full Access'}
|
| 179 |
""")
|
| 180 |
|
| 181 |
# About
|