Skip to content

Commit 0cbc2ff

Browse files
feat: centralize DB on HF Hub and add IST to home emulation
1 parent dbcfe3d commit 0cbc2ff

2 files changed

Lines changed: 27 additions & 3 deletions

File tree

backend/database.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import sqlite3
22
import os
3+
import time
34
from datetime import datetime
45
from huggingface_hub import HfApi, hf_hub_download
56

@@ -15,18 +16,26 @@ def sync_from_hub():
1516
print("No HF_TOKEN found. Skipping sync.")
1617
return
1718
try:
19+
# Download with cache-busting logic if possible or just fresh download
1820
print(f"Downloading {DB_NAME} from Hub...")
19-
path = hf_hub_download(repo_id=REPO_ID, filename=DB_NAME, repo_type="dataset", token=HF_TOKEN)
21+
path = hf_hub_download(
22+
repo_id=REPO_ID,
23+
filename=DB_NAME,
24+
repo_type="dataset",
25+
token=HF_TOKEN,
26+
force_download=True
27+
)
2028
import shutil
2129
shutil.copy(path, DB_PATH)
2230
print("Sync from Hub complete.")
2331
except Exception as e:
24-
print(f"Sync from Hub failed (maybe first run?): {e}")
32+
print(f"Sync from Hub failed: {e}")
2533

2634
def sync_to_hub():
2735
if not HF_TOKEN:
2836
return
2937
try:
38+
# Uploading back to Hub centralizes it for other instances
3039
api.upload_file(
3140
path_or_fileobj=DB_PATH,
3241
path_in_repo=DB_NAME,
@@ -59,6 +68,9 @@ def init_db():
5968
conn.close()
6069

6170
def log_prediction(data, prediction_label, confidence):
71+
# Pull latest before writing to avoid overwriting others' work
72+
sync_from_hub()
73+
6274
conn = sqlite3.connect(DB_PATH)
6375
cursor = conn.cursor()
6476
cursor.execute('''
@@ -77,9 +89,14 @@ def log_prediction(data, prediction_label, confidence):
7789
))
7890
conn.commit()
7991
conn.close()
92+
93+
# Push immediately
8094
sync_to_hub()
8195

8296
def get_history():
97+
# Fresh pull for history view
98+
sync_from_hub()
99+
83100
if not os.path.exists(DB_PATH):
84101
return []
85102
conn = sqlite3.connect(DB_PATH)

frontend/Home.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import pandas as pd
66
import plotly.graph_objects as go
77
import numpy as np
8+
from datetime import timedelta
89

910
API_URL = os.getenv("API_URL", "http://localhost:8000")
1011
if "hf.space" in os.getenv("SPACE_ID", ""):
@@ -124,7 +125,13 @@ def _candle_at(t_epoch, prev_alerts, curr_alerts):
124125
l = min(o, c) - rng.uniform(0.5, spread * 0.6)
125126
h = min(h, ALERT_MAX + 5)
126127
l = max(l, 0)
127-
return {"time": pd.Timestamp.fromtimestamp(t_epoch), "open": round(o, 1), "high": round(h, 1), "low": round(l, 1), "close": round(c, 1)}
128+
129+
# Convert UTC timestamp to IST for display
130+
ts = pd.Timestamp.fromtimestamp(t_epoch)
131+
if "hf.space" in os.getenv("SPACE_ID", ""):
132+
ts = ts + timedelta(hours=5, minutes=30)
133+
134+
return {"time": ts, "open": round(o, 1), "high": round(h, 1), "low": round(l, 1), "close": round(c, 1)}
128135

129136
# ──────── Initialize: pre-fill chart as if running since startup ────────
130137
if "chart_candles" not in st.session_state:

0 commit comments

Comments
 (0)