puffy310 commited on
Commit
bc759ab
Β·
verified Β·
1 Parent(s): 9e21bc6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -69
app.py CHANGED
@@ -1,73 +1,56 @@
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")
@@ -80,14 +63,4 @@ with gr.Blocks(title="Banking System") as demo:
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()
 
1
  import gradio as gr
2
  import subprocess
 
 
3
 
4
+ # Run setup.sh once at startup
5
+ def run_setup():
6
+ result = subprocess.run(
7
+ ["sh", "scripts/setup.sh"],
8
+ capture_output=True,
9
+ text=True
10
+ )
11
+ return result.stdout + "\n" + result.stderr
12
 
13
+ # Run setup to compile COBOL files
14
+ print("πŸš€ Running setup...")
15
+ setup_output = run_setup()
16
+ print(setup_output)
17
 
18
+ # Helper function to run COBOL binaries
19
+ def run_cobol_binary(binary_name, input_data=None):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  try:
21
+ if input_data:
22
+ result = subprocess.run(
23
+ [f"./cobol/{binary_name}"],
24
+ input=input_data,
25
+ capture_output=True,
26
+ text=True,
27
+ timeout=10
28
+ )
29
+ else:
30
+ result = subprocess.run(
31
+ [f"./cobol/{binary_name}"],
32
+ capture_output=True,
33
+ text=True,
34
+ timeout=10
35
+ )
36
+ return result.stdout or result.stderr
37
+ except Exception as e:
38
+ return str(e)
39
 
40
+ # --- Account Balance ---
 
 
 
 
 
 
 
 
 
41
  def get_balance(account_number):
42
+ input_str = f"{account_number}\n"
43
+ return run_cobol_binary("account", input_str)
 
 
44
 
45
+ # --- Loan Status ---
46
  def check_loan_status(account_number):
47
+ input_str = f"{account_number}\n"
48
+ return run_cobol_binary("loan", input_str)
 
 
49
 
50
+ # --- Gradio UI ---
51
+ with gr.Blocks(title="COBOL Bank Demo") as demo:
52
+ gr.Markdown("# πŸ’° COBOL Banking System\nRun real COBOL programs in your browser!")
 
 
 
 
53
 
 
 
 
 
54
  with gr.Tab("Account Balance"):
55
  acc_input = gr.Number(label="Enter Account Number")
56
  acc_output = gr.Textbox(label="Balance Info")
 
63
  loan_btn = gr.Button("Check Loan")
64
  loan_btn.click(fn=check_loan_status, inputs=loan_input, outputs=loan_output)
65
 
66
+ demo.launch()