Spaces:
Runtime error
Runtime error
| import bcrypt | |
| import gradio as gr | |
| from simple_salesforce import Salesforce | |
| # Salesforce Connection | |
| sf = Salesforce(username='diggavalli98@gmail.com', password='Sati@1020', security_token='sSSjyhInIsUohKpG8sHzty2q') | |
| # Hash Password | |
| def hash_password(password): | |
| return bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()).decode('utf-8') | |
| # Verify Password | |
| def verify_password(plain_password, hashed_password): | |
| return bcrypt.checkpw(plain_password.encode('utf-8'), hashed_password.encode('utf-8')) | |
| # Signup Function | |
| def signup(name, email, phone, password): | |
| try: | |
| query = f"SELECT Id FROM Customer_Login__c WHERE Email__c = '{email.strip()}'" | |
| result = sf.query(query) | |
| if len(result['records']) > 0: | |
| return "Email already exists! Please use a different email." | |
| hashed_password = hash_password(password) | |
| sf.Customer_Login__c.create({ | |
| 'Name': name.strip(), | |
| 'Email__c': email.strip(), | |
| 'Phone_Number__c': phone.strip(), | |
| 'Password__c': hashed_password | |
| }) | |
| return "Signup successful! You can now login." | |
| except Exception as e: | |
| return f"Error during signup: {str(e)}" | |
| # Login Function | |
| def login(email, password): | |
| try: | |
| query = f"SELECT Name, Password__c FROM Customer_Login__c WHERE Email__c = '{email.strip()}'" | |
| result = sf.query(query) | |
| if len(result['records']) == 0: | |
| return "Invalid email or password.", None | |
| user = result['records'][0] | |
| if verify_password(password.strip(), user['Password__c']): | |
| return "Login successful!", user['Name'] | |
| else: | |
| return "Invalid email or password.", None | |
| except Exception as e: | |
| return f"Error during login: {str(e)}", None | |
| # Load Menu Data from Salesforce | |
| def load_menu_from_salesforce(): | |
| try: | |
| query = "SELECT Name, Price__c, Description__c, Image1__c, Veg_NonVeg__c FROM Menu_Item__c" | |
| result = sf.query(query) | |
| return result['records'] | |
| except Exception as e: | |
| return [] | |
| # Filter Menu Items | |
| def filter_menu(preference): | |
| """ | |
| Filters the menu items based on the given preference. | |
| Preference can be "All", "Veg", or "Non-Veg". | |
| """ | |
| menu_data = load_menu_from_salesforce() | |
| filtered_data = [] | |
| for item in menu_data: | |
| veg_nonveg = item.get("Veg_NonVeg__c", "").strip().lower() | |
| if preference == "All": | |
| filtered_data.append(item) | |
| elif preference == "Veg" and veg_nonveg == "veg": | |
| filtered_data.append(item) | |
| elif preference == "Non-Veg" and veg_nonveg == "non-Veg": | |
| filtered_data.append(item) | |
| return filtered_data | |
| # Render Menu as HTML | |
| def render_menu_html(menu_data): | |
| """ | |
| Renders menu items as HTML cards in a grid layout. | |
| """ | |
| if not menu_data: | |
| return '<div style="text-align: center; font-size: 1.2em; color: gray;">No items match your filter.</div>' | |
| html_content = '<div style="display: flex; flex-wrap: wrap; gap: 20px; justify-content: center; align-items: center; padding: 20px;">' | |
| for item in menu_data: | |
| html_content += f""" | |
| <div class="menu-card"> | |
| <img src="{item.get('Image1__c', '')}" alt="{item['Name']}" class="menu-image"> | |
| <div class="menu-content"> | |
| <h3 class="menu-title">{item['Name']}</h3> | |
| <p class="menu-price">${item['Price__c']}</p> | |
| <p class="menu-description">{item['Description__c']}</p> | |
| </div> | |
| </div> | |
| """ | |
| html_content += '</div>' | |
| return html_content | |
| # Add CSS for Styling | |
| menu_styles = """ | |
| <style> | |
| body { | |
| margin: 0; | |
| font-family: Arial, sans-serif; | |
| display: flex; | |
| flex-direction: column; | |
| justify-content: flex-start; | |
| align-items: center; | |
| } | |
| .header { | |
| margin-top: 0; | |
| padding: 20px; | |
| font-size: 2.5em; | |
| font-weight: bold; | |
| text-align: center; | |
| background-color: #f8f9fa; | |
| width: 100%; | |
| box-shadow: 0px 4px 6px rgba(0, 0, 0, 0.1); | |
| position: sticky; | |
| top: 0; | |
| } | |
| .filter-container { | |
| margin: 20px 0; | |
| text-align: center; | |
| width: 100%; | |
| } | |
| .menu-container { | |
| display: flex; | |
| flex-wrap: wrap; | |
| gap: 20px; | |
| justify-content: center; | |
| align-items: center; | |
| padding: 20px; | |
| width: 100%; | |
| } | |
| .menu-card { | |
| border: 1px solid #ddd; | |
| border-radius: 10px; | |
| width: 300px; | |
| box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); | |
| overflow: hidden; | |
| transition: transform 0.2s, box-shadow 0.2s; | |
| cursor: pointer; | |
| text-align: center; | |
| } | |
| .menu-card:hover { | |
| transform: scale(1.05); | |
| box-shadow: 0 8px 16px rgba(0, 0, 0, 0.2); | |
| } | |
| .menu-image { | |
| width: 100%; | |
| height: 180px; | |
| object-fit: cover; | |
| } | |
| .menu-content { | |
| padding: 15px; | |
| } | |
| .menu-title { | |
| font-size: 1.5em; | |
| margin: 0; | |
| } | |
| .menu-price { | |
| font-size: 1.2em; | |
| color: green; | |
| margin: 5px 0; | |
| } | |
| .menu-description { | |
| font-size: 1em; | |
| color: #555; | |
| } | |
| </style> | |
| """ | |
| # Gradio App | |
| with gr.Blocks() as app: | |
| gr.HTML(menu_styles) # Add the CSS | |
| # Header | |
| gr.HTML('<div class="header">Welcome to Biryani Hub</div>') | |
| # Login Page | |
| with gr.Row(visible=True) as login_page: | |
| with gr.Column(): | |
| login_email = gr.Textbox(label="Email") | |
| login_password = gr.Textbox(label="Password", type="password") | |
| login_button = gr.Button("Login") | |
| signup_button = gr.Button("Go to Signup") | |
| login_output = gr.Textbox(label="Status") | |
| # Signup Page | |
| with gr.Row(visible=False) as signup_page: | |
| with gr.Column(): | |
| signup_name = gr.Textbox(label="Name") | |
| signup_email = gr.Textbox(label="Email") | |
| signup_phone = gr.Textbox(label="Phone") | |
| signup_password = gr.Textbox(label="Password", type="password") | |
| submit_signup = gr.Button("Signup") | |
| login_redirect = gr.Button("Go to Login") | |
| signup_output = gr.Textbox(label="Status") | |
| # Menu Page | |
| with gr.Row(visible=False) as menu_page: | |
| preference = gr.Radio(choices=["All", "Veg", "Non-Veg"], label="Filter Preference", value="All") | |
| menu_output = gr.HTML() | |
| preference.change(lambda p: render_menu_html(filter_menu(p)), inputs=preference, outputs=menu_output) | |
| # Define Button Actions | |
| def handle_login(email, password): | |
| status, user = login(email, password) | |
| if status == "Login successful!": | |
| content = render_menu_html(filter_menu("All")) | |
| return gr.update(visible=False), gr.update(visible=True), gr.update(value=content), status | |
| else: | |
| return gr.update(), gr.update(), gr.update(), status | |
| def handle_signup(name, email, phone, password): | |
| return signup(name, email, phone, password) | |
| login_button.click(handle_login, [login_email, login_password], [login_page, menu_page, menu_output, login_output]) | |
| signup_button.click(lambda: (gr.update(visible=False), gr.update(visible=True)), None, [login_page, signup_page]) | |
| submit_signup.click(handle_signup, [signup_name, signup_email, signup_phone, signup_password], signup_output) | |
| login_redirect.click(lambda: (gr.update(visible=True), gr.update(visible=False)), None, [login_page, signup_page]) | |
| app.launch() | |