Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,73 +1,56 @@
|
|
1 |
import gradio as gr
|
2 |
import subprocess
|
3 |
-
import os
|
4 |
-
import time
|
5 |
|
6 |
-
#
|
7 |
-
|
8 |
-
|
9 |
-
|
|
|
|
|
|
|
|
|
10 |
|
11 |
-
#
|
12 |
-
|
|
|
|
|
13 |
|
14 |
-
|
15 |
-
|
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 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
-
|
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 |
-
|
49 |
-
|
50 |
-
text=True)
|
51 |
-
return result.stdout or "Account not found"
|
52 |
|
|
|
53 |
def check_loan_status(account_number):
|
54 |
-
|
55 |
-
|
56 |
-
text=True)
|
57 |
-
return result.stdout or "Loan not found"
|
58 |
|
59 |
-
|
60 |
-
|
61 |
-
|
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 |
-
|
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()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|