Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -21,6 +21,11 @@ ADMIN_PASS = os.environ.get("ADMIN_PASS")
|
|
| 21 |
if not ADMIN_PASS:
|
| 22 |
raise ValueError("CRITICAL SECURITY ERROR: ADMIN_PASS environment variable is not set.")
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
def get_faqs():
|
| 26 |
data = fetch_all_faq_metadata()
|
|
@@ -57,51 +62,59 @@ def run_sync():
|
|
| 57 |
except Exception as e:
|
| 58 |
return f"Sync Failed: {e}"
|
| 59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
def check_login(username, password):
|
| 61 |
-
"""Validate credentials and return visibility updates."""
|
| 62 |
if username == ADMIN_USER and password == ADMIN_PASS:
|
| 63 |
return (
|
| 64 |
gr.update(visible=False), # Hide login panel
|
| 65 |
gr.update(visible=True), # Show dashboard
|
| 66 |
-
"" # Clear error
|
| 67 |
)
|
| 68 |
else:
|
| 69 |
return (
|
| 70 |
-
gr.update(visible=True),
|
| 71 |
-
gr.update(visible=False),
|
| 72 |
"β Invalid username or password."
|
| 73 |
)
|
| 74 |
|
| 75 |
def logout():
|
| 76 |
-
"""Log out and show login panel again."""
|
| 77 |
return (
|
| 78 |
gr.update(visible=True), # Show login panel
|
| 79 |
gr.update(visible=False), # Hide dashboard
|
| 80 |
-
"", # Clear username
|
| 81 |
-
"" # Clear password
|
| 82 |
)
|
| 83 |
|
| 84 |
|
|
|
|
|
|
|
| 85 |
with gr.Blocks(title="Get Scene Admin Dashboard", theme=gr.themes.Base()) as demo:
|
| 86 |
|
| 87 |
# ββ LOGIN PANEL ββββββββββββββββββββββββββββββββββββββββββββββ
|
| 88 |
with gr.Column(visible=True, elem_id="login_panel") as login_panel:
|
| 89 |
-
gr.Markdown("# Get Scene Admin\nPlease log in to continue.")
|
| 90 |
login_user = gr.Textbox(label="Username", placeholder="Enter username")
|
| 91 |
login_pass = gr.Textbox(label="Password", placeholder="Enter password", type="password")
|
| 92 |
-
login_error = gr.Markdown(""
|
| 93 |
login_btn = gr.Button("Log In", variant="primary")
|
| 94 |
|
| 95 |
# ββ DASHBOARD (hidden until login) βββββββββββββββββββββββββββ
|
| 96 |
with gr.Column(visible=False) as dashboard:
|
| 97 |
with gr.Row():
|
| 98 |
-
gr.Markdown("# Get Scene Admin Dashboard")
|
| 99 |
logout_btn = gr.Button("Log Out", variant="stop", scale=0)
|
| 100 |
|
| 101 |
gr.Markdown("Manage FAQs, Podcasts, and Knowledge Embeddings.")
|
| 102 |
|
| 103 |
with gr.Tabs():
|
| 104 |
-
|
|
|
|
| 105 |
with gr.TabItem("Manage FAQs"):
|
| 106 |
with gr.Row():
|
| 107 |
faq_df = gr.Dataframe(
|
|
@@ -132,7 +145,7 @@ with gr.Blocks(title="Get Scene Admin Dashboard", theme=gr.themes.Base()) as dem
|
|
| 132 |
add_btn.click(add_and_refresh, [new_q, new_a], [faq_df, new_q, new_a])
|
| 133 |
upload_faq_btn.click(handle_faq_upload, [faq_file], [faq_upload_status])
|
| 134 |
|
| 135 |
-
# Tab 2: Podcasts
|
| 136 |
with gr.TabItem("Podcasts"):
|
| 137 |
pod_df = gr.Dataframe(
|
| 138 |
value=get_podcasts,
|
|
@@ -147,15 +160,47 @@ with gr.Blocks(title="Get Scene Admin Dashboard", theme=gr.themes.Base()) as dem
|
|
| 147 |
|
| 148 |
upload_pod_btn.click(handle_podcast_upload, [pod_file], [pod_upload_status])
|
| 149 |
|
| 150 |
-
# Tab 3: Sync
|
| 151 |
with gr.TabItem("Sync & Embed"):
|
| 152 |
gr.Markdown("### Recalculate Embeddings")
|
| 153 |
-
gr.Markdown(
|
|
|
|
|
|
|
|
|
|
| 154 |
sync_btn = gr.Button("π Sync & Recalculate Embeddings", variant="primary", scale=2)
|
| 155 |
sync_status = gr.Textbox(label="Sync Status", interactive=False)
|
| 156 |
|
| 157 |
sync_btn.click(run_sync, None, [sync_status])
|
| 158 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
# ββ WIRING ββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 160 |
login_btn.click(
|
| 161 |
check_login,
|
|
@@ -163,7 +208,6 @@ with gr.Blocks(title="Get Scene Admin Dashboard", theme=gr.themes.Base()) as dem
|
|
| 163 |
outputs=[login_panel, dashboard, login_error]
|
| 164 |
)
|
| 165 |
|
| 166 |
-
# Allow pressing Enter in password field to log in
|
| 167 |
login_pass.submit(
|
| 168 |
check_login,
|
| 169 |
inputs=[login_user, login_pass],
|
|
@@ -178,5 +222,4 @@ with gr.Blocks(title="Get Scene Admin Dashboard", theme=gr.themes.Base()) as dem
|
|
| 178 |
|
| 179 |
|
| 180 |
if __name__ == "__main__":
|
| 181 |
-
# No auth= parameter β login is handled inside the app
|
| 182 |
demo.launch(server_name="0.0.0.0")
|
|
|
|
| 21 |
if not ADMIN_PASS:
|
| 22 |
raise ValueError("CRITICAL SECURITY ERROR: ADMIN_PASS environment variable is not set.")
|
| 23 |
|
| 24 |
+
# Path to SQLite DB β override via DB_PATH env var if your file has a different name
|
| 25 |
+
DB_PATH = os.environ.get("DB_PATH", "database.db")
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
# ββ Helper functions ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 29 |
|
| 30 |
def get_faqs():
|
| 31 |
data = fetch_all_faq_metadata()
|
|
|
|
| 62 |
except Exception as e:
|
| 63 |
return f"Sync Failed: {e}"
|
| 64 |
|
| 65 |
+
def prepare_download():
|
| 66 |
+
"""Return the SQLite file path for Gradio to serve as a download."""
|
| 67 |
+
if not os.path.exists(DB_PATH):
|
| 68 |
+
return None, f"β Database file not found at: `{DB_PATH}`. Check your DB_PATH env var."
|
| 69 |
+
size_mb = os.path.getsize(DB_PATH) / (1024 * 1024)
|
| 70 |
+
return DB_PATH, f"β
Ready β `{os.path.basename(DB_PATH)}` ({size_mb:.2f} MB). Click the file below to save it."
|
| 71 |
+
|
| 72 |
def check_login(username, password):
|
|
|
|
| 73 |
if username == ADMIN_USER and password == ADMIN_PASS:
|
| 74 |
return (
|
| 75 |
gr.update(visible=False), # Hide login panel
|
| 76 |
gr.update(visible=True), # Show dashboard
|
| 77 |
+
"" # Clear error
|
| 78 |
)
|
| 79 |
else:
|
| 80 |
return (
|
| 81 |
+
gr.update(visible=True),
|
| 82 |
+
gr.update(visible=False),
|
| 83 |
"β Invalid username or password."
|
| 84 |
)
|
| 85 |
|
| 86 |
def logout():
|
|
|
|
| 87 |
return (
|
| 88 |
gr.update(visible=True), # Show login panel
|
| 89 |
gr.update(visible=False), # Hide dashboard
|
| 90 |
+
"", # Clear username field
|
| 91 |
+
"" # Clear password field
|
| 92 |
)
|
| 93 |
|
| 94 |
|
| 95 |
+
# ββ UI ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 96 |
+
|
| 97 |
with gr.Blocks(title="Get Scene Admin Dashboard", theme=gr.themes.Base()) as demo:
|
| 98 |
|
| 99 |
# ββ LOGIN PANEL ββββββββββββββββββββββββββββββββββββββββββββββ
|
| 100 |
with gr.Column(visible=True, elem_id="login_panel") as login_panel:
|
| 101 |
+
gr.Markdown("# π Get Scene Admin\nPlease log in to continue.")
|
| 102 |
login_user = gr.Textbox(label="Username", placeholder="Enter username")
|
| 103 |
login_pass = gr.Textbox(label="Password", placeholder="Enter password", type="password")
|
| 104 |
+
login_error = gr.Markdown("")
|
| 105 |
login_btn = gr.Button("Log In", variant="primary")
|
| 106 |
|
| 107 |
# ββ DASHBOARD (hidden until login) βββββββββββββββββββββββββββ
|
| 108 |
with gr.Column(visible=False) as dashboard:
|
| 109 |
with gr.Row():
|
| 110 |
+
gr.Markdown("# π Get Scene Admin Dashboard")
|
| 111 |
logout_btn = gr.Button("Log Out", variant="stop", scale=0)
|
| 112 |
|
| 113 |
gr.Markdown("Manage FAQs, Podcasts, and Knowledge Embeddings.")
|
| 114 |
|
| 115 |
with gr.Tabs():
|
| 116 |
+
|
| 117 |
+
# ββ Tab 1: FAQs ββββββββββββββββββββββββββββββββββββββ
|
| 118 |
with gr.TabItem("Manage FAQs"):
|
| 119 |
with gr.Row():
|
| 120 |
faq_df = gr.Dataframe(
|
|
|
|
| 145 |
add_btn.click(add_and_refresh, [new_q, new_a], [faq_df, new_q, new_a])
|
| 146 |
upload_faq_btn.click(handle_faq_upload, [faq_file], [faq_upload_status])
|
| 147 |
|
| 148 |
+
# ββ Tab 2: Podcasts ββββββββββββββββββββββββββββββββββ
|
| 149 |
with gr.TabItem("Podcasts"):
|
| 150 |
pod_df = gr.Dataframe(
|
| 151 |
value=get_podcasts,
|
|
|
|
| 160 |
|
| 161 |
upload_pod_btn.click(handle_podcast_upload, [pod_file], [pod_upload_status])
|
| 162 |
|
| 163 |
+
# ββ Tab 3: Sync & Embed ββββββββββββββββββββββββββββββ
|
| 164 |
with gr.TabItem("Sync & Embed"):
|
| 165 |
gr.Markdown("### Recalculate Embeddings")
|
| 166 |
+
gr.Markdown(
|
| 167 |
+
"When you change text or upload new data, the 'embeddings' (AI understanding) "
|
| 168 |
+
"must be recalculated for the chatbot to recognize the new information."
|
| 169 |
+
)
|
| 170 |
sync_btn = gr.Button("π Sync & Recalculate Embeddings", variant="primary", scale=2)
|
| 171 |
sync_status = gr.Textbox(label="Sync Status", interactive=False)
|
| 172 |
|
| 173 |
sync_btn.click(run_sync, None, [sync_status])
|
| 174 |
|
| 175 |
+
# ββ Tab 4: Download Database βββββββββββββββββββββββββ
|
| 176 |
+
with gr.TabItem("β¬οΈ Download Database"):
|
| 177 |
+
gr.Markdown("### Download SQLite Database")
|
| 178 |
+
gr.Markdown(
|
| 179 |
+
"Click the button below to download the raw `database.db` SQLite file. "
|
| 180 |
+
"You can open it locally with [DB Browser for SQLite](https://sqlitebrowser.org/) "
|
| 181 |
+
"or any other SQLite viewer."
|
| 182 |
+
)
|
| 183 |
+
download_btn = gr.Button("β¬οΈ Download database.db", variant="primary")
|
| 184 |
+
download_status = gr.Markdown("")
|
| 185 |
+
db_file_output = gr.File(
|
| 186 |
+
label="π¦ database.db β click to save",
|
| 187 |
+
visible=False,
|
| 188 |
+
interactive=False
|
| 189 |
+
)
|
| 190 |
+
|
| 191 |
+
def on_download_click():
|
| 192 |
+
path, msg = prepare_download()
|
| 193 |
+
if path:
|
| 194 |
+
return gr.update(value=path, visible=True), msg
|
| 195 |
+
else:
|
| 196 |
+
return gr.update(visible=False), msg
|
| 197 |
+
|
| 198 |
+
download_btn.click(
|
| 199 |
+
on_download_click,
|
| 200 |
+
inputs=None,
|
| 201 |
+
outputs=[db_file_output, download_status]
|
| 202 |
+
)
|
| 203 |
+
|
| 204 |
# ββ WIRING ββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 205 |
login_btn.click(
|
| 206 |
check_login,
|
|
|
|
| 208 |
outputs=[login_panel, dashboard, login_error]
|
| 209 |
)
|
| 210 |
|
|
|
|
| 211 |
login_pass.submit(
|
| 212 |
check_login,
|
| 213 |
inputs=[login_user, login_pass],
|
|
|
|
| 222 |
|
| 223 |
|
| 224 |
if __name__ == "__main__":
|
|
|
|
| 225 |
demo.launch(server_name="0.0.0.0")
|