SergeyO7 commited on
Commit
082b485
·
verified ·
1 Parent(s): 008435d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -23
app.py CHANGED
@@ -1,10 +1,9 @@
1
  from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
2
  import datetime
3
- import requests
4
  import pytz
5
  import yaml
6
- from tools.final_answer import FinalAnswerTool
7
  from skyfield.api import load, Topos
 
8
  from Gradio_UI import GradioUI
9
 
10
  # Define Zodiac signs and their boundaries
@@ -23,32 +22,72 @@ ZODIAC_SIGNS = [
23
  ("Pisces", 330, 360),
24
  ]
25
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  @tool
27
- def get_moon_position_in_zodiac() -> str:
28
- """A tool that calculates the current position of the Moon in the Zodiac."""
29
- # Load astronomical data
30
- planets = load('de421.bsp') # Ensure this file is accessible
31
- ts = load.timescale()
 
 
 
 
 
 
 
 
 
 
 
 
 
32
 
33
- # Get the current time in UTC
34
- now = datetime.datetime.now(pytz.utc)
35
- t = ts.from_datetime(now)
36
 
37
- # Get the geocentric position of the Moon
38
- earth = planets['earth']
39
- moon = planets['moon']
40
- astrometric = earth.at(t).observe(moon)
41
- ra, dec, distance = astrometric.radec()
42
 
43
- # Convert RA to degrees
44
- ra_in_degrees = ra._degrees
45
 
46
- # Determine the Zodiac sign based on RA
47
- for sign, start, end in ZODIAC_SIGNS:
48
- if start <= ra_in_degrees < end:
49
- return f"The Moon is currently in {sign} ({start}° - {end}°)"
 
 
50
 
51
- return "Unknown"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
 
53
  @tool
54
  def get_current_time_in_timezone(timezone: str) -> str:
@@ -83,7 +122,7 @@ with open("prompts.yaml", 'r') as stream:
83
 
84
  agent = CodeAgent(
85
  model=model,
86
- tools=[final_answer, get_moon_position_in_zodiac], # Add the updated tool here
87
  max_steps=6,
88
  verbosity_level=1,
89
  grammar=None,
 
1
  from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
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
 
22
  ("Pisces", 330, 360),
23
  ]
24
 
25
+ # Define Moon phases
26
+ MOON_PHASES = [
27
+ ("New Moon", 0, 45),
28
+ ("Waxing Crescent", 45, 90),
29
+ ("First Quarter", 90, 135),
30
+ ("Waxing Gibbous", 135, 180),
31
+ ("Full Moon", 180, 225),
32
+ ("Waning Gibbous", 225, 270),
33
+ ("Last Quarter", 270, 315),
34
+ ("Waning Crescent", 315, 360),
35
+ ]
36
+
37
  @tool
38
+ def get_moon_info(date_time: str) -> dict:
39
+ """
40
+ A tool that calculates the Moon's Zodiac sign and phase for a given date and time.
41
+
42
+ Args:
43
+ date_time: A string in ISO 8601 format (YYYY-MM-DDTHH:MM:SS), e.g., '2023-10-05T12:00:00'.
44
+
45
+ Returns:
46
+ A dictionary containing the Moon's Zodiac sign and phase.
47
+ """
48
+ try:
49
+ # Parse input date and time
50
+ user_time = datetime.datetime.strptime(date_time, "%Y-%m-%dT%H:%M:%S")
51
+ user_time = pytz.utc.localize(user_time)
52
+
53
+ # Load astronomical data
54
+ planets = load('de421.bsp') # Ensure this file is accessible
55
+ ts = load.timescale()
56
 
57
+ # Convert user time to Skyfield timescale
58
+ t = ts.from_datetime(user_time)
 
59
 
60
+ # Get the geocentric position of the Moon
61
+ earth = planets['earth']
62
+ moon = planets['moon']
63
+ astrometric = earth.at(t).observe(moon)
64
+ ra, dec, distance = astrometric.radec()
65
 
66
+ # Convert RA to degrees
67
+ ra_in_degrees = ra._degrees
68
 
69
+ # Determine the Zodiac sign based on RA
70
+ zodiac_sign = "Unknown"
71
+ for sign, start, end in ZODIAC_SIGNS:
72
+ if start <= ra_in_degrees < end:
73
+ zodiac_sign = f"{sign} ({start}° - {end}°)"
74
+ break
75
 
76
+ # Calculate the Moon's phase
77
+ moon_phase_angle = earth.at(t).observe(moon).apparent().phase_angle().degrees
78
+ moon_phase = "Unknown"
79
+ for phase, start, end in MOON_PHASES:
80
+ if start <= moon_phase_angle < end:
81
+ moon_phase = phase
82
+ break
83
+
84
+ return {
85
+ "zodiac_sign": zodiac_sign,
86
+ "moon_phase": moon_phase,
87
+ }
88
+
89
+ except Exception as e:
90
+ return {"error": f"Invalid input or calculation error: {str(e)}"}
91
 
92
  @tool
93
  def get_current_time_in_timezone(timezone: str) -> str:
 
122
 
123
  agent = CodeAgent(
124
  model=model,
125
+ tools=[final_answer, get_moon_info], # Add the updated tool here
126
  max_steps=6,
127
  verbosity_level=1,
128
  grammar=None,