IDKHowToCodeFr commited on
Commit
4dccba4
·
verified ·
1 Parent(s): f7b331f

Upload folder using huggingface_hub

Browse files
Files changed (3) hide show
  1. database.py +38 -6
  2. main.py +15 -0
  3. requirements.txt +1 -0
database.py CHANGED
@@ -1,10 +1,44 @@
1
  import sqlite3
2
  import os
3
  from datetime import datetime
 
4
 
5
- DB_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'patient_history.db')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
  def init_db():
 
8
  conn = sqlite3.connect(DB_PATH)
9
  cursor = conn.cursor()
10
  cursor.execute('''
@@ -43,23 +77,21 @@ def log_prediction(data, prediction_label, confidence):
43
  ))
44
  conn.commit()
45
  conn.close()
 
46
 
47
  def get_history():
 
 
48
  conn = sqlite3.connect(DB_PATH)
49
  cursor = conn.cursor()
50
  cursor.execute('SELECT * FROM predictions ORDER BY timestamp DESC LIMIT 100')
51
  rows = cursor.fetchall()
52
-
53
- # Get column names
54
  col_names = [description[0] for description in cursor.description]
55
-
56
  conn.close()
57
 
58
- # Format as list of dicts
59
  history = []
60
  for row in rows:
61
  history.append(dict(zip(col_names, row)))
62
-
63
  return history
64
 
65
  # Initialize db when this module is loaded
 
1
  import sqlite3
2
  import os
3
  from datetime import datetime
4
+ from huggingface_hub import HfApi, hf_hub_download
5
 
6
+ REPO_ID = "IDKHowToCodeFr/tinyml-logs"
7
+ DB_NAME = 'patient_history.db'
8
+ DB_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), DB_NAME)
9
+ HF_TOKEN = os.getenv("HF_TOKEN")
10
+
11
+ api = HfApi()
12
+
13
+ def sync_from_hub():
14
+ if not HF_TOKEN:
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(repo_id=REPO_ID, filename=DB_NAME, repo_type="dataset", token=HF_TOKEN)
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 (maybe first run?): {e}")
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,
33
+ repo_id=REPO_ID,
34
+ repo_type="dataset",
35
+ token=HF_TOKEN
36
+ )
37
+ except Exception as e:
38
+ print(f"Sync to Hub failed: {e}")
39
 
40
  def init_db():
41
+ sync_from_hub()
42
  conn = sqlite3.connect(DB_PATH)
43
  cursor = conn.cursor()
44
  cursor.execute('''
 
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)
86
  cursor = conn.cursor()
87
  cursor.execute('SELECT * FROM predictions ORDER BY timestamp DESC LIMIT 100')
88
  rows = cursor.fetchall()
 
 
89
  col_names = [description[0] for description in cursor.description]
 
90
  conn.close()
91
 
 
92
  history = []
93
  for row in rows:
94
  history.append(dict(zip(col_names, row)))
 
95
  return history
96
 
97
  # Initialize db when this module is loaded
main.py CHANGED
@@ -99,6 +99,21 @@ async def predict(data: PatientData):
99
  def history():
100
  return get_history()
101
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
102
  @app.post("/explain")
103
  async def explain(data: PatientData):
104
  eng = get_ensemble()
 
99
  def history():
100
  return get_history()
101
 
102
+ @app.get("/dataset")
103
+ async def get_dataset():
104
+ data_path = '/app/data/patient_dataset.csv' if os.path.exists('/app/data') else '../data/patient_dataset.csv' if os.path.exists('../data') else 'data/patient_dataset.csv'
105
+ if os.path.exists(data_path):
106
+ df = pd.read_csv(data_path)
107
+ # Return summary or partial data to avoid huge payloads
108
+ return df.to_dict(orient="records")
109
+ return {"error": "Dataset not found"}
110
+
111
+ @app.get("/sync")
112
+ def force_sync():
113
+ from database import sync_from_hub, sync_to_hub
114
+ sync_from_hub()
115
+ return {"status": "Sync attempted"}
116
+
117
  @app.post("/explain")
118
  async def explain(data: PatientData):
119
  eng = get_ensemble()
requirements.txt CHANGED
@@ -8,3 +8,4 @@ shap
8
  m2cgen
9
  matplotlib
10
  python-multipart
 
 
8
  m2cgen
9
  matplotlib
10
  python-multipart
11
+ huggingface_hub