Spaces:
Sleeping
Sleeping
Update utils.py
Browse files
utils.py
CHANGED
|
@@ -7,31 +7,49 @@ import os
|
|
| 7 |
from PIL import Image
|
| 8 |
from collections import Counter
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
def load_users():
|
| 11 |
-
"""Load users from JSON file"""
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
| 17 |
|
| 18 |
def save_users(users):
|
| 19 |
-
"""Save users to JSON file"""
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
def load_labels():
|
| 24 |
-
"""Load labels from JSON file"""
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
| 30 |
|
| 31 |
def save_labels(labels):
|
| 32 |
-
"""Save labels to JSON file"""
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
def validate_image(uploaded_file):
|
| 37 |
"""Validate uploaded image file"""
|
|
|
|
| 7 |
from PIL import Image
|
| 8 |
from collections import Counter
|
| 9 |
|
| 10 |
+
# File paths for data storage (compatible with pre-existing folders)
|
| 11 |
+
DATA_DIR = 'data'
|
| 12 |
+
USERS_FILE = os.path.join(DATA_DIR, 'users.json')
|
| 13 |
+
LABELS_FILE = os.path.join(DATA_DIR, 'labels.json')
|
| 14 |
+
IMAGES_DIR = os.path.join(DATA_DIR, 'images')
|
| 15 |
+
|
| 16 |
def load_users():
|
| 17 |
+
"""Load users from JSON file with error handling for pre-existing files"""
|
| 18 |
+
if os.path.exists(USERS_FILE):
|
| 19 |
+
try:
|
| 20 |
+
with open(USERS_FILE, 'r') as f:
|
| 21 |
+
return json.load(f)
|
| 22 |
+
except (FileNotFoundError, json.JSONDecodeError, PermissionError):
|
| 23 |
+
pass
|
| 24 |
+
return {}
|
| 25 |
|
| 26 |
def save_users(users):
|
| 27 |
+
"""Save users to JSON file with error handling for read-only environments"""
|
| 28 |
+
if os.path.exists(DATA_DIR):
|
| 29 |
+
try:
|
| 30 |
+
with open(USERS_FILE, 'w') as f:
|
| 31 |
+
json.dump(users, f, indent=2)
|
| 32 |
+
except (OSError, PermissionError):
|
| 33 |
+
pass
|
| 34 |
|
| 35 |
def load_labels():
|
| 36 |
+
"""Load labels from JSON file with error handling for pre-existing files"""
|
| 37 |
+
if os.path.exists(LABELS_FILE):
|
| 38 |
+
try:
|
| 39 |
+
with open(LABELS_FILE, 'r') as f:
|
| 40 |
+
return json.load(f)
|
| 41 |
+
except (FileNotFoundError, json.JSONDecodeError, PermissionError):
|
| 42 |
+
pass
|
| 43 |
+
return {}
|
| 44 |
|
| 45 |
def save_labels(labels):
|
| 46 |
+
"""Save labels to JSON file with error handling for read-only environments"""
|
| 47 |
+
if os.path.exists(DATA_DIR):
|
| 48 |
+
try:
|
| 49 |
+
with open(LABELS_FILE, 'w') as f:
|
| 50 |
+
json.dump(labels, f, indent=2)
|
| 51 |
+
except (OSError, PermissionError):
|
| 52 |
+
pass
|
| 53 |
|
| 54 |
def validate_image(uploaded_file):
|
| 55 |
"""Validate uploaded image file"""
|