File size: 2,936 Bytes
0b860c0
 
 
 
 
 
4700455
1a292d9
0b860c0
4700455
 
0b860c0
 
 
 
 
 
 
 
1a292d9
0b860c0
 
4700455
0b860c0
 
4700455
 
0b860c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1a292d9
0b860c0
 
 
 
 
 
 
1a292d9
0b860c0
 
 
 
1a292d9
 
 
 
 
0b860c0
 
1a292d9
 
4700455
0b860c0
1a292d9
 
 
 
 
4700455
0b860c0
 
6312ae6
0b860c0
 
 
 
 
 
 
 
 
 
316e9b1
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import json
import os
from hashlib import sha256
import gradio as gr


def save_user_data_to_json(user, conf_file_path='users.json'):
    username = user['name']
    # Load existing data
    if os.path.exists(conf_file_path):
        with open(conf_file_path, 'r') as file:
            try:
                existing_data = json.load(file)
            except json.JSONDecodeError:
                existing_data = {}
    else:
        existing_data = {}

    
    existing_data[username] = user

    # Save updated data back to file
    with open(conf_file_path, 'w') as file:
        json.dump(existing_data, file, indent=4)

    return conf_file_path

def load_from_json(file_path='users.json'):
    """
    Load user data from a JSON file and return it as a dictionary.

    :param file_path: The path to the JSON file from which to load user data.
    :return: A dictionary containing the user data loaded from the JSON file.
    """
    if os.path.exists(file_path):
        with open(file_path, 'r') as file:
            try:
                user_data = json.load(file)
                return user_data
            except json.JSONDecodeError as e:
                print(f"Error decoding JSON from {file_path}: {e}")
                return {}
    else:
        print(f"File {file_path} not found.")
        return {}


def add_user_pref(user, input_value, input_type='keywords'):

    if not user['history']:
        user['history'] = {"keywords": [], "prompts": []}

    for word in input_value:
        if word not in user['history'][input_type]:
            user['history'][input_type].append(word)
    return user


def update_json(user, prompt, keywords):
    username = user['name']
    users = load_from_json()
    user = users.get(username)

    print(f"TYPE PROMPT = {type(prompt)}")
    

    if username != 'Guest':
        user = add_user_pref(user, prompt, input_type='prompts')
        user = add_user_pref(user, keywords, input_type='keywords')
        conf_file_path = save_user_data_to_json(user)

        # with open('users.json', 'r') as file:
        #     saved_data = json.load(file)

    users = load_from_json()
    user = users.get(username)
    return gr.update(choices=user['history']['prompts']), gr.update(choices=user['history']['keywords']), user, conf_file_path

def auth_user(username, password):
    users = load_from_json()
    if username in users and sha256(password.encode()).hexdigest() == users[username]['hashed_password']:
        user = users.get(username)
    else:
        username = 'Guest'
        user = users.get(username)
    return user, f"## Hi {username}!", gr.update(choices=user['history']['prompts']), gr.update(choices=user['history']['keywords'])

def logout():
    username = 'Guest'
    user = users.get(username)
    return user, f"## Hi {username}!", gr.update(choices=user['history']['prompts']), gr.update(choices=user['history']['keywords'])

users = load_from_json()