Spaces:
Running
Running
Update auth.py
Browse files
auth.py
CHANGED
|
@@ -24,10 +24,15 @@ def register_user(username, password, logged_in_username, auth_globals):
|
|
| 24 |
except:
|
| 25 |
pass
|
| 26 |
|
|
|
|
| 27 |
user_data = {
|
| 28 |
"password": generate_password_hash(password),
|
| 29 |
-
"
|
|
|
|
|
|
|
|
|
|
| 30 |
}
|
|
|
|
| 31 |
hf_api.upload_file(
|
| 32 |
path_or_fileobj=json.dumps(user_data).encode('utf-8'),
|
| 33 |
path_in_repo=f"{username}.json",
|
|
@@ -35,8 +40,9 @@ def register_user(username, password, logged_in_username, auth_globals):
|
|
| 35 |
repo_type="dataset",
|
| 36 |
token=hf_token
|
| 37 |
)
|
| 38 |
-
|
| 39 |
return username, "Registration successful! Logged in.", gr.update(visible=True)
|
|
|
|
| 40 |
except Exception as e:
|
| 41 |
return logged_in_username, f"Registration failed: {str(e)}", gr.update(visible=bool(logged_in_username))
|
| 42 |
|
|
@@ -46,7 +52,7 @@ def login_user(username, password, logged_in_username, auth_globals):
|
|
| 46 |
hf_api = HfApi(token=hf_token)
|
| 47 |
|
| 48 |
if not username or not password:
|
| 49 |
-
return [], logged_in_username, "Please enter username and password.", gr.update(visible=bool(logged_in_username))
|
| 50 |
|
| 51 |
try:
|
| 52 |
user_file = hf_hub_download(
|
|
@@ -55,24 +61,34 @@ def login_user(username, password, logged_in_username, auth_globals):
|
|
| 55 |
repo_type="dataset",
|
| 56 |
token=hf_token
|
| 57 |
)
|
|
|
|
| 58 |
with open(user_file, 'r') as f:
|
| 59 |
data = json.load(f)
|
|
|
|
| 60 |
if check_password_hash(data["password"], password):
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
else:
|
| 63 |
-
return [], logged_in_username, "Incorrect password.", gr.update(visible=bool(logged_in_username))
|
|
|
|
| 64 |
except Exception as e:
|
| 65 |
-
return [], logged_in_username, f"Login failed: {str(e)}", gr.update(visible=bool(logged_in_username))
|
| 66 |
|
| 67 |
def logout_user():
|
| 68 |
-
return [], None, "Logged out successfully!", gr.update(visible=False)
|
| 69 |
|
| 70 |
-
def save_history(username, history, auth_globals):
|
| 71 |
if not username:
|
| 72 |
return "No user logged in!"
|
|
|
|
| 73 |
hf_token = auth_globals["hf_token"]
|
| 74 |
user_repo = auth_globals["user_repo"]
|
| 75 |
hf_api = HfApi(token=hf_token)
|
|
|
|
| 76 |
try:
|
| 77 |
user_file = hf_hub_download(
|
| 78 |
repo_id=user_repo,
|
|
@@ -80,9 +96,12 @@ def save_history(username, history, auth_globals):
|
|
| 80 |
repo_type="dataset",
|
| 81 |
token=hf_token
|
| 82 |
)
|
|
|
|
| 83 |
with open(user_file, 'r') as f:
|
| 84 |
data = json.load(f)
|
| 85 |
-
|
|
|
|
|
|
|
| 86 |
hf_api.upload_file(
|
| 87 |
path_or_fileobj=json.dumps(data).encode('utf-8'),
|
| 88 |
path_in_repo=f"{username}.json",
|
|
@@ -92,4 +111,102 @@ def save_history(username, history, auth_globals):
|
|
| 92 |
)
|
| 93 |
return "History saved!"
|
| 94 |
except Exception as e:
|
| 95 |
-
return f"Failed to save history: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
except:
|
| 25 |
pass
|
| 26 |
|
| 27 |
+
# Create default conversation
|
| 28 |
user_data = {
|
| 29 |
"password": generate_password_hash(password),
|
| 30 |
+
"conversations": {
|
| 31 |
+
"default": []
|
| 32 |
+
},
|
| 33 |
+
"current_conversation": "default"
|
| 34 |
}
|
| 35 |
+
|
| 36 |
hf_api.upload_file(
|
| 37 |
path_or_fileobj=json.dumps(user_data).encode('utf-8'),
|
| 38 |
path_in_repo=f"{username}.json",
|
|
|
|
| 40 |
repo_type="dataset",
|
| 41 |
token=hf_token
|
| 42 |
)
|
| 43 |
+
|
| 44 |
return username, "Registration successful! Logged in.", gr.update(visible=True)
|
| 45 |
+
|
| 46 |
except Exception as e:
|
| 47 |
return logged_in_username, f"Registration failed: {str(e)}", gr.update(visible=bool(logged_in_username))
|
| 48 |
|
|
|
|
| 52 |
hf_api = HfApi(token=hf_token)
|
| 53 |
|
| 54 |
if not username or not password:
|
| 55 |
+
return [], "default", logged_in_username, "Please enter username and password.", gr.update(visible=bool(logged_in_username))
|
| 56 |
|
| 57 |
try:
|
| 58 |
user_file = hf_hub_download(
|
|
|
|
| 61 |
repo_type="dataset",
|
| 62 |
token=hf_token
|
| 63 |
)
|
| 64 |
+
|
| 65 |
with open(user_file, 'r') as f:
|
| 66 |
data = json.load(f)
|
| 67 |
+
|
| 68 |
if check_password_hash(data["password"], password):
|
| 69 |
+
current_conv = data.get("current_conversation", "default")
|
| 70 |
+
return (data["conversations"].get(current_conv, []),
|
| 71 |
+
current_conv,
|
| 72 |
+
username,
|
| 73 |
+
"Login successful!",
|
| 74 |
+
gr.update(visible=True))
|
| 75 |
else:
|
| 76 |
+
return [], "default", logged_in_username, "Incorrect password.", gr.update(visible=bool(logged_in_username))
|
| 77 |
+
|
| 78 |
except Exception as e:
|
| 79 |
+
return [], "default", logged_in_username, f"Login failed: {str(e)}", gr.update(visible=bool(logged_in_username))
|
| 80 |
|
| 81 |
def logout_user():
|
| 82 |
+
return [], "default", None, "Logged out successfully!", gr.update(visible=False)
|
| 83 |
|
| 84 |
+
def save_history(username, history, current_conversation, auth_globals):
|
| 85 |
if not username:
|
| 86 |
return "No user logged in!"
|
| 87 |
+
|
| 88 |
hf_token = auth_globals["hf_token"]
|
| 89 |
user_repo = auth_globals["user_repo"]
|
| 90 |
hf_api = HfApi(token=hf_token)
|
| 91 |
+
|
| 92 |
try:
|
| 93 |
user_file = hf_hub_download(
|
| 94 |
repo_id=user_repo,
|
|
|
|
| 96 |
repo_type="dataset",
|
| 97 |
token=hf_token
|
| 98 |
)
|
| 99 |
+
|
| 100 |
with open(user_file, 'r') as f:
|
| 101 |
data = json.load(f)
|
| 102 |
+
|
| 103 |
+
data["conversations"][current_conversation] = history
|
| 104 |
+
|
| 105 |
hf_api.upload_file(
|
| 106 |
path_or_fileobj=json.dumps(data).encode('utf-8'),
|
| 107 |
path_in_repo=f"{username}.json",
|
|
|
|
| 111 |
)
|
| 112 |
return "History saved!"
|
| 113 |
except Exception as e:
|
| 114 |
+
return f"Failed to save history: {str(e)}"
|
| 115 |
+
|
| 116 |
+
def create_new_conversation(username, current_conversation, new_conversation_name, auth_globals):
|
| 117 |
+
if not username:
|
| 118 |
+
return current_conversation, "No user logged in!", []
|
| 119 |
+
|
| 120 |
+
hf_token = auth_globals["hf_token"]
|
| 121 |
+
user_repo = auth_globals["user_repo"]
|
| 122 |
+
hf_api = HfApi(token=hf_token)
|
| 123 |
+
|
| 124 |
+
try:
|
| 125 |
+
user_file = hf_hub_download(
|
| 126 |
+
repo_id=user_repo,
|
| 127 |
+
filename=f"{username}.json",
|
| 128 |
+
repo_type="dataset",
|
| 129 |
+
token=hf_token
|
| 130 |
+
)
|
| 131 |
+
|
| 132 |
+
with open(user_file, 'r') as f:
|
| 133 |
+
data = json.load(f)
|
| 134 |
+
|
| 135 |
+
if new_conversation_name in data["conversations"]:
|
| 136 |
+
return current_conversation, f"Conversation '{new_conversation_name}' already exists!", data["conversations"][current_conversation]
|
| 137 |
+
|
| 138 |
+
data["conversations"][new_conversation_name] = []
|
| 139 |
+
data["current_conversation"] = new_conversation_name
|
| 140 |
+
|
| 141 |
+
hf_api.upload_file(
|
| 142 |
+
path_or_fileobj=json.dumps(data).encode('utf-8'),
|
| 143 |
+
path_in_repo=f"{username}.json",
|
| 144 |
+
repo_id=user_repo,
|
| 145 |
+
repo_type="dataset",
|
| 146 |
+
token=hf_token
|
| 147 |
+
)
|
| 148 |
+
|
| 149 |
+
return new_conversation_name, f"Created new conversation: {new_conversation_name}", []
|
| 150 |
+
|
| 151 |
+
except Exception as e:
|
| 152 |
+
return current_conversation, f"Failed to create conversation: {str(e)}", data["conversations"][current_conversation]
|
| 153 |
+
|
| 154 |
+
def switch_conversation(username, current_conversation, target_conversation, auth_globals):
|
| 155 |
+
if not username:
|
| 156 |
+
return current_conversation, "No user logged in!", []
|
| 157 |
+
|
| 158 |
+
hf_token = auth_globals["hf_token"]
|
| 159 |
+
user_repo = auth_globals["user_repo"]
|
| 160 |
+
hf_api = HfApi(token=hf_token)
|
| 161 |
+
|
| 162 |
+
try:
|
| 163 |
+
user_file = hf_hub_download(
|
| 164 |
+
repo_id=user_repo,
|
| 165 |
+
filename=f"{username}.json",
|
| 166 |
+
repo_type="dataset",
|
| 167 |
+
token=hf_token
|
| 168 |
+
)
|
| 169 |
+
|
| 170 |
+
with open(user_file, 'r') as f:
|
| 171 |
+
data = json.load(f)
|
| 172 |
+
|
| 173 |
+
if target_conversation not in data["conversations"]:
|
| 174 |
+
return current_conversation, f"Conversation '{target_conversation}' doesn't exist!", data["conversations"][current_conversation]
|
| 175 |
+
|
| 176 |
+
data["current_conversation"] = target_conversation
|
| 177 |
+
|
| 178 |
+
hf_api.upload_file(
|
| 179 |
+
path_or_fileobj=json.dumps(data).encode('utf-8'),
|
| 180 |
+
path_in_repo=f"{username}.json",
|
| 181 |
+
repo_id=user_repo,
|
| 182 |
+
repo_type="dataset",
|
| 183 |
+
token=hf_token
|
| 184 |
+
)
|
| 185 |
+
|
| 186 |
+
return target_conversation, f"Switched to conversation: {target_conversation}", data["conversations"][target_conversation]
|
| 187 |
+
|
| 188 |
+
except Exception as e:
|
| 189 |
+
return current_conversation, f"Failed to switch conversation: {str(e)}", data["conversations"][current_conversation]
|
| 190 |
+
|
| 191 |
+
def get_conversation_list(username, auth_globals):
|
| 192 |
+
if not username:
|
| 193 |
+
return []
|
| 194 |
+
|
| 195 |
+
hf_token = auth_globals["hf_token"]
|
| 196 |
+
user_repo = auth_globals["user_repo"]
|
| 197 |
+
|
| 198 |
+
try:
|
| 199 |
+
user_file = hf_hub_download(
|
| 200 |
+
repo_id=user_repo,
|
| 201 |
+
filename=f"{username}.json",
|
| 202 |
+
repo_type="dataset",
|
| 203 |
+
token=hf_token
|
| 204 |
+
)
|
| 205 |
+
|
| 206 |
+
with open(user_file, 'r') as f:
|
| 207 |
+
data = json.load(f)
|
| 208 |
+
|
| 209 |
+
return list(data["conversations"].keys())
|
| 210 |
+
|
| 211 |
+
except Exception:
|
| 212 |
+
return []
|