Spaces:
Sleeping
Sleeping
import gradio as gr | |
import subprocess | |
import os | |
import time | |
# Directories | |
COBOL_DIR = "cobol" | |
DATA_DIR = "data" | |
BIN_DIR = "cobol/bin" | |
# Ensure bin directory exists | |
os.makedirs(BIN_DIR, exist_ok=True) | |
def compile_cobol(): | |
print("π Checking for existing compiled binaries...") | |
programs = ['account', 'loan', 'interest'] | |
all_compiled = True | |
for prog in programs: | |
if not os.path.exists(f"{BIN_DIR}/{prog}"): | |
all_compiled = False | |
break | |
if all_compiled: | |
print("β All COBOL binaries already compiled.") | |
return | |
print("βοΈ Compiling COBOL programs...") | |
# Install GnuCOBOL if needed | |
try: | |
subprocess.run(["cobc", "--version"], capture_output=True, check=True) | |
except FileNotFoundError: | |
print("π¦ Installing GNU COBOL...") | |
subprocess.run(["apt-get", "update"], check=True) | |
subprocess.run(["apt-get", "install", "-y", "gnucobol4"], check=True) | |
# Compile each program | |
for prog in programs: | |
cbl_file = f"{COBOL_DIR}/{prog}.cbl" | |
print(f"π§Ύ Compiling {prog} from {cbl_file}") | |
subprocess.run(["cobc", "-x", cbl_file, "-o", f"{BIN_DIR}/{prog}"], check=True) | |
print(f"β Compiled {prog}") | |
print("π All COBOL programs compiled successfully!") | |
# Functions to call compiled COBOL binaries | |
def get_balance(account_number): | |
result = subprocess.run([f"{BIN_DIR}/account", str(account_number)], | |
capture_output=True, | |
text=True) | |
return result.stdout or "Account not found" | |
def check_loan_status(account_number): | |
result = subprocess.run([f"{BIN_DIR}/loan", str(account_number)], | |
capture_output=True, | |
text=True) | |
return result.stdout or "Loan not found" | |
def calculate_interest(principal, rate, time): | |
input_data = f"{principal}\n{rate}\n{time}\n" | |
result = subprocess.run([f"{BIN_DIR}/interest"], | |
input=input_data, | |
capture_output=True, | |
text=True) | |
return result.stdout or "Error calculating interest" | |
# Start compilation on startup | |
with gr.Blocks(title="Banking System") as demo: | |
gr.Markdown("# π° COBOL Banking System\nRun in-browser using Gradio + HF Spaces") | |
with gr.Tab("Account Balance"): | |
acc_input = gr.Number(label="Enter Account Number") | |
acc_output = gr.Textbox(label="Balance Info") | |
acc_btn = gr.Button("Check Balance") | |
acc_btn.click(fn=get_balance, inputs=acc_input, outputs=acc_output) | |
with gr.Tab("Loan Status"): | |
loan_input = gr.Number(label="Enter Account Number") | |
loan_output = gr.Textbox(label="Loan Info") | |
loan_btn = gr.Button("Check Loan") | |
loan_btn.click(fn=check_loan_status, inputs=loan_input, outputs=loan_output) | |
with gr.Tab("Interest Calculator"): | |
prin = gr.Number(label="Principal Amount") | |
rt = gr.Number(label="Rate (%)") | |
tm = gr.Number(label="Time (years)") | |
int_output = gr.Textbox(label="Result") | |
int_btn = gr.Button("Calculate Interest") | |
int_btn.click(fn=calculate_interest, inputs=[prin, rt, tm], outputs=int_output) | |
if __name__ == "__main__": | |
compile_cobol() | |
demo.launch() |