nagasurendra commited on
Commit
9a22878
·
verified ·
1 Parent(s): 0b1fd91

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -30
app.py CHANGED
@@ -1,14 +1,45 @@
1
  import gradio as gr
2
  import pandas as pd
 
3
 
4
- # Placeholder functions for user validation and creation
5
- def check_credentials(email, password):
6
- return email == "[email protected]" and password == "password123"
7
 
 
8
  def save_user(name, phone, email, password):
9
- if email == "existing@example.com":
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  return False
11
- return True
 
 
 
 
 
 
 
 
 
 
 
12
  # Function to load the menu data
13
  def load_menu():
14
  menu_file = "menu.xlsx" # Ensure this file exists in the same directory
@@ -251,22 +282,22 @@ def app():
251
  # Login Page
252
  with gr.Column(visible=True) as login_section:
253
  gr.Markdown("# Login Page")
254
- email = gr.Textbox(label="Email", placeholder="Enter your email")
255
- password = gr.Textbox(label="Password", placeholder="Enter your password", type="password")
256
- error_box = gr.Markdown("", visible=False)
257
- login_btn = gr.Button("Login")
258
- create_account_btn = gr.Button("Create an Account")
259
 
260
- # Sign-Up Page
261
  with gr.Column(visible=False) as signup_section:
262
- gr.Markdown("# Sign Up Page")
263
- name = gr.Textbox(label="Name", placeholder="Enter your full name")
264
- phone = gr.Textbox(label="Phone", placeholder="Enter your phone number")
265
  signup_email = gr.Textbox(label="Email", placeholder="Enter your email")
266
- signup_password = gr.Textbox(label="Password", placeholder="Enter a password", type="password")
267
- success_message = gr.Markdown("", visible=False)
268
- submit_btn = gr.Button("Sign Up")
269
- back_to_login_btn = gr.Button("Back to Login")
270
 
271
  # Menu Page
272
  with gr.Column(visible=False) as menu_section:
@@ -327,22 +358,23 @@ def app():
327
  gr.HTML(modal_and_cart_js)
328
 
329
  # Button Bindings
330
- login_btn.click(
331
- authenticate_user,
332
- inputs=[email, password],
333
- outputs=[login_section, menu_section, error_box],
 
334
  )
335
- create_account_btn.click(
336
- navigate_to_signup,
337
  outputs=[login_section, signup_section],
338
  )
339
- submit_btn.click(
340
- create_account,
341
- inputs=[name, phone, signup_email, signup_password],
342
- outputs=[success_message, login_section, signup_section],
343
  )
344
- back_to_login_btn.click(
345
- navigate_to_login,
346
  outputs=[login_section, signup_section],
347
  )
348
 
 
1
  import gradio as gr
2
  import pandas as pd
3
+ from bcrypt import hashpw, gensalt, checkpw
4
 
5
+ # File for storing user data
6
+ USER_FILE = "users.xlsx"
 
7
 
8
+ # Utility Functions
9
  def save_user(name, phone, email, password):
10
+ """Save user details to Excel file."""
11
+ try:
12
+ # Load existing users
13
+ try:
14
+ users = pd.read_excel(USER_FILE)
15
+ except FileNotFoundError:
16
+ users = pd.DataFrame(columns=["Name", "Phone", "Email", "Password"])
17
+
18
+ # Check if email already exists
19
+ if email in users["Email"].values:
20
+ return False # User already exists
21
+
22
+ # Add new user
23
+ hashed_password = hashpw(password.encode(), gensalt()).decode()
24
+ new_user = {"Name": name, "Phone": phone, "Email": email, "Password": hashed_password}
25
+ users = pd.concat([users, pd.DataFrame([new_user])], ignore_index=True)
26
+ users.to_excel(USER_FILE, index=False)
27
+ return True
28
+ except Exception as e:
29
+ print(f"Error saving user: {e}")
30
  return False
31
+
32
+ def check_credentials(email, password):
33
+ """Check user credentials during login."""
34
+ try:
35
+ users = pd.read_excel(USER_FILE)
36
+ user = users[users["Email"] == email]
37
+ if not user.empty:
38
+ return checkpw(password.encode(), user.iloc[0]["Password"].encode())
39
+ return False
40
+ except FileNotFoundError:
41
+ return False
42
+
43
  # Function to load the menu data
44
  def load_menu():
45
  menu_file = "menu.xlsx" # Ensure this file exists in the same directory
 
282
  # Login Page
283
  with gr.Column(visible=True) as login_section:
284
  gr.Markdown("# Login Page")
285
+ login_email = gr.Textbox(label="Email", placeholder="Enter your email")
286
+ login_password = gr.Textbox(label="Password", placeholder="Enter your password", type="password")
287
+ login_error = gr.Label("")
288
+ login_button = gr.Button("Login")
289
+ go_to_signup = gr.Button("Create an Account")
290
 
291
+ # Signup Page
292
  with gr.Column(visible=False) as signup_section:
293
+ gr.Markdown("# Signup Page")
294
+ signup_name = gr.Textbox(label="Name", placeholder="Enter your full name")
295
+ signup_phone = gr.Textbox(label="Phone", placeholder="Enter your phone number")
296
  signup_email = gr.Textbox(label="Email", placeholder="Enter your email")
297
+ signup_password = gr.Textbox(label="Password", placeholder="Enter your password", type="password")
298
+ signup_message = gr.Label("")
299
+ signup_button = gr.Button("Sign Up")
300
+ go_to_login = gr.Button("Back to Login")
301
 
302
  # Menu Page
303
  with gr.Column(visible=False) as menu_section:
 
358
  gr.HTML(modal_and_cart_js)
359
 
360
  # Button Bindings
361
+ # Button Logic
362
+ login_button.click(
363
+ lambda email, password: (gr.update(visible=False), gr.update(visible=True), "") if check_credentials(email, password) else (gr.update(), gr.update(), "Invalid email or password."),
364
+ inputs=[login_email, login_password],
365
+ outputs=[login_section, menu_section, login_error],
366
  )
367
+ go_to_signup.click(
368
+ lambda: (gr.update(visible=False), gr.update(visible=True)),
369
  outputs=[login_section, signup_section],
370
  )
371
+ signup_button.click(
372
+ lambda name, phone, email, password: ("Signup successful! Please login.", gr.update(visible=True), gr.update(visible=False)) if save_user(name, phone, email, password) else ("Email already exists.", gr.update(visible=False), gr.update(visible=True)),
373
+ inputs=[signup_name, signup_phone, signup_email, signup_password],
374
+ outputs=[signup_message, login_section, signup_section],
375
  )
376
+ go_to_login.click(
377
+ lambda: (gr.update(visible=True), gr.update(visible=False)),
378
  outputs=[login_section, signup_section],
379
  )
380