Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,62 +9,52 @@ USER_FILE = "users.xlsx"
|
|
| 9 |
def load_users():
|
| 10 |
try:
|
| 11 |
df = pd.read_excel(USER_FILE)
|
| 12 |
-
return {row['
|
| 13 |
except FileNotFoundError:
|
| 14 |
-
df = pd.DataFrame(columns=["
|
| 15 |
df.to_excel(USER_FILE, index=False)
|
| 16 |
return {}
|
| 17 |
|
| 18 |
# Save users to Excel
|
| 19 |
def save_users(users):
|
| 20 |
-
df = pd.DataFrame(
|
| 21 |
df.to_excel(USER_FILE, index=False)
|
| 22 |
|
| 23 |
# Signup user
|
| 24 |
-
def signup_user(
|
| 25 |
users = load_users()
|
| 26 |
-
if
|
| 27 |
-
return "
|
| 28 |
hashed_password = hashpw(password.encode(), gensalt()).decode()
|
| 29 |
-
users[
|
| 30 |
save_users(users)
|
| 31 |
return "Signup successful! Please log in."
|
| 32 |
|
| 33 |
# Validate login
|
| 34 |
-
def validate_login(
|
| 35 |
users = load_users()
|
| 36 |
-
if
|
| 37 |
-
return True, "Login successful!
|
| 38 |
-
return False, "Invalid
|
| 39 |
|
| 40 |
# Session management
|
| 41 |
active_sessions = {}
|
| 42 |
|
| 43 |
-
def login_user(
|
| 44 |
-
success, message = validate_login(
|
| 45 |
if success:
|
| 46 |
-
active_sessions[
|
| 47 |
return message
|
| 48 |
return message
|
| 49 |
|
| 50 |
-
def is_logged_in(
|
| 51 |
-
return active_sessions.get(
|
| 52 |
|
| 53 |
-
def logout_user(
|
| 54 |
-
if
|
| 55 |
-
del active_sessions[
|
| 56 |
-
return
|
| 57 |
-
return "No active session found for this
|
| 58 |
-
|
| 59 |
-
# Menu functionality: Your original code will remain here as-is
|
| 60 |
-
# Keeping all your existing menu, cart, modal logic intact
|
| 61 |
-
# Function to load the menu data
|
| 62 |
-
def load_menu():
|
| 63 |
-
menu_file = "menu.xlsx" # Ensure this file exists in the same directory
|
| 64 |
-
try:
|
| 65 |
-
return pd.read_excel(menu_file)
|
| 66 |
-
except Exception as e:
|
| 67 |
-
raise ValueError(f"Error loading menu file: {e}")
|
| 68 |
|
| 69 |
# Function to filter menu items based on preference
|
| 70 |
def filter_menu(preference):
|
|
@@ -95,7 +85,7 @@ def filter_menu(preference):
|
|
| 95 |
"""
|
| 96 |
return html_content
|
| 97 |
|
| 98 |
-
# JavaScript for modal and cart
|
| 99 |
modal_and_cart_js = """
|
| 100 |
<style>
|
| 101 |
.cart-container {
|
|
@@ -276,6 +266,7 @@ modal_and_cart_js = """
|
|
| 276 |
</script>
|
| 277 |
"""
|
| 278 |
|
|
|
|
| 279 |
# Gradio app
|
| 280 |
def app():
|
| 281 |
with gr.Blocks() as demo:
|
|
@@ -283,29 +274,57 @@ def app():
|
|
| 283 |
|
| 284 |
# Login and Signup Tabs
|
| 285 |
with gr.Tabs():
|
| 286 |
-
|
| 287 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 288 |
password = gr.Textbox(label="Password", type="password")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 289 |
login_btn = gr.Button("Login")
|
| 290 |
login_message = gr.Label()
|
| 291 |
|
| 292 |
-
login_btn.click(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 293 |
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
signup_btn = gr.Button("Signup")
|
| 298 |
-
signup_message = gr.Label()
|
| 299 |
|
| 300 |
-
|
|
|
|
|
|
|
|
|
|
| 301 |
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 309 |
selected_preference = gr.Radio(
|
| 310 |
choices=["All", "Vegetarian", "Halal/Non-Veg", "Guilt-Free"],
|
| 311 |
value="All",
|
|
@@ -321,7 +340,7 @@ def app():
|
|
| 321 |
# Final order display
|
| 322 |
final_order_output = gr.HTML(value="", elem_id="final-order")
|
| 323 |
|
| 324 |
-
# Modal window
|
| 325 |
modal_window = gr.HTML("""
|
| 326 |
<div id="modal" style="display: none; position: fixed; background: white; border-radius: 8px; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2); padding: 20px; z-index: 1000;">
|
| 327 |
<div style="text-align: right;">
|
|
@@ -363,7 +382,9 @@ def app():
|
|
| 363 |
gr.Row(modal_window)
|
| 364 |
gr.Row(final_order_output)
|
| 365 |
gr.HTML(modal_and_cart_js)
|
|
|
|
| 366 |
return demo
|
| 367 |
|
|
|
|
| 368 |
if __name__ == "__main__":
|
| 369 |
app().launch()
|
|
|
|
| 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 {}
|
| 17 |
|
| 18 |
# Save users to Excel
|
| 19 |
def save_users(users):
|
| 20 |
+
df = pd.DataFrame(users.values())
|
| 21 |
df.to_excel(USER_FILE, index=False)
|
| 22 |
|
| 23 |
# Signup user
|
| 24 |
+
def signup_user(name, phone, email, password):
|
| 25 |
users = load_users()
|
| 26 |
+
if email in users:
|
| 27 |
+
return "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 "Signup successful! Please log in."
|
| 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!"
|
| 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 message
|
| 48 |
return message
|
| 49 |
|
| 50 |
+
def is_logged_in(email):
|
| 51 |
+
return active_sessions.get(email, False)
|
| 52 |
|
| 53 |
+
def logout_user(email):
|
| 54 |
+
if email in active_sessions:
|
| 55 |
+
del active_sessions[email]
|
| 56 |
+
return "Logged out successfully!"
|
| 57 |
+
return "No active session found for this email."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
# Function to filter menu items based on preference
|
| 60 |
def filter_menu(preference):
|
|
|
|
| 85 |
"""
|
| 86 |
return html_content
|
| 87 |
|
| 88 |
+
# Preserving your JavaScript for modal and cart functionality (Your original JavaScript logic)
|
| 89 |
modal_and_cart_js = """
|
| 90 |
<style>
|
| 91 |
.cart-container {
|
|
|
|
| 266 |
</script>
|
| 267 |
"""
|
| 268 |
|
| 269 |
+
|
| 270 |
# Gradio app
|
| 271 |
def app():
|
| 272 |
with gr.Blocks() as demo:
|
|
|
|
| 274 |
|
| 275 |
# Login and Signup Tabs
|
| 276 |
with gr.Tabs():
|
| 277 |
+
# Signup Tab
|
| 278 |
+
with gr.Tab("Signup"):
|
| 279 |
+
gr.Markdown("### Signup Page")
|
| 280 |
+
name = gr.Textbox(label="Name")
|
| 281 |
+
phone = gr.Textbox(label="Phone Number")
|
| 282 |
+
email = gr.Textbox(label="Email")
|
| 283 |
password = gr.Textbox(label="Password", type="password")
|
| 284 |
+
signup_btn = gr.Button("Signup")
|
| 285 |
+
signup_message = gr.Label()
|
| 286 |
+
|
| 287 |
+
signup_btn.click(
|
| 288 |
+
signup_user,
|
| 289 |
+
inputs=[name, phone, email, password],
|
| 290 |
+
outputs=[signup_message],
|
| 291 |
+
)
|
| 292 |
+
|
| 293 |
+
# Login Tab
|
| 294 |
+
with gr.Tab("Login"):
|
| 295 |
+
gr.Markdown("### Login Page")
|
| 296 |
+
login_email = gr.Textbox(label="Email")
|
| 297 |
+
login_password = gr.Textbox(label="Password", type="password")
|
| 298 |
login_btn = gr.Button("Login")
|
| 299 |
login_message = gr.Label()
|
| 300 |
|
| 301 |
+
login_btn.click(
|
| 302 |
+
login_user,
|
| 303 |
+
inputs=[login_email, login_password],
|
| 304 |
+
outputs=[login_message],
|
| 305 |
+
)
|
| 306 |
|
| 307 |
+
# Menu Page
|
| 308 |
+
with gr.Tab("Menu Page"):
|
| 309 |
+
gr.Markdown("### Menu Page (Accessible Only After Login)")
|
|
|
|
|
|
|
| 310 |
|
| 311 |
+
# Input for user email to verify login
|
| 312 |
+
email_input = gr.Textbox(label="Enter Email to Verify Access")
|
| 313 |
+
verify_btn = gr.Button("Load Menu")
|
| 314 |
+
menu_output = gr.HTML(value="") # Output for menu items
|
| 315 |
|
| 316 |
+
def verify_and_load_menu(email):
|
| 317 |
+
if is_logged_in(email):
|
| 318 |
+
return filter_menu("All") # Filtered menu based on preference
|
| 319 |
+
return "Access Denied! Please log in."
|
| 320 |
+
|
| 321 |
+
verify_btn.click(
|
| 322 |
+
verify_and_load_menu,
|
| 323 |
+
inputs=[email_input],
|
| 324 |
+
outputs=[menu_output],
|
| 325 |
+
)
|
| 326 |
+
|
| 327 |
+
# Radio button for selecting preference
|
| 328 |
selected_preference = gr.Radio(
|
| 329 |
choices=["All", "Vegetarian", "Halal/Non-Veg", "Guilt-Free"],
|
| 330 |
value="All",
|
|
|
|
| 340 |
# Final order display
|
| 341 |
final_order_output = gr.HTML(value="", elem_id="final-order")
|
| 342 |
|
| 343 |
+
# Modal window
|
| 344 |
modal_window = gr.HTML("""
|
| 345 |
<div id="modal" style="display: none; position: fixed; background: white; border-radius: 8px; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2); padding: 20px; z-index: 1000;">
|
| 346 |
<div style="text-align: right;">
|
|
|
|
| 382 |
gr.Row(modal_window)
|
| 383 |
gr.Row(final_order_output)
|
| 384 |
gr.HTML(modal_and_cart_js)
|
| 385 |
+
|
| 386 |
return demo
|
| 387 |
|
| 388 |
+
|
| 389 |
if __name__ == "__main__":
|
| 390 |
app().launch()
|