Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,48 +1,14 @@
|
|
1 |
import gradio as gr
|
2 |
import pandas as pd
|
3 |
-
from bcrypt import hashpw, gensalt, checkpw
|
4 |
|
5 |
-
#
|
6 |
-
|
|
|
7 |
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
df = pd.read_excel(USER_FILE)
|
13 |
-
return {row['Email']: row['Password'] 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 {}
|
19 |
-
|
20 |
-
# Save users to Excel
|
21 |
-
def save_users(users):
|
22 |
-
df = pd.DataFrame(
|
23 |
-
[{"Name": name, "Phone": phone, "Email": email, "Password": password} for email, (name, phone, password) in users.items()],
|
24 |
-
columns=["Name", "Phone", "Email", "Password"]
|
25 |
-
)
|
26 |
-
df.to_excel(USER_FILE, index=False)
|
27 |
-
|
28 |
-
# Signup user
|
29 |
-
def signup_user(name, phone, email, password):
|
30 |
-
users = load_users()
|
31 |
-
if email in users:
|
32 |
-
return "Email already exists. Please log in."
|
33 |
-
# Hash the password before saving
|
34 |
-
hashed_password = hashpw(password.encode(), gensalt()).decode()
|
35 |
-
users[email] = (name, phone, hashed_password)
|
36 |
-
save_users(users)
|
37 |
-
return "Signup successful! Redirecting to login..."
|
38 |
-
|
39 |
-
# Validate login
|
40 |
-
def validate_login(email, password):
|
41 |
-
users = load_users()
|
42 |
-
if email in users and checkpw(password.encode(), users[email][2].encode()):
|
43 |
-
return True, "Login successful! Redirecting to the menu page..."
|
44 |
-
return False, "Invalid email or password. Please try again."
|
45 |
-
|
46 |
# Function to load the menu data
|
47 |
def load_menu():
|
48 |
menu_file = "menu.xlsx" # Ensure this file exists in the same directory
|
@@ -80,7 +46,6 @@ def filter_menu(preference):
|
|
80 |
</div>
|
81 |
"""
|
82 |
return html_content
|
83 |
-
|
84 |
# Preserving your JavaScript for modal and cart functionality (Your original JavaScript logic)
|
85 |
modal_and_cart_js = """
|
86 |
<style>
|
@@ -261,117 +226,125 @@ modal_and_cart_js = """
|
|
261 |
}
|
262 |
</script>
|
263 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
264 |
|
|
|
|
|
265 |
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
# Signup Page
|
272 |
-
with gr.Row(visible=lambda state: state == "signup", state=current_page):
|
273 |
-
gr.Markdown("### Signup Page")
|
274 |
-
name = gr.Textbox(label="Name")
|
275 |
-
phone = gr.Textbox(label="Phone Number")
|
276 |
-
email = gr.Textbox(label="Email")
|
277 |
-
password = gr.Textbox(label="Password", type="password")
|
278 |
-
signup_btn = gr.Button("Signup")
|
279 |
-
signup_message = gr.Label()
|
280 |
-
|
281 |
-
def handle_signup(name, phone, email, password):
|
282 |
-
message = signup_user(name, phone, email, password)
|
283 |
-
return "login", message
|
284 |
|
285 |
-
|
286 |
-
|
287 |
-
inputs=[name, phone, email, password],
|
288 |
-
outputs=[current_page, signup_message],
|
289 |
-
)
|
290 |
|
|
|
|
|
|
|
291 |
# Login Page
|
292 |
-
with gr.
|
293 |
-
gr.Markdown("
|
294 |
-
|
295 |
-
|
|
|
296 |
login_btn = gr.Button("Login")
|
297 |
-
|
298 |
-
|
299 |
-
def handle_login(email, password):
|
300 |
-
success, message = validate_login(email, password)
|
301 |
-
if success:
|
302 |
-
return "menu", message
|
303 |
-
return "login", message
|
304 |
|
305 |
-
|
306 |
-
|
307 |
-
|
308 |
-
|
309 |
-
)
|
|
|
|
|
|
|
|
|
|
|
310 |
|
311 |
# Menu Page
|
312 |
-
with gr.
|
313 |
gr.Markdown("### Menu Page (Accessible Only After Login)")
|
314 |
|
315 |
-
#
|
316 |
selected_preference = gr.Radio(
|
317 |
-
choices=["All", "Vegetarian", "Halal/Non-Veg"
|
318 |
value="All",
|
319 |
label="Choose a Preference",
|
320 |
)
|
|
|
|
|
321 |
menu_output = gr.HTML(value=filter_menu("All"))
|
|
|
|
|
322 |
cart_output = gr.HTML(value="Your cart is empty.", elem_id="floating-cart")
|
323 |
-
|
|
|
324 |
modal_window = gr.HTML("""
|
325 |
-
|
326 |
-
|
327 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
328 |
</div>
|
329 |
-
|
330 |
-
|
331 |
-
|
332 |
-
|
333 |
-
|
334 |
-
<div id="biryani-extras-options" style="display: flex; flex-wrap: wrap; gap: 10px; margin: 10px 0;">
|
335 |
-
<label><input type="checkbox" name="biryani-extra" value="Thums up" /> Thums up + $2.00</label>
|
336 |
-
<label><input type="checkbox" name="biryani-extra" value="Sprite" /> Sprite + $2.00</label>
|
337 |
-
<label><input type="checkbox" name="biryani-extra" value="Extra Raitha" /> Extra Raitha + $1.00</label>
|
338 |
-
<label><input type="checkbox" name="biryani-extra" value="Extra Salan" /> Extra Salan + $2.00</label>
|
339 |
-
<label><input type="checkbox" name="biryani-extra" value="Extra Onion & Lemon" /> Extra Onion & Lemon + $2.00</label>
|
340 |
-
<label><input type="checkbox" name="biryani-extra" value="Chilli Chicken" /> Chilli Chicken + $14.00</label>
|
341 |
-
<label><input type="checkbox" name="biryani-extra" value="Veg Manchurian" /> Veg Manchurian + $12.00</label>
|
342 |
-
</div>
|
343 |
-
<label for="quantity">Quantity:</label>
|
344 |
-
<input type="number" id="quantity" value="1" min="1" style="width: 50px;" />
|
345 |
-
<br><br>
|
346 |
-
<textarea id="special-instructions" placeholder="Add special instructions here..." style="width: 100%; height: 60px;"></textarea>
|
347 |
-
<br><br>
|
348 |
-
<button style="background-color: #28a745; color: white; border: none; padding: 10px 20px; font-size: 14px; border-radius: 5px; cursor: pointer;" onclick="addToCart()">Add to Cart</button>
|
349 |
-
</div>
|
350 |
-
""")
|
351 |
gr.Row([selected_preference])
|
352 |
gr.Row(menu_output)
|
353 |
gr.Row(cart_output)
|
354 |
gr.Row(modal_window)
|
355 |
-
gr.Row(final_order_output)
|
356 |
gr.HTML(modal_and_cart_js)
|
357 |
-
logout_btn = gr.Button("Logout")
|
358 |
-
|
359 |
-
# Callback functions for page navigation
|
360 |
-
def handle_signup(name, phone, email, password):
|
361 |
-
message, next_page = signup_user(name, phone, email, password)
|
362 |
-
return message, next_page
|
363 |
-
|
364 |
-
def handle_login(email, password):
|
365 |
-
message, next_page = validate_login(email, password)
|
366 |
-
return message, next_page
|
367 |
-
|
368 |
-
def handle_logout():
|
369 |
-
return "Logged out!", "signup"
|
370 |
|
371 |
-
# Button
|
372 |
-
|
373 |
-
|
374 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
375 |
|
376 |
return demo
|
377 |
|
|
|
1 |
import gradio as gr
|
2 |
import pandas as pd
|
|
|
3 |
|
4 |
+
# Placeholder functions for user validation and creation
|
5 |
+
def check_credentials(email, password):
|
6 |
+
return email == "[email protected]" and password == "password123"
|
7 |
|
8 |
+
def save_user(name, phone, email, password):
|
9 |
+
if email == "[email protected]":
|
10 |
+
return False
|
11 |
+
return True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
# Function to load the menu data
|
13 |
def load_menu():
|
14 |
menu_file = "menu.xlsx" # Ensure this file exists in the same directory
|
|
|
46 |
</div>
|
47 |
"""
|
48 |
return html_content
|
|
|
49 |
# Preserving your JavaScript for modal and cart functionality (Your original JavaScript logic)
|
50 |
modal_and_cart_js = """
|
51 |
<style>
|
|
|
226 |
}
|
227 |
</script>
|
228 |
"""
|
229 |
+
# Authentication and Navigation Logic
|
230 |
+
def authenticate_user(email, password):
|
231 |
+
if check_credentials(email, password):
|
232 |
+
return gr.update(visible=False), gr.update(visible=True), ""
|
233 |
+
else:
|
234 |
+
return gr.update(visible=True), gr.update(visible=False), "Invalid email or password. Try again."
|
235 |
|
236 |
+
def navigate_to_signup():
|
237 |
+
return gr.update(visible=False), gr.update(visible=True)
|
238 |
|
239 |
+
def create_account(name, phone, email, password):
|
240 |
+
if save_user(name, phone, email, password):
|
241 |
+
return "Account created successfully! You can now log in.", gr.update(visible=True), gr.update(visible=False)
|
242 |
+
else:
|
243 |
+
return "Email already exists. Try logging in.", gr.update(visible=False), gr.update(visible=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
244 |
|
245 |
+
def navigate_to_login():
|
246 |
+
return gr.update(visible=True), gr.update(visible=False)
|
|
|
|
|
|
|
247 |
|
248 |
+
# Gradio App
|
249 |
+
def app():
|
250 |
+
with gr.Blocks() as demo:
|
251 |
# Login Page
|
252 |
+
with gr.Column(visible=True) as login_section:
|
253 |
+
gr.Markdown("# Login Page")
|
254 |
+
email = gr.Textbox(label="Email", placeholder="Enter your email")
|
255 |
+
password = gr.Textbox(label="Password", placeholder="Enter your password", type="password")
|
256 |
+
error_box = gr.Markdown("", visible=False)
|
257 |
login_btn = gr.Button("Login")
|
258 |
+
create_account_btn = gr.Button("Create an Account")
|
|
|
|
|
|
|
|
|
|
|
|
|
259 |
|
260 |
+
# Sign-Up Page
|
261 |
+
with gr.Column(visible=False) as signup_section:
|
262 |
+
gr.Markdown("# Sign Up Page")
|
263 |
+
name = gr.Textbox(label="Name", placeholder="Enter your full name")
|
264 |
+
phone = gr.Textbox(label="Phone", placeholder="Enter your phone number")
|
265 |
+
signup_email = gr.Textbox(label="Email", placeholder="Enter your email")
|
266 |
+
signup_password = gr.Textbox(label="Password", placeholder="Enter a password", type="password")
|
267 |
+
success_message = gr.Markdown("", visible=False)
|
268 |
+
submit_btn = gr.Button("Sign Up")
|
269 |
+
back_to_login_btn = gr.Button("Back to Login")
|
270 |
|
271 |
# Menu Page
|
272 |
+
with gr.Column(visible=False) as menu_section:
|
273 |
gr.Markdown("### Menu Page (Accessible Only After Login)")
|
274 |
|
275 |
+
# Radio button for selecting preference
|
276 |
selected_preference = gr.Radio(
|
277 |
+
choices=["All", "Vegetarian", "Halal/Non-Veg"],
|
278 |
value="All",
|
279 |
label="Choose a Preference",
|
280 |
)
|
281 |
+
|
282 |
+
# Output area for menu items
|
283 |
menu_output = gr.HTML(value=filter_menu("All"))
|
284 |
+
|
285 |
+
# Floating cart display
|
286 |
cart_output = gr.HTML(value="Your cart is empty.", elem_id="floating-cart")
|
287 |
+
|
288 |
+
# Modal window
|
289 |
modal_window = gr.HTML("""
|
290 |
+
<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;">
|
291 |
+
<div style="text-align: right;">
|
292 |
+
<button onclick="closeModal()" style="background: none; border: none; font-size: 18px; cursor: pointer;">×</button>
|
293 |
+
</div>
|
294 |
+
<img id="modal-image" style="width: 100%; height: auto; border-radius: 8px; margin-bottom: 20px;" />
|
295 |
+
<h2 id="modal-name"></h2>
|
296 |
+
<p id="modal-description"></p>
|
297 |
+
<p id="modal-price"></p>
|
298 |
+
<!-- Biryani Extras -->
|
299 |
+
<label for="biryani-extras">Biryani Extras :</label>
|
300 |
+
<div id="biryani-extras-options" style="display: flex; flex-wrap: wrap; gap: 10px; margin: 10px 0;">
|
301 |
+
<label><input type="checkbox" name="biryani-extra" value="Thums up" /> Thums up + $2.00</label>
|
302 |
+
<label><input type="checkbox" name="biryani-extra" value="Sprite" /> Sprite + $2.00</label>
|
303 |
+
<label><input type="checkbox" name="biryani-extra" value="Extra Raitha" /> Extra Raitha + $1.00</label>
|
304 |
+
<label><input type="checkbox" name="biryani-extra" value="Extra Salan" /> Extra Salan + $2.00</label>
|
305 |
+
<label><input type="checkbox" name="biryani-extra" value="Extra Onion & Lemon" /> Extra Onion & Lemon + $2.00</label>
|
306 |
+
<label><input type="checkbox" name="biryani-extra" value="Chilli Chicken" /> Chilli Chicken + $14.00</label>
|
307 |
+
<label><input type="checkbox" name="biryani-extra" value="Veg Manchurian" /> Veg Manchurian + $12.00</label>
|
308 |
+
</div>
|
309 |
+
<!-- Quantity and Special Instructions -->
|
310 |
+
<label for="quantity">Quantity:</label>
|
311 |
+
<input type="number" id="quantity" value="1" min="1" style="width: 50px;" />
|
312 |
+
<br><br>
|
313 |
+
<textarea id="special-instructions" placeholder="Add special instructions here..." style="width: 100%; height: 60px;"></textarea>
|
314 |
+
<br><br>
|
315 |
+
<!-- Add to Cart Button -->
|
316 |
+
<button style="background-color: #28a745; color: white; border: none; padding: 10px 20px; font-size: 14px; border-radius: 5px; cursor: pointer;" onclick="addToCart()">Add to Cart</button>
|
317 |
</div>
|
318 |
+
""")
|
319 |
+
# Update menu dynamically based on preference
|
320 |
+
selected_preference.change(filter_menu, inputs=[selected_preference], outputs=[menu_output])
|
321 |
+
|
322 |
+
# Layout
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
323 |
gr.Row([selected_preference])
|
324 |
gr.Row(menu_output)
|
325 |
gr.Row(cart_output)
|
326 |
gr.Row(modal_window)
|
|
|
327 |
gr.HTML(modal_and_cart_js)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
328 |
|
329 |
+
# Button Bindings
|
330 |
+
login_btn.click(
|
331 |
+
authenticate_user,
|
332 |
+
inputs=[email, password],
|
333 |
+
outputs=[login_section, menu_section, error_box],
|
334 |
+
)
|
335 |
+
create_account_btn.click(
|
336 |
+
navigate_to_signup,
|
337 |
+
outputs=[login_section, signup_section],
|
338 |
+
)
|
339 |
+
submit_btn.click(
|
340 |
+
create_account,
|
341 |
+
inputs=[name, phone, signup_email, signup_password],
|
342 |
+
outputs=[success_message, login_section, signup_section],
|
343 |
+
)
|
344 |
+
back_to_login_btn.click(
|
345 |
+
navigate_to_login,
|
346 |
+
outputs=[login_section, signup_section],
|
347 |
+
)
|
348 |
|
349 |
return demo
|
350 |
|