Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,31 +1,44 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
|
| 4 |
-
# Salary Distributor Function
|
| 5 |
-
def
|
| 6 |
try:
|
|
|
|
| 7 |
categories = []
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
value = float(value)
|
| 11 |
amount = (gross * value / 100) if mode.strip() == "%" else value
|
| 12 |
categories.append({
|
| 13 |
-
"
|
| 14 |
-
"
|
| 15 |
-
"
|
| 16 |
-
"
|
| 17 |
})
|
| 18 |
|
| 19 |
-
|
| 20 |
-
total_allocated = round(df["Amount"].sum(), 2)
|
| 21 |
remaining = round(gross - total_allocated, 2)
|
| 22 |
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
except Exception as e:
|
| 25 |
-
return pd.DataFrame(), 0,
|
| 26 |
|
| 27 |
with gr.Blocks() as demo:
|
| 28 |
-
gr.Markdown("## Salary Distributor Calculator")
|
| 29 |
|
| 30 |
with gr.Row():
|
| 31 |
gross_salary = gr.Number(label="Gross Salary", value=1000)
|
|
@@ -35,11 +48,12 @@ with gr.Blocks() as demo:
|
|
| 35 |
lines=6
|
| 36 |
)
|
| 37 |
|
|
|
|
| 38 |
output_table = gr.Dataframe(headers=["Category", "Mode", "Value", "Amount"], datatype=["str", "str", "number", "number"])
|
| 39 |
total_output = gr.Number(label="Total Allocated")
|
| 40 |
remaining_output = gr.Number(label="Remaining")
|
| 41 |
|
| 42 |
run_btn = gr.Button("Calculate")
|
| 43 |
-
run_btn.click(fn=
|
| 44 |
|
| 45 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
|
| 4 |
+
# Salary Distributor Function with Tree Structure Output
|
| 5 |
+
def salary_distributor_tree(gross, categories_input):
|
| 6 |
try:
|
| 7 |
+
lines = categories_input.strip().split("\n")
|
| 8 |
categories = []
|
| 9 |
+
|
| 10 |
+
for line in lines:
|
| 11 |
+
parts = line.split(",")
|
| 12 |
+
if len(parts) != 3:
|
| 13 |
+
continue
|
| 14 |
+
name, mode, value = parts
|
| 15 |
value = float(value)
|
| 16 |
amount = (gross * value / 100) if mode.strip() == "%" else value
|
| 17 |
categories.append({
|
| 18 |
+
"name": name.strip(),
|
| 19 |
+
"mode": mode.strip(),
|
| 20 |
+
"value": value,
|
| 21 |
+
"amount": round(amount, 2)
|
| 22 |
})
|
| 23 |
|
| 24 |
+
total_allocated = sum(c["amount"] for c in categories)
|
|
|
|
| 25 |
remaining = round(gross - total_allocated, 2)
|
| 26 |
|
| 27 |
+
# Create a tree-like representation
|
| 28 |
+
tree_lines = [f"Gross Salary: {gross}"]
|
| 29 |
+
for cat in categories:
|
| 30 |
+
tree_lines.append(f" ├─ {cat['name']} ({cat['mode']} {cat['value']}): {cat['amount']}")
|
| 31 |
+
tree_lines.append(f" └─ Remaining: {remaining}")
|
| 32 |
+
|
| 33 |
+
tree_str = "\n".join(tree_lines)
|
| 34 |
+
|
| 35 |
+
df = pd.DataFrame(categories)
|
| 36 |
+
return tree_str, df, round(total_allocated, 2), remaining
|
| 37 |
except Exception as e:
|
| 38 |
+
return f"Error: {e}", pd.DataFrame(), 0, 0
|
| 39 |
|
| 40 |
with gr.Blocks() as demo:
|
| 41 |
+
gr.Markdown("## Salary Distributor Calculator (Tree View)")
|
| 42 |
|
| 43 |
with gr.Row():
|
| 44 |
gross_salary = gr.Number(label="Gross Salary", value=1000)
|
|
|
|
| 48 |
lines=6
|
| 49 |
)
|
| 50 |
|
| 51 |
+
output_tree = gr.Textbox(label="Salary Distribution Tree", lines=10)
|
| 52 |
output_table = gr.Dataframe(headers=["Category", "Mode", "Value", "Amount"], datatype=["str", "str", "number", "number"])
|
| 53 |
total_output = gr.Number(label="Total Allocated")
|
| 54 |
remaining_output = gr.Number(label="Remaining")
|
| 55 |
|
| 56 |
run_btn = gr.Button("Calculate")
|
| 57 |
+
run_btn.click(fn=salary_distributor_tree, inputs=[gross_salary, categories_input], outputs=[output_tree, output_table, total_output, remaining_output])
|
| 58 |
|
| 59 |
demo.launch()
|