nagasurendra commited on
Commit
8e40f0d
·
verified ·
1 Parent(s): 2d1d827

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +169 -150
app.py CHANGED
@@ -1,161 +1,180 @@
 
1
  import gradio as gr
2
- from simplesalesforce import Salesforce
3
- import re
4
- import random
5
- import smtplib
6
- from email.mime.text import MIMEText
7
  from datetime import datetime, timedelta
 
 
 
8
 
9
- # Salesforce Connection
10
  sf = Salesforce(username='diggavalli98@gmail.com', password='Sati@1020', security_token='sSSjyhInIsUohKpG8sHzty2q')
11
 
12
- # Helper Functions
13
- otp_storage = {}
14
 
15
- def send_otp(email):
16
- otp = random.randint(100000, 999999)
17
- otp_expiration = datetime.now() + timedelta(minutes=2)
18
- otp_storage[email] = {'otp': otp, 'expires': otp_expiration}
19
-
20
- # Sending email (Replace with actual SMTP setup)
21
- msg = MIMEText(f"Your OTP is {otp}. It expires in 2 minutes.")
22
- msg['Subject'] = 'Your OTP'
23
- msg['From'] = 'your_email@example.com'
24
- msg['To'] = email
 
 
25
  try:
26
- with smtplib.SMTP('smtp.example.com', 587) as server:
27
- server.starttls()
28
- server.login('your_email@example.com', 'your_email_password')
29
- server.send_message(msg)
30
- return "OTP sent successfully."
 
 
 
 
 
 
31
  except Exception as e:
32
- return f"Error sending OTP: {str(e)}"
33
-
34
- def validate_email(email):
35
- regex = r'^\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b'
36
- return re.match(regex, email)
37
 
38
- def validate_password(password):
39
- if (len(password) >= 6 and len(password) <= 10 and
40
- re.search(r'[A-Z]', password) and
41
- re.search(r'[a-z]', password) and
42
- re.search(r'\d', password) and
43
- re.search(r'[!@#$%^&*]', password)):
44
- return True
45
- return False
46
 
47
- # Functions for Gradio
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
 
49
- def login(email, password):
50
- if not email or not password:
51
- return "Email and Password cannot be empty."
52
-
53
- query = sf.query(f"SELECT Id, Password__c FROM Customer_Login__c WHERE Email__c = '{email}'")
54
- if not query['records']:
55
- return "Invalid email or password."
56
-
57
- record = query['records'][0]
58
- if password == record['Password__c']:
59
- sf.Customer_Login__c.update(record['Id'], {'Login_Status__c': 'Logged In'})
60
- return "Login successful. Redirecting to the menu page..."
61
- else:
62
- return "Invalid email or password."
63
-
64
- def signup(name, email, password, phone, otp, verify_otp):
65
- if not all([name, email, password, phone, otp]):
66
- return "All fields are required."
67
-
68
- if not validate_email(email):
69
- return "Invalid email format."
70
-
71
- if not validate_password(password):
72
- return "Password does not meet complexity requirements."
73
-
74
- if otp_storage.get(email) and otp_storage[email]['otp'] == int(otp):
75
- if datetime.now() > otp_storage[email]['expires']:
76
- return "OTP has expired. Please resend OTP."
77
-
78
- # Store data in Salesforce
79
- try:
80
- sf.Customer_Login__c.create({
81
- 'Name': name,
82
- 'Email__c': email,
83
- 'Password__c': password,
84
- 'Phone_Number__c': phone,
85
- 'OTP__c': str(otp),
86
- 'OTP_Expiration__c': otp_storage[email]['expires'],
87
- 'Login_Status__c': 'Logged Out'
88
- })
89
- return "Signup successful. Please log in."
90
- except Exception as e:
91
- return f"Error storing data: {str(e)}"
92
- else:
93
- return "Invalid OTP."
94
-
95
- def forgot_password(email, otp, new_password, confirm_password):
96
- if not email:
97
- return "Email is required."
98
-
99
- if not validate_email(email):
100
- return "Invalid email format."
101
-
102
- if email in otp_storage and otp_storage[email]['otp'] == int(otp):
103
- if datetime.now() > otp_storage[email]['expires']:
104
- return "OTP has expired."
105
-
106
- if new_password != confirm_password:
107
- return "Passwords do not match."
108
-
109
- if not validate_password(new_password):
110
- return "Password does not meet complexity requirements."
111
-
112
- # Update password in Salesforce
113
- query = sf.query(f"SELECT Id FROM Customer_Login__c WHERE Email__c = '{email}'")
114
- if query['records']:
115
- record_id = query['records'][0]['Id']
116
- sf.Customer_Login__c.update(record_id, {'Password__c': new_password})
117
- return "Password updated successfully."
118
- else:
119
- return "Email not found."
120
- else:
121
- return "Invalid OTP."
122
-
123
- # Gradio Interface
124
- login_ui = gr.Interface(
125
- fn=login,
126
- inputs=[gr.Textbox(label="Email"), gr.Textbox(label="Password", type="password")],
127
- outputs="text",
128
- title="Login Page"
129
- )
130
-
131
- signup_ui = gr.Interface(
132
- fn=signup,
133
- inputs=[
134
- gr.Textbox(label="Name"),
135
- gr.Textbox(label="Email"),
136
- gr.Textbox(label="Password", type="password"),
137
- gr.Textbox(label="Phone Number"),
138
- gr.Textbox(label="Enter OTP"),
139
- ],
140
- outputs="text",
141
- title="Signup Page"
142
- )
143
-
144
- forgot_password_ui = gr.Interface(
145
- fn=forgot_password,
146
- inputs=[
147
- gr.Textbox(label="Email"),
148
- gr.Textbox(label="OTP"),
149
- gr.Textbox(label="New Password", type="password"),
150
- gr.Textbox(label="Confirm Password", type="password")
151
- ],
152
- outputs="text",
153
- title="Forgot Password"
154
- )
155
-
156
- dashboard = gr.TabbedInterface(
157
- [login_ui, signup_ui, forgot_password_ui],
158
- tab_names=["Login", "Signup", "Forgot Password"]
159
- )
160
-
161
- dashboard.launch()
 
 
 
 
 
1
+ # app.py
2
  import gradio as gr
3
+ from simple_salesforce import Salesforce
 
 
 
 
4
  from datetime import datetime, timedelta
5
+ import random
6
+
7
+ # Salesforce Authentication
8
 
 
9
  sf = Salesforce(username='diggavalli98@gmail.com', password='Sati@1020', security_token='sSSjyhInIsUohKpG8sHzty2q')
10
 
 
 
11
 
12
+ otp_data = {} # Temporary OTP storage
13
+
14
+ # Navigation state
15
+ current_page = "login"
16
+
17
+ def set_page(page):
18
+ global current_page
19
+ current_page = page
20
+
21
+ # Login Page Function
22
+ def login(email, password):
23
+ global current_page
24
  try:
25
+ query_result = sf.query(f"SELECT Password__c FROM Customer_Login__c WHERE Email__c = '{email}'")
26
+ if not query_result['records']:
27
+ return "Error: Email not found."
28
+
29
+ stored_password = query_result['records'][0]['Password__c']
30
+ if password != stored_password:
31
+ return "Error: Invalid password."
32
+
33
+ sf.Customer_Login__c.update(query_result['records'][0]['Id'], {'Login_Status__c': 'Logged In'})
34
+ set_page("menu")
35
+ return "Login successful! Redirecting to menu..."
36
  except Exception as e:
37
+ return f"Error: {str(e)}"
 
 
 
 
38
 
39
+ def login_ui():
40
+ return gr.Interface(
41
+ fn=login,
42
+ inputs=[gr.Textbox(label="Email"), gr.Password(label="Password")],
43
+ outputs="text",
44
+ title="Login Page"
45
+ )
 
46
 
47
+ # Signup Page Functions
48
+ def send_otp(email):
49
+ try:
50
+ query_result = sf.query(f"SELECT Id FROM Customer_Login__c WHERE Email__c = '{email}'")
51
+ if query_result['records']:
52
+ return "Error: Email already registered."
53
+
54
+ otp = random.randint(100000, 999999)
55
+ otp_data[email] = {
56
+ "otp": otp,
57
+ "expiration": datetime.now() + timedelta(minutes=2)
58
+ }
59
+ print(f"OTP for {email}: {otp}") # Simulated email sending
60
+ return "OTP sent successfully! Check your email."
61
+ except Exception as e:
62
+ return f"Error: {str(e)}"
63
 
64
+ def verify_otp(email, otp, name, password, phone):
65
+ try:
66
+ if email not in otp_data or otp_data[email]['expiration'] < datetime.now():
67
+ return "Error: OTP expired or invalid."
68
+
69
+ if otp_data[email]['otp'] != int(otp):
70
+ return "Error: Incorrect OTP."
71
+
72
+ if not (6 <= len(password) <= 10 and
73
+ any(c.isupper() for c in password) and
74
+ any(c.islower() for c in password) and
75
+ any(c.isdigit() for c in password) and
76
+ any(c in "!@#$%^&*" for c in password)):
77
+ return "Error: Password does not meet complexity requirements."
78
+
79
+ sf.Customer_Login__c.create({
80
+ "Name": name,
81
+ "Email__c": email,
82
+ "Password__c": password,
83
+ "Phone_Number__c": phone,
84
+ "Login_Status__c": "Logged Out"
85
+ })
86
+ del otp_data[email]
87
+ set_page("login")
88
+ return "Signup successful! Redirecting to login page..."
89
+ except Exception as e:
90
+ return f"Error: {str(e)}"
91
+
92
+ def signup_ui():
93
+ return gr.Interface(
94
+ fn=lambda email, otp, name, password, phone: (
95
+ send_otp(email) if otp == "" else verify_otp(email, otp, name, password, phone)
96
+ ),
97
+ inputs=[
98
+ gr.Textbox(label="Email"),
99
+ gr.Textbox(label="OTP (Leave blank to send OTP)"),
100
+ gr.Textbox(label="Name"),
101
+ gr.Password(label="Password"),
102
+ gr.Textbox(label="Phone Number")
103
+ ],
104
+ outputs="text",
105
+ title="Signup Page"
106
+ )
107
+
108
+ # Forgot Password Page Function
109
+ def forgot_password(email, otp, new_password):
110
+ global current_page
111
+ try:
112
+ query_result = sf.query(f"SELECT Id FROM Customer_Login__c WHERE Email__c = '{email}'")
113
+ if not query_result['records']:
114
+ return "Error: Email not found."
115
+
116
+ if otp == "":
117
+ otp = random.randint(100000, 999999)
118
+ otp_data[email] = {
119
+ "otp": otp,
120
+ "expiration": datetime.now() + timedelta(minutes=2)
121
+ }
122
+ print(f"OTP for {email}: {otp}")
123
+ return "OTP sent successfully! Check your email."
124
+
125
+ if email in otp_data and otp_data[email]['otp'] == int(otp) and otp_data[email]['expiration'] > datetime.now():
126
+ if not (6 <= len(new_password) <= 10 and
127
+ any(c.isupper() for c in new_password) and
128
+ any(c.islower() for c in new_password) and
129
+ any(c.isdigit() for c in new_password) and
130
+ any(c in "!@#$%^&*" for c in new_password)):
131
+ return "Error: Password does not meet complexity requirements."
132
+
133
+ sf.Customer_Login__c.update(query_result['records'][0]['Id'], {"Password__c": new_password})
134
+ del otp_data[email]
135
+ set_page("login")
136
+ return "Password updated successfully! Redirecting to login page..."
137
+
138
+ return "Error: Invalid or expired OTP."
139
+ except Exception as e:
140
+ return f"Error: {str(e)}"
141
+
142
+ def forgot_password_ui():
143
+ return gr.Interface(
144
+ fn=forgot_password,
145
+ inputs=[
146
+ gr.Textbox(label="Email"),
147
+ gr.Textbox(label="OTP (Leave blank to send OTP)"),
148
+ gr.Password(label="New Password")
149
+ ],
150
+ outputs="text",
151
+ title="Forgot Password Page"
152
+ )
153
+
154
+ # Menu Page Function
155
+ def menu_page():
156
+ global current_page
157
+ return "Welcome to the Menu Page! Select your options here."
158
+
159
+ def menu_ui():
160
+ return gr.Interface(
161
+ fn=menu_page,
162
+ inputs=None,
163
+ outputs="text",
164
+ title="Menu Page"
165
+ )
166
+
167
+ # Main App Logic
168
+ def render_page():
169
+ if current_page == "login":
170
+ return login_ui()
171
+ elif current_page == "signup":
172
+ return signup_ui()
173
+ elif current_page == "forgot_password":
174
+ return forgot_password_ui()
175
+ elif current_page == "menu":
176
+ return menu_ui()
177
+
178
+ # Launch App
179
+ app = render_page()
180
+ app.launch()