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