Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,89 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from datetime import datetime
|
| 3 |
import dateutil.relativedelta
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
def calculate_age(birthdate):
|
| 6 |
try:
|
|
@@ -10,7 +93,7 @@ def calculate_age(birthdate):
|
|
| 10 |
|
| 11 |
# Check if birthdate is in the future
|
| 12 |
if birth_date > today:
|
| 13 |
-
return "Error: Birthdate cannot be in the future!"
|
| 14 |
|
| 15 |
# Calculate the difference
|
| 16 |
delta = dateutil.relativedelta.relativedelta(today, birth_date)
|
|
@@ -20,25 +103,34 @@ def calculate_age(birthdate):
|
|
| 20 |
months = delta.months
|
| 21 |
days = delta.days
|
| 22 |
|
| 23 |
-
return f"Your age is {years} years
|
| 24 |
except ValueError:
|
| 25 |
-
return "Error: Please enter a valid date in YYYY-MM-DD format."
|
| 26 |
|
| 27 |
# Gradio interface
|
| 28 |
-
with gr.Blocks() as demo:
|
| 29 |
-
gr.Markdown("# Age Calculator")
|
| 30 |
-
gr.Markdown("
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
inputs=birthdate,
|
| 39 |
-
outputs=output
|
| 40 |
-
)
|
| 41 |
|
| 42 |
# Launch the app
|
| 43 |
-
if __name__ == "__main__
|
| 44 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from datetime import datetime
|
| 3 |
import dateutil.relativedelta
|
| 4 |
+
from gradio.themes import Soft, Glass
|
| 5 |
+
|
| 6 |
+
# Custom theme combining Soft and Glass for modern look
|
| 7 |
+
custom_theme = Soft(
|
| 8 |
+
primary_hue="blue",
|
| 9 |
+
secondary_hue="purple",
|
| 10 |
+
radius_size="lg",
|
| 11 |
+
).set(
|
| 12 |
+
body_background_fill="*linear-gradient(135deg, #1e3a8a, #6b21a8)",
|
| 13 |
+
button_primary_background_fill="*linear-gradient(45deg, #3b82f6, #8b5cf6)",
|
| 14 |
+
button_primary_background_fill_hover="*linear-gradient(45deg, #2563eb, #7c3aed)",
|
| 15 |
+
block_background_fill="rgba(255, 255, 255, 0.1)",
|
| 16 |
+
block_border_color="rgba(255, 255, 255, 0.2)",
|
| 17 |
+
block_shadow="0 4px 6px rgba(0, 0, 0, 0.1)",
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
# Custom CSS for additional styling
|
| 21 |
+
custom_css = """
|
| 22 |
+
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@400;600;700&display=swap');
|
| 23 |
+
|
| 24 |
+
.gradio-container {
|
| 25 |
+
font-family: 'Poppins', sans-serif !important;
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
h1 {
|
| 29 |
+
color: white;
|
| 30 |
+
text-align: center;
|
| 31 |
+
font-size: 3rem;
|
| 32 |
+
margin-bottom: 0.5rem;
|
| 33 |
+
text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3);
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
.subtitle {
|
| 37 |
+
color: #d1d5db;
|
| 38 |
+
text-align: center;
|
| 39 |
+
font-size: 1.25rem;
|
| 40 |
+
margin-bottom: 2rem;
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
#birthdate-input input {
|
| 44 |
+
background: rgba(255, 255, 255, 0.1);
|
| 45 |
+
border: 2px solid rgba(255, 255, 255, 0.2);
|
| 46 |
+
color: white;
|
| 47 |
+
font-size: 1.1rem;
|
| 48 |
+
padding: 0.75rem;
|
| 49 |
+
transition: border-color 0.3s ease;
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
#birthdate-input input:focus {
|
| 53 |
+
border-color: #3b82f6;
|
| 54 |
+
outline: none;
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
#calculate-button {
|
| 58 |
+
transition: transform 0.2s ease;
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
#calculate-button:hover {
|
| 62 |
+
transform: scale(1.05);
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
#result-output {
|
| 66 |
+
background: rgba(255, 255, 255, 0.15);
|
| 67 |
+
border-radius: 12px;
|
| 68 |
+
padding: 1.5rem;
|
| 69 |
+
color: white;
|
| 70 |
+
font-size: 1.2rem;
|
| 71 |
+
text-align: center;
|
| 72 |
+
animation: fadeIn 0.5s ease-in;
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
@keyframes fadeIn {
|
| 76 |
+
from { opacity: 0; transform: translateY(10px); }
|
| 77 |
+
to { opacity: 1; transform: translateY(0); }
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
.footer {
|
| 81 |
+
color: #9ca3af;
|
| 82 |
+
text-align: center;
|
| 83 |
+
margin-top: 2rem;
|
| 84 |
+
font-size: 0.9rem;
|
| 85 |
+
}
|
| 86 |
+
"""
|
| 87 |
|
| 88 |
def calculate_age(birthdate):
|
| 89 |
try:
|
|
|
|
| 93 |
|
| 94 |
# Check if birthdate is in the future
|
| 95 |
if birth_date > today:
|
| 96 |
+
return "π« Error: Birthdate cannot be in the future!"
|
| 97 |
|
| 98 |
# Calculate the difference
|
| 99 |
delta = dateutil.relativedelta.relativedelta(today, birth_date)
|
|
|
|
| 103 |
months = delta.months
|
| 104 |
days = delta.days
|
| 105 |
|
| 106 |
+
return f"π Your age is: **{years} years**, **{months} months**, and **{days} days**! π"
|
| 107 |
except ValueError:
|
| 108 |
+
return "π« Error: Please enter a valid date in YYYY-MM-DD format (e.g., 1990-01-01)."
|
| 109 |
|
| 110 |
# Gradio interface
|
| 111 |
+
with gr.Blocks(theme=custom_theme, css=custom_css) as demo:
|
| 112 |
+
gr.Markdown("# Age Calculator π")
|
| 113 |
+
gr.Markdown("### Discover your age in years, months, and days with style! β¨", className="subtitle")
|
| 114 |
|
| 115 |
+
with gr.Row():
|
| 116 |
+
with gr.Column(scale=1):
|
| 117 |
+
birthdate = gr.Textbox(
|
| 118 |
+
label="π
Enter Your Birthdate (YYYY-MM-DD)",
|
| 119 |
+
placeholder="e.g., 1990-01-01",
|
| 120 |
+
elem_id="birthdate-input"
|
| 121 |
+
)
|
| 122 |
+
submit_btn = gr.Button("π Calculate Age", variant="primary", elem_id="submit-button")
|
| 123 |
+
output = gr.Markdown(label="Result", elem_id="result-output")
|
| 124 |
+
|
| 125 |
+
submit_btn.click(
|
| 126 |
+
fn=calculate_age,
|
| 127 |
+
inputs=birthdate,
|
| 128 |
+
outputs=output
|
| 129 |
+
)
|
| 130 |
|
| 131 |
+
gr.Markdown("---")
|
| 132 |
+
gr.Markdown("Developed with β€οΈ for a modern world", className="footer")
|
|
|
|
|
|
|
|
|
|
| 133 |
|
| 134 |
# Launch the app
|
| 135 |
+
if __name__ == "__main__':
|
| 136 |
demo.launch()
|