jmullings commited on
Commit ·
9ad792d
1
Parent(s): d7f0565
Base DB Call updates
Browse files
app.py
CHANGED
|
@@ -204,6 +204,15 @@ def init_leaderboard(dataframe):
|
|
| 204 |
)
|
| 205 |
|
| 206 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
custom_head_js = """
|
| 208 |
<script>
|
| 209 |
window.selectTopModel = function(modelName) {
|
|
@@ -296,6 +305,13 @@ with demo:
|
|
| 296 |
outputs=[results_display_panel],
|
| 297 |
)
|
| 298 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 299 |
with gr.Row():
|
| 300 |
with gr.Accordion("📙 Methodology & Limitations", open=False):
|
| 301 |
gr.Textbox(value=CITATION_BUTTON_TEXT, label=CITATION_BUTTON_LABEL, lines=8, show_copy_button=True)
|
|
@@ -305,3 +321,5 @@ if __name__ == "__main__":
|
|
| 305 |
demo.queue(default_concurrency_limit=5).launch()
|
| 306 |
# if __name__ == "__main__":
|
| 307 |
# demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
|
|
|
|
|
| 204 |
)
|
| 205 |
|
| 206 |
|
| 207 |
+
def refresh_dashboard():
|
| 208 |
+
"""Re-pulls leaderboard + top-3 data. Bound to demo.load() so that if
|
| 209 |
+
the module-import-time fetch raced ahead of a still-connecting Mongo
|
| 210 |
+
client on a cold start, the UI corrects itself once the page mounts."""
|
| 211 |
+
df = get_leaderboard_df(EVAL_RESULTS_PATH, "", COLS, BENCHMARK_COLS)
|
| 212 |
+
top3 = get_top_3_eval_cards(EVAL_RESULTS_PATH)
|
| 213 |
+
return df, build_top_3_cards_html(top3)
|
| 214 |
+
|
| 215 |
+
|
| 216 |
custom_head_js = """
|
| 217 |
<script>
|
| 218 |
window.selectTopModel = function(modelName) {
|
|
|
|
| 305 |
outputs=[results_display_panel],
|
| 306 |
)
|
| 307 |
|
| 308 |
+
# Page mount -> re-pull leaderboard + top-3 cards.
|
| 309 |
+
# Self-healing refresh: guards against a cold-start race where the
|
| 310 |
+
# module-level fetch above ran before the Mongo client finished
|
| 311 |
+
# connecting, which would otherwise freeze a stale/incomplete
|
| 312 |
+
# snapshot for every visitor until the container restarts.
|
| 313 |
+
demo.load(fn=refresh_dashboard, outputs=[leaderboard_table, top_cards_display])
|
| 314 |
+
|
| 315 |
with gr.Row():
|
| 316 |
with gr.Accordion("📙 Methodology & Limitations", open=False):
|
| 317 |
gr.Textbox(value=CITATION_BUTTON_TEXT, label=CITATION_BUTTON_LABEL, lines=8, show_copy_button=True)
|
|
|
|
| 321 |
demo.queue(default_concurrency_limit=5).launch()
|
| 322 |
# if __name__ == "__main__":
|
| 323 |
# demo.launch(server_name="0.0.0.0", server_port=7860)
|
| 324 |
+
# if __name__ == "__main__":
|
| 325 |
+
# demo.launch(server_name="0.0.0.0", server_port=7860)
|
src/db.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import os
|
| 2 |
import re
|
|
|
|
| 3 |
from typing import Dict, List, Optional
|
| 4 |
|
| 5 |
try:
|
|
@@ -16,27 +17,37 @@ COLLECTION_NAME = "audit_certificates"
|
|
| 16 |
_mongo_client = None
|
| 17 |
|
| 18 |
|
| 19 |
-
def get_db_collection():
|
| 20 |
-
"""Initializes and returns the MongoDB collection using HF Secrets.
|
|
|
|
|
|
|
|
|
|
| 21 |
global _mongo_client
|
| 22 |
if not HAS_PYMONGO:
|
| 23 |
return None
|
| 24 |
if not MONGODB_URI:
|
| 25 |
-
# Space secret MONGODB_URI not yet configured; gracefully fallback to disk cache
|
| 26 |
-
return None
|
| 27 |
-
try:
|
| 28 |
-
if _mongo_client is None:
|
| 29 |
-
_mongo_client = pymongo.MongoClient(
|
| 30 |
-
MONGODB_URI,
|
| 31 |
-
serverSelectionTimeoutMS=5000,
|
| 32 |
-
connectTimeoutMS=5000,
|
| 33 |
-
)
|
| 34 |
-
db = _mongo_client[DB_NAME]
|
| 35 |
-
return db[COLLECTION_NAME]
|
| 36 |
-
except Exception as e:
|
| 37 |
-
print(f"[MongoDB] Connection warning: {e}", flush=True)
|
| 38 |
return None
|
| 39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
def mongo_save_certificate(cert: Dict) -> bool:
|
| 42 |
"""Persists an audit certificate permanently to MongoDB Atlas."""
|
|
|
|
| 1 |
import os
|
| 2 |
import re
|
| 3 |
+
import time
|
| 4 |
from typing import Dict, List, Optional
|
| 5 |
|
| 6 |
try:
|
|
|
|
| 17 |
_mongo_client = None
|
| 18 |
|
| 19 |
|
| 20 |
+
def get_db_collection(retries: int = 3, delay: float = 2.0):
|
| 21 |
+
"""Initializes and returns the MongoDB collection using HF Secrets.
|
| 22 |
+
Retries the initial connection — HF Space cold starts often need a
|
| 23 |
+
few extra seconds for DNS/TLS to Atlas before serverSelection succeeds.
|
| 24 |
+
"""
|
| 25 |
global _mongo_client
|
| 26 |
if not HAS_PYMONGO:
|
| 27 |
return None
|
| 28 |
if not MONGODB_URI:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
return None
|
| 30 |
|
| 31 |
+
for attempt in range(1, retries + 1):
|
| 32 |
+
try:
|
| 33 |
+
if _mongo_client is None:
|
| 34 |
+
_mongo_client = pymongo.MongoClient(
|
| 35 |
+
MONGODB_URI,
|
| 36 |
+
serverSelectionTimeoutMS=8000,
|
| 37 |
+
connectTimeoutMS=8000,
|
| 38 |
+
)
|
| 39 |
+
# Force a real round-trip here (pymongo is lazy otherwise),
|
| 40 |
+
# so a slow cold-start connection is retried at this single
|
| 41 |
+
# choke point rather than surfacing as "0 certificates".
|
| 42 |
+
_mongo_client.admin.command("ping")
|
| 43 |
+
return _mongo_client[DB_NAME][COLLECTION_NAME]
|
| 44 |
+
except Exception as e:
|
| 45 |
+
print(f"[MongoDB] Connection attempt {attempt}/{retries} warning: {e}", flush=True)
|
| 46 |
+
_mongo_client = None
|
| 47 |
+
if attempt < retries:
|
| 48 |
+
time.sleep(delay)
|
| 49 |
+
return None
|
| 50 |
+
|
| 51 |
|
| 52 |
def mongo_save_certificate(cert: Dict) -> bool:
|
| 53 |
"""Persists an audit certificate permanently to MongoDB Atlas."""
|