Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,22 +1,64 @@
|
|
1 |
import gradio as gr
|
2 |
import pandas as pd
|
|
|
3 |
|
4 |
-
#
|
5 |
-
|
6 |
|
7 |
-
#
|
8 |
-
def
|
9 |
-
|
10 |
-
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
-
#
|
|
|
|
|
|
|
|
|
|
|
14 |
def signup_user(username, password):
|
|
|
15 |
if username in users:
|
16 |
return "Username already exists. Please choose a different username."
|
17 |
-
|
|
|
|
|
|
|
18 |
return "Signup successful! Please log in."
|
19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
# Function to load menu data
|
21 |
def load_menu():
|
22 |
menu_file = "menu.xlsx" # Ensure this file exists
|
@@ -238,10 +280,10 @@ modal_and_cart_js = """
|
|
238 |
# Gradio app definition
|
239 |
def app():
|
240 |
with gr.Blocks() as demo:
|
241 |
-
gr.Markdown("##
|
242 |
|
243 |
-
# Login and Signup Tabs
|
244 |
with gr.Tabs():
|
|
|
245 |
with gr.Tab("Login"):
|
246 |
gr.Markdown("### Login Page")
|
247 |
username = gr.Textbox(label="Username")
|
@@ -250,11 +292,12 @@ def app():
|
|
250 |
login_message = gr.Label()
|
251 |
|
252 |
login_btn.click(
|
253 |
-
|
254 |
inputs=[username, password],
|
255 |
outputs=[login_message],
|
256 |
)
|
257 |
|
|
|
258 |
with gr.Tab("Signup"):
|
259 |
gr.Markdown("### Signup Page")
|
260 |
signup_username = gr.Textbox(label="Username")
|
@@ -268,12 +311,23 @@ def app():
|
|
268 |
outputs=[signup_message],
|
269 |
)
|
270 |
|
|
|
271 |
with gr.Tab("Menu Page"):
|
272 |
-
gr.Markdown("### Menu Page")
|
273 |
-
|
274 |
-
|
275 |
-
|
276 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
277 |
)
|
278 |
|
279 |
# Output area for menu items
|
|
|
1 |
import gradio as gr
|
2 |
import pandas as pd
|
3 |
+
from bcrypt import hashpw, gensalt, checkpw
|
4 |
|
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['Username']: row['Password'] for _, row in df.iterrows()}
|
13 |
+
except FileNotFoundError:
|
14 |
+
# Create the file if it doesn't exist
|
15 |
+
df = pd.DataFrame(columns=["Username", "Password"])
|
16 |
+
df.to_excel(USER_FILE, index=False)
|
17 |
+
return {}
|
18 |
|
19 |
+
# Save users to Excel
|
20 |
+
def save_users(users):
|
21 |
+
df = pd.DataFrame(list(users.items()), columns=["Username", "Password"])
|
22 |
+
df.to_excel(USER_FILE, index=False)
|
23 |
+
|
24 |
+
# Signup user
|
25 |
def signup_user(username, password):
|
26 |
+
users = load_users()
|
27 |
if username in users:
|
28 |
return "Username already exists. Please choose a different username."
|
29 |
+
# Hash the password before saving
|
30 |
+
hashed_password = hashpw(password.encode(), gensalt()).decode()
|
31 |
+
users[username] = hashed_password
|
32 |
+
save_users(users)
|
33 |
return "Signup successful! Please log in."
|
34 |
|
35 |
+
# Validate login
|
36 |
+
def validate_login(username, password):
|
37 |
+
users = load_users()
|
38 |
+
if username in users and checkpw(password.encode(), users[username].encode()):
|
39 |
+
return True, "Login successful! Redirecting to the menu page..."
|
40 |
+
return False, "Invalid username or password. Please try again."
|
41 |
+
|
42 |
+
# Session management
|
43 |
+
active_sessions = {}
|
44 |
+
|
45 |
+
def login_user(username, password):
|
46 |
+
success, message = validate_login(username, password)
|
47 |
+
if success:
|
48 |
+
active_sessions[username] = True # Add to active sessions
|
49 |
+
return message
|
50 |
+
return message
|
51 |
+
|
52 |
+
def is_logged_in(username):
|
53 |
+
return active_sessions.get(username, False)
|
54 |
+
|
55 |
+
def logout_user(username):
|
56 |
+
if username in active_sessions:
|
57 |
+
del active_sessions[username]
|
58 |
+
return f"{username} has been logged out successfully."
|
59 |
+
return "No active session found for this user."
|
60 |
+
|
61 |
+
|
62 |
# Function to load menu data
|
63 |
def load_menu():
|
64 |
menu_file = "menu.xlsx" # Ensure this file exists
|
|
|
280 |
# Gradio app definition
|
281 |
def app():
|
282 |
with gr.Blocks() as demo:
|
283 |
+
gr.Markdown("## Secure Food Ordering System with Login")
|
284 |
|
|
|
285 |
with gr.Tabs():
|
286 |
+
# Login Tab
|
287 |
with gr.Tab("Login"):
|
288 |
gr.Markdown("### Login Page")
|
289 |
username = gr.Textbox(label="Username")
|
|
|
292 |
login_message = gr.Label()
|
293 |
|
294 |
login_btn.click(
|
295 |
+
login_user,
|
296 |
inputs=[username, password],
|
297 |
outputs=[login_message],
|
298 |
)
|
299 |
|
300 |
+
# Signup Tab
|
301 |
with gr.Tab("Signup"):
|
302 |
gr.Markdown("### Signup Page")
|
303 |
signup_username = gr.Textbox(label="Username")
|
|
|
311 |
outputs=[signup_message],
|
312 |
)
|
313 |
|
314 |
+
# Menu Tab
|
315 |
with gr.Tab("Menu Page"):
|
316 |
+
gr.Markdown("### Menu Page (Only for Logged-In Users)")
|
317 |
+
menu_username = gr.Textbox(label="Enter Username to Verify Login", placeholder="Enter your username")
|
318 |
+
menu_check = gr.Button("Check Login Status")
|
319 |
+
login_status = gr.Label()
|
320 |
+
menu_content = gr.HTML(value="")
|
321 |
+
|
322 |
+
def check_login_status(username):
|
323 |
+
if is_logged_in(username):
|
324 |
+
return "Welcome to the menu page!", filter_menu("All")
|
325 |
+
return "You are not logged in!", ""
|
326 |
+
|
327 |
+
menu_check.click(
|
328 |
+
check_login_status,
|
329 |
+
inputs=[menu_username],
|
330 |
+
outputs=[login_status, menu_content],
|
331 |
)
|
332 |
|
333 |
# Output area for menu items
|