nagasurendra commited on
Commit
e39b8fd
·
verified ·
1 Parent(s): bfddad2

Update components/login_signup.py

Browse files
Files changed (1) hide show
  1. components/login_signup.py +71 -42
components/login_signup.py CHANGED
@@ -1,44 +1,73 @@
1
  import gradio as gr
2
- from utils.excel_operations import read_excel, write_excel
3
- from utils.state_management import state
4
-
5
- def login_signup():
6
- def authenticate(email, password):
7
- customers = read_excel('data/customers.xlsx')
8
- user = next((c for c in customers if c['Email'] == email), None)
9
- if user and user['Password'] == password:
10
- state["user"] = user
11
- return "Login Successful!"
12
- return "Invalid credentials!"
13
-
14
- def register(name, email, password, preferences, allergies, occasion):
15
- customers = read_excel('data/customers.xlsx')
16
- if any(c['Email'] == email for c in customers):
17
- return "Email already exists!"
18
- customers.append({
19
- "Name": name, "Email": email, "Password": password,
20
- "Preferences": preferences, "Allergies": allergies, "Occasion": occasion
21
- })
22
- write_excel('data/customers.xlsx', customers)
23
- return "Registration Successful!"
24
-
25
- with gr.Group():
26
- with gr.Box():
27
- gr.Markdown("### Login")
28
- email = gr.Textbox(label="Email")
29
- password = gr.Textbox(label="Password", type="password")
 
 
30
  login_btn = gr.Button("Login")
31
- login_result = gr.Label()
32
- login_btn.click(authenticate, inputs=[email, password], outputs=login_result)
33
-
34
- with gr.Box():
35
- gr.Markdown("### Register")
36
- name = gr.Textbox(label="Name")
37
- reg_email = gr.Textbox(label="Email")
38
- reg_password = gr.Textbox(label="Password", type="password")
39
- preferences = gr.Dropdown(["Vegetarian", "Vegan", "Halal", "Full Menu"], label="Preferences")
40
- allergies = gr.Textbox(label="Allergies (comma-separated)")
41
- occasion = gr.Textbox(label="Special Occasion")
42
- register_btn = gr.Button("Register")
43
- register_result = gr.Label()
44
- register_btn.click(register, inputs=[name, reg_email, reg_password, preferences, allergies, occasion], outputs=register_result)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ from utils.database_handler import check_credentials, save_user
3
+
4
+ def login_page():
5
+ # Login Button Actions
6
+ def authenticate_user(email, password):
7
+ if check_credentials(email, password):
8
+ return gr.update(visible=False), gr.update(visible=True), "Welcome to Biryani Hub!"
9
+ else:
10
+ return gr.update(visible=True), gr.update(visible=False), "Invalid email or password. Try again."
11
+
12
+ def navigate_to_signup():
13
+ return gr.update(visible=False), gr.update(visible=True)
14
+
15
+ def create_account(name, phone, email, password):
16
+ if save_user(name, phone, email, password):
17
+ return "Account created successfully! You can now log in.", gr.update(visible=True), gr.update(visible=False)
18
+ else:
19
+ return "Email already exists. Try logging in.", gr.update(visible=False), gr.update(visible=True)
20
+
21
+ def navigate_to_login():
22
+ return gr.update(visible=True), gr.update(visible=False)
23
+
24
+ with gr.Blocks() as login_interface:
25
+ # Login Section
26
+ with gr.Column(visible=True) as login_section:
27
+ gr.Markdown("# Login to Biryani Hub")
28
+ email = gr.Textbox(label="Email", placeholder="Enter your email")
29
+ password = gr.Textbox(label="Password", placeholder="Enter your password", type="password")
30
+ error_box = gr.Markdown("", visible=False)
31
+ success_box = gr.Markdown("", visible=False)
32
  login_btn = gr.Button("Login")
33
+ create_account_btn = gr.Button("Create an Account")
34
+
35
+ # Sign-Up Section
36
+ with gr.Column(visible=False) as signup_section:
37
+ gr.Markdown("# Create Your Account")
38
+ name = gr.Textbox(label="Name", placeholder="Enter your full name")
39
+ phone = gr.Textbox(label="Phone", placeholder="Enter your phone number")
40
+ signup_email = gr.Textbox(label="Email", placeholder="Enter your email")
41
+ signup_password = gr.Textbox(label="Password", placeholder="Enter a password", type="password")
42
+ success_message = gr.Markdown("", visible=False)
43
+ error_message = gr.Markdown("", visible=False)
44
+ submit_btn = gr.Button("Sign Up")
45
+ back_to_login_btn = gr.Button("Back to Login")
46
+
47
+ # Button Actions
48
+ login_btn.click(
49
+ authenticate_user,
50
+ inputs=[email, password],
51
+ outputs=[error_box, success_box, success_box]
52
+ )
53
+ create_account_btn.click(
54
+ navigate_to_signup,
55
+ inputs=[],
56
+ outputs=[login_section, signup_section]
57
+ )
58
+ submit_btn.click(
59
+ create_account,
60
+ inputs=[name, phone, signup_email, signup_password],
61
+ outputs=[success_message, signup_section, login_section]
62
+ )
63
+ back_to_login_btn.click(
64
+ navigate_to_login,
65
+ inputs=[],
66
+ outputs=[login_section, signup_section]
67
+ )
68
+
69
+ return login_interface
70
+
71
+ # Expose login_page for integration in the main app
72
+ if __name__ == "__main__":
73
+ login_page().launch()