import gradio as gr import pandas as pd from bcrypt import hashpw, gensalt, checkpw # File for storing user data USER_FILE = "users.xlsx" # Utility Functions from simple_salesforce import Salesforce from bcrypt import hashpw, gensalt, checkpw import datetime # Salesforce connection setup sf = Salesforce(username='diggavalli98@gmail.com', password='Sati@1020', security_token='sSSjyhInIsUohKpG8sHzty2q') # Utility Functions def save_user(name, phone, email, password): """Save user details to Salesforce.""" try: # Check if the email already exists in Salesforce query = f"SELECT Id FROM Customer_Login__c WHERE Email__c = '{email}'" result = sf.query(query) if result['totalSize'] > 0: return False # User already exists # Hash the password hashed_password = hashpw(password.encode(), gensalt()).decode() # Insert new user record into Salesforce new_user = { "Name": name, "Email__c": email, "Password__c": hashed_password, "Phone_Number__c": phone, "Login_Status__c": "Logged Out" } sf.Customer_Login__c.create(new_user) return True except Exception as e: print(f"Error saving user: {e}") return False def check_credentials(email, password): """Check user credentials during login.""" try: # Query user by email query = f"SELECT Password__c FROM Customer_Login__c WHERE Email__c = '{email}'" result = sf.query(query) if result['totalSize'] == 1: hashed_password = result['records'][0]['Password__c'] return checkpw(password.encode(), hashed_password.encode()) return False except Exception as e: print(f"Error checking credentials: {e}") return False # Function to load the menu data def load_menu(): menu_file = "menu.xlsx" # Ensure this file exists in the same directory try: return pd.read_excel(menu_file) except Exception as e: raise ValueError(f"Error loading menu file: {e}") # Function to filter menu items based on preference def filter_menu(preference): menu_data = load_menu() if preference == "Halal/Non-Veg": filtered_data = menu_data[menu_data["Ingredients"].str.contains("Chicken|Mutton|Fish|Prawns|Goat", case=False, na=False)] elif preference == "Vegetarian": filtered_data = menu_data[~menu_data["Ingredients"].str.contains("Chicken|Mutton|Fish|Prawns|Goat", case=False, na=False)] elif preference == "Guilt-Free": filtered_data = menu_data[menu_data["Description"].str.contains(r"Fat: ([0-9]|10)g", case=False, na=False)] else: filtered_data = menu_data html_content = "" for _, item in filtered_data.iterrows(): html_content += f"""

{item['Dish Name']}

${item['Price ($)']}

{item['Description']}

{item['Dish Name']}
""" return html_content # Authentication and Navigation Logic def authenticate_user(email, password): if check_credentials(email, password): return gr.update(visible=False), gr.update(visible=True), "" else: return gr.update(visible=True), gr.update(visible=False), "Invalid email or password. Try again." def navigate_to_signup(): return gr.update(visible=False), gr.update(visible=True) def create_account(name, phone, email, password): if save_user(name, phone, email, password): return "Account created successfully! You can now log in.", gr.update(visible=True), gr.update(visible=False) else: return "Email already exists. Try logging in.", gr.update(visible=False), gr.update(visible=True) def navigate_to_login(): return gr.update(visible=True), gr.update(visible=False) def app(): with gr.Blocks() as demo: # JavaScript for dynamic Cart Modal positioning and resizing modal_js = """ """ # Cart Modal Window (Initially Hidden) cart_modal = gr.HTML( """ """ ) # Login Page with gr.Column(visible=True) as login_section: gr.Markdown("# Login Page") login_email = gr.Textbox(label="Email", placeholder="Enter your email") login_password = gr.Textbox(label="Password", placeholder="Enter your password", type="password") login_error = gr.Label("") login_button = gr.Button("Login") go_to_signup = gr.Button("Create an Account") # Signup Page with gr.Column(visible=False) as signup_section: gr.Markdown("# Signup Page") signup_name = gr.Textbox(label="Name", placeholder="Enter your full name") signup_phone = gr.Textbox(label="Phone", placeholder="Enter your phone number") signup_email = gr.Textbox(label="Email", placeholder="Enter your email") signup_password = gr.Textbox(label="Password", placeholder="Enter your password", type="password") signup_message = gr.Label("") signup_button = gr.Button("Sign Up") go_to_login = gr.Button("Back to Login") # Menu Page with gr.Column(visible=False) as menu_section: gr.Markdown("### Menu Page") selected_preference = gr.Radio( choices=["All", "Vegetarian", "Halal/Non-Veg", "Guilt Free"], value="All", label="Choose a Preference", ) empty_div = gr.HTML('
') menu_output = gr.HTML(value=filter_menu("All")) empty_div = gr.HTML('
') # Modal window for items modal_window = gr.HTML(""" """) # Render modals gr.Row([selected_preference]) gr.Row(menu_output) gr.Row(modal_window) gr.Row(cart_modal) # Add JavaScript for modal functionality gr.HTML(modal_js) # Button Bindings # Login Button login_button.click( lambda email, password: (gr.update(visible=False), gr.update(visible=True), "") if check_credentials(email, password) else (gr.update(), gr.update(), "Invalid email or password."), inputs=[login_email, login_password], outputs=[login_section, menu_section, login_error], ) # Signup Button signup_button.click( lambda name, phone, email, password: ("Signup successful! Please login.", gr.update(visible=True), gr.update(visible=False)) if save_user(name, phone, email, password) else ("Email already exists.", gr.update(visible=False), gr.update(visible=True)), inputs=[signup_name, signup_phone, signup_email, signup_password], outputs=[signup_message, login_section, signup_section], ) # Navigate to Signup Page go_to_signup.click( lambda: (gr.update(visible=False), gr.update(visible=True)), outputs=[login_section, signup_section], ) # Navigate Back to Login Page go_to_login.click( lambda: (gr.update(visible=True), gr.update(visible=False)), outputs=[login_section, signup_section], ) return demo if __name__ == "__main__": app().launch()