Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,106 +1,29 @@
|
|
1 |
import bcrypt
|
2 |
import gradio as gr
|
3 |
-
from simple_salesforce import Salesforce
|
4 |
|
5 |
-
# Salesforce
|
6 |
-
sf = Salesforce(username='[email protected]', password='Sati@1020', security_token='sSSjyhInIsUohKpG8sHzty2q')
|
7 |
-
|
8 |
-
# Function to Hash Password
|
9 |
-
def hash_password(password):
|
10 |
-
return bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()).decode('utf-8')
|
11 |
-
|
12 |
-
# Function to Verify Password
|
13 |
-
def verify_password(plain_password, hashed_password):
|
14 |
-
return bcrypt.checkpw(plain_password.encode('utf-8'), hashed_password.encode('utf-8'))
|
15 |
-
|
16 |
-
# Signup function
|
17 |
-
def signup(name, email, phone, password):
|
18 |
-
try:
|
19 |
-
email = email.strip()
|
20 |
-
|
21 |
-
# Check if the email already exists in Salesforce
|
22 |
-
query = f"SELECT Id FROM Customer_Login__c WHERE Email__c = '{email}'"
|
23 |
-
result = sf.query(query)
|
24 |
-
|
25 |
-
if len(result['records']) > 0:
|
26 |
-
return "Email already exists! Please use a different email."
|
27 |
-
|
28 |
-
# Hash the password
|
29 |
-
hashed_password = hash_password(password)
|
30 |
-
|
31 |
-
# Create the new user record
|
32 |
-
sf.Customer_Login__c.create({
|
33 |
-
'Name': name.strip(),
|
34 |
-
'Email__c': email,
|
35 |
-
'Phone_Number__c': phone.strip(),
|
36 |
-
'Password__c': hashed_password
|
37 |
-
})
|
38 |
-
return "Signup successful! You can now login."
|
39 |
-
except Exception as e:
|
40 |
-
return f"Error during signup: {str(e)}"
|
41 |
-
|
42 |
-
# Login function
|
43 |
-
def login(email, password):
|
44 |
-
try:
|
45 |
-
email = email.strip()
|
46 |
-
password = password.strip()
|
47 |
-
|
48 |
-
# Query Salesforce for user details
|
49 |
-
query = f"SELECT Name, Password__c FROM Customer_Login__c WHERE Email__c = '{email}'"
|
50 |
-
result = sf.query(query)
|
51 |
-
|
52 |
-
if len(result['records']) == 0:
|
53 |
-
return "Invalid email or password.", None
|
54 |
-
|
55 |
-
user = result['records'][0]
|
56 |
-
if verify_password(password, user['Password__c']):
|
57 |
-
return "Login successful!", user['Name']
|
58 |
-
else:
|
59 |
-
return "Invalid email or password.", None
|
60 |
-
except Exception as e:
|
61 |
-
return f"Error during login: {str(e)}", None
|
62 |
-
|
63 |
-
# Function to load menu items from Salesforce
|
64 |
def load_menu_from_salesforce():
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
print(f"Error fetching menu data: {e}")
|
77 |
-
return []
|
78 |
|
79 |
# Function to filter menu items based on preference
|
80 |
def filter_menu(preferences):
|
81 |
menu_data = load_menu_from_salesforce()
|
82 |
|
83 |
-
# Debugging: Print the fetched menu data
|
84 |
-
print("Menu Data Fetched:")
|
85 |
-
for item in menu_data:
|
86 |
-
print(item)
|
87 |
-
|
88 |
-
# Check if data exists
|
89 |
-
if not menu_data:
|
90 |
-
return "<p style='text-align: center;'>No menu data available. Please try again later.</p>"
|
91 |
-
|
92 |
filtered_data = {}
|
93 |
-
|
94 |
for item in menu_data:
|
95 |
-
# Ensure the required fields exist
|
96 |
-
if "Section__c" not in item or "Veg_NonVeg__c" not in item:
|
97 |
-
continue
|
98 |
-
|
99 |
-
# Add the section to filtered data if not already present
|
100 |
if item["Section__c"] not in filtered_data:
|
101 |
filtered_data[item["Section__c"]] = []
|
102 |
|
103 |
-
# Apply filters based on preferences
|
104 |
if "All" in preferences:
|
105 |
filtered_data[item["Section__c"]].append(item)
|
106 |
elif "Veg" in preferences and item["Veg_NonVeg__c"] in ["Veg", "Both"]:
|
@@ -108,39 +31,35 @@ def filter_menu(preferences):
|
|
108 |
elif "Non-Veg" in preferences and item["Veg_NonVeg__c"] in ["Non-Veg", "Both"]:
|
109 |
filtered_data[item["Section__c"]].append(item)
|
110 |
|
111 |
-
|
112 |
-
html_content = '<div style="padding: 20px; max-width: 1200px; margin: auto; min-height: 2000px;">'
|
113 |
for section, items in filtered_data.items():
|
114 |
-
if items:
|
115 |
-
html_content += f"<h2 style='text-align: center;
|
116 |
-
html_content += '<div style="display: flex; flex-wrap: wrap; justify-content: center;
|
117 |
for item in items:
|
118 |
html_content += f"""
|
119 |
-
<div style="width: 300px; border: 1px solid #ddd; border-radius: 10px;
|
120 |
-
<img src="{item
|
121 |
-
<div style="padding:
|
122 |
-
<h3
|
123 |
-
<p
|
124 |
-
<p
|
125 |
</div>
|
126 |
</div>
|
127 |
"""
|
128 |
html_content += '</div>'
|
129 |
html_content += '</div>'
|
130 |
|
131 |
-
# Check if no items were matched
|
132 |
if not any(filtered_data.values()):
|
133 |
-
return "<p
|
134 |
|
135 |
return html_content
|
136 |
|
137 |
# Gradio App
|
138 |
with gr.Blocks() as app:
|
139 |
-
login_status = gr.State(value=False)
|
140 |
-
|
141 |
# Header
|
142 |
with gr.Row():
|
143 |
-
gr.HTML("<h1 style='text-align: center;
|
144 |
|
145 |
# Login Page
|
146 |
with gr.Row(visible=True) as login_page:
|
@@ -148,51 +67,34 @@ with gr.Blocks() as app:
|
|
148 |
login_email = gr.Textbox(label="Email")
|
149 |
login_password = gr.Textbox(label="Password", type="password")
|
150 |
login_button = gr.Button("Login")
|
151 |
-
signup_button = gr.Button("Go to Signup")
|
152 |
login_output = gr.Textbox(label="Status")
|
153 |
|
154 |
-
# Signup Page
|
155 |
-
with gr.Row(visible=False) as signup_page:
|
156 |
-
with gr.Column():
|
157 |
-
signup_name = gr.Textbox(label="Name")
|
158 |
-
signup_email = gr.Textbox(label="Email")
|
159 |
-
signup_phone = gr.Textbox(label="Phone")
|
160 |
-
signup_password = gr.Textbox(label="Password", type="password")
|
161 |
-
submit_signup = gr.Button("Signup")
|
162 |
-
login_redirect = gr.Button("Go to Login")
|
163 |
-
signup_output = gr.Textbox(label="Status")
|
164 |
-
|
165 |
# Menu Page
|
166 |
with gr.Row(visible=False) as menu_page:
|
167 |
preferences = gr.CheckboxGroup(
|
168 |
-
choices=["All", "Veg", "Non-Veg"], label="Filter Preference"
|
169 |
)
|
170 |
menu_output = gr.HTML()
|
171 |
-
footer = gr.HTML("<footer style='text-align: center; color: #666; padding: 20px; background-color: #f9f9f9;'>Thank you! Welcome again!</footer>")
|
172 |
|
173 |
-
#
|
|
|
|
|
|
|
|
|
174 |
def handle_login(email, password):
|
175 |
-
|
176 |
-
|
177 |
-
return gr.update(visible=False), gr.update(visible=True), status
|
178 |
else:
|
179 |
-
return gr.update(), gr.update(),
|
180 |
-
|
181 |
-
def handle_signup(name, email, phone, password):
|
182 |
-
return signup(name, email, phone, password)
|
183 |
|
|
|
184 |
def handle_menu(preferences):
|
185 |
content = filter_menu(preferences)
|
186 |
-
|
187 |
-
return gr.update(value=content, style={"min-height": "auto"})
|
188 |
-
else:
|
189 |
-
return gr.update(value=content, style={"min-height": "2000px"})
|
190 |
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
submit_signup.click(handle_signup, [signup_name, signup_email, signup_phone, signup_password], signup_output)
|
195 |
-
login_redirect.click(lambda: (gr.update(visible=False), gr.update(visible=True)), None, [signup_page, login_page])
|
196 |
preferences.change(handle_menu, [preferences], menu_output)
|
197 |
|
198 |
-
app.launch
|
|
|
1 |
import bcrypt
|
2 |
import gradio as gr
|
|
|
3 |
|
4 |
+
# Dummy data for testing (replace Salesforce queries temporarily)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
def load_menu_from_salesforce():
|
6 |
+
# Replace with a simple dummy dataset to test UI functionality
|
7 |
+
return [
|
8 |
+
{"Name": "Chicken Biryani", "Price__c": "8.99", "Description__c": "Delicious chicken biryani",
|
9 |
+
"Image1__c": "https://via.placeholder.com/200", "Veg_NonVeg__c": "Non-Veg", "Section__c": "Biryanis"},
|
10 |
+
{"Name": "Veg Biryani", "Price__c": "6.99", "Description__c": "Healthy veg biryani",
|
11 |
+
"Image1__c": "https://via.placeholder.com/200", "Veg_NonVeg__c": "Veg", "Section__c": "Biryanis"},
|
12 |
+
{"Name": "Paneer Tikka", "Price__c": "5.99", "Description__c": "Grilled paneer tikka",
|
13 |
+
"Image1__c": "https://via.placeholder.com/200", "Veg_NonVeg__c": "Veg", "Section__c": "Starters"},
|
14 |
+
{"Name": "Chicken Wings", "Price__c": "7.99", "Description__c": "Spicy chicken wings",
|
15 |
+
"Image1__c": "https://via.placeholder.com/200", "Veg_NonVeg__c": "Non-Veg", "Section__c": "Starters"}
|
16 |
+
]
|
|
|
|
|
17 |
|
18 |
# Function to filter menu items based on preference
|
19 |
def filter_menu(preferences):
|
20 |
menu_data = load_menu_from_salesforce()
|
21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
filtered_data = {}
|
|
|
23 |
for item in menu_data:
|
|
|
|
|
|
|
|
|
|
|
24 |
if item["Section__c"] not in filtered_data:
|
25 |
filtered_data[item["Section__c"]] = []
|
26 |
|
|
|
27 |
if "All" in preferences:
|
28 |
filtered_data[item["Section__c"]].append(item)
|
29 |
elif "Veg" in preferences and item["Veg_NonVeg__c"] in ["Veg", "Both"]:
|
|
|
31 |
elif "Non-Veg" in preferences and item["Veg_NonVeg__c"] in ["Non-Veg", "Both"]:
|
32 |
filtered_data[item["Section__c"]].append(item)
|
33 |
|
34 |
+
html_content = '<div style="padding: 20px; max-width: 1200px; margin: auto;">'
|
|
|
35 |
for section, items in filtered_data.items():
|
36 |
+
if items:
|
37 |
+
html_content += f"<h2 style='text-align: center;'>{section}</h2>"
|
38 |
+
html_content += '<div style="display: flex; flex-wrap: wrap; gap: 20px; justify-content: center;">'
|
39 |
for item in items:
|
40 |
html_content += f"""
|
41 |
+
<div style="width: 300px; border: 1px solid #ddd; border-radius: 10px; box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.1);">
|
42 |
+
<img src="{item['Image1__c']}" style="width: 100%; height: 200px; object-fit: cover;">
|
43 |
+
<div style="padding: 10px;">
|
44 |
+
<h3>{item['Name']}</h3>
|
45 |
+
<p>${item['Price__c']}</p>
|
46 |
+
<p>{item['Description__c']}</p>
|
47 |
</div>
|
48 |
</div>
|
49 |
"""
|
50 |
html_content += '</div>'
|
51 |
html_content += '</div>'
|
52 |
|
|
|
53 |
if not any(filtered_data.values()):
|
54 |
+
return "<p>No items match your filter.</p>"
|
55 |
|
56 |
return html_content
|
57 |
|
58 |
# Gradio App
|
59 |
with gr.Blocks() as app:
|
|
|
|
|
60 |
# Header
|
61 |
with gr.Row():
|
62 |
+
gr.HTML("<h1 style='text-align: center;'>Welcome to Biryani Hub</h1>")
|
63 |
|
64 |
# Login Page
|
65 |
with gr.Row(visible=True) as login_page:
|
|
|
67 |
login_email = gr.Textbox(label="Email")
|
68 |
login_password = gr.Textbox(label="Password", type="password")
|
69 |
login_button = gr.Button("Login")
|
|
|
70 |
login_output = gr.Textbox(label="Status")
|
71 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
# Menu Page
|
73 |
with gr.Row(visible=False) as menu_page:
|
74 |
preferences = gr.CheckboxGroup(
|
75 |
+
choices=["All", "Veg", "Non-Veg"], label="Filter Preference", value=["All"]
|
76 |
)
|
77 |
menu_output = gr.HTML()
|
|
|
78 |
|
79 |
+
# Footer
|
80 |
+
with gr.Row(visible=False) as footer:
|
81 |
+
gr.HTML("<footer style='text-align: center;'>Thank you! Welcome again!</footer>")
|
82 |
+
|
83 |
+
# Login Button Logic
|
84 |
def handle_login(email, password):
|
85 |
+
if email == "[email protected]" and password == "password":
|
86 |
+
return gr.update(visible=False), gr.update(visible=True), gr.update(visible=True), "Login successful!"
|
|
|
87 |
else:
|
88 |
+
return gr.update(), gr.update(), gr.update(), "Invalid login credentials."
|
|
|
|
|
|
|
89 |
|
90 |
+
# Menu Logic
|
91 |
def handle_menu(preferences):
|
92 |
content = filter_menu(preferences)
|
93 |
+
return content
|
|
|
|
|
|
|
94 |
|
95 |
+
login_button.click(
|
96 |
+
handle_login, [login_email, login_password], [login_page, menu_page, footer, login_output]
|
97 |
+
)
|
|
|
|
|
98 |
preferences.change(handle_menu, [preferences], menu_output)
|
99 |
|
100 |
+
app.launch()
|