Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from datetime import datetime
|
| 3 |
+
|
| 4 |
+
# ---------------- FAKE DATA ----------------
|
| 5 |
+
users = {
|
| 6 |
+
"You": {"pin": "1234", "balance": 8000.0},
|
| 7 |
+
"Maria": {"pin": "1111", "balance": 3500.0},
|
| 8 |
+
"John": {"pin": "2222", "balance": 2000.0}
|
| 9 |
+
}
|
| 10 |
+
|
| 11 |
+
history = []
|
| 12 |
+
session = {"user": None}
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
# ---------------- FUNCTIONS ----------------
|
| 16 |
+
|
| 17 |
+
def login(user, pin):
|
| 18 |
+
if user in users and users[user]["pin"] == pin:
|
| 19 |
+
session["user"] = user
|
| 20 |
+
return f"Welcome {user}", dashboard()
|
| 21 |
+
return "Invalid login", ""
|
| 22 |
+
|
| 23 |
+
def dashboard():
|
| 24 |
+
u = session["user"]
|
| 25 |
+
if not u:
|
| 26 |
+
return "Not logged in"
|
| 27 |
+
|
| 28 |
+
return f"""
|
| 29 |
+
π³ Wallet Dashboard
|
| 30 |
+
|
| 31 |
+
π€ User: {u}
|
| 32 |
+
π° Balance: β±{users[u]['balance']:,.2f}
|
| 33 |
+
"""
|
| 34 |
+
|
| 35 |
+
def add_money(amount):
|
| 36 |
+
u = session["user"]
|
| 37 |
+
try:
|
| 38 |
+
amount = float(amount)
|
| 39 |
+
except:
|
| 40 |
+
return dashboard(), "Invalid amount", "\n".join(history)
|
| 41 |
+
|
| 42 |
+
users[u]["balance"] += amount
|
| 43 |
+
history.insert(0, f"{datetime.now().strftime('%H:%M')} - +β±{amount} added")
|
| 44 |
+
|
| 45 |
+
return dashboard(), "Money added successfully", "\n".join(history)
|
| 46 |
+
|
| 47 |
+
def send_money(receiver, amount):
|
| 48 |
+
sender = session["user"]
|
| 49 |
+
|
| 50 |
+
if receiver not in users:
|
| 51 |
+
return dashboard(), "User not found", "\n".join(history)
|
| 52 |
+
|
| 53 |
+
try:
|
| 54 |
+
amount = float(amount)
|
| 55 |
+
except:
|
| 56 |
+
return dashboard(), "Invalid amount", "\n".join(history)
|
| 57 |
+
|
| 58 |
+
if users[sender]["balance"] < amount:
|
| 59 |
+
return dashboard(), "Insufficient balance", "\n".join(history)
|
| 60 |
+
|
| 61 |
+
users[sender]["balance"] -= amount
|
| 62 |
+
users[receiver]["balance"] += amount
|
| 63 |
+
|
| 64 |
+
history.insert(
|
| 65 |
+
0,
|
| 66 |
+
f"{datetime.now().strftime('%H:%M')} - {sender} sent β±{amount} to {receiver}"
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
return dashboard(), "Transfer successful", "\n".join(history)
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
# ---------------- UI STYLE ----------------
|
| 73 |
+
|
| 74 |
+
css = """
|
| 75 |
+
body {
|
| 76 |
+
background-color: #0b1220;
|
| 77 |
+
color: white;
|
| 78 |
+
font-family: Arial;
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
.gr-button {
|
| 82 |
+
background: #00c2a8 !important;
|
| 83 |
+
color: white !important;
|
| 84 |
+
border-radius: 10px !important;
|
| 85 |
+
font-weight: bold;
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
.gr-textbox, .gr-dropdown {
|
| 89 |
+
border-radius: 10px !important;
|
| 90 |
+
}
|
| 91 |
+
"""
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
# ---------------- APP ----------------
|
| 95 |
+
|
| 96 |
+
with gr.Blocks(css=css, title="Fintech Wallet Demo") as app:
|
| 97 |
+
|
| 98 |
+
gr.Markdown("# π³ Fintech Wallet App (Demo Only)")
|
| 99 |
+
gr.Markdown("β οΈ Educational project β no real banking system")
|
| 100 |
+
|
| 101 |
+
# LOGIN
|
| 102 |
+
gr.Markdown("## π Login")
|
| 103 |
+
user = gr.Dropdown(list(users.keys()), label="User")
|
| 104 |
+
pin = gr.Textbox(label="PIN", type="password")
|
| 105 |
+
login_btn = gr.Button("Login")
|
| 106 |
+
login_out = gr.Textbox()
|
| 107 |
+
|
| 108 |
+
dashboard_box = gr.Textbox(label="Dashboard")
|
| 109 |
+
|
| 110 |
+
login_btn.click(login, [user, pin], [login_out, dashboard_box])
|
| 111 |
+
|
| 112 |
+
# SEND MONEY
|
| 113 |
+
gr.Markdown("## πΈ Send Money")
|
| 114 |
+
receiver = gr.Dropdown(list(users.keys()), label="Receiver")
|
| 115 |
+
send_amount = gr.Number(label="Amount")
|
| 116 |
+
send_btn = gr.Button("Send")
|
| 117 |
+
send_out = gr.Textbox()
|
| 118 |
+
|
| 119 |
+
send_btn.click(send_money, [receiver, send_amount], [dashboard_box, send_out, gr.Textbox()])
|
| 120 |
+
|
| 121 |
+
# ADD MONEY
|
| 122 |
+
gr.Markdown("## β Add Money")
|
| 123 |
+
add_amount = gr.Number(label="Amount")
|
| 124 |
+
add_btn = gr.Button("Add Funds")
|
| 125 |
+
add_out = gr.Textbox()
|
| 126 |
+
|
| 127 |
+
add_btn.click(add_money, add_amount, [dashboard_box, add_out, gr.Textbox()])
|
| 128 |
+
|
| 129 |
+
# HISTORY
|
| 130 |
+
gr.Markdown("## π Transaction History")
|
| 131 |
+
history_box = gr.Textbox(lines=12)
|
| 132 |
+
|
| 133 |
+
app.launch()
|