NidhiS09 commited on
Commit
827ca18
·
verified ·
1 Parent(s): 3c13f90

Update src/main.py

Browse files

Added commitScheduler

Files changed (1) hide show
  1. src/main.py +33 -26
src/main.py CHANGED
@@ -8,8 +8,9 @@ from typing import Any, Dict, List, Tuple
8
  import streamlit as st
9
  import pandas as pd
10
  from PIL import Image
11
- from huggingface_hub import HfApi, hf_hub_download
12
  from huggingface_hub.utils import HfHubHTTPError
 
13
 
14
 
15
  # =========================
@@ -43,6 +44,24 @@ CHALLENGE_TYPES = ["Object Detection", "Instance Segmentation"]
43
  LEADERBOARD_METRICS = ["bbox_mAP", "bbox_AP50", "segm_mAP", "segm_AP50"]
44
  DEFAULT_SORT_METRIC = "segm_AP50"
45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
 
47
  # =========================
48
  # HELPERS
@@ -101,25 +120,14 @@ def _validate_submission_json(obj: Any) -> Tuple[bool, str]:
101
  return True, "OK"
102
 
103
 
104
- def _upload_json(api: HfApi, data: Dict[str, Any] | List[Any], path_in_repo: str) -> None:
105
- with tempfile.NamedTemporaryFile(mode="w", suffix=".json", delete=False) as tmp:
106
- json.dump(data, tmp, ensure_ascii=False)
107
- tmp_path = tmp.name
108
-
109
- try:
110
- api.upload_file(
111
- path_or_fileobj=tmp_path,
112
- path_in_repo=path_in_repo,
113
- repo_id=DB_REPO_ID,
114
- repo_type=DB_REPO_TYPE,
115
- token=SUBMISSIONS_TOKEN,
116
- commit_message=f"Add {path_in_repo}",
117
- )
118
- finally:
119
- try:
120
- os.remove(tmp_path)
121
- except OSError:
122
- pass
123
 
124
 
125
  def _create_submission_record(
@@ -132,11 +140,10 @@ def _create_submission_record(
132
  original_filename: str,
133
  ) -> str:
134
  """
135
- Writes pred/meta/status to the private DB dataset repo.
136
  Returns submission_id.
137
  """
138
  _require_token()
139
- api = HfApi()
140
 
141
  submission_id = str(uuid.uuid4())
142
  ts = int(time.time())
@@ -153,10 +160,10 @@ def _create_submission_record(
153
 
154
  status = {"state": "queued", "timestamp": ts}
155
 
156
- base = f"submissions/{submission_id}"
157
- _upload_json(api, pred, f"{base}/pred.json")
158
- _upload_json(api, meta, f"{base}/meta.json")
159
- _upload_json(api, status, f"{base}/status.json")
160
 
161
  return submission_id
162
 
 
8
  import streamlit as st
9
  import pandas as pd
10
  from PIL import Image
11
+ from huggingface_hub import HfApi, hf_hub_download, CommitScheduler
12
  from huggingface_hub.utils import HfHubHTTPError
13
+ from pathlib import Path
14
 
15
 
16
  # =========================
 
44
  LEADERBOARD_METRICS = ["bbox_mAP", "bbox_AP50", "segm_mAP", "segm_AP50"]
45
  DEFAULT_SORT_METRIC = "segm_AP50"
46
 
47
+ # =========================
48
+ # COMMIT SCHEDULER SETUP
49
+ # =========================
50
+
51
+ # Local folder for scheduler to watch
52
+ SCHEDULER_FOLDER = Path("submissions_queue")
53
+ SCHEDULER_FOLDER.mkdir(exist_ok=True)
54
+
55
+ # Initialize CommitScheduler
56
+ scheduler = CommitScheduler(
57
+ repo_id=DB_REPO_ID,
58
+ repo_type=DB_REPO_TYPE,
59
+ folder_path=SCHEDULER_FOLDER,
60
+ token=SUBMISSIONS_TOKEN,
61
+ path_in_repo="submissions", # Where files go in the repo
62
+ every=5, # Commit every 5 minutes
63
+ )
64
+
65
 
66
  # =========================
67
  # HELPERS
 
120
  return True, "OK"
121
 
122
 
123
+ def _save_json_to_scheduler(data: Dict[str, Any] | List[Any], filename: str) -> None:
124
+ """
125
+ Save JSON to the scheduler folder.
126
+ The scheduler will automatically commit it to the repo.
127
+ """
128
+ file_path = SCHEDULER_FOLDER / filename
129
+ with open(file_path, "w") as f:
130
+ json.dump(data, f, ensure_ascii=False, indent=2)
 
 
 
 
 
 
 
 
 
 
 
131
 
132
 
133
  def _create_submission_record(
 
140
  original_filename: str,
141
  ) -> str:
142
  """
143
+ Writes pred/meta/status to the scheduler folder.
144
  Returns submission_id.
145
  """
146
  _require_token()
 
147
 
148
  submission_id = str(uuid.uuid4())
149
  ts = int(time.time())
 
160
 
161
  status = {"state": "queued", "timestamp": ts}
162
 
163
+ # Save to scheduler folder (will be auto-committed)
164
+ _save_json_to_scheduler(pred, f"{submission_id}_pred.json")
165
+ _save_json_to_scheduler(meta, f"{submission_id}_meta.json")
166
+ _save_json_to_scheduler(status, f"{submission_id}_status.json")
167
 
168
  return submission_id
169