Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -5,14 +5,12 @@ 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 |
# Load users from Excel
|
10 |
def load_users():
|
11 |
try:
|
12 |
df = pd.read_excel(USER_FILE)
|
13 |
return {row['Email']: row 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 {}
|
@@ -26,20 +24,32 @@ def save_users(users):
|
|
26 |
def signup_user(name, phone, email, password):
|
27 |
users = load_users()
|
28 |
if email in users:
|
29 |
-
return "Email already exists. Please use a different email."
|
30 |
hashed_password = hashpw(password.encode(), gensalt()).decode()
|
31 |
users[email] = {"Name": name, "Phone": phone, "Email": email, "Password": hashed_password}
|
32 |
save_users(users)
|
33 |
-
return "Signup successful! Redirecting to login page..."
|
34 |
|
35 |
# Validate login
|
36 |
def validate_login(email, password):
|
37 |
users = load_users()
|
38 |
if email in users and checkpw(password.encode(), users[email]["Password"].encode()):
|
39 |
-
return "Login successful! Redirecting to menu page..."
|
40 |
-
return "Invalid email or password.
|
|
|
|
|
|
|
41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
@@ -258,29 +268,19 @@ modal_and_cart_js = """
|
|
258 |
}
|
259 |
</script>
|
260 |
"""
|
|
|
|
|
|
|
261 |
def app():
|
262 |
with gr.Blocks() as demo:
|
263 |
-
|
264 |
-
login_visible = gr.State(True)
|
265 |
-
signup_visible = gr.State(False)
|
266 |
-
menu_visible = gr.State(False)
|
267 |
-
|
268 |
-
# Login Page
|
269 |
-
with gr.Row(visible=login_visible):
|
270 |
-
gr.Markdown("### Login Page")
|
271 |
-
login_email = gr.Textbox(label="Email")
|
272 |
-
login_password = gr.Textbox(label="Password", type="password")
|
273 |
-
login_btn = gr.Button("Login")
|
274 |
-
login_message = gr.Label()
|
275 |
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
outputs=[login_message, login_visible, signup_visible, menu_visible],
|
280 |
-
)
|
281 |
|
282 |
# Signup Page
|
283 |
-
with gr.Row(visible=
|
284 |
gr.Markdown("### Signup Page")
|
285 |
name = gr.Textbox(label="Name")
|
286 |
phone = gr.Textbox(label="Phone Number")
|
@@ -289,15 +289,45 @@ def app():
|
|
289 |
signup_btn = gr.Button("Signup")
|
290 |
signup_message = gr.Label()
|
291 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
292 |
signup_btn.click(
|
293 |
-
|
294 |
inputs=[name, phone, email, password],
|
295 |
-
outputs=[signup_message
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
296 |
)
|
297 |
|
298 |
# Menu Page
|
299 |
-
with gr.Row(visible=
|
300 |
-
gr.Markdown("### Menu Page")
|
|
|
|
|
301 |
selected_preference = gr.Radio(
|
302 |
choices=["All", "Vegetarian", "Halal/Non-Veg", "Guilt-Free"],
|
303 |
value="All",
|
@@ -360,4 +390,4 @@ def app():
|
|
360 |
|
361 |
|
362 |
if __name__ == "__main__":
|
363 |
-
app().launch()
|
|
|
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 {}
|
|
|
24 |
def signup_user(name, phone, email, password):
|
25 |
users = load_users()
|
26 |
if email in users:
|
27 |
+
return False, "Email already exists. Please use a different email."
|
28 |
hashed_password = hashpw(password.encode(), gensalt()).decode()
|
29 |
users[email] = {"Name": name, "Phone": phone, "Email": email, "Password": hashed_password}
|
30 |
save_users(users)
|
31 |
+
return True, "Signup successful! Redirecting to the login page..."
|
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]["Password"].encode()):
|
37 |
+
return True, "Login successful! Redirecting to the menu page..."
|
38 |
+
return False, "Invalid email or password."
|
39 |
+
|
40 |
+
# Session management
|
41 |
+
active_sessions = {}
|
42 |
|
43 |
+
def login_user(email, password):
|
44 |
+
success, message = validate_login(email, password)
|
45 |
+
if success:
|
46 |
+
active_sessions[email] = True
|
47 |
+
return True, message
|
48 |
+
return False, message
|
49 |
|
50 |
+
def is_logged_in(email):
|
51 |
+
return active_sessions.get(email, False)
|
52 |
+
|
53 |
# Function to load the menu data
|
54 |
def load_menu():
|
55 |
menu_file = "menu.xlsx" # Ensure this file exists in the same directory
|
|
|
268 |
}
|
269 |
</script>
|
270 |
"""
|
271 |
+
|
272 |
+
|
273 |
+
# Gradio app
|
274 |
def app():
|
275 |
with gr.Blocks() as demo:
|
276 |
+
gr.Markdown("## Secure Food Ordering System")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
277 |
|
278 |
+
# State variables to track redirection
|
279 |
+
with gr.State() as current_page: # Holds the current visible page
|
280 |
+
current_page.set("signup") # Default to signup page
|
|
|
|
|
281 |
|
282 |
# Signup Page
|
283 |
+
with gr.Row(visible=lambda state: state == "signup", state=current_page):
|
284 |
gr.Markdown("### Signup Page")
|
285 |
name = gr.Textbox(label="Name")
|
286 |
phone = gr.Textbox(label="Phone Number")
|
|
|
289 |
signup_btn = gr.Button("Signup")
|
290 |
signup_message = gr.Label()
|
291 |
|
292 |
+
# Handle Signup
|
293 |
+
def handle_signup(name, phone, email, password):
|
294 |
+
success, message = signup_user(name, phone, email, password)
|
295 |
+
if success:
|
296 |
+
current_page.set("login") # Redirect to login page
|
297 |
+
return message
|
298 |
+
|
299 |
signup_btn.click(
|
300 |
+
handle_signup,
|
301 |
inputs=[name, phone, email, password],
|
302 |
+
outputs=[signup_message],
|
303 |
+
)
|
304 |
+
|
305 |
+
# Login Page
|
306 |
+
with gr.Row(visible=lambda state: state == "login", state=current_page):
|
307 |
+
gr.Markdown("### Login Page")
|
308 |
+
login_email = gr.Textbox(label="Email")
|
309 |
+
login_password = gr.Textbox(label="Password", type="password")
|
310 |
+
login_btn = gr.Button("Login")
|
311 |
+
login_message = gr.Label()
|
312 |
+
|
313 |
+
# Handle Login
|
314 |
+
def handle_login(email, password):
|
315 |
+
success, message = login_user(email, password)
|
316 |
+
if success:
|
317 |
+
current_page.set("menu") # Redirect to menu page
|
318 |
+
return message
|
319 |
+
|
320 |
+
login_btn.click(
|
321 |
+
handle_login,
|
322 |
+
inputs=[login_email, login_password],
|
323 |
+
outputs=[login_message],
|
324 |
)
|
325 |
|
326 |
# Menu Page
|
327 |
+
with gr.Row(visible=lambda state: state == "menu", state=current_page):
|
328 |
+
gr.Markdown("### Menu Page (Accessible Only After Login)")
|
329 |
+
|
330 |
+
# Radio button for selecting preference
|
331 |
selected_preference = gr.Radio(
|
332 |
choices=["All", "Vegetarian", "Halal/Non-Veg", "Guilt-Free"],
|
333 |
value="All",
|
|
|
390 |
|
391 |
|
392 |
if __name__ == "__main__":
|
393 |
+
app().launch()
|