Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,183 +1,148 @@
|
|
|
|
|
|
|
|
| 1 |
import os
|
|
|
|
|
|
|
| 2 |
import json
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
body {
|
| 16 |
-
background-color: #f5f7fa;
|
| 17 |
-
}
|
| 18 |
-
h1 {
|
| 19 |
-
color: #1f77b4;
|
| 20 |
-
text-align: center;
|
| 21 |
-
font-family: 'Arial', sans-serif;
|
| 22 |
-
}
|
| 23 |
-
.stButton>button {
|
| 24 |
-
background-color: #1f77b4;
|
| 25 |
-
color: white;
|
| 26 |
-
height: 3em;
|
| 27 |
-
width: 100%;
|
| 28 |
-
border-radius: 10px;
|
| 29 |
-
border: none;
|
| 30 |
-
font-size: 16px;
|
| 31 |
-
margin-top: 10px;
|
| 32 |
-
}
|
| 33 |
-
.stTextInput>div>input {
|
| 34 |
-
height: 2.5em;
|
| 35 |
-
border-radius: 10px;
|
| 36 |
-
border: 1px solid #ccc;
|
| 37 |
-
padding-left: 10px;
|
| 38 |
-
}
|
| 39 |
-
</style>
|
| 40 |
-
""", unsafe_allow_html=True)
|
| 41 |
-
|
| 42 |
-
st.markdown("<h1>π Baryashah Bank Pro π</h1>", unsafe_allow_html=True)
|
| 43 |
-
st.write("---")
|
| 44 |
-
|
| 45 |
-
# ----------------- Helper Functions -----------------
|
| 46 |
-
def load_accounts():
|
| 47 |
-
with open("accounts.json","r") as f:
|
| 48 |
-
return json.load(f)
|
| 49 |
-
|
| 50 |
-
def save_accounts(data):
|
| 51 |
-
with open("accounts.json","w") as f:
|
| 52 |
json.dump(data, f, indent=4)
|
| 53 |
|
| 54 |
-
def
|
| 55 |
-
otp = str(random.randint(1000,9999))
|
| 56 |
st.session_state['otp'] = otp
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
return otp
|
| 59 |
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
|
|
|
| 64 |
choice = st.sidebar.selectbox("Menu", menu)
|
| 65 |
|
| 66 |
-
# ----------
|
| 67 |
-
if choice=="Create Account":
|
| 68 |
-
st.
|
| 69 |
name = st.text_input("Full Name")
|
| 70 |
email = st.text_input("Email")
|
| 71 |
age = st.number_input("Age", min_value=1, max_value=120)
|
| 72 |
-
password = st.text_input("Password", type="password")
|
| 73 |
-
|
| 74 |
-
|
| 75 |
if st.button("Create Account"):
|
| 76 |
if age < 18:
|
| 77 |
-
st.error("β
|
| 78 |
-
elif email in
|
| 79 |
-
st.error("β Account
|
| 80 |
-
elif len(pin)!=4 or not pin.isdigit():
|
| 81 |
-
st.error("β PIN must be 4 digits")
|
| 82 |
else:
|
| 83 |
-
|
| 84 |
"name": name,
|
| 85 |
"age": age,
|
| 86 |
"password": password,
|
| 87 |
-
"pin": pin,
|
| 88 |
"balance": 0,
|
| 89 |
"transactions": []
|
| 90 |
}
|
| 91 |
-
|
| 92 |
-
st.success(f"
|
| 93 |
-
st.balloons()
|
| 94 |
-
|
| 95 |
-
# ----------------- Reset Password -----------------
|
| 96 |
-
elif choice=="Reset Password":
|
| 97 |
-
st.subheader("Reset Password")
|
| 98 |
-
email = st.text_input("Enter Email")
|
| 99 |
-
if email in accounts:
|
| 100 |
-
if st.button("Send OTP"):
|
| 101 |
-
send_otp_simulation(email)
|
| 102 |
-
otp_input = st.text_input("Enter OTP")
|
| 103 |
-
new_pass = st.text_input("New Password", type="password")
|
| 104 |
-
if st.button("Reset Password Now"):
|
| 105 |
-
if 'otp' in st.session_state and otp_input == st.session_state['otp']:
|
| 106 |
-
accounts[email]['password'] = new_pass
|
| 107 |
-
save_accounts(accounts)
|
| 108 |
-
st.success("β
Password reset successfully!")
|
| 109 |
-
else:
|
| 110 |
-
st.error("β Invalid OTP!")
|
| 111 |
-
|
| 112 |
-
# ----------------- Recover Account Name -----------------
|
| 113 |
-
elif choice=="Recover Account Name":
|
| 114 |
-
st.subheader("Recover Account Name")
|
| 115 |
-
email = st.text_input("Enter Email")
|
| 116 |
-
password = st.text_input("Enter Password", type="password")
|
| 117 |
-
if st.button("Recover"):
|
| 118 |
-
if email in accounts and accounts[email]['password']==password:
|
| 119 |
-
st.success(f"Your account name is: {accounts[email]['name']}")
|
| 120 |
-
else:
|
| 121 |
-
st.error("β Invalid credentials!")
|
| 122 |
|
| 123 |
-
# ----------
|
| 124 |
-
elif choice=="Login":
|
| 125 |
-
st.
|
| 126 |
email = st.text_input("Email")
|
| 127 |
password = st.text_input("Password", type="password")
|
| 128 |
-
|
| 129 |
-
if st.button("
|
| 130 |
-
if email in
|
| 131 |
-
st.
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
else:
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
fig = px.bar(x=list(range(1,len(amounts)+1)), y=amounts, color=types,
|
| 177 |
-
labels={'x':'Transaction','y':'Amount'},
|
| 178 |
-
title="Transaction History")
|
| 179 |
-
st.plotly_chart(fig)
|
| 180 |
-
else:
|
| 181 |
-
st.error("β 2FA OTP Invalid!")
|
| 182 |
else:
|
| 183 |
-
st.error("β
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import random
|
| 3 |
import os
|
| 4 |
+
import smtplib
|
| 5 |
+
from email.message import EmailMessage
|
| 6 |
import json
|
| 7 |
+
|
| 8 |
+
# ---------- UTILITY FUNCTIONS ----------
|
| 9 |
+
|
| 10 |
+
def load_data():
|
| 11 |
+
try:
|
| 12 |
+
with open('accounts.json', 'r') as f:
|
| 13 |
+
return json.load(f)
|
| 14 |
+
except:
|
| 15 |
+
return {}
|
| 16 |
+
|
| 17 |
+
def save_data(data):
|
| 18 |
+
with open('accounts.json', 'w') as f:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
json.dump(data, f, indent=4)
|
| 20 |
|
| 21 |
+
def send_otp(email):
|
| 22 |
+
otp = str(random.randint(1000, 9999))
|
| 23 |
st.session_state['otp'] = otp
|
| 24 |
+
|
| 25 |
+
sender_email = os.environ['EMAIL_USER']
|
| 26 |
+
sender_password = os.environ['EMAIL_PASS']
|
| 27 |
+
|
| 28 |
+
msg = EmailMessage()
|
| 29 |
+
msg['Subject'] = "Your Baryashah Bank OTP"
|
| 30 |
+
msg['From'] = sender_email
|
| 31 |
+
msg['To'] = email
|
| 32 |
+
msg.set_content(f"Your OTP for Baryashah Bank is: {otp}")
|
| 33 |
+
|
| 34 |
+
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:
|
| 35 |
+
smtp.login(sender_email, sender_password)
|
| 36 |
+
smtp.send_message(msg)
|
| 37 |
+
|
| 38 |
+
st.info(f"β
OTP sent to {email}")
|
| 39 |
return otp
|
| 40 |
|
| 41 |
+
def verify_otp(user_input):
|
| 42 |
+
return user_input == st.session_state.get('otp')
|
| 43 |
+
|
| 44 |
+
# ---------- APP UI ----------
|
| 45 |
+
|
| 46 |
+
st.title("π Baryashah Bank Pro Dashboard π")
|
| 47 |
+
st.write("Professional banking simulation. Login, withdraw, deposit & reset password securely.")
|
| 48 |
|
| 49 |
+
data = load_data()
|
| 50 |
+
|
| 51 |
+
menu = ["Create Account", "Login", "Reset Password"]
|
| 52 |
choice = st.sidebar.selectbox("Menu", menu)
|
| 53 |
|
| 54 |
+
# ---------- CREATE ACCOUNT ----------
|
| 55 |
+
if choice == "Create Account":
|
| 56 |
+
st.header("π Create New Account")
|
| 57 |
name = st.text_input("Full Name")
|
| 58 |
email = st.text_input("Email")
|
| 59 |
age = st.number_input("Age", min_value=1, max_value=120)
|
| 60 |
+
password = st.text_input("Create Password", type="password")
|
| 61 |
+
|
|
|
|
| 62 |
if st.button("Create Account"):
|
| 63 |
if age < 18:
|
| 64 |
+
st.error("β Must be 18 or older to create an account.")
|
| 65 |
+
elif email in data:
|
| 66 |
+
st.error("β Account with this email already exists.")
|
|
|
|
|
|
|
| 67 |
else:
|
| 68 |
+
data[email] = {
|
| 69 |
"name": name,
|
| 70 |
"age": age,
|
| 71 |
"password": password,
|
|
|
|
| 72 |
"balance": 0,
|
| 73 |
"transactions": []
|
| 74 |
}
|
| 75 |
+
save_data(data)
|
| 76 |
+
st.success(f"β
Account created for {name}!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
+
# ---------- LOGIN ----------
|
| 79 |
+
elif choice == "Login":
|
| 80 |
+
st.header("π Login")
|
| 81 |
email = st.text_input("Email")
|
| 82 |
password = st.text_input("Password", type="password")
|
| 83 |
+
|
| 84 |
+
if st.button("Send OTP"):
|
| 85 |
+
if email not in data:
|
| 86 |
+
st.error("β Email not registered!")
|
| 87 |
+
elif data[email]["password"] != password:
|
| 88 |
+
st.error("β Wrong password!")
|
| 89 |
+
else:
|
| 90 |
+
send_otp(email)
|
| 91 |
+
|
| 92 |
+
otp_input = st.text_input("Enter OTP")
|
| 93 |
+
if st.button("Verify OTP"):
|
| 94 |
+
if verify_otp(otp_input):
|
| 95 |
+
st.success(f"β
Welcome {data[email]['name']}!")
|
| 96 |
+
# Dashboard
|
| 97 |
+
st.subheader("π° Account Dashboard")
|
| 98 |
+
st.metric("Balance", f"${data[email]['balance']}")
|
| 99 |
+
col1, col2 = st.columns(2)
|
| 100 |
+
with col1:
|
| 101 |
+
deposit_amount = st.number_input("Deposit Amount", min_value=1)
|
| 102 |
+
if st.button("Deposit"):
|
| 103 |
+
data[email]['balance'] += deposit_amount
|
| 104 |
+
data[email]['transactions'].append(f"Deposit: +${deposit_amount}")
|
| 105 |
+
save_data(data)
|
| 106 |
+
st.success(f"β
Deposited ${deposit_amount} successfully!")
|
| 107 |
+
with col2:
|
| 108 |
+
withdraw_amount = st.number_input("Withdraw Amount", min_value=1)
|
| 109 |
+
if st.button("Withdraw"):
|
| 110 |
+
# Send OTP before withdrawal
|
| 111 |
+
send_otp(email)
|
| 112 |
+
otp_withdraw = st.text_input("Enter OTP to withdraw")
|
| 113 |
+
if st.button("Confirm Withdrawal"):
|
| 114 |
+
if verify_otp(otp_withdraw):
|
| 115 |
+
if withdraw_amount <= data[email]['balance']:
|
| 116 |
+
data[email]['balance'] -= withdraw_amount
|
| 117 |
+
data[email]['transactions'].append(f"Withdraw: -${withdraw_amount}")
|
| 118 |
+
save_data(data)
|
| 119 |
+
st.success(f"β
Withdrawn ${withdraw_amount} successfully!")
|
| 120 |
else:
|
| 121 |
+
st.error("β Insufficient Balance!")
|
| 122 |
+
else:
|
| 123 |
+
st.error("β Wrong OTP!")
|
| 124 |
+
# Show transactions
|
| 125 |
+
st.subheader("π Transaction History")
|
| 126 |
+
for t in data[email]['transactions']:
|
| 127 |
+
st.write(t)
|
| 128 |
+
else:
|
| 129 |
+
st.error("β OTP incorrect!")
|
| 130 |
+
|
| 131 |
+
# ---------- PASSWORD RESET ----------
|
| 132 |
+
elif choice == "Reset Password":
|
| 133 |
+
st.header("π Reset Password")
|
| 134 |
+
email = st.text_input("Registered Email")
|
| 135 |
+
if st.button("Send OTP for Reset"):
|
| 136 |
+
if email not in data:
|
| 137 |
+
st.error("β Email not registered!")
|
| 138 |
+
else:
|
| 139 |
+
send_otp(email)
|
| 140 |
+
otp_input = st.text_input("Enter OTP received")
|
| 141 |
+
new_pass = st.text_input("Enter New Password", type="password")
|
| 142 |
+
if st.button("Reset Password"):
|
| 143 |
+
if verify_otp(otp_input):
|
| 144 |
+
data[email]['password'] = new_pass
|
| 145 |
+
save_data(data)
|
| 146 |
+
st.success("β
Password reset successfully!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
else:
|
| 148 |
+
st.error("β OTP incorrect!")
|