nagasurendra commited on
Commit
91f1be6
·
verified ·
1 Parent(s): e8fd9dc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +92 -104
app.py CHANGED
@@ -1,13 +1,10 @@
1
-
2
-
3
-
4
  import bcrypt
5
  import gradio as gr
6
  from simple_salesforce import Salesforce
 
7
  # Salesforce Connection
8
  sf = Salesforce(username='diggavalli98@gmail.com', password='Sati@1020', security_token='sSSjyhInIsUohKpG8sHzty2q')
9
 
10
-
11
  # Function to Hash Password
12
  def hash_password(password):
13
  return bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()).decode('utf-8')
@@ -16,111 +13,102 @@ def hash_password(password):
16
  def verify_password(plain_password, hashed_password):
17
  return bcrypt.checkpw(plain_password.encode('utf-8'), hashed_password.encode('utf-8'))
18
 
19
- # Signup function with email validation
20
  def signup(name, email, phone, password):
21
- try:
22
- email = email.strip()
23
-
24
- # Check if the email already exists in Salesforce
25
- query = f"SELECT Id FROM Customer_Login__c WHERE Email__c = '{email}'"
26
- result = sf.query(query)
27
-
28
- if len(result['records']) > 0:
29
- # Email already exists
30
- return "Email already exists! Please use a different email."
31
-
32
- # Hash the password
33
- hashed_password = hash_password(password)
34
-
35
- # Create the new user record
36
- sf.Customer_Login__c.create({
37
- 'Name': name.strip(),
38
- 'Email__c': email,
39
- 'Phone_Number__c': phone.strip(),
40
- 'Password__c': hashed_password # Use Password3__c field for hashed passwords
41
- })
42
- return "Signup successful! You can now login."
43
- except Exception as e:
44
- return f"Error during signup: {str(e)}"
45
-
46
- # Login function
47
  def login(email, password):
48
- try:
49
- email = email.strip()
50
- password = password.strip()
51
-
52
- # Query Salesforce for user details
53
- query = f"SELECT Name, Email__c, Phone_Number__c, Password__c FROM Customer_Login__c WHERE Email__c = '{email}'"
54
- result = sf.query(query)
55
-
56
- if len(result['records']) == 0:
57
- return "Invalid email or password.", None, None, None
58
-
59
- user = result['records'][0]
60
- stored_password = user['Password__c']
61
-
62
- print(f"Entered Email: {email}")
63
- print(f"Entered Password: {password}")
64
- print(f"Stored Hashed Password: {stored_password}")
65
-
66
- # Verify the entered password with the stored hash
67
- if verify_password(password, stored_password):
68
- print("Password matched!")
69
- return "Login successful!", user['Name'], user['Email__c'], user['Phone_Number__c']
70
- else:
71
- print("Password did not match!")
72
- return "Invalid email or password.", None, None, None
73
- except Exception as e:
74
- print(f"Error during login: {str(e)}")
75
- return f"Error during login: {str(e)}", None, None, None
76
-
77
- # Gradio Signup Page
78
- def signup_page(name, email, phone, password):
79
- response = signup(name, email, phone, password)
80
- return response
81
-
82
- signup_interface = gr.Interface(
83
- fn=signup_page,
84
- inputs=[
85
- gr.Textbox(label="Name"),
86
- gr.Textbox(label="Email"),
87
- gr.Textbox(label="Phone"),
88
- gr.Textbox(label="Password", type="password"),
89
- ],
90
- outputs=gr.Textbox(label="Signup Status"),
91
- title="Signup Page"
92
- )
93
-
94
- # Gradio Login Page
95
- def login_page(email, password):
96
- login_status, name, email, phone = login(email, password)
97
- if login_status == "Login successful!":
98
- return login_status, name, email, phone
99
  else:
100
- return login_status, "Error", "Error", "Error"
101
-
102
- login_interface = gr.Interface(
103
- fn=login_page,
104
- inputs=[
105
- gr.Textbox(label="Email"),
106
- gr.Textbox(label="Password", type="password"),
107
- ],
108
- outputs=[
109
- gr.Textbox(label="Login Status"),
110
- gr.Textbox(label="Name"),
111
- gr.Textbox(label="Email"),
112
- gr.Textbox(label="Phone"),
113
- ],
114
- title="Login Page"
115
- )
116
-
117
- # Combine Pages in Gradio
118
  with gr.Blocks() as app:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
119
  with gr.Tab("Signup"):
120
- signup_interface.render()
 
 
 
 
 
 
 
121
 
122
- with gr.Tab("Login"):
123
- login_interface.render()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
124
 
125
- # Launch Gradio App
126
  app.launch()
 
 
 
 
1
  import bcrypt
2
  import gradio as gr
3
  from simple_salesforce import Salesforce
4
+
5
  # Salesforce Connection
6
  sf = Salesforce(username='diggavalli98@gmail.com', 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')
 
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
+ email = email.strip()
19
+ query = f"SELECT Id FROM Customer_Login__c WHERE Email__c = '{email}'"
20
+ result = sf.query(query)
21
+
22
+ if len(result['records']) > 0:
23
+ return "Email already exists! Please use a different email."
24
+
25
+ hashed_password = hash_password(password)
26
+ sf.Customer_Login__c.create({
27
+ 'Name': name.strip(),
28
+ 'Email__c': email,
29
+ 'Phone_Number__c': phone.strip(),
30
+ 'Password__c': hashed_password
31
+ })
32
+ return "Signup successful! You can now login."
33
+
34
+ # Login Function
 
 
 
 
 
 
 
 
 
35
  def login(email, password):
36
+ query = f"SELECT Name, Password__c FROM Customer_Login__c WHERE Email__c = '{email.strip()}'"
37
+ result = sf.query(query)
38
+
39
+ if len(result['records']) == 0:
40
+ return "Invalid email or password.", None
41
+
42
+ user = result['records'][0]
43
+ if verify_password(password.strip(), user['Password__c']):
44
+ return "Login successful!", user['Name']
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  else:
46
+ return "Invalid email or password.", None
47
+
48
+ # Fetch Menu Records Based on Filters
49
+ def fetch_menu(veg, non_veg):
50
+ filters = []
51
+ if veg:
52
+ filters.append("'Veg'")
53
+ if non_veg:
54
+ filters.append("'Non Veg'")
55
+ if len(filters) == 0:
56
+ filters.append("'Veg', 'Non Veg', 'Both'")
57
+
58
+ query = f"SELECT Name, Price__c, Image1__c, Description__c FROM Menu_Item__c WHERE Veg_NonVeg__c IN ({', '.join(filters)})"
59
+ result = sf.query(query)
60
+ return [{"Name": record['Name'], "Price": record['Price__c'], "Image": record['Image1__c'], "Description": record['Description__c']} for record in result['records']]
61
+
62
+ # Gradio Pages
 
63
  with gr.Blocks() as app:
64
+ with gr.Tab("Login"):
65
+ with gr.Row():
66
+ login_email = gr.Textbox(label="Email")
67
+ login_password = gr.Textbox(label="Password", type="password")
68
+ login_btn = gr.Button("Login")
69
+ login_output = gr.Textbox(label="Status")
70
+ signup_btn = gr.Button("Go to Signup")
71
+
72
+ def login_action(email, password):
73
+ status, user = login(email, password)
74
+ if status == "Login successful!":
75
+ return gr.update(visible=False), gr.update(visible=False), gr.update(visible=True, value=f"Welcome, {user}!")
76
+ else:
77
+ return gr.update(value=status), gr.update(value="")
78
+
79
+ login_btn.click(login_action, [login_email, login_password], [login_output, gr.State()])
80
+
81
+ signup_btn.click(lambda: (gr.update(visible=False), gr.update(visible=True)), None, [gr.Row(), gr.Tab("Signup")])
82
+
83
  with gr.Tab("Signup"):
84
+ with gr.Row():
85
+ signup_name = gr.Textbox(label="Name")
86
+ signup_email = gr.Textbox(label="Email")
87
+ signup_phone = gr.Textbox(label="Phone")
88
+ signup_password = gr.Textbox(label="Password", type="password")
89
+ signup_btn = gr.Button("Signup")
90
+ signup_output = gr.Textbox(label="Status")
91
+ back_to_login = gr.Button("Go to Login")
92
 
93
+ def signup_action(name, email, phone, password):
94
+ status = signup(name, email, phone, password)
95
+ return status
96
+
97
+ signup_btn.click(signup_action, [signup_name, signup_email, signup_phone, signup_password], [signup_output])
98
+ back_to_login.click(lambda: (gr.update(visible=False), gr.update(visible=True)), None, [gr.Row(), gr.Tab("Login")])
99
+
100
+ with gr.Tab("Menu"):
101
+ with gr.Row():
102
+ veg_filter = gr.Checkbox(label="Veg")
103
+ non_veg_filter = gr.Checkbox(label="Non-Veg")
104
+ show_menu_btn = gr.Button("Show Menu")
105
+
106
+ menu_display = gr.Dataframe(headers=["Name", "Price", "Image", "Description"])
107
+
108
+ def show_menu(veg, non_veg):
109
+ menu = fetch_menu(veg, non_veg)
110
+ return menu
111
+
112
+ show_menu_btn.click(show_menu, [veg_filter, non_veg_filter], menu_display)
113
 
 
114
  app.launch()