Spaces:
Sleeping
Sleeping
| 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() |