Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import random | |
| import os | |
| import smtplib | |
| from email.message import EmailMessage | |
| import json | |
| # ---------- UTILITY FUNCTIONS ---------- | |
| def load_data(): | |
| try: | |
| with open('accounts.json', 'r') as f: | |
| return json.load(f) | |
| except: | |
| return {} | |
| def save_data(data): | |
| with open('accounts.json', 'w') as f: | |
| json.dump(data, f, indent=4) | |
| def send_otp(email): | |
| otp = str(random.randint(1000, 9999)) | |
| st.session_state['otp'] = otp | |
| sender_email = os.environ['EMAIL_USER'] | |
| sender_password = os.environ['EMAIL_PASS'] | |
| msg = EmailMessage() | |
| msg['Subject'] = "Your Baryashah Bank OTP" | |
| msg['From'] = sender_email | |
| msg['To'] = email | |
| msg.set_content(f"Your OTP for Baryashah Bank is: {otp}") | |
| with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp: | |
| smtp.login(sender_email, sender_password) | |
| smtp.send_message(msg) | |
| st.info(f"β OTP sent to {email}") | |
| return otp | |
| def verify_otp(user_input): | |
| return user_input == st.session_state.get('otp') | |
| # ---------- APP UI ---------- | |
| st.title("π Baryashah Bank Pro Dashboard π") | |
| st.write("Professional banking simulation. Login, withdraw, deposit & reset password securely.") | |
| data = load_data() | |
| menu = ["Create Account", "Login", "Reset Password"] | |
| choice = st.sidebar.selectbox("Menu", menu) | |
| # ---------- CREATE ACCOUNT ---------- | |
| if choice == "Create Account": | |
| st.header("π Create New Account") | |
| name = st.text_input("Full Name") | |
| email = st.text_input("Email") | |
| age = st.number_input("Age", min_value=1, max_value=120) | |
| password = st.text_input("Create Password", type="password") | |
| if st.button("Create Account"): | |
| if age < 18: | |
| st.error("β Must be 18 or older to create an account.") | |
| elif email in data: | |
| st.error("β Account with this email already exists.") | |
| else: | |
| data[email] = { | |
| "name": name, | |
| "age": age, | |
| "password": password, | |
| "balance": 0, | |
| "transactions": [] | |
| } | |
| save_data(data) | |
| st.success(f"β Account created for {name}!") | |
| # ---------- LOGIN ---------- | |
| elif choice == "Login": | |
| st.header("π Login") | |
| email = st.text_input("Email") | |
| password = st.text_input("Password", type="password") | |
| if st.button("Send OTP"): | |
| if email not in data: | |
| st.error("β Email not registered!") | |
| elif data[email]["password"] != password: | |
| st.error("β Wrong password!") | |
| else: | |
| send_otp(email) | |
| otp_input = st.text_input("Enter OTP") | |
| if st.button("Verify OTP"): | |
| if verify_otp(otp_input): | |
| st.success(f"β Welcome {data[email]['name']}!") | |
| # Dashboard | |
| st.subheader("π° Account Dashboard") | |
| st.metric("Balance", f"${data[email]['balance']}") | |
| col1, col2 = st.columns(2) | |
| with col1: | |
| deposit_amount = st.number_input("Deposit Amount", min_value=1) | |
| if st.button("Deposit"): | |
| data[email]['balance'] += deposit_amount | |
| data[email]['transactions'].append(f"Deposit: +${deposit_amount}") | |
| save_data(data) | |
| st.success(f"β Deposited ${deposit_amount} successfully!") | |
| with col2: | |
| withdraw_amount = st.number_input("Withdraw Amount", min_value=1) | |
| if st.button("Withdraw"): | |
| # Send OTP before withdrawal | |
| send_otp(email) | |
| otp_withdraw = st.text_input("Enter OTP to withdraw") | |
| if st.button("Confirm Withdrawal"): | |
| if verify_otp(otp_withdraw): | |
| if withdraw_amount <= data[email]['balance']: | |
| data[email]['balance'] -= withdraw_amount | |
| data[email]['transactions'].append(f"Withdraw: -${withdraw_amount}") | |
| save_data(data) | |
| st.success(f"β Withdrawn ${withdraw_amount} successfully!") | |
| else: | |
| st.error("β Insufficient Balance!") | |
| else: | |
| st.error("β Wrong OTP!") | |
| # Show transactions | |
| st.subheader("π Transaction History") | |
| for t in data[email]['transactions']: | |
| st.write(t) | |
| else: | |
| st.error("β OTP incorrect!") | |
| # ---------- PASSWORD RESET ---------- | |
| elif choice == "Reset Password": | |
| st.header("π Reset Password") | |
| email = st.text_input("Registered Email") | |
| if st.button("Send OTP for Reset"): | |
| if email not in data: | |
| st.error("β Email not registered!") | |
| else: | |
| send_otp(email) | |
| otp_input = st.text_input("Enter OTP received") | |
| new_pass = st.text_input("Enter New Password", type="password") | |
| if st.button("Reset Password"): | |
| if verify_otp(otp_input): | |
| data[email]['password'] = new_pass | |
| save_data(data) | |
| st.success("β Password reset successfully!") | |
| else: | |
| st.error("β OTP incorrect!") |