Spaces:
Sleeping
Sleeping
File size: 2,552 Bytes
9779de2 | 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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | import gradio as gr
import requests
import time
# π Base world population + growth rate
BASE_POP = 8040000000
GROWTH_PER_SEC = 2.6
start_time = time.time()
# π Live world population counter
def live_world_population():
elapsed = time.time() - start_time
current_pop = int(BASE_POP + elapsed * GROWTH_PER_SEC)
return f"π World Population: {current_pop:,}"
# π Get country population from API
def get_country_population(country):
try:
url = f"https://restcountries.com/v3.1/name/{country}"
response = requests.get(url, timeout=5)
data = response.json()
pop = data[0]["population"]
return f"π {country} Population: {pop:,}"
except Exception:
return "β Country not found or API error"
# π Country list
countries = [
"India", "China", "United States", "Indonesia",
"Pakistan", "Brazil", "Nigeria", "Bangladesh", "Russia"
]
# π¨ Modern CSS
css = """
body {
background: linear-gradient(135deg, #141E30, #243B55);
font-family: 'Poppins', sans-serif;
color: white;
}
h1 {
text-align: center;
font-size: 42px;
background: linear-gradient(90deg, #00c6ff, #0072ff);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}
.population-box {
font-size: 26px;
text-align: center;
padding: 20px;
border-radius: 15px;
background: rgba(255,255,255,0.1);
backdrop-filter: blur(10px);
margin-top: 15px;
}
button {
background: linear-gradient(90deg, #ff512f, #dd2476);
border: none;
border-radius: 10px;
padding: 10px;
color: white;
font-weight: bold;
transition: 0.3s;
}
button:hover {
transform: scale(1.05);
}
"""
# π§ UI
with gr.Blocks() as demo:
gr.Markdown("# π Live World Population Dashboard")
world_output = gr.Textbox(
label="Live World Population",
elem_classes="population-box"
)
gr.Markdown("## π Check Country Population")
dropdown = gr.Dropdown(
choices=countries,
value="India",
label="Select Country"
)
country_output = gr.Textbox(elem_classes="population-box")
btn = gr.Button("Get Population")
# Button click event
btn.click(
fn=get_country_population,
inputs=dropdown,
outputs=country_output
)
# π₯ Gradio 6 Timer for live updates
timer = gr.Timer(1.0)
timer.tick(
fn=live_world_population,
outputs=world_output
)
# π Launch with CSS (Gradio 6 correct way)
demo.launch(css=css) |