Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,9 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from datetime import datetime
|
| 3 |
import dateutil.relativedelta
|
| 4 |
-
from gradio.themes import Soft
|
| 5 |
|
| 6 |
-
# Custom theme
|
| 7 |
custom_theme = Soft(
|
| 8 |
primary_hue="blue",
|
| 9 |
secondary_hue="purple",
|
|
@@ -54,11 +54,11 @@ h1 {
|
|
| 54 |
outline: none;
|
| 55 |
}
|
| 56 |
|
| 57 |
-
#
|
| 58 |
transition: transform 0.2s ease;
|
| 59 |
}
|
| 60 |
|
| 61 |
-
#
|
| 62 |
transform: scale(1.05);
|
| 63 |
}
|
| 64 |
|
|
@@ -87,22 +87,14 @@ h1 {
|
|
| 87 |
|
| 88 |
def calculate_age(birthdate):
|
| 89 |
try:
|
| 90 |
-
# Convert birthdate string to datetime object
|
| 91 |
birth_date = datetime.strptime(birthdate, "%Y-%m-%d")
|
| 92 |
today = datetime.now()
|
| 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)
|
| 100 |
-
|
| 101 |
-
# Extract years, months, days
|
| 102 |
years = delta.years
|
| 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)."
|
|
@@ -110,8 +102,7 @@ def calculate_age(birthdate):
|
|
| 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! β¨",
|
| 114 |
-
|
| 115 |
with gr.Row():
|
| 116 |
with gr.Column(scale=1):
|
| 117 |
birthdate = gr.Textbox(
|
|
@@ -121,16 +112,13 @@ with gr.Blocks(theme=custom_theme, css=custom_css) as demo:
|
|
| 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",
|
| 133 |
|
| 134 |
-
|
| 135 |
-
if __name__ == "__main__':
|
| 136 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from datetime import datetime
|
| 3 |
import dateutil.relativedelta
|
| 4 |
+
from gradio.themes import Soft
|
| 5 |
|
| 6 |
+
# Custom theme for modern look
|
| 7 |
custom_theme = Soft(
|
| 8 |
primary_hue="blue",
|
| 9 |
secondary_hue="purple",
|
|
|
|
| 54 |
outline: none;
|
| 55 |
}
|
| 56 |
|
| 57 |
+
#submit-button {
|
| 58 |
transition: transform 0.2s ease;
|
| 59 |
}
|
| 60 |
|
| 61 |
+
#submit-button:hover {
|
| 62 |
transform: scale(1.05);
|
| 63 |
}
|
| 64 |
|
|
|
|
| 87 |
|
| 88 |
def calculate_age(birthdate):
|
| 89 |
try:
|
|
|
|
| 90 |
birth_date = datetime.strptime(birthdate, "%Y-%m-%d")
|
| 91 |
today = datetime.now()
|
|
|
|
|
|
|
| 92 |
if birth_date > today:
|
| 93 |
return "π« Error: Birthdate cannot be in the future!"
|
|
|
|
|
|
|
| 94 |
delta = dateutil.relativedelta.relativedelta(today, birth_date)
|
|
|
|
|
|
|
| 95 |
years = delta.years
|
| 96 |
months = delta.months
|
| 97 |
days = delta.days
|
|
|
|
| 98 |
return f"π Your age is: **{years} years**, **{months} months**, and **{days} days**! π"
|
| 99 |
except ValueError:
|
| 100 |
return "π« Error: Please enter a valid date in YYYY-MM-DD format (e.g., 1990-01-01)."
|
|
|
|
| 102 |
# Gradio interface
|
| 103 |
with gr.Blocks(theme=custom_theme, css=custom_css) as demo:
|
| 104 |
gr.Markdown("# Age Calculator π")
|
| 105 |
+
gr.Markdown("### Discover your age in years, months, and days with style! β¨", elem_classes=["subtitle"])
|
|
|
|
| 106 |
with gr.Row():
|
| 107 |
with gr.Column(scale=1):
|
| 108 |
birthdate = gr.Textbox(
|
|
|
|
| 112 |
)
|
| 113 |
submit_btn = gr.Button("π Calculate Age", variant="primary", elem_id="submit-button")
|
| 114 |
output = gr.Markdown(label="Result", elem_id="result-output")
|
|
|
|
| 115 |
submit_btn.click(
|
| 116 |
fn=calculate_age,
|
| 117 |
inputs=birthdate,
|
| 118 |
outputs=output
|
| 119 |
)
|
|
|
|
| 120 |
gr.Markdown("---")
|
| 121 |
+
gr.Markdown("Developed with β€οΈ for a modern world", elem_classes=["footer"])
|
| 122 |
|
| 123 |
+
if __name__ == "__main__":
|
|
|
|
| 124 |
demo.launch()
|