Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from datetime import datetime
|
| 3 |
+
import dateutil.relativedelta
|
| 4 |
+
|
| 5 |
+
def calculate_age(birthdate):
|
| 6 |
+
try:
|
| 7 |
+
# Convert birthdate string to datetime object
|
| 8 |
+
birth_date = datetime.strptime(birthdate, "%Y-%m-%d")
|
| 9 |
+
today = datetime.now()
|
| 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)
|
| 17 |
+
|
| 18 |
+
# Extract years, months, days
|
| 19 |
+
years = delta.years
|
| 20 |
+
months = delta.months
|
| 21 |
+
days = delta.days
|
| 22 |
+
|
| 23 |
+
return f"Your age is {years} years, {months} months, and {days} days."
|
| 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("Enter your birthdate to calculate your age in years, months, and days.")
|
| 31 |
+
|
| 32 |
+
birthdate = gr.Textbox(label="Birthdate (YYYY-MM-DD)", placeholder="e.g., 1990-01-01")
|
| 33 |
+
submit_btn = gr.Button("Calculate Age")
|
| 34 |
+
output = gr.Textbox(label="Result")
|
| 35 |
+
|
| 36 |
+
submit_btn.click(
|
| 37 |
+
fn=calculate_age,
|
| 38 |
+
inputs=birthdate,
|
| 39 |
+
outputs=output
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
# Launch the app
|
| 43 |
+
if __name__ == "__main__":
|
| 44 |
+
demo.launch()
|