File size: 3,538 Bytes
be840cf
 
 
65f052d
64d728d
65f052d
64d728d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65f052d
64d728d
 
 
65f052d
64d728d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
be840cf
 
 
 
 
 
64d728d
be840cf
 
 
 
64d728d
be840cf
64d728d
be840cf
 
64d728d
 
65f052d
64d728d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65f052d
be840cf
65f052d
be840cf
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import gradio as gr
from datetime import datetime
import dateutil.relativedelta
from gradio.themes import Soft

# Custom theme for modern look
custom_theme = Soft(
    primary_hue="blue",
    secondary_hue="purple",
    radius_size="lg",
).set(
    body_background_fill="*linear-gradient(135deg, #1e3a8a, #6b21a8)",
    button_primary_background_fill="*linear-gradient(45deg, #3b82f6, #8b5cf6)",
    button_primary_background_fill_hover="*linear-gradient(45deg, #2563eb, #7c3aed)",
    block_background_fill="rgba(255, 255, 255, 0.1)",
    block_border_color="rgba(255, 255, 255, 0.2)",
    block_shadow="0 4px 6px rgba(0, 0, 0, 0.1)",
)

# Custom CSS for additional styling
custom_css = """
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@400;600;700&display=swap');

.gradio-container {
    font-family: 'Poppins', sans-serif !important;
}

h1 {
    color: white;
    text-align: center;
    font-size: 3rem;
    margin-bottom: 0.5rem;
    text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3);
}

.subtitle {
    color: #d1d5db;
    text-align: center;
    font-size: 1.25rem;
    margin-bottom: 2rem;
}

#birthdate-input input {
    background: rgba(255, 255, 255, 0.1);
    border: 2px solid rgba(255, 255, 255, 0.2);
    color: white;
    font-size: 1.1rem;
    padding: 0.75rem;
    transition: border-color 0.3s ease;
}

#birthdate-input input:focus {
    border-color: #3b82f6;
    outline: none;
}

#submit-button {
    transition: transform 0.2s ease;
}

#submit-button:hover {
    transform: scale(1.05);
}

#result-output {
    background: rgba(255, 255, 255, 0.15);
    border-radius: 12px;
    padding: 1.5rem;
    color: white;
    font-size: 1.2rem;
    text-align: center;
    animation: fadeIn 0.5s ease-in;
}

@keyframes fadeIn {
    from { opacity: 0; transform: translateY(10px); }
    to { opacity: 1; transform: translateY(0); }
}

.footer {
    color: #9ca3af;
    text-align: center;
    margin-top: 2rem;
    font-size: 0.9rem;
}
"""

def calculate_age(birthdate):
    try:
        birth_date = datetime.strptime(birthdate, "%Y-%m-%d")
        today = datetime.now()
        if birth_date > today:
            return "🚫 Error: Birthdate cannot be in the future!"
        delta = dateutil.relativedelta.relativedelta(today, birth_date)
        years = delta.years
        months = delta.months
        days = delta.days
        return f"πŸŽ‰ Your age is: **{years} years**, **{months} months**, and **{days} days**! πŸŽ‚"
    except ValueError:
        return "🚫 Error: Please enter a valid date in YYYY-MM-DD format (e.g., 1990-01-01)."

# Gradio interface
with gr.Blocks(theme=custom_theme, css=custom_css) as demo:
    gr.Markdown("# Age Calculator 🌟")
    gr.Markdown("### Discover your age in years, months, and days with style! ✨", elem_classes=["subtitle"])
    with gr.Row():
        with gr.Column(scale=1):
            birthdate = gr.Textbox(
                label="πŸ“… Enter Your Birthdate (YYYY-MM-DD)",
                placeholder="e.g., 1990-01-01",
                elem_id="birthdate-input"
            )
            submit_btn = gr.Button("πŸ” Calculate Age", variant="primary", elem_id="submit-button")
            output = gr.Markdown(label="Result", elem_id="result-output")
            submit_btn.click(
                fn=calculate_age,
                inputs=birthdate,
                outputs=output
            )
    gr.Markdown("---")
    gr.Markdown("Developed with ❀️ for a modern world", elem_classes=["footer"])

if __name__ == "__main__":
    demo.launch()