Spaces:
Runtime error
Runtime error
Upload folder using huggingface_hub
Browse files- backend/database.py +19 -2
- frontend/Home.py +8 -1
backend/database.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import sqlite3
|
| 2 |
import os
|
|
|
|
| 3 |
from datetime import datetime
|
| 4 |
from huggingface_hub import HfApi, hf_hub_download
|
| 5 |
|
|
@@ -15,18 +16,26 @@ def sync_from_hub():
|
|
| 15 |
print("No HF_TOKEN found. Skipping sync.")
|
| 16 |
return
|
| 17 |
try:
|
|
|
|
| 18 |
print(f"Downloading {DB_NAME} from Hub...")
|
| 19 |
-
path = hf_hub_download(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
import shutil
|
| 21 |
shutil.copy(path, DB_PATH)
|
| 22 |
print("Sync from Hub complete.")
|
| 23 |
except Exception as e:
|
| 24 |
-
print(f"Sync from Hub failed
|
| 25 |
|
| 26 |
def sync_to_hub():
|
| 27 |
if not HF_TOKEN:
|
| 28 |
return
|
| 29 |
try:
|
|
|
|
| 30 |
api.upload_file(
|
| 31 |
path_or_fileobj=DB_PATH,
|
| 32 |
path_in_repo=DB_NAME,
|
|
@@ -59,6 +68,9 @@ def init_db():
|
|
| 59 |
conn.close()
|
| 60 |
|
| 61 |
def log_prediction(data, prediction_label, confidence):
|
|
|
|
|
|
|
|
|
|
| 62 |
conn = sqlite3.connect(DB_PATH)
|
| 63 |
cursor = conn.cursor()
|
| 64 |
cursor.execute('''
|
|
@@ -77,9 +89,14 @@ def log_prediction(data, prediction_label, confidence):
|
|
| 77 |
))
|
| 78 |
conn.commit()
|
| 79 |
conn.close()
|
|
|
|
|
|
|
| 80 |
sync_to_hub()
|
| 81 |
|
| 82 |
def get_history():
|
|
|
|
|
|
|
|
|
|
| 83 |
if not os.path.exists(DB_PATH):
|
| 84 |
return []
|
| 85 |
conn = sqlite3.connect(DB_PATH)
|
|
|
|
| 1 |
import sqlite3
|
| 2 |
import os
|
| 3 |
+
import time
|
| 4 |
from datetime import datetime
|
| 5 |
from huggingface_hub import HfApi, hf_hub_download
|
| 6 |
|
|
|
|
| 16 |
print("No HF_TOKEN found. Skipping sync.")
|
| 17 |
return
|
| 18 |
try:
|
| 19 |
+
# Download with cache-busting logic if possible or just fresh download
|
| 20 |
print(f"Downloading {DB_NAME} from Hub...")
|
| 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 |
+
)
|
| 28 |
import shutil
|
| 29 |
shutil.copy(path, DB_PATH)
|
| 30 |
print("Sync from Hub complete.")
|
| 31 |
except Exception as e:
|
| 32 |
+
print(f"Sync from Hub failed: {e}")
|
| 33 |
|
| 34 |
def sync_to_hub():
|
| 35 |
if not HF_TOKEN:
|
| 36 |
return
|
| 37 |
try:
|
| 38 |
+
# Uploading back to Hub centralizes it for other instances
|
| 39 |
api.upload_file(
|
| 40 |
path_or_fileobj=DB_PATH,
|
| 41 |
path_in_repo=DB_NAME,
|
|
|
|
| 68 |
conn.close()
|
| 69 |
|
| 70 |
def log_prediction(data, prediction_label, confidence):
|
| 71 |
+
# Pull latest before writing to avoid overwriting others' work
|
| 72 |
+
sync_from_hub()
|
| 73 |
+
|
| 74 |
conn = sqlite3.connect(DB_PATH)
|
| 75 |
cursor = conn.cursor()
|
| 76 |
cursor.execute('''
|
|
|
|
| 89 |
))
|
| 90 |
conn.commit()
|
| 91 |
conn.close()
|
| 92 |
+
|
| 93 |
+
# Push immediately
|
| 94 |
sync_to_hub()
|
| 95 |
|
| 96 |
def get_history():
|
| 97 |
+
# Fresh pull for history view
|
| 98 |
+
sync_from_hub()
|
| 99 |
+
|
| 100 |
if not os.path.exists(DB_PATH):
|
| 101 |
return []
|
| 102 |
conn = sqlite3.connect(DB_PATH)
|
frontend/Home.py
CHANGED
|
@@ -5,6 +5,7 @@ import time
|
|
| 5 |
import pandas as pd
|
| 6 |
import plotly.graph_objects as go
|
| 7 |
import numpy as np
|
|
|
|
| 8 |
|
| 9 |
API_URL = os.getenv("API_URL", "http://localhost:8000")
|
| 10 |
if "hf.space" in os.getenv("SPACE_ID", ""):
|
|
@@ -124,7 +125,13 @@ def _candle_at(t_epoch, prev_alerts, curr_alerts):
|
|
| 124 |
l = min(o, c) - rng.uniform(0.5, spread * 0.6)
|
| 125 |
h = min(h, ALERT_MAX + 5)
|
| 126 |
l = max(l, 0)
|
| 127 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
|
| 129 |
# ββββββββ Initialize: pre-fill chart as if running since startup ββββββββ
|
| 130 |
if "chart_candles" not in st.session_state:
|
|
|
|
| 5 |
import pandas as pd
|
| 6 |
import plotly.graph_objects as go
|
| 7 |
import numpy as np
|
| 8 |
+
from datetime import timedelta
|
| 9 |
|
| 10 |
API_URL = os.getenv("API_URL", "http://localhost:8000")
|
| 11 |
if "hf.space" in os.getenv("SPACE_ID", ""):
|
|
|
|
| 125 |
l = min(o, c) - rng.uniform(0.5, spread * 0.6)
|
| 126 |
h = min(h, ALERT_MAX + 5)
|
| 127 |
l = max(l, 0)
|
| 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)}
|
| 135 |
|
| 136 |
# ββββββββ Initialize: pre-fill chart as if running since startup ββββββββ
|
| 137 |
if "chart_candles" not in st.session_state:
|