Spaces:
Sleeping
Sleeping
import gradio as gr | |
import math | |
from datetime import datetime | |
# Current (latest available) per capita GDP in USD for more countries and the reference year | |
current_year = 2024 | |
current_gdp = { | |
"Afghanistan": (502, 2023), | |
"Algeria": (3397, 2023), | |
"Argentina": (10320, 2023), | |
"Armenia": (4800, 2023), | |
"Australia": (62070, 2023), | |
"Austria": (55300, 2023), | |
"Bangladesh": (2227, 2023), | |
"Belarus": (6700, 2023), | |
"Belgium": (49900, 2023), | |
"Bhutan": (3224, 2023), | |
"Brazil": (7570, 2023), | |
"Canada": (52330, 2023), | |
"Chile": (15970, 2023), | |
"China": (12552, 2023), | |
"Colombia": (6416, 2023), | |
"Croatia": (15800, 2023), | |
"Czech Republic": (24100, 2023), | |
"Denmark": (62000, 2023), | |
"Egypt": (3660, 2023), | |
"Ethiopia": (936, 2023), | |
"Finland": (50800, 2023), | |
"France": (44960, 2023), | |
"Georgia": (5000, 2023), | |
"Germany": (51830, 2023), | |
"Greece": (28300, 2023), | |
"Great Britain": (46330, 2023), | |
"Hungary": (18300, 2023), | |
"India": (2277, 2023), | |
"Indonesia": (3895, 2023), | |
"Iran": (5860, 2023), | |
"Iraq": (5020, 2023), | |
"Ireland": (87200, 2023), | |
"Israel": (43300, 2023), | |
"Italy": (35180, 2023), | |
"Japan": (39540, 2023), | |
"Jordan": (4280, 2023), | |
"Kazakhstan": (11000, 2023), | |
"Kenya": (1856, 2023), | |
"Kuwait": (32300, 2023), | |
"Kyrgyzstan": (1300, 2023), | |
"Latvia": (20000, 2023), | |
"Lebanon": (7600, 2023), | |
"Lithuania": (23700, 2023), | |
"Malaysia": (11480, 2023), | |
"Maldives": (9700, 2023), | |
"Mexico": (10512, 2023), | |
"Morocco": (3322, 2023), | |
"Myanmar": (1327, 2023), | |
"Nepal": (1264, 2023), | |
"Netherlands": (58300, 2023), | |
"New Zealand": (42000, 2023), | |
"Nigeria": (2400, 2023), | |
"Norway": (75450, 2023), | |
"Pakistan": (1538, 2023), | |
"Peru": (7020, 2023), | |
"Philippines": (3700, 2023), | |
"Poland": (17800, 2023), | |
"Portugal": (26200, 2023), | |
"Qatar": (58300, 2023), | |
"Romania": (13800, 2023), | |
"Russia": (10843, 2023), | |
"Saudi Arabia": (23500, 2023), | |
"Singapore": (65000, 2023), | |
"Slovakia": (21000, 2023), | |
"Slovenia": (28000, 2023), | |
"South Africa": (6347, 2023), | |
"South Korea": (33800, 2023), | |
"Spain": (30210, 2023), | |
"Sri Lanka": (3684, 2023), | |
"Sweden": (53500, 2023), | |
"Switzerland": (87900, 2023), | |
"Tanzania": (1122, 2023), | |
"Thailand": (7288, 2023), | |
"Turkey": (9337, 2023), | |
"Turkmenistan": (7000, 2023), | |
"Uganda": (859, 2023), | |
"Ukraine": (3700, 2023), | |
"United Arab Emirates": (43200, 2023), | |
"USA": (75370, 2023), | |
"Uzbekistan": (1700, 2023), | |
"Vietnam": (3685, 2023) | |
} | |
# Sorting the dictionary by country names for display | |
sorted_countries = dict(sorted(current_gdp.items())) | |
# Function to calculate the number of years required to reach $25,000 GDP per capita | |
def calculate_years_to_25000(current_gdp, growth_rate): | |
if current_gdp >= 25000: | |
return "Already a Developed Country" | |
years = math.log(25000 / current_gdp) / math.log(1 + growth_rate / 100) | |
return round(years, 2) | |
def developed_economy_calculator(country, growth_rate): | |
gdp, year = sorted_countries[country] | |
years = calculate_years_to_25000(gdp, growth_rate) | |
if years == "Already a Developed Country": | |
return f"{country} has a current per capita GDP of ${gdp} as of {year}. {country} is already a Developed Country." | |
developed_year = current_year + years | |
return f"{country} has a current per capita GDP of ${gdp} as of {year}. It will take {years} years for {country} to become a Developed Country with a per capita GDP of $25,000 at a growth rate of {growth_rate}%. The year will be {int(developed_year)}." | |
def developed_economy_calculator_years(country, years): | |
gdp, year = sorted_countries[country] | |
if gdp >= 25000: | |
return f"{country} has a current per capita GDP of ${gdp} as of {year}. {country} is already a Developed Country." | |
growth_rate = (math.exp(math.log(25000 / gdp) / years) - 1) * 100 | |
developed_year = current_year + years | |
return f"{country} has a current per capita GDP of ${gdp} as of {year}. {country} needs an annual growth rate of {round(growth_rate, 2)}% to become a Developed Country with a per capita GDP of $25,000 in {years} years. The year will be {int(developed_year)}." | |
def clear_output(): | |
return "", "" | |
with gr.Blocks() as demo: | |
gr.Markdown("# Developed Economy Calculator") | |
with gr.Tab("Growth Rate to Years"): | |
country = gr.Dropdown(list(sorted_countries.keys()), label="Country") | |
growth_rate = gr.Slider(0.1, 20.0, step=0.1, label="Annual Growth Rate (%)") | |
output = gr.Textbox(label="Result") | |
with gr.Row(): | |
calculate_btn = gr.Button("Calculate") | |
clear_btn = gr.Button("Clear") | |
calculate_btn.click(developed_economy_calculator, inputs=[country, growth_rate], outputs=output) | |
clear_btn.click(clear_output, outputs=output) | |
with gr.Tab("Years to Growth Rate"): | |
country_2 = gr.Dropdown(list(sorted_countries.keys()), label="Country") | |
years = gr.Slider(1, 100, step=1, label="Number of Years") | |
output_2 = gr.Textbox(label="Result") | |
with gr.Row(): | |
calculate_btn_2 = gr.Button("Calculate") | |
clear_btn_2 = gr.Button("Clear") | |
calculate_btn_2.click(developed_economy_calculator_years, inputs=[country_2, years], outputs=output_2) | |
clear_btn_2.click(clear_output, outputs=output_2) | |
gr.Markdown("Built by S.Venkataraghavan. Email: [[email protected]](mailto:[email protected])") | |
demo.launch() | |