Spaces:
Runtime error
Runtime error
| # app.py | |
| import gradio as gr | |
| from simple_salesforce import Salesforce | |
| from datetime import datetime, timedelta | |
| import random | |
| # Salesforce Authentication | |
| sf = Salesforce(username='diggavalli98@gmail.com', password='Sati@1020', security_token='sSSjyhInIsUohKpG8sHzty2q') | |
| otp_data = {} # Temporary OTP storage | |
| # Navigation state | |
| current_page = "login" | |
| def set_page(page): | |
| global current_page | |
| current_page = page | |
| # Login Page Function | |
| def login(email, password): | |
| global current_page | |
| try: | |
| query_result = sf.query(f"SELECT Password__c FROM Customer_Login__c WHERE Email__c = '{email}'") | |
| if not query_result['records']: | |
| return "Error: Email not found." | |
| stored_password = query_result['records'][0]['Password__c'] | |
| if password != stored_password: | |
| return "Error: Invalid password." | |
| sf.Customer_Login__c.update(query_result['records'][0]['Id'], {'Login_Status__c': 'Logged In'}) | |
| set_page("menu") | |
| return "Login successful! Redirecting to menu..." | |
| except Exception as e: | |
| return f"Error: {str(e)}" | |
| def login_ui(): | |
| return gr.Interface( | |
| fn=login, | |
| inputs=[gr.Textbox(label="Email"), gr.Password(label="Password")], | |
| outputs="text", | |
| title="Login Page" | |
| ) | |
| # Signup Page Functions | |
| def send_otp(email): | |
| try: | |
| query_result = sf.query(f"SELECT Id FROM Customer_Login__c WHERE Email__c = '{email}'") | |
| if query_result['records']: | |
| return "Error: Email already registered." | |
| otp = random.randint(100000, 999999) | |
| otp_data[email] = { | |
| "otp": otp, | |
| "expiration": datetime.now() + timedelta(minutes=2) | |
| } | |
| print(f"OTP for {email}: {otp}") # Simulated email sending | |
| return "OTP sent successfully! Check your email." | |
| except Exception as e: | |
| return f"Error: {str(e)}" | |
| def verify_otp(email, otp, name, password, phone): | |
| try: | |
| if email not in otp_data or otp_data[email]['expiration'] < datetime.now(): | |
| return "Error: OTP expired or invalid." | |
| if otp_data[email]['otp'] != int(otp): | |
| return "Error: Incorrect OTP." | |
| if not (6 <= len(password) <= 10 and | |
| any(c.isupper() for c in password) and | |
| any(c.islower() for c in password) and | |
| any(c.isdigit() for c in password) and | |
| any(c in "!@#$%^&*" for c in password)): | |
| return "Error: Password does not meet complexity requirements." | |
| sf.Customer_Login__c.create({ | |
| "Name": name, | |
| "Email__c": email, | |
| "Password__c": password, | |
| "Phone_Number__c": phone, | |
| "Login_Status__c": "Logged Out" | |
| }) | |
| del otp_data[email] | |
| set_page("login") | |
| return "Signup successful! Redirecting to login page..." | |
| except Exception as e: | |
| return f"Error: {str(e)}" | |
| def signup_ui(): | |
| return gr.Interface( | |
| fn=lambda email, otp, name, password, phone: ( | |
| send_otp(email) if otp == "" else verify_otp(email, otp, name, password, phone) | |
| ), | |
| inputs=[ | |
| gr.Textbox(label="Email"), | |
| gr.Textbox(label="OTP (Leave blank to send OTP)"), | |
| gr.Textbox(label="Name"), | |
| gr.Password(label="Password"), | |
| gr.Textbox(label="Phone Number") | |
| ], | |
| outputs="text", | |
| title="Signup Page" | |
| ) | |
| # Forgot Password Page Function | |
| def forgot_password(email, otp, new_password): | |
| global current_page | |
| try: | |
| query_result = sf.query(f"SELECT Id FROM Customer_Login__c WHERE Email__c = '{email}'") | |
| if not query_result['records']: | |
| return "Error: Email not found." | |
| if otp == "": | |
| otp = random.randint(100000, 999999) | |
| otp_data[email] = { | |
| "otp": otp, | |
| "expiration": datetime.now() + timedelta(minutes=2) | |
| } | |
| print(f"OTP for {email}: {otp}") | |
| return "OTP sent successfully! Check your email." | |
| if email in otp_data and otp_data[email]['otp'] == int(otp) and otp_data[email]['expiration'] > datetime.now(): | |
| if not (6 <= len(new_password) <= 10 and | |
| any(c.isupper() for c in new_password) and | |
| any(c.islower() for c in new_password) and | |
| any(c.isdigit() for c in new_password) and | |
| any(c in "!@#$%^&*" for c in new_password)): | |
| return "Error: Password does not meet complexity requirements." | |
| sf.Customer_Login__c.update(query_result['records'][0]['Id'], {"Password__c": new_password}) | |
| del otp_data[email] | |
| set_page("login") | |
| return "Password updated successfully! Redirecting to login page..." | |
| return "Error: Invalid or expired OTP." | |
| except Exception as e: | |
| return f"Error: {str(e)}" | |
| def forgot_password_ui(): | |
| return gr.Interface( | |
| fn=forgot_password, | |
| inputs=[ | |
| gr.Textbox(label="Email"), | |
| gr.Textbox(label="OTP (Leave blank to send OTP)"), | |
| gr.Password(label="New Password") | |
| ], | |
| outputs="text", | |
| title="Forgot Password Page" | |
| ) | |
| # Menu Page Function | |
| def menu_page(): | |
| global current_page | |
| return "Welcome to the Menu Page! Select your options here." | |
| def menu_ui(): | |
| return gr.Interface( | |
| fn=menu_page, | |
| inputs=None, | |
| outputs="text", | |
| title="Menu Page" | |
| ) | |
| # Main App Logic | |
| def render_page(): | |
| if current_page == "login": | |
| return login_ui() | |
| elif current_page == "signup": | |
| return signup_ui() | |
| elif current_page == "forgot_password": | |
| return forgot_password_ui() | |
| elif current_page == "menu": | |
| return menu_ui() | |
| # Launch App | |
| app = render_page() | |
| app.launch() | |