Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,12 +1,13 @@
|
|
1 |
import gradio as gr
|
2 |
import os
|
3 |
import hashlib
|
|
|
4 |
|
5 |
# Constants and Settings
|
6 |
UPLOAD_FOLDER = "uploads"
|
7 |
PASSWORD_FILE = "password_file.txt"
|
8 |
-
ADMIN_USERNAME = "admin"
|
9 |
-
ADMIN_PASSWORD_HASH = hashlib.sha256("admin_password".encode()).hexdigest()
|
10 |
app_settings = {
|
11 |
"file_uploads_enabled": True
|
12 |
}
|
@@ -14,26 +15,39 @@ app_settings = {
|
|
14 |
# Ensure the upload directory exists
|
15 |
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
# Function to register a user
|
18 |
def register(username, password):
|
19 |
hashed_password = hashlib.sha256(password.encode()).hexdigest()
|
|
|
20 |
|
21 |
with open(PASSWORD_FILE, "a") as f:
|
22 |
-
f.write(f"{username}:{
|
23 |
|
24 |
return f"User '{username}' registered successfully!"
|
25 |
|
26 |
# Function to log in a user
|
27 |
def login(username, password):
|
28 |
hashed_password = hashlib.sha256(password.encode()).hexdigest()
|
|
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
|
|
37 |
|
38 |
return "Invalid username or password."
|
39 |
|
@@ -58,8 +72,8 @@ def get_users():
|
|
58 |
if os.path.exists(PASSWORD_FILE):
|
59 |
with open(PASSWORD_FILE, "r") as f:
|
60 |
for line in f:
|
61 |
-
username,
|
62 |
-
users[username] =
|
63 |
return users
|
64 |
|
65 |
# Gradio Interface
|
|
|
1 |
import gradio as gr
|
2 |
import os
|
3 |
import hashlib
|
4 |
+
import base64
|
5 |
|
6 |
# Constants and Settings
|
7 |
UPLOAD_FOLDER = "uploads"
|
8 |
PASSWORD_FILE = "password_file.txt"
|
9 |
+
ADMIN_USERNAME = "admin"
|
10 |
+
ADMIN_PASSWORD_HASH = hashlib.sha256("admin_password".encode()).hexdigest()
|
11 |
app_settings = {
|
12 |
"file_uploads_enabled": True
|
13 |
}
|
|
|
15 |
# Ensure the upload directory exists
|
16 |
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
17 |
|
18 |
+
# Function to encode the password in a "Very Hard Encoded Language"
|
19 |
+
def encode_password(password):
|
20 |
+
encoded_bytes = base64.b64encode(password.encode('utf-8'))
|
21 |
+
return encoded_bytes.decode('utf-8')
|
22 |
+
|
23 |
+
# Function to decode the password
|
24 |
+
def decode_password(encoded_password):
|
25 |
+
decoded_bytes = base64.b64decode(encoded_password.encode('utf-8'))
|
26 |
+
return decoded_bytes.decode('utf-8')
|
27 |
+
|
28 |
# Function to register a user
|
29 |
def register(username, password):
|
30 |
hashed_password = hashlib.sha256(password.encode()).hexdigest()
|
31 |
+
encoded_password = encode_password(hashed_password)
|
32 |
|
33 |
with open(PASSWORD_FILE, "a") as f:
|
34 |
+
f.write(f"{username}:{encoded_password}\n")
|
35 |
|
36 |
return f"User '{username}' registered successfully!"
|
37 |
|
38 |
# Function to log in a user
|
39 |
def login(username, password):
|
40 |
hashed_password = hashlib.sha256(password.encode()).hexdigest()
|
41 |
+
encoded_password = encode_password(hashed_password)
|
42 |
|
43 |
+
if os.path.exists(PASSWORD_FILE):
|
44 |
+
with open(PASSWORD_FILE, "r") as f:
|
45 |
+
users = f.readlines()
|
46 |
+
|
47 |
+
for user in users:
|
48 |
+
stored_username, stored_encoded_password = user.strip().split(":")
|
49 |
+
if stored_username == username and stored_encoded_password == encoded_password:
|
50 |
+
return "Login successful!"
|
51 |
|
52 |
return "Invalid username or password."
|
53 |
|
|
|
72 |
if os.path.exists(PASSWORD_FILE):
|
73 |
with open(PASSWORD_FILE, "r") as f:
|
74 |
for line in f:
|
75 |
+
username, encoded_password = line.strip().split(":")
|
76 |
+
users[username] = decode_password(encoded_password) # Decoding for verification
|
77 |
return users
|
78 |
|
79 |
# Gradio Interface
|