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()