Spaces:
Running
Running
import gradio as gr | |
def calculate(input_value: float, calculation_type: str) -> dict: | |
vat_rate = 0.07 | |
withholding_rate = 0.03 | |
if calculation_type == "base_amount": | |
base = input_value | |
vat = base * vat_rate | |
total_after_vat = base + vat | |
withholding_tax = base * withholding_rate | |
net_amount = total_after_vat - withholding_tax | |
elif calculation_type == "total_after_vat": | |
total_after_vat = input_value | |
base = total_after_vat / (1 + vat_rate) | |
vat = total_after_vat - base | |
withholding_tax = base * withholding_rate | |
net_amount = total_after_vat - withholding_tax | |
elif calculation_type == "net_amount": | |
net_amount = input_value | |
base = net_amount / (1 + vat_rate - withholding_rate) | |
vat = base * vat_rate | |
total_after_vat = base + vat | |
withholding_tax = base * withholding_rate | |
elif calculation_type == "withholding_tax": | |
withholding_tax = input_value | |
base = withholding_tax / withholding_rate | |
vat = base * vat_rate | |
total_after_vat = base + vat | |
net_amount = total_after_vat - withholding_tax | |
return { | |
"base_amount": base, | |
"vat": vat, | |
"total_after_vat": total_after_vat, | |
"withholding_tax": withholding_tax, | |
"net_amount": net_amount | |
} | |
def format_output(value: float) -> str: | |
return f"{value:,.2f} บาท" | |
def update_outputs(input_value, calculation_type): | |
if not input_value: | |
return ["", "", "", "", ""] | |
results = calculate(float(input_value), calculation_type) | |
return [ | |
format_output(results["base_amount"]), | |
format_output(results["vat"]), | |
format_output(results["total_after_vat"]), | |
format_output(results["withholding_tax"]), | |
format_output(results["net_amount"]) | |
] | |
with gr.Blocks(title="Tax Calculator") as demo: | |
gr.Markdown("# 📊 เครื่องคิดเลขภาษี 7% และหัก ณ ที่จ่าย 3%") | |
gr.Markdown("## ⚙️ ป้อนข้อมูลและเลือกประเภทการคำนวณ") | |
with gr.Row(): | |
calculation_type = gr.Dropdown( | |
choices=[ | |
("จำนวนเงินต้น (ก่อนภาษี)", "base_amount"), | |
("ยอดหลัง VAT (รวม VAT แล้ว)", "total_after_vat"), | |
("ยอดสุทธิ (หลังหัก ณ ที่จ่าย)", "net_amount"), | |
("จำนวนเงินหัก ณ ที่จ่าย", "withholding_tax") | |
], | |
label="ประเภทการคำนวณ", | |
value="base_amount" | |
) | |
input_value = gr.Number( | |
label="ป้อนยอดเงิน", | |
precision=2, | |
minimum=0 | |
) | |
gr.Markdown("## 📜 ผลลัพธ์การคำนวณ") | |
with gr.Row(): | |
with gr.Column(): | |
gr.Markdown("### เงินต้น (ก่อน VAT)") | |
base_amount = gr.Textbox() | |
with gr.Column(): | |
gr.Markdown("### VAT 7%") | |
vat = gr.Textbox() | |
with gr.Column(): | |
gr.Markdown("### ยอดหลัง VAT") | |
total_after_vat = gr.Textbox() | |
with gr.Column(): | |
gr.Markdown("### หัก ณ ที่จ่าย 3%") | |
withholding_tax = gr.Textbox() | |
with gr.Column(): | |
gr.Markdown("### ยอดสุทธิ") | |
net_amount = gr.Textbox() | |
input_value.change( | |
update_outputs, | |
[input_value, calculation_type], | |
[base_amount, vat, total_after_vat, withholding_tax, net_amount] | |
) | |
calculation_type.change( | |
update_outputs, | |
[input_value, calculation_type], | |
[base_amount, vat, total_after_vat, withholding_tax, net_amount] | |
) | |
demo.launch() |