Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -47,68 +47,66 @@ def login(email, password):
|
|
47 |
password = password.strip()
|
48 |
|
49 |
# Query Salesforce for user details
|
50 |
-
query = f"SELECT Name,
|
51 |
result = sf.query(query)
|
52 |
|
53 |
if len(result['records']) == 0:
|
54 |
return "Invalid email or password.", None
|
55 |
|
56 |
user = result['records'][0]
|
57 |
-
|
58 |
-
|
59 |
-
# Verify the entered password with the stored hash
|
60 |
-
if verify_password(password, stored_password):
|
61 |
return "Login successful!", user['Name']
|
62 |
else:
|
63 |
return "Invalid email or password.", None
|
64 |
except Exception as e:
|
65 |
return f"Error during login: {str(e)}", None
|
66 |
|
67 |
-
#
|
68 |
-
def
|
69 |
try:
|
70 |
-
query = "SELECT Name, Price__c, Description__c, Image1__c,
|
71 |
result = sf.query(query)
|
72 |
-
return [
|
73 |
-
{
|
74 |
-
"Name": item["Name"],
|
75 |
-
"Price": item["Price__c"],
|
76 |
-
"Description": item["Description__c"],
|
77 |
-
"Image": item["Image1__c"],
|
78 |
-
"Category": item["Veg_NonVeg__c"],
|
79 |
-
}
|
80 |
-
for item in result["records"]
|
81 |
-
]
|
82 |
except Exception as e:
|
83 |
-
|
84 |
-
|
85 |
-
#
|
86 |
-
def
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
|
|
|
|
|
|
|
|
|
|
97 |
|
98 |
html_content = ""
|
99 |
-
for item in
|
100 |
html_content += f"""
|
101 |
-
<div style="border: 1px solid #ddd;
|
102 |
-
<
|
103 |
-
|
104 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
105 |
</div>
|
106 |
"""
|
107 |
-
return html_content
|
108 |
|
109 |
# Gradio App
|
110 |
with gr.Blocks() as app:
|
111 |
-
# States for visibility
|
112 |
login_status = gr.State(value=False)
|
113 |
|
114 |
# Login Page
|
@@ -117,8 +115,27 @@ with gr.Blocks() as app:
|
|
117 |
login_email = gr.Textbox(label="Email")
|
118 |
login_password = gr.Textbox(label="Password", type="password")
|
119 |
login_button = gr.Button("Login")
|
|
|
120 |
login_output = gr.Textbox(label="Status")
|
121 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
122 |
def handle_login(email, password):
|
123 |
status, user = login(email, password)
|
124 |
if status == "Login successful!":
|
@@ -126,18 +143,17 @@ with gr.Blocks() as app:
|
|
126 |
else:
|
127 |
return gr.update(), gr.update(), status
|
128 |
|
129 |
-
|
130 |
-
|
131 |
-
# Menu Page
|
132 |
-
with gr.Row(visible=False) as menu_page:
|
133 |
-
veg_filter = gr.Checkbox(label="Veg", value=True)
|
134 |
-
non_veg_filter = gr.Checkbox(label="Non-Veg", value=True)
|
135 |
-
filter_button = gr.Button("Filter Menu")
|
136 |
-
menu_display = gr.HTML(label="Menu")
|
137 |
|
138 |
-
|
139 |
-
|
140 |
|
141 |
-
|
|
|
|
|
|
|
|
|
|
|
142 |
|
143 |
app.launch()
|
|
|
47 |
password = password.strip()
|
48 |
|
49 |
# Query Salesforce for user details
|
50 |
+
query = f"SELECT Name, Password__c FROM Customer_Login__c WHERE Email__c = '{email}'"
|
51 |
result = sf.query(query)
|
52 |
|
53 |
if len(result['records']) == 0:
|
54 |
return "Invalid email or password.", None
|
55 |
|
56 |
user = result['records'][0]
|
57 |
+
if verify_password(password, user['Password__c']):
|
|
|
|
|
|
|
58 |
return "Login successful!", user['Name']
|
59 |
else:
|
60 |
return "Invalid email or password.", None
|
61 |
except Exception as e:
|
62 |
return f"Error during login: {str(e)}", None
|
63 |
|
64 |
+
# Function to load menu items from Salesforce
|
65 |
+
def load_menu_from_salesforce():
|
66 |
try:
|
67 |
+
query = "SELECT Name, Price__c, Description__c, Image1__c, Ingredients__c FROM Menu_Item__c"
|
68 |
result = sf.query(query)
|
69 |
+
return result['records']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
except Exception as e:
|
71 |
+
raise ValueError(f"Error loading menu data from Salesforce: {e}")
|
72 |
+
|
73 |
+
# Function to filter menu items based on preference
|
74 |
+
def filter_menu_from_salesforce(preference):
|
75 |
+
menu_data = load_menu_from_salesforce()
|
76 |
+
filtered_data = []
|
77 |
+
|
78 |
+
for item in menu_data:
|
79 |
+
if preference == "Halal/Non-Veg":
|
80 |
+
if any(x in item.get("Ingredients__c", "").lower() for x in ["chicken", "mutton", "fish", "prawns", "goat"]):
|
81 |
+
filtered_data.append(item)
|
82 |
+
elif preference == "Vegetarian":
|
83 |
+
if not any(x in item.get("Ingredients__c", "").lower() for x in ["chicken", "mutton", "fish", "prawns", "goat"]):
|
84 |
+
filtered_data.append(item)
|
85 |
+
elif preference == "Guilt-Free":
|
86 |
+
if "fat:" in item.get("Description__c", "").lower():
|
87 |
+
filtered_data.append(item)
|
88 |
+
else:
|
89 |
+
filtered_data = menu_data
|
90 |
|
91 |
html_content = ""
|
92 |
+
for item in filtered_data:
|
93 |
html_content += f"""
|
94 |
+
<div style="display: flex; align-items: center; border: 1px solid #ddd; border-radius: 8px; padding: 15px; margin-bottom: 10px; box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.1);">
|
95 |
+
<div style="flex: 1; margin-right: 15px;">
|
96 |
+
<h3 style="margin: 0; font-size: 18px;">{item['Name']}</h3>
|
97 |
+
<p style="margin: 5px 0; font-size: 16px; color: #888;">${item['Price__c']}</p>
|
98 |
+
<p style="margin: 5px 0; font-size: 14px; color: #555;">{item['Description__c']}</p>
|
99 |
+
</div>
|
100 |
+
<div style="flex-shrink: 0; text-align: center;">
|
101 |
+
<img src="{item['Image1__c']}" alt="{item['Name']}" style="width: 100px; height: 100px; border-radius: 8px; object-fit: cover; margin-bottom: 10px;">
|
102 |
+
<button style="background-color: #28a745; color: white; border: none; padding: 8px 15px; font-size: 14px; border-radius: 5px; cursor: pointer;">Add</button>
|
103 |
+
</div>
|
104 |
</div>
|
105 |
"""
|
106 |
+
return html_content
|
107 |
|
108 |
# Gradio App
|
109 |
with gr.Blocks() as app:
|
|
|
110 |
login_status = gr.State(value=False)
|
111 |
|
112 |
# Login Page
|
|
|
115 |
login_email = gr.Textbox(label="Email")
|
116 |
login_password = gr.Textbox(label="Password", type="password")
|
117 |
login_button = gr.Button("Login")
|
118 |
+
signup_button = gr.Button("Go to Signup")
|
119 |
login_output = gr.Textbox(label="Status")
|
120 |
|
121 |
+
# Signup Page
|
122 |
+
with gr.Row(visible=False) as signup_page:
|
123 |
+
with gr.Column():
|
124 |
+
signup_name = gr.Textbox(label="Name")
|
125 |
+
signup_email = gr.Textbox(label="Email")
|
126 |
+
signup_phone = gr.Textbox(label="Phone")
|
127 |
+
signup_password = gr.Textbox(label="Password", type="password")
|
128 |
+
submit_signup = gr.Button("Signup")
|
129 |
+
login_redirect = gr.Button("Go to Login")
|
130 |
+
signup_output = gr.Textbox(label="Status")
|
131 |
+
|
132 |
+
# Menu Page
|
133 |
+
with gr.Row(visible=False) as menu_page:
|
134 |
+
preference = gr.Radio(["All", "Halal/Non-Veg", "Vegetarian", "Guilt-Free"], label="Filter Preference")
|
135 |
+
show_menu = gr.Button("Show Menu")
|
136 |
+
menu_output = gr.HTML()
|
137 |
+
|
138 |
+
# Functions for page transitions and operations
|
139 |
def handle_login(email, password):
|
140 |
status, user = login(email, password)
|
141 |
if status == "Login successful!":
|
|
|
143 |
else:
|
144 |
return gr.update(), gr.update(), status
|
145 |
|
146 |
+
def handle_signup(name, email, phone, password):
|
147 |
+
return signup(name, email, phone, password)
|
|
|
|
|
|
|
|
|
|
|
|
|
148 |
|
149 |
+
def handle_menu(preference):
|
150 |
+
return filter_menu_from_salesforce(preference)
|
151 |
|
152 |
+
# Button Actions
|
153 |
+
login_button.click(handle_login, [login_email, login_password], [login_page, menu_page, login_output])
|
154 |
+
signup_button.click(lambda: (gr.update(visible=False), gr.update(visible=True)), None, [login_page, signup_page])
|
155 |
+
submit_signup.click(handle_signup, [signup_name, signup_email, signup_phone, signup_password], signup_output)
|
156 |
+
login_redirect.click(lambda: (gr.update(visible=False), gr.update(visible=True)), None, [signup_page, login_page])
|
157 |
+
show_menu.click(handle_menu, [preference], menu_output)
|
158 |
|
159 |
app.launch()
|