rizwanaidris2 commited on
Commit
adee1b3
·
verified ·
1 Parent(s): 5391f6f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -0
app.py CHANGED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from datetime import datetime
3
+
4
+ def calculate_age_and_birthday(dob: str):
5
+ try:
6
+ # Convert the dob string to a datetime object
7
+ dob_date = datetime.strptime(dob, "%Y-%m-%d")
8
+ today = datetime.today()
9
+
10
+ # Calculate age in years, months, and days
11
+ years = today.year - dob_date.year
12
+ months = today.month - dob_date.month
13
+ days = today.day - dob_date.day
14
+
15
+ # Adjust months and days if necessary
16
+ if days < 0:
17
+ months -= 1
18
+ days += 30 # Approximate days in a month
19
+ if months < 0:
20
+ years -= 1
21
+ months += 12
22
+
23
+ # Calculate time remaining for next birthday
24
+ next_birthday = datetime(today.year, dob_date.month, dob_date.day)
25
+ if today > next_birthday: # If birthday already passed this year
26
+ next_birthday = datetime(today.year + 1, dob_date.month, dob_date.day)
27
+ remaining_days = (next_birthday - today).days
28
+
29
+ # Display the results
30
+ age_message = f"You are {years} years, {months} months, and {days} days old."
31
+ remaining_message = f"Your next birthday is in {remaining_days} days."
32
+
33
+ return age_message, remaining_message
34
+
35
+ except ValueError:
36
+ return "Invalid date format. Please use YYYY-MM-DD.", ""
37
+
38
+ # Create the Gradio interface
39
+ iface = gr.Interface(
40
+ fn=calculate_age_and_birthday,
41
+ inputs=gr.Textbox(label="Enter your Date of Birth (YYYY-MM-DD)", type="text"),
42
+ outputs=[gr.Textbox(label="Age in Years, Months, and Days"), gr.Textbox(label="Time Remaining for Next Birthday")],
43
+ live=False # We want to calculate when the button is clicked
44
+ )
45
+
46
+ # Launch the app
47
+ iface.launch()