YchKhan commited on
Commit
bf66de5
1 Parent(s): 52106fc

Create users_management.py

Browse files
Files changed (1) hide show
  1. users_management.py +93 -0
users_management.py ADDED
@@ -0,0 +1,93 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import os
3
+
4
+ def save_user_data_to_json(username, file_path='users.json'):
5
+ user = users[username]
6
+ # Load existing data
7
+ if os.path.exists(file_path):
8
+ with open(file_path, 'r') as file:
9
+ try:
10
+ existing_data = json.load(file)
11
+ except json.JSONDecodeError:
12
+ existing_data = {}
13
+ else:
14
+ existing_data = {}
15
+
16
+ # Update or add the user's data, including hashed password
17
+ user_data = {
18
+ "name": username,
19
+ "hashed_password": user['hashed_password'],
20
+ "history": user['history']
21
+ }
22
+
23
+ existing_data[username] = user_data
24
+
25
+ # Save updated data back to file
26
+ with open(file_path, 'w') as file:
27
+ json.dump(existing_data, file, indent=4)
28
+
29
+ def load_from_json(file_path='users.json'):
30
+ """
31
+ Load user data from a JSON file and return it as a dictionary.
32
+
33
+ :param file_path: The path to the JSON file from which to load user data.
34
+ :return: A dictionary containing the user data loaded from the JSON file.
35
+ """
36
+ if os.path.exists(file_path):
37
+ with open(file_path, 'r') as file:
38
+ try:
39
+ user_data = json.load(file)
40
+ return user_data
41
+ except json.JSONDecodeError as e:
42
+ print(f"Error decoding JSON from {file_path}: {e}")
43
+ return {}
44
+ else:
45
+ print(f"File {file_path} not found.")
46
+ return {}
47
+
48
+
49
+ def add_user_pref(username, input_type, input_value, users):
50
+ # Ensure the user exists
51
+ user = users[username]
52
+
53
+ if username not in users:
54
+ print(f"User {username} not found in {users}")
55
+ return
56
+
57
+ # Initialize the history as a list of dictionaries if empty
58
+ if not user['history']:
59
+ user['history'] = {"keywords": [], "prompts": []}
60
+
61
+ # Add the input value to the corresponding list
62
+ for word in input_value:
63
+ if word not in user['history'][input_type]:
64
+ user['history'][input_type].append(word)
65
+
66
+
67
+ def update_json(user, values, type='keywords'):
68
+ # users = load_from_json()
69
+ username = user['name']
70
+
71
+ if username != 'Guest':
72
+ add_user_pref(username, type, values, users)
73
+ save_user_data_to_json(username)
74
+
75
+ with open('users.json', 'r') as file:
76
+ saved_data = json.load(file)
77
+
78
+
79
+ def auth_user(username, password):
80
+ if username in users and sha256(password.encode()).hexdigest() == users[username]['hashed_password']:
81
+ #mistral_api_key = os.environ[username]
82
+ user = users.get(username)
83
+ else:
84
+ username = 'Guest'
85
+ user = users.get(username)
86
+ return user, f"## Hi {username}!", gr.update(choices=user['history']['keywords']) , gr.update(choices=user['history']['prompts'])
87
+
88
+ def logout():
89
+ username = 'Guest'
90
+ user = users.get(username)
91
+ return user, f"## Hi {username}!", gr.update(choices=user['history']['keywords']) , gr.update(choices=user['history']['prompts'])
92
+
93
+