Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,27 @@
|
|
| 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):
|
|
@@ -38,22 +60,36 @@ def salary_distributor_tree(gross, categories_input):
|
|
| 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.
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
+
import requests
|
| 4 |
+
|
| 5 |
+
# Function to fetch exchange rates and convert
|
| 6 |
+
def currency_exchange(amount_usd, amount_try):
|
| 7 |
+
try:
|
| 8 |
+
# Fetch exchange rates (USD -> PKR, TRY -> PKR)
|
| 9 |
+
url = "https://api.exchangerate.host/latest?base=USD&symbols=PKR,TRY"
|
| 10 |
+
response = requests.get(url)
|
| 11 |
+
data = response.json()
|
| 12 |
+
|
| 13 |
+
usd_to_pkr = data["rates"]["PKR"]
|
| 14 |
+
usd_to_try = data["rates"]["TRY"]
|
| 15 |
+
|
| 16 |
+
# Now get TRY -> PKR rate via USD as intermediary
|
| 17 |
+
try_to_pkr = usd_to_pkr / usd_to_try
|
| 18 |
+
|
| 19 |
+
converted_usd = round(amount_usd * usd_to_pkr, 2)
|
| 20 |
+
converted_try = round(amount_try * try_to_pkr, 2)
|
| 21 |
+
|
| 22 |
+
return usd_to_pkr, try_to_pkr, converted_usd, converted_try
|
| 23 |
+
except Exception as e:
|
| 24 |
+
return f"Error: {e}", None, None, None
|
| 25 |
|
| 26 |
# Salary Distributor Function with Tree Structure Output
|
| 27 |
def salary_distributor_tree(gross, categories_input):
|
|
|
|
| 60 |
return f"Error: {e}", pd.DataFrame(), 0, 0
|
| 61 |
|
| 62 |
with gr.Blocks() as demo:
|
| 63 |
+
gr.Markdown("## Salary Distributor Calculator (Tree View) + Currency Exchange")
|
| 64 |
+
|
| 65 |
+
with gr.Tab("Salary Distributor"):
|
| 66 |
+
with gr.Row():
|
| 67 |
+
gross_salary = gr.Number(label="Gross Salary", value=1000)
|
| 68 |
+
categories_input = gr.Textbox(
|
| 69 |
+
label="Categories (name, mode, value)",
|
| 70 |
+
value="Taxes,%,20\nSavings,%,10\nRent,fixed,300\nBills,%,5",
|
| 71 |
+
lines=6
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
output_tree = gr.Textbox(label="Salary Distribution Tree", lines=10)
|
| 75 |
+
output_table = gr.Dataframe(headers=["Category", "Mode", "Value", "Amount"], datatype=["str", "str", "number", "number"])
|
| 76 |
+
total_output = gr.Number(label="Total Allocated")
|
| 77 |
+
remaining_output = gr.Number(label="Remaining")
|
| 78 |
+
|
| 79 |
+
run_btn = gr.Button("Calculate")
|
| 80 |
+
run_btn.click(fn=salary_distributor_tree, inputs=[gross_salary, categories_input], outputs=[output_tree, output_table, total_output, remaining_output])
|
| 81 |
+
|
| 82 |
+
with gr.Tab("Currency Exchange"):
|
| 83 |
+
with gr.Row():
|
| 84 |
+
amount_usd = gr.Number(label="Amount in USD", value=1)
|
| 85 |
+
amount_try = gr.Number(label="Amount in TRY", value=1)
|
| 86 |
+
|
| 87 |
+
usd_to_pkr_rate = gr.Number(label="USD to PKR Rate")
|
| 88 |
+
try_to_pkr_rate = gr.Number(label="TRY to PKR Rate")
|
| 89 |
+
converted_usd = gr.Number(label="Converted USD to PKR")
|
| 90 |
+
converted_try = gr.Number(label="Converted TRY to PKR")
|
| 91 |
+
|
| 92 |
+
convert_btn = gr.Button("Convert")
|
| 93 |
+
convert_btn.click(fn=currency_exchange, inputs=[amount_usd, amount_try], outputs=[usd_to_pkr_rate, try_to_pkr_rate, converted_usd, converted_try])
|
| 94 |
|
| 95 |
demo.launch()
|