tdurbor commited on
Commit
9248120
1 Parent(s): 12ed30a

Test upload votes to dataset

Browse files
Files changed (2) hide show
  1. app.py +61 -3
  2. requirements.txt +2 -1
app.py CHANGED
@@ -7,6 +7,14 @@ import numpy as np
7
  from PIL import Image
8
  import random
9
  from db import compute_elo_scores, get_all_votes
 
 
 
 
 
 
 
 
10
 
11
  # Configure logging
12
  logging.basicConfig(level=logging.INFO)
@@ -14,9 +22,17 @@ logging.basicConfig(level=logging.INFO)
14
  # Load environment variables from .env file
15
  load_dotenv()
16
 
17
- # Access the API key
18
- PHOTOROOM_API_KEY = os.getenv('PHOTOROOM_API_KEY')
19
- CLIPDROP_API_KEY = os.getenv('CLIPDROP_API_KEY')
 
 
 
 
 
 
 
 
20
 
21
 
22
  def fetch_elo_scores():
@@ -261,6 +277,48 @@ def load_images_from_directory(directory):
261
  image_files = [f for f in os.listdir(directory) if f.endswith(('.png', '.jpg', '.jpeg'))]
262
  return [os.path.join(directory, f) for f in image_files]
263
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
264
  if __name__ == "__main__":
 
265
  demo = gradio_interface()
266
  demo.launch()
 
7
  from PIL import Image
8
  import random
9
  from db import compute_elo_scores, get_all_votes
10
+ import json
11
+ from pathlib import Path
12
+ from uuid import uuid4
13
+ import logging
14
+ import threading
15
+ import time
16
+
17
+ from huggingface_hub import CommitScheduler
18
 
19
  # Configure logging
20
  logging.basicConfig(level=logging.INFO)
 
22
  # Load environment variables from .env file
23
  load_dotenv()
24
 
25
+ # Directory and path setup for JSON dataset
26
+ JSON_DATASET_DIR = Path("data/json_dataset")
27
+ JSON_DATASET_DIR.mkdir(parents=True, exist_ok=True)
28
+
29
+ # Initialize CommitScheduler for Hugging Face
30
+ scheduler = CommitScheduler(
31
+ repo_id="bgsys/votes_datasets_test",
32
+ repo_type="dataset",
33
+ folder_path=JSON_DATASET_DIR,
34
+ path_in_repo="data",
35
+ )
36
 
37
 
38
  def fetch_elo_scores():
 
277
  image_files = [f for f in os.listdir(directory) if f.endswith(('.png', '.jpg', '.jpeg'))]
278
  return [os.path.join(directory, f) for f in image_files]
279
 
280
+
281
+ def dump_database_to_json():
282
+ """Dump the database to a JSON file and upload it to Hugging Face."""
283
+ votes = get_all_votes()
284
+ json_data = [
285
+ {
286
+ "id": vote.id,
287
+ "image_id": vote.image_id,
288
+ "model_a": vote.model_a,
289
+ "model_b": vote.model_b,
290
+ "winner": vote.winner,
291
+ "user_id": vote.user_id,
292
+ "fpath_a": vote.fpath_a,
293
+ "fpath_b": vote.fpath_b,
294
+ "timestamp": vote.timestamp.isoformat()
295
+ }
296
+ for vote in votes
297
+ ]
298
+
299
+ json_file_path = JSON_DATASET_DIR / "votes.json"
300
+ # Upload to Hugging Face
301
+ with scheduler.lock:
302
+ with json_file_path.open("w") as f:
303
+ json.dump(json_data, f, indent=4)
304
+
305
+ logging.info("Database dumped to JSON")
306
+
307
+ def schedule_dump_database(interval=60):
308
+ """Schedule the database dump to JSON every specified interval in seconds."""
309
+ def run():
310
+ while True:
311
+ logging.info("Starting database dump to JSON.")
312
+ dump_database_to_json()
313
+ logging.info("Database dump completed. Sleeping for %d seconds.", interval)
314
+ time.sleep(interval)
315
+
316
+ logging.info("Initializing database dump scheduler with interval: %d seconds.", interval)
317
+ thread = threading.Thread(target=run, daemon=True)
318
+ thread.start()
319
+ logging.info("Database dump scheduler started.")
320
+
321
  if __name__ == "__main__":
322
+ schedule_dump_database() # Start the periodic database dump
323
  demo = gradio_interface()
324
  demo.launch()
requirements.txt CHANGED
@@ -8,4 +8,5 @@ python-dotenv==1.0.1
8
  requests==2.32.3
9
  SQLAlchemy==2.0.36
10
  uvicorn==0.30.1
11
- py-spy
 
 
8
  requests==2.32.3
9
  SQLAlchemy==2.0.36
10
  uvicorn==0.30.1
11
+ py-spy
12
+ pandas