Spaces:
Running
Running
Update auth.py
Browse files
auth.py
CHANGED
|
@@ -2,7 +2,6 @@ import json
|
|
| 2 |
from huggingface_hub import HfApi, hf_hub_download
|
| 3 |
from werkzeug.security import generate_password_hash, check_password_hash
|
| 4 |
import gradio as gr
|
| 5 |
-
from datetime import datetime
|
| 6 |
|
| 7 |
def register_user(username, password, logged_in_username, auth_globals):
|
| 8 |
hf_token = auth_globals["hf_token"]
|
|
@@ -10,7 +9,7 @@ def register_user(username, password, logged_in_username, auth_globals):
|
|
| 10 |
hf_api = HfApi(token=hf_token)
|
| 11 |
|
| 12 |
if not username or not password:
|
| 13 |
-
return logged_in_username, "Please enter username and password.", gr.update(visible=bool(logged_in_username))
|
| 14 |
|
| 15 |
try:
|
| 16 |
# Check if user exists
|
|
@@ -21,19 +20,14 @@ def register_user(username, password, logged_in_username, auth_globals):
|
|
| 21 |
repo_type="dataset",
|
| 22 |
token=hf_token
|
| 23 |
)
|
| 24 |
-
return logged_in_username, "User already exists!", gr.update(visible=bool(logged_in_username))
|
| 25 |
except:
|
| 26 |
pass
|
| 27 |
|
| 28 |
-
# Create default chat
|
| 29 |
user_data = {
|
| 30 |
"password": generate_password_hash(password),
|
| 31 |
-
"
|
| 32 |
-
"default": []
|
| 33 |
-
},
|
| 34 |
-
"current_chat": "default"
|
| 35 |
}
|
| 36 |
-
|
| 37 |
hf_api.upload_file(
|
| 38 |
path_or_fileobj=json.dumps(user_data).encode('utf-8'),
|
| 39 |
path_in_repo=f"{username}.json",
|
|
@@ -41,11 +35,10 @@ def register_user(username, password, logged_in_username, auth_globals):
|
|
| 41 |
repo_type="dataset",
|
| 42 |
token=hf_token
|
| 43 |
)
|
| 44 |
-
|
| 45 |
-
return username, "Registration successful! Logged in.", gr.update(visible=True)
|
| 46 |
-
|
| 47 |
except Exception as e:
|
| 48 |
-
return logged_in_username, f"Registration failed: {str(e)}", gr.update(visible=bool(logged_in_username))
|
| 49 |
|
| 50 |
def login_user(username, password, logged_in_username, auth_globals):
|
| 51 |
hf_token = auth_globals["hf_token"]
|
|
@@ -53,7 +46,7 @@ def login_user(username, password, logged_in_username, auth_globals):
|
|
| 53 |
hf_api = HfApi(token=hf_token)
|
| 54 |
|
| 55 |
if not username or not password:
|
| 56 |
-
return [],
|
| 57 |
|
| 58 |
try:
|
| 59 |
user_file = hf_hub_download(
|
|
@@ -62,39 +55,24 @@ def login_user(username, password, logged_in_username, auth_globals):
|
|
| 62 |
repo_type="dataset",
|
| 63 |
token=hf_token
|
| 64 |
)
|
| 65 |
-
|
| 66 |
with open(user_file, 'r') as f:
|
| 67 |
data = json.load(f)
|
| 68 |
-
|
| 69 |
if check_password_hash(data["password"], password):
|
| 70 |
-
|
| 71 |
-
chats = list(data["chats"].keys())
|
| 72 |
-
return (
|
| 73 |
-
data["chats"].get(current_chat, []),
|
| 74 |
-
current_chat,
|
| 75 |
-
username,
|
| 76 |
-
"Login successful!",
|
| 77 |
-
gr.update(visible=True),
|
| 78 |
-
gr.update(choices=chats, visible=True),
|
| 79 |
-
gr.update(choices=chats, visible=True)
|
| 80 |
-
)
|
| 81 |
else:
|
| 82 |
-
return [],
|
| 83 |
-
|
| 84 |
except Exception as e:
|
| 85 |
-
return [],
|
| 86 |
|
| 87 |
def logout_user():
|
| 88 |
-
return [],
|
| 89 |
|
| 90 |
-
def save_history(username, history,
|
| 91 |
if not username:
|
| 92 |
return "No user logged in!"
|
| 93 |
-
|
| 94 |
hf_token = auth_globals["hf_token"]
|
| 95 |
user_repo = auth_globals["user_repo"]
|
| 96 |
hf_api = HfApi(token=hf_token)
|
| 97 |
-
|
| 98 |
try:
|
| 99 |
user_file = hf_hub_download(
|
| 100 |
repo_id=user_repo,
|
|
@@ -102,12 +80,9 @@ def save_history(username, history, current_chat, auth_globals):
|
|
| 102 |
repo_type="dataset",
|
| 103 |
token=hf_token
|
| 104 |
)
|
| 105 |
-
|
| 106 |
with open(user_file, 'r') as f:
|
| 107 |
data = json.load(f)
|
| 108 |
-
|
| 109 |
-
data["chats"][current_chat] = history
|
| 110 |
-
|
| 111 |
hf_api.upload_file(
|
| 112 |
path_or_fileobj=json.dumps(data).encode('utf-8'),
|
| 113 |
path_in_repo=f"{username}.json",
|
|
@@ -117,105 +92,4 @@ def save_history(username, history, current_chat, auth_globals):
|
|
| 117 |
)
|
| 118 |
return "History saved!"
|
| 119 |
except Exception as e:
|
| 120 |
-
return f"Failed to save history: {str(e)}"
|
| 121 |
-
|
| 122 |
-
def create_new_chat(username, current_chat, new_chat_name, auth_globals):
|
| 123 |
-
if not username:
|
| 124 |
-
return current_chat, "No user logged in!", []
|
| 125 |
-
|
| 126 |
-
hf_token = auth_globals["hf_token"]
|
| 127 |
-
user_repo = auth_globals["user_repo"]
|
| 128 |
-
hf_api = HfApi(token=hf_token)
|
| 129 |
-
|
| 130 |
-
try:
|
| 131 |
-
user_file = hf_hub_download(
|
| 132 |
-
repo_id=user_repo,
|
| 133 |
-
filename=f"{username}.json",
|
| 134 |
-
repo_type="dataset",
|
| 135 |
-
token=hf_token
|
| 136 |
-
)
|
| 137 |
-
|
| 138 |
-
with open(user_file, 'r') as f:
|
| 139 |
-
data = json.load(f)
|
| 140 |
-
|
| 141 |
-
if not new_chat_name:
|
| 142 |
-
new_chat_name = f"Chat {datetime.now().strftime('%Y-%m-%d %H:%M')}"
|
| 143 |
-
|
| 144 |
-
if new_chat_name in data["chats"]:
|
| 145 |
-
return current_chat, f"Chat '{new_chat_name}' already exists!", data["chats"][current_chat]
|
| 146 |
-
|
| 147 |
-
data["chats"][new_chat_name] = []
|
| 148 |
-
data["current_chat"] = new_chat_name
|
| 149 |
-
|
| 150 |
-
hf_api.upload_file(
|
| 151 |
-
path_or_fileobj=json.dumps(data).encode('utf-8'),
|
| 152 |
-
path_in_repo=f"{username}.json",
|
| 153 |
-
repo_id=user_repo,
|
| 154 |
-
repo_type="dataset",
|
| 155 |
-
token=hf_token
|
| 156 |
-
)
|
| 157 |
-
|
| 158 |
-
return new_chat_name, f"Created new chat: {new_chat_name}", []
|
| 159 |
-
|
| 160 |
-
except Exception as e:
|
| 161 |
-
return current_chat, f"Failed to create chat: {str(e)}", data["chats"][current_chat]
|
| 162 |
-
|
| 163 |
-
def switch_chat(username, current_chat, target_chat, auth_globals):
|
| 164 |
-
if not username:
|
| 165 |
-
return current_chat, "No user logged in!", []
|
| 166 |
-
|
| 167 |
-
hf_token = auth_globals["hf_token"]
|
| 168 |
-
user_repo = auth_globals["user_repo"]
|
| 169 |
-
hf_api = HfApi(token=hf_token)
|
| 170 |
-
|
| 171 |
-
try:
|
| 172 |
-
user_file = hf_hub_download(
|
| 173 |
-
repo_id=user_repo,
|
| 174 |
-
filename=f"{username}.json",
|
| 175 |
-
repo_type="dataset",
|
| 176 |
-
token=hf_token
|
| 177 |
-
)
|
| 178 |
-
|
| 179 |
-
with open(user_file, 'r') as f:
|
| 180 |
-
data = json.load(f)
|
| 181 |
-
|
| 182 |
-
if target_chat not in data["chats"]:
|
| 183 |
-
return current_chat, f"Chat '{target_chat}' doesn't exist!", data["chats"][current_chat]
|
| 184 |
-
|
| 185 |
-
data["current_chat"] = target_chat
|
| 186 |
-
|
| 187 |
-
hf_api.upload_file(
|
| 188 |
-
path_or_fileobj=json.dumps(data).encode('utf-8'),
|
| 189 |
-
path_in_repo=f"{username}.json",
|
| 190 |
-
repo_id=user_repo,
|
| 191 |
-
repo_type="dataset",
|
| 192 |
-
token=hf_token
|
| 193 |
-
)
|
| 194 |
-
|
| 195 |
-
return target_chat, f"Switched to chat: {target_chat}", data["chats"][target_chat]
|
| 196 |
-
|
| 197 |
-
except Exception as e:
|
| 198 |
-
return current_chat, f"Failed to switch chat: {str(e)}", data["chats"][current_chat]
|
| 199 |
-
|
| 200 |
-
def get_chat_list(username, auth_globals):
|
| 201 |
-
if not username:
|
| 202 |
-
return []
|
| 203 |
-
|
| 204 |
-
hf_token = auth_globals["hf_token"]
|
| 205 |
-
user_repo = auth_globals["user_repo"]
|
| 206 |
-
|
| 207 |
-
try:
|
| 208 |
-
user_file = hf_hub_download(
|
| 209 |
-
repo_id=user_repo,
|
| 210 |
-
filename=f"{username}.json",
|
| 211 |
-
repo_type="dataset",
|
| 212 |
-
token=hf_token
|
| 213 |
-
)
|
| 214 |
-
|
| 215 |
-
with open(user_file, 'r') as f:
|
| 216 |
-
data = json.load(f)
|
| 217 |
-
|
| 218 |
-
return list(data["chats"].keys())
|
| 219 |
-
|
| 220 |
-
except Exception:
|
| 221 |
-
return []
|
|
|
|
| 2 |
from huggingface_hub import HfApi, hf_hub_download
|
| 3 |
from werkzeug.security import generate_password_hash, check_password_hash
|
| 4 |
import gradio as gr
|
|
|
|
| 5 |
|
| 6 |
def register_user(username, password, logged_in_username, auth_globals):
|
| 7 |
hf_token = auth_globals["hf_token"]
|
|
|
|
| 9 |
hf_api = HfApi(token=hf_token)
|
| 10 |
|
| 11 |
if not username or not password:
|
| 12 |
+
return logged_in_username, "Please enter username and password.", gr.update(visible=bool(logged_in_username))
|
| 13 |
|
| 14 |
try:
|
| 15 |
# Check if user exists
|
|
|
|
| 20 |
repo_type="dataset",
|
| 21 |
token=hf_token
|
| 22 |
)
|
| 23 |
+
return logged_in_username, "User already exists!", gr.update(visible=bool(logged_in_username))
|
| 24 |
except:
|
| 25 |
pass
|
| 26 |
|
|
|
|
| 27 |
user_data = {
|
| 28 |
"password": generate_password_hash(password),
|
| 29 |
+
"history": []
|
|
|
|
|
|
|
|
|
|
| 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 |
repo_type="dataset",
|
| 36 |
token=hf_token
|
| 37 |
)
|
| 38 |
+
# Auto-login
|
| 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 |
|
| 43 |
def login_user(username, password, logged_in_username, auth_globals):
|
| 44 |
hf_token = auth_globals["hf_token"]
|
|
|
|
| 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 |
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 |
+
return data["history"], username, "Login successful!", gr.update(visible=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
repo_type="dataset",
|
| 81 |
token=hf_token
|
| 82 |
)
|
|
|
|
| 83 |
with open(user_file, 'r') as f:
|
| 84 |
data = json.load(f)
|
| 85 |
+
data["history"] = history
|
|
|
|
|
|
|
| 86 |
hf_api.upload_file(
|
| 87 |
path_or_fileobj=json.dumps(data).encode('utf-8'),
|
| 88 |
path_in_repo=f"{username}.json",
|
|
|
|
| 92 |
)
|
| 93 |
return "History saved!"
|
| 94 |
except Exception as e:
|
| 95 |
+
return f"Failed to save history: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|