Spaces:
Sleeping
Sleeping
import bcrypt | |
import gradio as gr | |
from simple_salesforce import Salesforce | |
# Salesforce Connection | |
sf = Salesforce(username='[email protected]', password='Sati@1020', security_token='sSSjyhInIsUohKpG8sHzty2q') | |
# Function to Hash Password | |
def hash_password(password): | |
return bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()).decode('utf-8') | |
# Function to 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: | |
email = email.strip() | |
# 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 len(result['records']) > 0: | |
return "Email already exists! Please use a different email." | |
# Hash the password | |
hashed_password = hash_password(password) | |
# Create the new user record | |
sf.Customer_Login__c.create({ | |
'Name': name.strip(), | |
'Email__c': email, | |
'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: | |
email = email.strip() | |
password = password.strip() | |
# Query Salesforce for user details | |
query = f"SELECT Name, Password__c FROM Customer_Login__c WHERE Email__c = '{email}'" | |
result = sf.query(query) | |
if len(result['records']) == 0: | |
return "Invalid email or password.", None | |
user = result['records'][0] | |
if verify_password(password, 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 | |
# Function to load menu items from Salesforce | |
def load_menu_from_salesforce(): | |
try: | |
query = "SELECT Name, Price__c, Description__c, Image1__c, Veg_NonVeg__c, Section__c FROM Menu_Item__c" | |
result = sf.query(query) | |
return result['records'] | |
except Exception as e: | |
raise ValueError(f"Error loading menu data from Salesforce: {e}") | |
# Function to filter menu items based on preference | |
def filter_menu(preferences): | |
menu_data = load_menu_from_salesforce() | |
filtered_data = {} | |
for item in menu_data: | |
if item["Section__c"] not in filtered_data: | |
filtered_data[item["Section__c"]] = [] | |
if "Veg" in preferences and item["Veg_NonVeg__c"] in ["Veg", "Both"]: | |
filtered_data[item["Section__c"]].append(item) | |
elif "Non-Veg" in preferences and item["Veg_NonVeg__c"] in ["Non-Veg", "Both"]: | |
filtered_data[item["Section__c"]].append(item) | |
html_content = '<div style="padding: 20px; max-width: 1200px; margin: auto;">' | |
for section, items in filtered_data.items(): | |
html_content += f"<h2 style='text-align: center; color: #333;'>{section}</h2>" | |
html_content += '<div style="display: flex; flex-wrap: wrap; justify-content: center; gap: 20px;">' | |
for item in items: | |
html_content += f""" | |
<div style="width: 300px; border: 1px solid #ddd; border-radius: 10px; overflow: hidden; box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.1);"> | |
<img src="{item['Image1__c']}" alt="{item['Name']}" style="width: 100%; height: 200px; object-fit: cover;"> | |
<div style="padding: 15px;"> | |
<h3 style="margin: 0; font-size: 20px; color: #333;">{item['Name']}</h3> | |
<p style="margin: 5px 0; font-size: 16px; color: #888;">${item['Price__c']}</p> | |
<p style="margin: 10px 0; font-size: 14px; color: #555;">{item['Description__c']}</p> | |
</div> | |
</div> | |
""" | |
html_content += '</div>' | |
html_content += '</div>' | |
return html_content or "<p style='text-align: center;'>No items match your filter.</p>" | |
# Gradio App | |
with gr.Blocks() as app: | |
login_status = gr.State(value=False) | |
# Header | |
with gr.Row(): | |
gr.HTML("<h1 style='text-align: center; color: #222;'>Welcome to Biryani Hub</h1>") | |
# 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: | |
preferences = gr.CheckboxGroup( | |
choices=["Veg", "Non-Veg"], label="Filter Preference" | |
) | |
menu_output = gr.HTML() | |
# Footer | |
with gr.Row(): | |
gr.HTML("<footer style='text-align: center; color: #666; padding: 20px; background-color: #f9f9f9;'>Thank you! Welcome again!</footer>") | |
# Functions for page transitions and operations | |
def handle_login(email, password): | |
status, user = login(email, password) | |
if status == "Login successful!": | |
return gr.update(visible=False), gr.update(visible=True), status | |
else: | |
return gr.update(), gr.update(), status | |
def handle_signup(name, email, phone, password): | |
return signup(name, email, phone, password) | |
def handle_menu(preferences): | |
return filter_menu(preferences) | |
# Button Actions | |
login_button.click(handle_login, [login_email, login_password], [login_page, menu_page, 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=False), gr.update(visible=True)), None, [signup_page, login_page]) | |
preferences.change(handle_menu, [preferences], menu_output) | |
app.launch() | |