rizwanaidris2 commited on
Commit
042b965
·
verified ·
1 Parent(s): 11d8e52

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -27
app.py CHANGED
@@ -1,47 +1,55 @@
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()
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
  from datetime import datetime
3
 
4
+ # Function to calculate the age and the time remaining for the next birthday
5
+ def calculate_age_and_birthday(birthday: str):
6
  try:
7
+ # Convert the string input into a datetime object
8
+ dob_date = datetime.strptime(birthday, "%m/%d/%Y")
9
  today = datetime.today()
10
+
11
+ # Calculate the age in years, months, and days
12
  years = today.year - dob_date.year
13
  months = today.month - dob_date.month
14
  days = today.day - dob_date.day
15
+
16
  # Adjust months and days if necessary
17
  if days < 0:
18
  months -= 1
19
+ days += 30 # Approximate number of days in a month
20
  if months < 0:
21
  years -= 1
22
  months += 12
23
+
24
+ # Calculate the time remaining for the next birthday
25
  next_birthday = datetime(today.year, dob_date.month, dob_date.day)
26
+ if today > next_birthday: # If the birthday has passed this year
27
  next_birthday = datetime(today.year + 1, dob_date.month, dob_date.day)
28
  remaining_days = (next_birthday - today).days
29
+
30
+ # Return the results
31
  age_message = f"You are {years} years, {months} months, and {days} days old."
32
  remaining_message = f"Your next birthday is in {remaining_days} days."
33
+
34
  return age_message, remaining_message
35
+
36
  except ValueError:
37
+ return "Invalid date format. Please use MM/DD/YYYY.", ""
38
+
39
+ # Streamlit app interface
40
+ def main():
41
+ st.title("Age Calculator")
42
+
43
+ # Input: User provides their date of birth in MM/DD/YYYY format
44
+ dob = st.text_input("Enter your Date of Birth (MM/DD/YYYY)", "")
45
+
46
+ if dob:
47
+ age_message, remaining_message = calculate_age_and_birthday(dob)
48
+
49
+ # Display the results
50
+ st.write(age_message)
51
+ st.write(remaining_message)
52
+
53
+ if __name__ == "__main__":
54
+ main()
55
+