HasanOJ commited on
Commit
27ff429
·
verified ·
1 Parent(s): ae7a494

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +88 -1
app.py CHANGED
@@ -34,6 +34,93 @@ def get_current_time_in_timezone(timezone: str) -> str:
34
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
35
 
36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
  final_answer = FinalAnswerTool()
38
 
39
  # If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
@@ -55,7 +142,7 @@ with open("prompts.yaml", 'r') as stream:
55
 
56
  agent = CodeAgent(
57
  model=model,
58
- tools=[final_answer], ## add your tools here (don't remove final answer)
59
  max_steps=6,
60
  verbosity_level=1,
61
  grammar=None,
 
34
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
35
 
36
 
37
+ @tool
38
+ def calculate_age(birth_date: str, reference_date: str = "today") -> str:
39
+ """Calculates precise age in years, months, and days from birth date to reference date.
40
+ Handles multiple date formats (YYYY-MM-DD, MM/DD/YYYY, DD.MM.YYYY, etc.).
41
+
42
+ Args:
43
+ birth_date: The birth date in any common format (e.g., "1990-05-15", "05/15/1990", "15.05.1990")
44
+ reference_date: The date to calculate age at (defaults to today). Use "today" or specific date in same formats.
45
+
46
+ Returns:
47
+ String describing age in years, months, and days (e.g., "34 years, 2 months, 15 days")
48
+ """
49
+
50
+ from datetime import datetime
51
+ import re
52
+
53
+ def parse_date(date_str):
54
+ """Parse various date formats into datetime object"""
55
+ if date_str.lower() == "today":
56
+ return datetime.today()
57
+
58
+ # Try different date patterns
59
+ patterns = [
60
+ (r"(\d{4})-(\d{1,2})-(\d{1,2})", "%Y-%m-%d"), # YYYY-MM-DD
61
+ (r"(\d{1,2})/(\d{1,2})/(\d{4})", "%m/%d/%Y"), # MM/DD/YYYY
62
+ (r"(\d{1,2})\.(\d{1,2})\.(\d{4})", "%d.%m.%Y"), # DD.MM.YYYY
63
+ (r"(\d{1,2}) (\w{3,}) (\d{4})", "%d %B %Y"), # 15 May 1990
64
+ (r"(\w{3,}) (\d{1,2}), (\d{4})", "%B %d, %Y"), # May 15, 1990
65
+ ]
66
+
67
+ for pattern, fmt in patterns:
68
+ if re.fullmatch(pattern, date_str):
69
+ try:
70
+ return datetime.strptime(date_str, fmt)
71
+ except ValueError:
72
+ continue
73
+
74
+ raise ValueError(f"Could not parse date: {date_str}")
75
+
76
+ try:
77
+ # Parse dates
78
+ birth_dt = parse_date(birth_date)
79
+ ref_dt = parse_date(reference_date)
80
+
81
+ # Calculate age
82
+ if ref_dt < birth_dt:
83
+ return "Error: Reference date is before birth date"
84
+
85
+ years = ref_dt.year - birth_dt.year
86
+ months = ref_dt.month - birth_dt.month
87
+ days = ref_dt.day - birth_dt.day
88
+
89
+ # Handle negative months/days
90
+ if days < 0:
91
+ months -= 1
92
+ # Get days in previous month
93
+ if ref_dt.month == 1:
94
+ prev_month = 12
95
+ prev_year = ref_dt.year - 1
96
+ else:
97
+ prev_month = ref_dt.month - 1
98
+ prev_year = ref_dt.year
99
+ days_in_prev_month = (datetime(prev_year, prev_month + 1, 1) - datetime(prev_year, prev_month, 1)).days
100
+ days += days_in_prev_month
101
+
102
+ if months < 0:
103
+ years -= 1
104
+ months += 12
105
+
106
+ # Format output
107
+ parts = []
108
+ if years > 0:
109
+ parts.append(f"{years} year{'s' if years != 1 else ''}")
110
+ if months > 0:
111
+ parts.append(f"{months} month{'s' if months != 1 else ''}")
112
+ if days > 0:
113
+ parts.append(f"{days} day{'s' if days != 1 else ''}")
114
+
115
+ if not parts:
116
+ return "0 days old (born today)"
117
+
118
+ return ", ".join(parts) + " old"
119
+
120
+ except Exception as e:
121
+ return f"Error calculating age: {str(e)}. Please check date formats."
122
+
123
+
124
  final_answer = FinalAnswerTool()
125
 
126
  # If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
 
142
 
143
  agent = CodeAgent(
144
  model=model,
145
+ tools=[final_answer, calculate_age], ## add your tools here (don't remove final answer)
146
  max_steps=6,
147
  verbosity_level=1,
148
  grammar=None,