RaghavenderReddy commited on
Commit
44f5bca
·
verified ·
1 Parent(s): dd5f348

Update utils.py

Browse files
Files changed (1) hide show
  1. utils.py +36 -18
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
- try:
13
- with open('data/users.json', 'r') as f:
14
- return json.load(f)
15
- except (FileNotFoundError, json.JSONDecodeError):
16
- return {}
 
 
17
 
18
  def save_users(users):
19
- """Save users to JSON file"""
20
- with open('data/users.json', 'w') as f:
21
- json.dump(users, f, indent=2)
 
 
 
 
22
 
23
  def load_labels():
24
- """Load labels from JSON file"""
25
- try:
26
- with open('data/labels.json', 'r') as f:
27
- return json.load(f)
28
- except (FileNotFoundError, json.JSONDecodeError):
29
- return {}
 
 
30
 
31
  def save_labels(labels):
32
- """Save labels to JSON file"""
33
- with open('data/labels.json', 'w') as f:
34
- json.dump(labels, f, indent=2)
 
 
 
 
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"""