SergeyO7 commited on
Commit
c8245a1
·
verified ·
1 Parent(s): c2d9b8a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -46
app.py CHANGED
@@ -2,12 +2,11 @@ from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, t
2
  import datetime
3
  import pytz
4
  import yaml
5
- from skyfield.api import load, Topos
6
  from tools.final_answer import FinalAnswerTool
7
  from Gradio_UI import GradioUI
8
 
9
- # Define Zodiac signs and their boundaries
10
-
11
  ZODIAC_SIGNS = [
12
  ("Aries", 0, 30),
13
  ("Taurus", 30, 60),
@@ -23,7 +22,7 @@ ZODIAC_SIGNS = [
23
  ("Pisces", 330, 360),
24
  ]
25
 
26
- # Define Moon phases
27
  MOON_PHASES = [
28
  ("New Moon", 0, 45),
29
  ("Waxing Crescent", 45, 90),
@@ -38,76 +37,79 @@ MOON_PHASES = [
38
  @tool
39
  def get_moon_info(date_time: str) -> dict:
40
  """
 
41
 
42
- A tool that calculates the Moon's Zodiac sign and phase for a given date and time.
43
-
44
  Args:
45
- date_time: A string in ISO 8601 format (YYYY-MM-DDTHH:MM:SS), e.g., '2023-10-05T12:00:00'.
46
-
47
  Returns:
48
- A dictionary containing the Moon's Zodiac sign and phase.
 
49
  """
50
-
51
  try:
52
- # Parse input date and time
53
  user_time = datetime.datetime.strptime(date_time, "%Y-%m-%dT%H:%M:%S")
54
  user_time = pytz.utc.localize(user_time)
55
 
56
  # Load astronomical data
57
- planets = load('de421.bsp') # Ensure this file is accessible
58
  ts = load.timescale()
59
-
60
- # Convert user time to Skyfield timescale
61
  t = ts.from_datetime(user_time)
62
 
63
- # Get the geocentric position of the Moon
64
  earth = planets['earth']
65
  moon = planets['moon']
 
 
 
66
  astrometric = earth.at(t).observe(moon)
67
- ra, dec, distance = astrometric.radec()
 
68
 
69
- # Convert RA to degrees
70
- ra_in_degrees = ra._degrees
71
 
72
- # Determine the Zodiac sign based on RA
73
  zodiac_sign = "Unknown"
 
74
  for sign, start, end in ZODIAC_SIGNS:
75
- if start <= ra_in_degrees < end:
76
- zodiac_sign = f"{sign} ({start}° - {end}°)"
 
77
  break
78
 
79
- # Calculate the Moon's phase
80
- moon_phase_angle = earth.at(t).observe(moon).apparent().phase_angle().degrees
 
 
 
 
81
  moon_phase = "Unknown"
82
  for phase, start, end in MOON_PHASES:
83
- if start <= moon_phase_angle < end:
84
  moon_phase = phase
85
  break
86
 
87
  return {
88
- "zodiac_sign": zodiac_sign,
89
- "moon_phase": moon_phase,
90
  }
91
 
92
  except Exception as e:
93
- return {"error": f"Invalid input or calculation error: {str(e)}"}
94
 
 
95
  @tool
96
  def get_current_time_in_timezone(timezone: str) -> str:
97
- """A tool that fetches the current local time in a specified timezone.
98
- Args:
99
- timezone: A string representing a valid timezone (e.g., 'America/New_York').
100
- """
101
  try:
102
- # Create timezone object
103
  tz = pytz.timezone(timezone)
104
- # Get current time in that timezone
105
  local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
106
- return f"The current local time in {timezone} is: {local_time}"
107
  except Exception as e:
108
- return f"Error fetching time for timezone '{timezone}': {str(e)}"
109
-
110
 
 
111
  final_answer = FinalAnswerTool()
112
  model = HfApiModel(
113
  max_tokens=2096,
@@ -116,24 +118,19 @@ model = HfApiModel(
116
  custom_role_conversions=None,
117
  )
118
 
119
-
120
- # Import tool from Hub
121
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
122
 
123
  with open("prompts.yaml", 'r') as stream:
124
  prompt_templates = yaml.safe_load(stream)
125
-
126
  agent = CodeAgent(
127
  model=model,
128
- tools=[final_answer, get_moon_info], # Add the updated tool here
129
  max_steps=6,
130
  verbosity_level=1,
131
- grammar=None,
132
- planning_interval=None,
133
- name=None,
134
- description=None,
135
  prompt_templates=prompt_templates
136
  )
137
 
138
-
139
- GradioUI(agent).launch()
 
2
  import datetime
3
  import pytz
4
  import yaml
5
+ from skyfield.api import load, Topos, load_file
6
  from tools.final_answer import FinalAnswerTool
7
  from Gradio_UI import GradioUI
8
 
9
+ # Define Zodiac signs and their boundaries (0° to 360° ecliptic longitude)
 
10
  ZODIAC_SIGNS = [
11
  ("Aries", 0, 30),
12
  ("Taurus", 30, 60),
 
22
  ("Pisces", 330, 360),
23
  ]
24
 
25
+ # Moon phase boundaries (0° to 360° phase angle)
26
  MOON_PHASES = [
27
  ("New Moon", 0, 45),
28
  ("Waxing Crescent", 45, 90),
 
37
  @tool
38
  def get_moon_info(date_time: str) -> dict:
39
  """
40
+ Returns Moon's Zodiac position with degrees/minutes and phase for given date/time.
41
 
 
 
42
  Args:
43
+ date_time (str): ISO 8601 format (YYYY-MM-DDTHH:MM:SS)
44
+
45
  Returns:
46
+ dict: {"zodiac_position": "Leo 15°30'",
47
+ "moon_phase": "Waxing Gibbous"}
48
  """
 
49
  try:
50
+ # Parse input time
51
  user_time = datetime.datetime.strptime(date_time, "%Y-%m-%dT%H:%M:%S")
52
  user_time = pytz.utc.localize(user_time)
53
 
54
  # Load astronomical data
55
+ planets = load('de421.bsp')
56
  ts = load.timescale()
 
 
57
  t = ts.from_datetime(user_time)
58
 
59
+ # Get celestial bodies
60
  earth = planets['earth']
61
  moon = planets['moon']
62
+ sun = planets['sun']
63
+
64
+ # Calculate geocentric ecliptic longitude
65
  astrometric = earth.at(t).observe(moon)
66
+ _, _, ecliptic_lon = astrometric.ecliptic_latlon() # Use ecliptic longitude
67
+ lon_deg = ecliptic_lon.degrees % 360 # Normalize to 0-360°
68
 
69
+ # Calculate phase angle
70
+ phase_angle = earth.at(t).observe(moon).apparent().phase_angle(sun).degrees
71
 
72
+ # Determine Zodiac sign and position
73
  zodiac_sign = "Unknown"
74
+ position_degrees = 0
75
  for sign, start, end in ZODIAC_SIGNS:
76
+ if start <= lon_deg < end:
77
+ zodiac_sign = sign
78
+ position_degrees = lon_deg - start
79
  break
80
 
81
+ # Convert to degrees/minutes
82
+ degrees = int(position_degrees)
83
+ minutes = int((position_degrees % 1) * 60)
84
+ position_str = f"{zodiac_sign} {degrees}°{minutes:02}'"
85
+
86
+ # Determine phase
87
  moon_phase = "Unknown"
88
  for phase, start, end in MOON_PHASES:
89
+ if start <= phase_angle < end:
90
  moon_phase = phase
91
  break
92
 
93
  return {
94
+ "zodiac_position": position_str,
95
+ "moon_phase": moon_phase
96
  }
97
 
98
  except Exception as e:
99
+ return {"error": f"Error: {str(e)}"}
100
 
101
+ # Existing timezone tool remains unchanged
102
  @tool
103
  def get_current_time_in_timezone(timezone: str) -> str:
104
+ """Returns current local time in specified timezone"""
 
 
 
105
  try:
 
106
  tz = pytz.timezone(timezone)
 
107
  local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
108
+ return f"Local time in {timezone}: {local_time}"
109
  except Exception as e:
110
+ return f"Error: {str(e)}"
 
111
 
112
+ # Model configuration as specified
113
  final_answer = FinalAnswerTool()
114
  model = HfApiModel(
115
  max_tokens=2096,
 
118
  custom_role_conversions=None,
119
  )
120
 
121
+ # Setup agent with updated tools
 
122
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
123
 
124
  with open("prompts.yaml", 'r') as stream:
125
  prompt_templates = yaml.safe_load(stream)
126
+
127
  agent = CodeAgent(
128
  model=model,
129
+ tools=[final_answer, get_moon_info, get_current_time_in_timezone, image_generation_tool],
130
  max_steps=6,
131
  verbosity_level=1,
 
 
 
 
132
  prompt_templates=prompt_templates
133
  )
134
 
135
+ if __name__ == "__main__":
136
+ GradioUI(agent).launch()