sajjadmosaheb commited on
Commit
1ff5702
·
verified ·
1 Parent(s): b7b0aac

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +80 -0
app.py ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pandas as pd
3
+
4
+ # Create the tax rule DataFrame
5
+ data = {
6
+ "Salary Range (Million Toman)": ["0 - 12", "12 - 16.5", "16.5 - 27", "27 - 40", "Above 40"],
7
+ "Tax Rate (%)": [0, 10, 15, 20, 30],
8
+ "Description": [
9
+ "Exempt from tax",
10
+ "10% tax for this range",
11
+ "15% tax for this range",
12
+ "20% tax for this range",
13
+ "30% tax for this range"
14
+ ]
15
+ }
16
+
17
+ df = pd.DataFrame(data)
18
+
19
+ def calculate_tax(salary):
20
+ insurance = salary * 0.07 # 7% insurance deduction
21
+ tax = 0
22
+
23
+ if salary <= 12:
24
+ tax = 0
25
+ elif salary <= 16.5:
26
+ tax = (salary - 12) * 0.10
27
+ elif salary <= 27:
28
+ tax = (16.5 - 12) * 0.10 + (salary - 16.5) * 0.15
29
+ elif salary <= 4.0:
30
+ tax = (16.5 - 12) * 0.10 + (27 - 16.5) * 0.15 + (salary - 27) * 0.20
31
+ else:
32
+ tax = (16.5 - 12) * 0.10 + (27 - 16.5) * 0.15 + (40 - 27) * 0.20 + (salary - 40) * 0.30
33
+
34
+ net_income = salary - tax - insurance
35
+ cost_percent = ((tax + insurance) / salary) * 100 # percentage of total cost
36
+ 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}%"
37
+
38
+ def reverse_calculate(actual_income):
39
+ low = 0.0 # minimum salary boundary
40
+ high = actual_income / 0.93 * 1.5 # estimate upper bound
41
+ gross_salary = 0
42
+
43
+ # Using binary search to find the correct gross salary
44
+ while high - low > 0.0001:
45
+ mid = (low + high) / 2
46
+ net_income_from_mid = float(calculate_tax(mid)[2].split()[2])
47
+ if net_income_from_mid < actual_income:
48
+ low = mid
49
+ else:
50
+ high = mid
51
+
52
+ gross_salary = (low + high) / 2
53
+ tax, insurance, _, _ = calculate_tax(gross_salary)
54
+ cost_percent = ((gross_salary - actual_income) / gross_salary) * 100
55
+ return f"Tax: {tax}", f"Insurance: {insurance}", f"Gross Salary: {gross_salary:.3f} Million Toman", f"Cost Percentage: {cost_percent:.2f}%"
56
+
57
+ # Gradio app interface
58
+ with gr.Blocks() as interface:
59
+ gr.Markdown("### Iran Salary Tax Calculator (1403) - Million Toman")
60
+
61
+ with gr.Row():
62
+ with gr.Column():
63
+ salary_input = gr.Number(label="Enter Gross Salary (Million Toman)", precision=3)
64
+ tax_output = gr.Textbox(label="Tax")
65
+ insurance_output = gr.Textbox(label="Insurance")
66
+ net_income_output = gr.Textbox(label="Net Salary")
67
+ cost_percent_output = gr.Textbox(label="Cost Percentage")
68
+ salary_input.change(fn=calculate_tax, inputs=salary_input, outputs=[tax_output, insurance_output, net_income_output, cost_percent_output])
69
+
70
+ with gr.Column():
71
+ actual_income_input = gr.Number(label="Enter Net Salary (Million Toman)", precision=3)
72
+ reverse_tax_output = gr.Textbox(label="Tax")
73
+ reverse_insurance_output = gr.Textbox(label="Insurance")
74
+ gross_salary_output = gr.Textbox(label="Gross Salary")
75
+ reverse_cost_percent_output = gr.Textbox(label="Cost Percentage")
76
+ 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])
77
+ gr.Markdown("### Tax Calculation Rules")
78
+ gr.DataFrame(df)
79
+
80
+ interface.launch()