File size: 2,038 Bytes
2699bad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr

def calculate_tvm(initial_investment, annual_revenue, annual_opex, tax_rate, years, discount_rate):
    # Convert percentages to decimals
    i = discount_rate / 100
    t_rate = tax_rate / 100
    
    annual_cash_flow = (annual_revenue - annual_opex) * (1 - t_rate)
    
    # Calculate NPV
    npv = -initial_investment
    for t in range(1, int(years) + 1):
        npv += annual_cash_flow / ((1 + i) ** t)
    
    # Calculate Simple Payback Period
    if annual_cash_flow > 0:
        payback = initial_investment / annual_cash_flow
    else:
        payback = float('inf')
        
    return (
        f"Resulting NPV: ${round(npv, 2):,}",
        f"Annual After-Tax Cash Flow: ${round(annual_cash_flow, 2):,}",
        f"Simple Payback Period: {round(payback, 2)} years"
    )

# Define the Interface
with gr.Blocks(theme=gr.themes.Soft()) as demo:
    gr.Markdown("# 🧪 Chemical Engineering TVM Calculator")
    gr.Markdown("Evaluate the economic viability of process equipment or plant expansions.")
    
    with gr.Row():
        with gr.Column():
            inv = gr.Number(label="Initial Investment ($)", value=1000000)
            rev = gr.Number(label="Annual Revenue ($)", value=500000)
            opex = gr.Number(label="Annual OPEX ($)", value=150000)
        with gr.Column():
            tax = gr.Slider(0, 50, value=21, label="Tax Rate (%)")
            years = gr.Slider(1, 30, value=10, step=1, label="Project Life (Years)")
            disc = gr.Slider(0, 20, value=10, label="Discount Rate / MARR (%)")
            
    btn = gr.Button("Calculate Economic Viability", variant="primary")
    
    with gr.Row():
        out_npv = gr.Textbox(label="Net Present Value (NPV)")
        out_cash = gr.Textbox(label="Annual Cash Flow")
        out_payback = gr.Textbox(label="Payback Period")

    btn.click(
        fn=calculate_tvm, 
        inputs=[inv, rev, opex, tax, years, disc], 
        outputs=[out_npv, out_cash, out_payback]
    )

if __name__ == "__main__":
    demo.launch()