Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from datetime import datetime
|
| 3 |
+
import qrcode
|
| 4 |
+
from PIL import Image
|
| 5 |
+
|
| 6 |
+
# ---------------- FAKE DATABASE ----------------
|
| 7 |
+
users = {
|
| 8 |
+
"You": {"pin": "1234", "balance": 7500.0},
|
| 9 |
+
"Maria": {"pin": "1111", "balance": 3000.0},
|
| 10 |
+
"John": {"pin": "2222", "balance": 2000.0}
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
+
history = []
|
| 14 |
+
|
| 15 |
+
current_user = {"name": None}
|
| 16 |
+
|
| 17 |
+
# ---------------- FUNCTIONS ----------------
|
| 18 |
+
|
| 19 |
+
def login(name, pin):
|
| 20 |
+
if name in users and users[name]["pin"] == pin:
|
| 21 |
+
current_user["name"] = name
|
| 22 |
+
return f"Welcome {name}", get_balance()
|
| 23 |
+
return "Invalid login", ""
|
| 24 |
+
|
| 25 |
+
def get_balance():
|
| 26 |
+
u = current_user["name"]
|
| 27 |
+
if not u:
|
| 28 |
+
return "Not logged in"
|
| 29 |
+
return f"β±{users[u]['balance']:,.2f}"
|
| 30 |
+
|
| 31 |
+
def add_money(amount):
|
| 32 |
+
u = current_user["name"]
|
| 33 |
+
try:
|
| 34 |
+
amount = float(amount)
|
| 35 |
+
except:
|
| 36 |
+
return get_balance(), "Invalid amount", "\n".join(history)
|
| 37 |
+
|
| 38 |
+
users[u]["balance"] += amount
|
| 39 |
+
history.insert(0, f"{datetime.now().strftime('%H:%M')} - Added β±{amount:.2f}")
|
| 40 |
+
|
| 41 |
+
return get_balance(), "Money added", "\n".join(history)
|
| 42 |
+
|
| 43 |
+
def send_money(receiver, amount):
|
| 44 |
+
sender = current_user["name"]
|
| 45 |
+
|
| 46 |
+
if receiver not in users:
|
| 47 |
+
return get_balance(), "User not found", "\n".join(history)
|
| 48 |
+
|
| 49 |
+
try:
|
| 50 |
+
amount = float(amount)
|
| 51 |
+
except:
|
| 52 |
+
return get_balance(), "Invalid amount", "\n".join(history)
|
| 53 |
+
|
| 54 |
+
if users[sender]["balance"] < amount:
|
| 55 |
+
return get_balance(), "Insufficient balance", "\n".join(history)
|
| 56 |
+
|
| 57 |
+
users[sender]["balance"] -= amount
|
| 58 |
+
users[receiver]["balance"] += amount
|
| 59 |
+
|
| 60 |
+
history.insert(
|
| 61 |
+
0,
|
| 62 |
+
f"{datetime.now().strftime('%H:%M')} - Sent β±{amount:.2f} to {receiver}"
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
return get_balance(), "Sent successfully", "\n".join(history)
|
| 66 |
+
|
| 67 |
+
def generate_qr(text):
|
| 68 |
+
img = qrcode.make(text)
|
| 69 |
+
path = "/tmp/qr.png"
|
| 70 |
+
img.save(path)
|
| 71 |
+
return path
|
| 72 |
+
|
| 73 |
+
# ---------------- UI ----------------
|
| 74 |
+
|
| 75 |
+
css = """
|
| 76 |
+
body { background:#0b1220; color:white; }
|
| 77 |
+
.gr-button { background:#22c55e !important; color:white !important; }
|
| 78 |
+
"""
|
| 79 |
+
|
| 80 |
+
with gr.Blocks(css=css, title="Wallet Demo App") as app:
|
| 81 |
+
|
| 82 |
+
gr.Markdown("# π³ Fintech Wallet Demo (Educational Only)")
|
| 83 |
+
gr.Markdown("β οΈ This is NOT a real banking or GCash system.")
|
| 84 |
+
|
| 85 |
+
# LOGIN
|
| 86 |
+
gr.Markdown("## π Login")
|
| 87 |
+
name = gr.Dropdown(list(users.keys()), label="User")
|
| 88 |
+
pin = gr.Textbox(label="PIN", type="password")
|
| 89 |
+
login_btn = gr.Button("Login")
|
| 90 |
+
login_status = gr.Textbox()
|
| 91 |
+
|
| 92 |
+
balance_box = gr.Textbox(label="Balance")
|
| 93 |
+
|
| 94 |
+
login_btn.click(login, [name, pin], [login_status, balance_box])
|
| 95 |
+
|
| 96 |
+
# TRANSACTIONS
|
| 97 |
+
gr.Markdown("## πΈ Send Money")
|
| 98 |
+
receiver = gr.Dropdown(list(users.keys()), label="Receiver")
|
| 99 |
+
amount = gr.Number(label="Amount")
|
| 100 |
+
send_btn = gr.Button("Send")
|
| 101 |
+
send_status = gr.Textbox()
|
| 102 |
+
|
| 103 |
+
send_btn.click(send_money, [receiver, amount], [balance_box, send_status, gr.Textbox()])
|
| 104 |
+
|
| 105 |
+
gr.Markdown("## β Add Money")
|
| 106 |
+
add_amount = gr.Number(label="Amount")
|
| 107 |
+
add_btn = gr.Button("Add")
|
| 108 |
+
add_status = gr.Textbox()
|
| 109 |
+
|
| 110 |
+
add_btn.click(add_money, add_amount, [balance_box, add_status, gr.Textbox()])
|
| 111 |
+
|
| 112 |
+
# QR CODE
|
| 113 |
+
gr.Markdown("## π± QR Demo")
|
| 114 |
+
qr_input = gr.Textbox(label="Text for QR (demo)")
|
| 115 |
+
qr_btn = gr.Button("Generate QR")
|
| 116 |
+
qr_out = gr.Image()
|
| 117 |
+
|
| 118 |
+
qr_btn.click(generate_qr, qr_input, qr_out)
|
| 119 |
+
|
| 120 |
+
# HISTORY
|
| 121 |
+
gr.Markdown("## π Transactions")
|
| 122 |
+
history_box = gr.Textbox(lines=10)
|
| 123 |
+
|
| 124 |
+
app.launch()
|