MarcoParola commited on
Commit
169c4de
·
1 Parent(s): 673055c

fix hf dataset storage

Browse files
Files changed (1) hide show
  1. app.py +28 -7
app.py CHANGED
@@ -11,6 +11,12 @@ from src.utils import load_words, load_image_and_saliency, load_example_images
11
  from src.style import css
12
  from src.user import UserID
13
 
 
 
 
 
 
 
14
  def main():
15
  config = yaml.safe_load(open("config/config.yaml"))
16
  words = ['grad-cam', 'lime', 'sidu', 'rise']
@@ -131,18 +137,33 @@ def main():
131
 
132
  def save_results(answers):
133
  api = HfApi()
134
- json_file_results = config['results']['exp1_dir']
135
  JSON_DATASET_DIR = Path("json_dataset")
136
  JSON_DATASET_DIR.mkdir(parents=True, exist_ok=True)
137
- JSON_DATASET_PATH = JSON_DATASET_DIR / json_file_results
 
 
 
 
 
 
138
 
139
  info_to_push = {
140
  "user_id": time.time(),
141
- "answer": {i: answer[i] for i in range(len(answer))}}
142
-
143
- # use api to push the results to the hub
144
- api.push_to_hub(info_to_push, json_file_results, use_temp_dir=True)
145
-
 
 
 
 
 
 
 
 
 
146
 
147
  def add_answer(dropdown1,dropdown2,dropdown3,dropdown4, answers):
148
  rank = [dropdown1,dropdown2,dropdown3,dropdown4]
 
11
  from src.style import css
12
  from src.user import UserID
13
 
14
+ from datetime import datetime
15
+ from pathlib import Path
16
+ from uuid import uuid4
17
+ import json
18
+ from huggingface_hub import CommitScheduler, HfApi
19
+
20
  def main():
21
  config = yaml.safe_load(open("config/config.yaml"))
22
  words = ['grad-cam', 'lime', 'sidu', 'rise']
 
137
 
138
  def save_results(answers):
139
  api = HfApi()
140
+ json_file_results = config['results']['exp1_dir'] # 'exp1'
141
  JSON_DATASET_DIR = Path("json_dataset")
142
  JSON_DATASET_DIR.mkdir(parents=True, exist_ok=True)
143
+ JSON_DATASET_PATH = JSON_DATASET_DIR / f"train-{uuid4()}.json"
144
+ scheduler = CommitScheduler(
145
+ repo_id="example-space-to-dataset-json",
146
+ repo_type="dataset",
147
+ folder_path=JSON_DATASET_DIR,
148
+ path_in_repo="data"
149
+ )
150
 
151
  info_to_push = {
152
  "user_id": time.time(),
153
+ "answer": {i: answer for i, answer in enumerate(answers)}
154
+ }
155
+
156
+ # Save the results into huggingface hub
157
+ with scheduler.lock:
158
+ with JSON_DATASET_PATH.open("a") as f:
159
+ json.dump({
160
+ "user_id": info_to_push["user_id"],
161
+ "answers": info_to_push["answer"],
162
+ "datetime": datetime.now().isoformat()
163
+ }, f)
164
+ f.write("\n")
165
+ scheduler.push_to_hub()
166
+
167
 
168
  def add_answer(dropdown1,dropdown2,dropdown3,dropdown4, answers):
169
  rank = [dropdown1,dropdown2,dropdown3,dropdown4]