Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -5,37 +5,43 @@ from bcrypt import hashpw, gensalt, checkpw
|
|
5 |
# Path to the Excel file for user storage
|
6 |
USER_FILE = "users.xlsx"
|
7 |
|
|
|
8 |
# Load users from Excel
|
9 |
def load_users():
|
10 |
try:
|
11 |
df = pd.read_excel(USER_FILE)
|
12 |
-
return {row['Email']: row for _, row in df.iterrows()}
|
13 |
except FileNotFoundError:
|
|
|
14 |
df = pd.DataFrame(columns=["Name", "Phone", "Email", "Password"])
|
15 |
df.to_excel(USER_FILE, index=False)
|
16 |
return {}
|
17 |
|
18 |
# Save users to Excel
|
19 |
def save_users(users):
|
20 |
-
df = pd.DataFrame(
|
|
|
|
|
|
|
21 |
df.to_excel(USER_FILE, index=False)
|
22 |
|
23 |
# Signup user
|
24 |
def signup_user(name, phone, email, password):
|
25 |
users = load_users()
|
26 |
if email in users:
|
27 |
-
return
|
|
|
28 |
hashed_password = hashpw(password.encode(), gensalt()).decode()
|
29 |
-
users[email] =
|
30 |
save_users(users)
|
31 |
-
return "Signup successful! Redirecting to
|
32 |
|
33 |
# Validate login
|
34 |
def validate_login(email, password):
|
35 |
users = load_users()
|
36 |
-
if email in users and checkpw(password.encode(), users[email][
|
37 |
-
return "Login successful! Redirecting to the menu page..."
|
38 |
-
return "Invalid email or password.
|
39 |
|
40 |
# Function to load the menu data
|
41 |
def load_menu():
|
@@ -260,14 +266,10 @@ modal_and_cart_js = """
|
|
260 |
# Gradio app
|
261 |
def app():
|
262 |
with gr.Blocks() as demo:
|
263 |
-
gr.
|
264 |
-
|
265 |
-
# State variables to track the current page and active user
|
266 |
-
current_page = gr.State("signup") # Default page is Signup
|
267 |
-
active_user = gr.State(None) # No user is active initially
|
268 |
-
|
269 |
# Signup Page
|
270 |
-
with gr.Row(visible=lambda
|
271 |
gr.Markdown("### Signup Page")
|
272 |
name = gr.Textbox(label="Name")
|
273 |
phone = gr.Textbox(label="Phone Number")
|
@@ -276,16 +278,38 @@ def app():
|
|
276 |
signup_btn = gr.Button("Signup")
|
277 |
signup_message = gr.Label()
|
278 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
279 |
# Login Page
|
280 |
-
with gr.Row(visible=lambda
|
281 |
gr.Markdown("### Login Page")
|
282 |
login_email = gr.Textbox(label="Email")
|
283 |
login_password = gr.Textbox(label="Password", type="password")
|
284 |
login_btn = gr.Button("Login")
|
285 |
login_message = gr.Label()
|
286 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
287 |
# Menu Page
|
288 |
-
with gr.Row(visible=lambda
|
289 |
gr.Markdown("### Menu Page (Accessible Only After Login)")
|
290 |
|
291 |
# Adding your original code here without any changes
|
|
|
5 |
# Path to the Excel file for user storage
|
6 |
USER_FILE = "users.xlsx"
|
7 |
|
8 |
+
# Load users from Excel
|
9 |
# Load users from Excel
|
10 |
def load_users():
|
11 |
try:
|
12 |
df = pd.read_excel(USER_FILE)
|
13 |
+
return {row['Email']: row['Password'] for _, row in df.iterrows()}
|
14 |
except FileNotFoundError:
|
15 |
+
# Create the file if it doesn't exist
|
16 |
df = pd.DataFrame(columns=["Name", "Phone", "Email", "Password"])
|
17 |
df.to_excel(USER_FILE, index=False)
|
18 |
return {}
|
19 |
|
20 |
# Save users to Excel
|
21 |
def save_users(users):
|
22 |
+
df = pd.DataFrame(
|
23 |
+
[{"Name": name, "Phone": phone, "Email": email, "Password": password} for email, (name, phone, password) in users.items()],
|
24 |
+
columns=["Name", "Phone", "Email", "Password"]
|
25 |
+
)
|
26 |
df.to_excel(USER_FILE, index=False)
|
27 |
|
28 |
# Signup user
|
29 |
def signup_user(name, phone, email, password):
|
30 |
users = load_users()
|
31 |
if email in users:
|
32 |
+
return "Email already exists. Please log in."
|
33 |
+
# Hash the password before saving
|
34 |
hashed_password = hashpw(password.encode(), gensalt()).decode()
|
35 |
+
users[email] = (name, phone, hashed_password)
|
36 |
save_users(users)
|
37 |
+
return "Signup successful! Redirecting to login..."
|
38 |
|
39 |
# Validate login
|
40 |
def validate_login(email, password):
|
41 |
users = load_users()
|
42 |
+
if email in users and checkpw(password.encode(), users[email][2].encode()):
|
43 |
+
return True, "Login successful! Redirecting to the menu page..."
|
44 |
+
return False, "Invalid email or password. Please try again."
|
45 |
|
46 |
# Function to load the menu data
|
47 |
def load_menu():
|
|
|
266 |
# Gradio app
|
267 |
def app():
|
268 |
with gr.Blocks() as demo:
|
269 |
+
current_page = gr.State(value="signup") # Track current visible page
|
270 |
+
|
|
|
|
|
|
|
|
|
271 |
# Signup Page
|
272 |
+
with gr.Row(visible=lambda state: state == "signup", state=current_page):
|
273 |
gr.Markdown("### Signup Page")
|
274 |
name = gr.Textbox(label="Name")
|
275 |
phone = gr.Textbox(label="Phone Number")
|
|
|
278 |
signup_btn = gr.Button("Signup")
|
279 |
signup_message = gr.Label()
|
280 |
|
281 |
+
def handle_signup(name, phone, email, password):
|
282 |
+
message = signup_user(name, phone, email, password)
|
283 |
+
return "login", message
|
284 |
+
|
285 |
+
signup_btn.click(
|
286 |
+
handle_signup,
|
287 |
+
inputs=[name, phone, email, password],
|
288 |
+
outputs=[current_page, signup_message],
|
289 |
+
)
|
290 |
+
|
291 |
# Login Page
|
292 |
+
with gr.Row(visible=lambda state: state == "login", state=current_page):
|
293 |
gr.Markdown("### Login Page")
|
294 |
login_email = gr.Textbox(label="Email")
|
295 |
login_password = gr.Textbox(label="Password", type="password")
|
296 |
login_btn = gr.Button("Login")
|
297 |
login_message = gr.Label()
|
298 |
|
299 |
+
def handle_login(email, password):
|
300 |
+
success, message = validate_login(email, password)
|
301 |
+
if success:
|
302 |
+
return "menu", message
|
303 |
+
return "login", message
|
304 |
+
|
305 |
+
login_btn.click(
|
306 |
+
handle_login,
|
307 |
+
inputs=[login_email, login_password],
|
308 |
+
outputs=[current_page, login_message],
|
309 |
+
)
|
310 |
+
|
311 |
# Menu Page
|
312 |
+
with gr.Row(visible=lambda state: state == "menu", state=current_page):
|
313 |
gr.Markdown("### Menu Page (Accessible Only After Login)")
|
314 |
|
315 |
# Adding your original code here without any changes
|