File size: 2,749 Bytes
e8e0706
 
63d5f31
42d73cb
e8e0706
63d5f31
 
 
 
e8e0706
63d5f31
 
 
 
e8e0706
63d5f31
 
 
 
 
e8e0706
63d5f31
 
 
 
 
e8e0706
63d5f31
42d73cb
 
 
 
 
 
 
 
 
 
 
e8e0706
63d5f31
 
e8e0706
63d5f31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42d73cb
63d5f31
 
e8e0706
bc759ab
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import gradio as gr
import subprocess
import pandas as pd
import os

def create_account(number, name, balance):
    input_str = f"{number},{name},{balance}"
    result = subprocess.run(["./cobol/account", "CREATE", input_str], capture_output=True, text=True)
    return "βœ… Account created"

def create_loan(lid, acc_id, amount, rate, status):
    input_str = f"{lid},{acc_id},{amount},{rate},{status}"
    result = subprocess.run(["./cobol/loan", "CREATE", input_str], capture_output=True, text=True)
    return "βœ… Loan created"

def export_accounts():
    df = pd.read_csv("data/accounts.csv")
    path = "export_accounts.csv"
    df.to_csv(path, index=False)
    return path

def export_loans():
    df = pd.read_csv("data/loans.csv")
    path = "export_loans.csv"
    df.to_csv(path, index=False)
    return path

def get_log():
    try:
        with open("data/transactions.log", "r") as f:
            content = f.read()
        html_content = content.replace('\n', '<br>')
        return f"""
        <div class="transaction-log">
        {html_content}
        </div>
        """
    except Exception as e:
        return f"<div class='transaction-log'>Error reading log: {str(e)}</div>"

with gr.Blocks(title="COBOL Bank Demo", css="file=static/style.css") as demo:
    gr.Markdown("# πŸ’° COBOL Banking System\nCreate, read, and export accounts and loans")

    with gr.Tab("Accounts"):
        with gr.Row():
            acc_num = gr.Number(label="Account Number")
            acc_name = gr.Textbox(label="Name")
            acc_bal = gr.Number(label="Balance")
        acc_btn = gr.Button("Create Account")
        acc_btn.click(fn=create_account, inputs=[acc_num, acc_name, acc_bal], outputs=[])

        exp_acc_btn = gr.Button("Export Accounts")
        exp_acc_file = gr.File(label="Download Accounts CSV")
        exp_acc_btn.click(fn=export_accounts, outputs=exp_acc_file)

    with gr.Tab("Loans"):
        with gr.Row():
            loan_id = gr.Textbox(label="Loan ID")
            loan_acc = gr.Number(label="Account Number")
            loan_amt = gr.Number(label="Amount")
            loan_rate = gr.Number(label="Rate (%)")
            loan_status = gr.Textbox(label="Status")
        loan_btn = gr.Button("Create Loan")
        loan_btn.click(fn=create_loan, inputs=[loan_id, loan_acc, loan_amt, loan_rate, loan_status], outputs=[])

        exp_loan_btn = gr.Button("Export Loans")
        exp_loan_file = gr.File(label="Download Loans CSV")
        exp_loan_btn.click(fn=export_loans, outputs=exp_loan_file)

    with gr.Tab("Transaction Log"):
        log_box = gr.HTML(label="Logs", value=get_log())
        refresh_btn = gr.Button("Refresh Log")
        refresh_btn.click(fn=get_log, outputs=log_box)

demo.launch()