Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| from datetime import datetime | |
| import qrcode | |
| from PIL import Image | |
| # ---------------- FAKE DATABASE ---------------- | |
| users = { | |
| "You": {"pin": "1234", "balance": 7500.0}, | |
| "Maria": {"pin": "1111", "balance": 3000.0}, | |
| "John": {"pin": "2222", "balance": 2000.0} | |
| } | |
| history = [] | |
| current_user = {"name": None} | |
| # ---------------- FUNCTIONS ---------------- | |
| def login(name, pin): | |
| if name in users and users[name]["pin"] == pin: | |
| current_user["name"] = name | |
| return f"Welcome {name}", get_balance() | |
| return "Invalid login", "" | |
| def get_balance(): | |
| u = current_user["name"] | |
| if not u: | |
| return "Not logged in" | |
| return f"β±{users[u]['balance']:,.2f}" | |
| def add_money(amount): | |
| u = current_user["name"] | |
| try: | |
| amount = float(amount) | |
| except: | |
| return get_balance(), "Invalid amount", "\n".join(history) | |
| users[u]["balance"] += amount | |
| history.insert(0, f"{datetime.now().strftime('%H:%M')} - Added β±{amount:.2f}") | |
| return get_balance(), "Money added", "\n".join(history) | |
| def send_money(receiver, amount): | |
| sender = current_user["name"] | |
| if receiver not in users: | |
| return get_balance(), "User not found", "\n".join(history) | |
| try: | |
| amount = float(amount) | |
| except: | |
| return get_balance(), "Invalid amount", "\n".join(history) | |
| if users[sender]["balance"] < amount: | |
| return get_balance(), "Insufficient balance", "\n".join(history) | |
| users[sender]["balance"] -= amount | |
| users[receiver]["balance"] += amount | |
| history.insert( | |
| 0, | |
| f"{datetime.now().strftime('%H:%M')} - Sent β±{amount:.2f} to {receiver}" | |
| ) | |
| return get_balance(), "Sent successfully", "\n".join(history) | |
| def generate_qr(text): | |
| img = qrcode.make(text) | |
| path = "/tmp/qr.png" | |
| img.save(path) | |
| return path | |
| # ---------------- UI ---------------- | |
| css = """ | |
| body { background:#0b1220; color:white; } | |
| .gr-button { background:#22c55e !important; color:white !important; } | |
| """ | |
| with gr.Blocks(css=css, title="Wallet Demo App") as app: | |
| gr.Markdown("# π³ Fintech Wallet Demo (Educational Only)") | |
| gr.Markdown("β οΈ This is NOT a real banking or GCash system.") | |
| # LOGIN | |
| gr.Markdown("## π Login") | |
| name = gr.Dropdown(list(users.keys()), label="User") | |
| pin = gr.Textbox(label="PIN", type="password") | |
| login_btn = gr.Button("Login") | |
| login_status = gr.Textbox() | |
| balance_box = gr.Textbox(label="Balance") | |
| login_btn.click(login, [name, pin], [login_status, balance_box]) | |
| # TRANSACTIONS | |
| gr.Markdown("## πΈ Send Money") | |
| receiver = gr.Dropdown(list(users.keys()), label="Receiver") | |
| amount = gr.Number(label="Amount") | |
| send_btn = gr.Button("Send") | |
| send_status = gr.Textbox() | |
| send_btn.click(send_money, [receiver, amount], [balance_box, send_status, gr.Textbox()]) | |
| gr.Markdown("## β Add Money") | |
| add_amount = gr.Number(label="Amount") | |
| add_btn = gr.Button("Add") | |
| add_status = gr.Textbox() | |
| add_btn.click(add_money, add_amount, [balance_box, add_status, gr.Textbox()]) | |
| # QR CODE | |
| gr.Markdown("## π± QR Demo") | |
| qr_input = gr.Textbox(label="Text for QR (demo)") | |
| qr_btn = gr.Button("Generate QR") | |
| qr_out = gr.Image() | |
| qr_btn.click(generate_qr, qr_input, qr_out) | |
| # HISTORY | |
| gr.Markdown("## π Transactions") | |
| history_box = gr.Textbox(lines=10) | |
| app.launch() |