geethareddy commited on
Commit
ebfb2eb
·
verified ·
1 Parent(s): c44b748

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +78 -50
app.py CHANGED
@@ -1,58 +1,86 @@
1
  import gradio as gr
2
- from routes.auth import login, sign_up
3
-
4
- def login_interface():
5
- def handle_login(email, password):
6
- success, user_data = login(email, password)
7
- if success:
8
- return f"Welcome, {user_data['Name']}! Redirecting to the menu...", gr.update(visible=False), gr.update(visible=True)
 
 
 
9
  else:
10
- return "Invalid email or password. Please try again.", gr.update(visible=True), gr.update(visible=False)
11
-
12
- with gr.Blocks() as login_page:
13
- gr.Markdown("# Login to BH Restaurant")
14
- email = gr.Textbox(label="Email", placeholder="Enter your email")
15
- password = gr.Textbox(label="Password", placeholder="Enter your password", type="password")
16
- error_message = gr.Markdown(visible=False)
17
- login_button = gr.Button("Login")
18
- login_button.click(handle_login, inputs=[email, password], outputs=[error_message])
19
- return login_page
20
-
21
- def sign_up_interface():
22
- def handle_sign_up(name, email, password, phone, preferences, allergies):
23
- success = sign_up(name, email, password, phone, preferences, allergies)
24
- if success:
25
- return "Account created successfully! Please log in."
26
  else:
27
- return "This email is already registered. Please log in instead."
28
-
29
- with gr.Blocks() as sign_up_page:
30
- gr.Markdown("# Sign Up for BH Restaurant")
31
- name = gr.Textbox(label="Name", placeholder="Enter your name")
32
- email = gr.Textbox(label="Email", placeholder="Enter your email")
33
- password = gr.Textbox(label="Password", placeholder="Enter a secure password", type="password")
34
- phone = gr.Textbox(label="Phone Number", placeholder="Enter your phone number")
35
- preferences = gr.Dropdown(
36
- label="Food Preferences", choices=["Vegetarian", "Vegan", "Halal", "Complete Menu"]
37
- )
38
- allergies = gr.Textbox(label="Allergies", placeholder="List any allergies (comma-separated)")
39
- sign_up_button = gr.Button("Sign Up")
40
- output_message = gr.Markdown()
41
- sign_up_button.click(handle_sign_up, inputs=[name, email, password, phone, preferences, allergies], outputs=[output_message])
42
- return sign_up_page
43
-
44
- def main_interface():
45
- login_page = login_interface()
46
- sign_up_page = sign_up_interface()
47
-
48
  with gr.Blocks() as app:
49
- with gr.Tabs():
50
- with gr.Tab("Login"):
51
- login_page.render()
52
- with gr.Tab("Sign Up"):
53
- sign_up_page.render()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  return app
55
 
 
56
  if __name__ == "__main__":
57
- app = main_interface()
58
  app.launch()
 
1
  import gradio as gr
2
+ from utils.database_handler import check_credentials, save_user
3
+ def app_interface():
4
+ # Login Button Actions
5
+ def authenticate_user(email, password):
6
+ if check_credentials(email, password):
7
+ return (
8
+ gr.update(visible=False), # Hide login page
9
+ gr.update(visible=True), # Show preferences page
10
+ "", # Clear any error message
11
+ )
12
  else:
13
+ return (
14
+ gr.update(visible=True), # Show error box
15
+ gr.update(visible=False), # Keep preferences page hidden
16
+ "Invalid email or password. Try again.", # Error message
17
+ )
18
+
19
+ def navigate_to_signup():
20
+ return gr.update(visible=False), gr.update(visible=True)
21
+
22
+ def create_account(name, phone, email, password):
23
+ if save_user(name, phone, email, password):
24
+ return "Account created successfully! You can now log in.", gr.update(visible=True), gr.update(visible=False)
 
 
 
 
25
  else:
26
+ return "Email already exists. Try logging in.", gr.update(visible=False), gr.update(visible=True)
27
+
28
+ def navigate_to_login():
29
+ return gr.update(visible=True), gr.update(visible=False)
30
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  with gr.Blocks() as app:
32
+ # Login Page
33
+ with gr.Column(visible=True) as login_section:
34
+ gr.Markdown("# Login Page")
35
+ email = gr.Textbox(label="Email", placeholder="Enter your email")
36
+ password = gr.Textbox(label="Password", placeholder="Enter your password", type="password")
37
+ error_box = gr.Markdown("", visible=False)
38
+ login_btn = gr.Button("Login")
39
+ create_account_btn = gr.Button("Create an Account")
40
+
41
+ # Sign-Up Page
42
+ with gr.Column(visible=False) as signup_section:
43
+ gr.Markdown("# Sign Up Page")
44
+ name = gr.Textbox(label="Name", placeholder="Enter your full name")
45
+ phone = gr.Textbox(label="Phone", placeholder="Enter your phone number")
46
+ signup_email = gr.Textbox(label="Email", placeholder="Enter your email")
47
+ signup_password = gr.Textbox(label="Password", placeholder="Enter a password", type="password")
48
+ success_message = gr.Markdown("", visible=False)
49
+ error_message = gr.Markdown("", visible=False)
50
+ submit_btn = gr.Button("Sign Up")
51
+ back_to_login_btn = gr.Button("Back to Login")
52
+
53
+ # Preferences Page
54
+ with gr.Column(visible=False) as preference_section:
55
+ gr.Markdown("# Preferences Page")
56
+
57
+ # Button Actions for Login
58
+ login_btn.click(
59
+ authenticate_user,
60
+ inputs=[email, password],
61
+ outputs=[login_section, preference_section, error_box],
62
+ )
63
+ create_account_btn.click(
64
+ navigate_to_signup,
65
+ inputs=[],
66
+ outputs=[login_section, signup_section],
67
+ )
68
+
69
+ # Button Actions for Sign-Up
70
+ submit_btn.click(
71
+ create_account,
72
+ inputs=[name, phone, signup_email, signup_password],
73
+ outputs=[success_message, signup_section, login_section],
74
+ )
75
+ back_to_login_btn.click(
76
+ navigate_to_login,
77
+ inputs=[],
78
+ outputs=[login_section, signup_section],
79
+ )
80
+
81
  return app
82
 
83
+
84
  if __name__ == "__main__":
85
+ app = app_interface()
86
  app.launch()