Spaces:
Sleeping
Sleeping
Update users_management.py
Browse files- users_management.py +19 -24
users_management.py
CHANGED
|
@@ -4,8 +4,8 @@ from hashlib import sha256
|
|
| 4 |
import gradio as gr
|
| 5 |
|
| 6 |
|
| 7 |
-
def save_user_data_to_json(
|
| 8 |
-
|
| 9 |
# Load existing data
|
| 10 |
if os.path.exists(file_path):
|
| 11 |
with open(file_path, 'r') as file:
|
|
@@ -16,14 +16,8 @@ def save_user_data_to_json(username, file_path='users.json'):
|
|
| 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] =
|
| 27 |
|
| 28 |
# Save updated data back to file
|
| 29 |
with open(file_path, 'w') as file:
|
|
@@ -49,35 +43,36 @@ def load_from_json(file_path='users.json'):
|
|
| 49 |
return {}
|
| 50 |
|
| 51 |
|
| 52 |
-
def add_user_pref(
|
| 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(
|
| 76 |
-
add_user_pref(
|
| 77 |
-
save_user_data_to_json(
|
| 78 |
-
|
| 79 |
-
return gr.update(choices=user['history']['prompts']), gr.update(choices=user['history']['keywords'])
|
| 80 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
def auth_user(username, password):
|
| 83 |
if username in users and sha256(password.encode()).hexdigest() == users[username]['hashed_password']:
|
|
|
|
| 4 |
import gradio as gr
|
| 5 |
|
| 6 |
|
| 7 |
+
def save_user_data_to_json(user, file_path='users.json'):
|
| 8 |
+
username = user['name']
|
| 9 |
# Load existing data
|
| 10 |
if os.path.exists(file_path):
|
| 11 |
with open(file_path, 'r') as file:
|
|
|
|
| 16 |
else:
|
| 17 |
existing_data = {}
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
existing_data[username] = user
|
| 21 |
|
| 22 |
# Save updated data back to file
|
| 23 |
with open(file_path, 'w') as file:
|
|
|
|
| 43 |
return {}
|
| 44 |
|
| 45 |
|
| 46 |
+
def add_user_pref(user, input_value, input_type='keywords'):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
|
|
|
| 48 |
if not user['history']:
|
| 49 |
user['history'] = {"keywords": [], "prompts": []}
|
| 50 |
|
|
|
|
| 51 |
for word in input_value:
|
| 52 |
if word not in user['history'][input_type]:
|
| 53 |
user['history'][input_type].append(word)
|
| 54 |
+
return user
|
| 55 |
|
| 56 |
|
| 57 |
def update_json(user, prompt, keywords):
|
|
|
|
| 58 |
username = user['name']
|
| 59 |
+
users = load_from_json()
|
| 60 |
+
user = users.get(username)
|
| 61 |
+
|
| 62 |
+
print(f"TYPE PROMPT = {type(prompt)}")
|
| 63 |
+
|
| 64 |
|
| 65 |
if username != 'Guest':
|
| 66 |
+
user = add_user_pref(user, prompt, input_type='prompts')
|
| 67 |
+
user = add_user_pref(user, keywords, input_type='keywords')
|
| 68 |
+
save_user_data_to_json(user)
|
|
|
|
|
|
|
| 69 |
|
| 70 |
+
# with open('users.json', 'r') as file:
|
| 71 |
+
# saved_data = json.load(file)
|
| 72 |
+
|
| 73 |
+
users = load_from_json()
|
| 74 |
+
user = users.get(username)
|
| 75 |
+
return gr.update(choices=user['history']['prompts']), gr.update(choices=user['history']['keywords']), user
|
| 76 |
|
| 77 |
def auth_user(username, password):
|
| 78 |
if username in users and sha256(password.encode()).hexdigest() == users[username]['hashed_password']:
|