sajjadmosaheb's picture
Create app.py
1ff5702 verified
import gradio as gr
import pandas as pd
# Create the tax rule DataFrame
data = {
"Salary Range (Million Toman)": ["0 - 12", "12 - 16.5", "16.5 - 27", "27 - 40", "Above 40"],
"Tax Rate (%)": [0, 10, 15, 20, 30],
"Description": [
"Exempt from tax",
"10% tax for this range",
"15% tax for this range",
"20% tax for this range",
"30% tax for this range"
]
}
df = pd.DataFrame(data)
def calculate_tax(salary):
insurance = salary * 0.07 # 7% insurance deduction
tax = 0
if salary <= 12:
tax = 0
elif salary <= 16.5:
tax = (salary - 12) * 0.10
elif salary <= 27:
tax = (16.5 - 12) * 0.10 + (salary - 16.5) * 0.15
elif salary <= 4.0:
tax = (16.5 - 12) * 0.10 + (27 - 16.5) * 0.15 + (salary - 27) * 0.20
else:
tax = (16.5 - 12) * 0.10 + (27 - 16.5) * 0.15 + (40 - 27) * 0.20 + (salary - 40) * 0.30
net_income = salary - tax - insurance
cost_percent = ((tax + insurance) / salary) * 100 # percentage of total cost
return f"Tax: {tax:.3f} Million Toman", f"Insurance: {insurance:.3f} Million Toman", f"Net Salary: {net_income:.3f} Million Toman", f"Cost Percentage: {cost_percent:.2f}%"
def reverse_calculate(actual_income):
low = 0.0 # minimum salary boundary
high = actual_income / 0.93 * 1.5 # estimate upper bound
gross_salary = 0
# Using binary search to find the correct gross salary
while high - low > 0.0001:
mid = (low + high) / 2
net_income_from_mid = float(calculate_tax(mid)[2].split()[2])
if net_income_from_mid < actual_income:
low = mid
else:
high = mid
gross_salary = (low + high) / 2
tax, insurance, _, _ = calculate_tax(gross_salary)
cost_percent = ((gross_salary - actual_income) / gross_salary) * 100
return f"Tax: {tax}", f"Insurance: {insurance}", f"Gross Salary: {gross_salary:.3f} Million Toman", f"Cost Percentage: {cost_percent:.2f}%"
# Gradio app interface
with gr.Blocks() as interface:
gr.Markdown("### Iran Salary Tax Calculator (1403) - Million Toman")
with gr.Row():
with gr.Column():
salary_input = gr.Number(label="Enter Gross Salary (Million Toman)", precision=3)
tax_output = gr.Textbox(label="Tax")
insurance_output = gr.Textbox(label="Insurance")
net_income_output = gr.Textbox(label="Net Salary")
cost_percent_output = gr.Textbox(label="Cost Percentage")
salary_input.change(fn=calculate_tax, inputs=salary_input, outputs=[tax_output, insurance_output, net_income_output, cost_percent_output])
with gr.Column():
actual_income_input = gr.Number(label="Enter Net Salary (Million Toman)", precision=3)
reverse_tax_output = gr.Textbox(label="Tax")
reverse_insurance_output = gr.Textbox(label="Insurance")
gross_salary_output = gr.Textbox(label="Gross Salary")
reverse_cost_percent_output = gr.Textbox(label="Cost Percentage")
actual_income_input.change(fn=reverse_calculate, inputs=actual_income_input, outputs=[reverse_tax_output, reverse_insurance_output, gross_salary_output, reverse_cost_percent_output])
gr.Markdown("### Tax Calculation Rules")
gr.DataFrame(df)
interface.launch()