Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import subprocess
|
3 |
+
import os
|
4 |
+
import time
|
5 |
+
|
6 |
+
# Directories
|
7 |
+
COBOL_DIR = "cobol"
|
8 |
+
DATA_DIR = "data"
|
9 |
+
BIN_DIR = "cobol/bin"
|
10 |
+
|
11 |
+
# Ensure bin directory exists
|
12 |
+
os.makedirs(BIN_DIR, exist_ok=True)
|
13 |
+
|
14 |
+
def compile_cobol():
|
15 |
+
print("π Checking for existing compiled binaries...")
|
16 |
+
programs = ['account', 'loan', 'interest']
|
17 |
+
all_compiled = True
|
18 |
+
for prog in programs:
|
19 |
+
if not os.path.exists(f"{BIN_DIR}/{prog}"):
|
20 |
+
all_compiled = False
|
21 |
+
break
|
22 |
+
|
23 |
+
if all_compiled:
|
24 |
+
print("β
All COBOL binaries already compiled.")
|
25 |
+
return
|
26 |
+
|
27 |
+
print("βοΈ Compiling COBOL programs...")
|
28 |
+
|
29 |
+
# Install GnuCOBOL if needed
|
30 |
+
try:
|
31 |
+
subprocess.run(["cobc", "--version"], capture_output=True, check=True)
|
32 |
+
except FileNotFoundError:
|
33 |
+
print("π¦ Installing GNU COBOL...")
|
34 |
+
subprocess.run(["apt-get", "update"], check=True)
|
35 |
+
subprocess.run(["apt-get", "install", "-y", "gnucobol4"], check=True)
|
36 |
+
|
37 |
+
# Compile each program
|
38 |
+
for prog in programs:
|
39 |
+
cbl_file = f"{COBOL_DIR}/{prog}.cbl"
|
40 |
+
print(f"π§Ύ Compiling {prog} from {cbl_file}")
|
41 |
+
subprocess.run(["cobc", "-x", cbl_file, "-o", f"{BIN_DIR}/{prog}"], check=True)
|
42 |
+
print(f"β
Compiled {prog}")
|
43 |
+
|
44 |
+
print("π All COBOL programs compiled successfully!")
|
45 |
+
|
46 |
+
# Functions to call compiled COBOL binaries
|
47 |
+
def get_balance(account_number):
|
48 |
+
result = subprocess.run([f"{BIN_DIR}/account", str(account_number)],
|
49 |
+
capture_output=True,
|
50 |
+
text=True)
|
51 |
+
return result.stdout or "Account not found"
|
52 |
+
|
53 |
+
def check_loan_status(account_number):
|
54 |
+
result = subprocess.run([f"{BIN_DIR}/loan", str(account_number)],
|
55 |
+
capture_output=True,
|
56 |
+
text=True)
|
57 |
+
return result.stdout or "Loan not found"
|
58 |
+
|
59 |
+
def calculate_interest(principal, rate, time):
|
60 |
+
input_data = f"{principal}\n{rate}\n{time}\n"
|
61 |
+
result = subprocess.run([f"{BIN_DIR}/interest"],
|
62 |
+
input=input_data,
|
63 |
+
capture_output=True,
|
64 |
+
text=True)
|
65 |
+
return result.stdout or "Error calculating interest"
|
66 |
+
|
67 |
+
# Start compilation on startup
|
68 |
+
with gr.Blocks(title="Banking System") as demo:
|
69 |
+
gr.Markdown("# π° COBOL Banking System\nRun in-browser using Gradio + HF Spaces")
|
70 |
+
|
71 |
+
with gr.Tab("Account Balance"):
|
72 |
+
acc_input = gr.Number(label="Enter Account Number")
|
73 |
+
acc_output = gr.Textbox(label="Balance Info")
|
74 |
+
acc_btn = gr.Button("Check Balance")
|
75 |
+
acc_btn.click(fn=get_balance, inputs=acc_input, outputs=acc_output)
|
76 |
+
|
77 |
+
with gr.Tab("Loan Status"):
|
78 |
+
loan_input = gr.Number(label="Enter Account Number")
|
79 |
+
loan_output = gr.Textbox(label="Loan Info")
|
80 |
+
loan_btn = gr.Button("Check Loan")
|
81 |
+
loan_btn.click(fn=check_loan_status, inputs=loan_input, outputs=loan_output)
|
82 |
+
|
83 |
+
with gr.Tab("Interest Calculator"):
|
84 |
+
prin = gr.Number(label="Principal Amount")
|
85 |
+
rt = gr.Number(label="Rate (%)")
|
86 |
+
tm = gr.Number(label="Time (years)")
|
87 |
+
int_output = gr.Textbox(label="Result")
|
88 |
+
int_btn = gr.Button("Calculate Interest")
|
89 |
+
int_btn.click(fn=calculate_interest, inputs=[prin, rt, tm], outputs=int_output)
|
90 |
+
|
91 |
+
if __name__ == "__main__":
|
92 |
+
compile_cobol()
|
93 |
+
demo.launch()
|