krishbaresha commited on
Commit
1766c2e
Β·
verified Β·
1 Parent(s): d755156

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +121 -156
app.py CHANGED
@@ -1,183 +1,148 @@
 
 
1
  import os
 
 
2
  import json
3
- import random
4
- import streamlit as st
5
- import plotly.express as px
6
-
7
- # ----------------- Setup -----------------
8
- if not os.path.exists("accounts.json"):
9
- with open("accounts.json", "w") as f:
10
- json.dump({}, f)
11
-
12
- st.set_page_config(page_title="Baryashah Bank Pro", layout="wide")
13
- st.markdown("""
14
- <style>
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 send_otp_simulation(email):
55
- otp = str(random.randint(1000,9999))
56
  st.session_state['otp'] = otp
57
- st.info(f"βœ… OTP sent to {email} (simulated): {otp}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
  return otp
59
 
60
- accounts = load_accounts()
 
 
 
 
 
 
61
 
62
- # ----------------- Sidebar Menu -----------------
63
- menu = ["Create Account","Login","Reset Password","Recover Account Name"]
 
64
  choice = st.sidebar.selectbox("Menu", menu)
65
 
66
- # ----------------- Create Account -----------------
67
- if choice=="Create Account":
68
- st.subheader("Create Account")
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
- pin = st.text_input("4-digit PIN for withdrawals", type="password", max_chars=4)
74
-
75
  if st.button("Create Account"):
76
  if age < 18:
77
- st.error("❌ You must be 18+ to open an account!")
78
- elif email in accounts:
79
- st.error("❌ Account already exists with this email!")
80
- elif len(pin)!=4 or not pin.isdigit():
81
- st.error("❌ PIN must be 4 digits")
82
  else:
83
- accounts[email] = {
84
  "name": name,
85
  "age": age,
86
  "password": password,
87
- "pin": pin,
88
  "balance": 0,
89
  "transactions": []
90
  }
91
- save_accounts(accounts)
92
- st.success(f"πŸŽ‰ Account created successfully for {name}!")
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
- # ----------------- Login -----------------
124
- elif choice=="Login":
125
- st.subheader("Login")
126
  email = st.text_input("Email")
127
  password = st.text_input("Password", type="password")
128
-
129
- if st.button("Login"):
130
- if email in accounts and accounts[email]['password']==password:
131
- st.info("OTP sent to your email for 2FA (simulated).")
132
- otp = send_otp_simulation(email)
133
- otp_input = st.text_input("Enter OTP for 2FA")
134
- if st.button("Verify OTP"):
135
- if otp_input == otp:
136
- st.success(f"Welcome {accounts[email]['name']}!")
137
- st.markdown(f"### πŸ’° Balance: {accounts[email]['balance']}")
138
-
139
- # ---------------- Transaction Section ----------------
140
- col1, col2 = st.columns(2)
141
- with col1:
142
- withdraw_amt = st.number_input("Withdraw Amount", min_value=0)
143
- user_pin = st.text_input("Enter PIN for Withdrawal", type="password")
144
- if st.button("Withdraw", key="withdraw"):
145
- if user_pin != accounts[email]['pin']:
146
- st.error("❌ Incorrect PIN!")
147
- elif withdraw_amt > accounts[email]['balance']:
148
- st.error("❌ Insufficient Balance!")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
149
  else:
150
- otp_w = send_otp_simulation(email)
151
- otp_input_w = st.text_input("Enter OTP for Withdrawal")
152
- if st.button("Confirm Withdraw"):
153
- if otp_input_w == otp_w:
154
- accounts[email]['balance'] -= withdraw_amt
155
- accounts[email]['transactions'].append({"type":"Withdraw","amount":withdraw_amt})
156
- save_accounts(accounts)
157
- st.success(f"βœ… Withdrawal Successful! New Balance: {accounts[email]['balance']}")
158
- st.balloons()
159
- else:
160
- st.error("❌ Invalid OTP for withdrawal!")
161
-
162
- with col2:
163
- deposit_amt = st.number_input("Deposit Amount", min_value=0)
164
- if st.button("Deposit", key="deposit"):
165
- accounts[email]['balance'] += deposit_amt
166
- accounts[email]['transactions'].append({"type":"Deposit","amount":deposit_amt})
167
- save_accounts(accounts)
168
- st.success(f"βœ… Deposit Successful! New Balance: {accounts[email]['balance']}")
169
- st.balloons()
170
-
171
- # ---------------- Transaction Chart ----------------
172
- if accounts[email]['transactions']:
173
- df = accounts[email]['transactions']
174
- amounts = [t['amount'] if t['type']=='Deposit' else -t['amount'] for t in df]
175
- types = [t['type'] for t in df]
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("❌ Login failed!")
 
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!")