File size: 3,752 Bytes
9a955c6
 
e409a7e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9a955c6
1ee407f
 
9a955c6
1ee407f
9a955c6
1ee407f
 
 
 
 
 
9a955c6
 
 
1ee407f
 
 
 
9a955c6
 
1ee407f
9a955c6
 
1ee407f
 
 
 
 
 
 
 
 
 
9a955c6
1ee407f
9a955c6
 
e409a7e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9a955c6
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import gradio as gr
import pandas as pd
import requests

# Function to fetch exchange rates and convert
def currency_exchange(amount_usd, amount_try):
    try:
        # Fetch exchange rates (USD -> PKR, TRY -> PKR)
        url = "https://api.exchangerate.host/latest?base=USD&symbols=PKR,TRY"
        response = requests.get(url)
        data = response.json()

        usd_to_pkr = data["rates"]["PKR"]
        usd_to_try = data["rates"]["TRY"]

        # Now get TRY -> PKR rate via USD as intermediary
        try_to_pkr = usd_to_pkr / usd_to_try

        converted_usd = round(amount_usd * usd_to_pkr, 2)
        converted_try = round(amount_try * try_to_pkr, 2)

        return usd_to_pkr, try_to_pkr, converted_usd, converted_try
    except Exception as e:
        return f"Error: {e}", None, None, None

# Salary Distributor Function with Tree Structure Output
def salary_distributor_tree(gross, categories_input):
    try:
        lines = categories_input.strip().split("\n")
        categories = []

        for line in lines:
            parts = line.split(",")
            if len(parts) != 3:
                continue
            name, mode, value = parts
            value = float(value)
            amount = (gross * value / 100) if mode.strip() == "%" else value
            categories.append({
                "name": name.strip(),
                "mode": mode.strip(),
                "value": value,
                "amount": round(amount, 2)
            })

        total_allocated = sum(c["amount"] for c in categories)
        remaining = round(gross - total_allocated, 2)

        # Create a tree-like representation
        tree_lines = [f"Gross Salary: {gross}"]
        for cat in categories:
            tree_lines.append(f" ├─ {cat['name']} ({cat['mode']} {cat['value']}): {cat['amount']}")
        tree_lines.append(f" └─ Remaining: {remaining}")

        tree_str = "\n".join(tree_lines)

        df = pd.DataFrame(categories)
        return tree_str, df, round(total_allocated, 2), remaining
    except Exception as e:
        return f"Error: {e}", pd.DataFrame(), 0, 0

with gr.Blocks() as demo:
    gr.Markdown("## Salary Distributor Calculator (Tree View) + Currency Exchange")

    with gr.Tab("Salary Distributor"):
        with gr.Row():
            gross_salary = gr.Number(label="Gross Salary", value=1000)
            categories_input = gr.Textbox(
                label="Categories (name, mode, value)",
                value="Taxes,%,20\nSavings,%,10\nRent,fixed,300\nBills,%,5",
                lines=6
            )

        output_tree = gr.Textbox(label="Salary Distribution Tree", lines=10)
        output_table = gr.Dataframe(headers=["Category", "Mode", "Value", "Amount"], datatype=["str", "str", "number", "number"])
        total_output = gr.Number(label="Total Allocated")
        remaining_output = gr.Number(label="Remaining")

        run_btn = gr.Button("Calculate")
        run_btn.click(fn=salary_distributor_tree, inputs=[gross_salary, categories_input], outputs=[output_tree, output_table, total_output, remaining_output])

    with gr.Tab("Currency Exchange"):
        with gr.Row():
            amount_usd = gr.Number(label="Amount in USD", value=1)
            amount_try = gr.Number(label="Amount in TRY", value=1)

        usd_to_pkr_rate = gr.Number(label="USD to PKR Rate")
        try_to_pkr_rate = gr.Number(label="TRY to PKR Rate")
        converted_usd = gr.Number(label="Converted USD to PKR")
        converted_try = gr.Number(label="Converted TRY to PKR")

        convert_btn = gr.Button("Convert")
        convert_btn.click(fn=currency_exchange, inputs=[amount_usd, amount_try], outputs=[usd_to_pkr_rate, try_to_pkr_rate, converted_usd, converted_try])

demo.launch()