wakeupmh commited on
Commit
966694f
·
verified ·
1 Parent(s): ae7a494

meteor tool

Browse files
Files changed (1) hide show
  1. app.py +127 -8
app.py CHANGED
@@ -7,17 +7,136 @@ from tools.final_answer import FinalAnswerTool
7
 
8
  from Gradio_UI import GradioUI
9
 
10
- # Below is an example of a tool that does nothing. Amaze us with your creativity !
11
  @tool
12
- def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
13
- #Keep this format for the description / args / args description but feel free to modify the tool
14
- """A tool that does nothing yet
15
  Args:
16
- arg1: the first argument
17
- arg2: the second argument
18
  """
19
- return "What magic will you build ?"
20
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  @tool
22
  def get_current_time_in_timezone(timezone: str) -> str:
23
  """A tool that fetches the current local time in a specified timezone.
 
7
 
8
  from Gradio_UI import GradioUI
9
 
 
10
  @tool
11
+ def find_nearest_meteor(address: str) -> str:
12
+ """A tool that finds the nearest meteor landing site based on a given address.
13
+
14
  Args:
15
+ address: A string representing an address (e.g., '123 Main St, New York, NY')
 
16
  """
17
+ import requests
18
+ import math
19
+ from urllib.parse import quote
20
+
21
+ def geocode_address(address):
22
+ """Convert address to latitude and longitude using OpenCage API."""
23
+ base_url = "https://api.opencagedata.com/geocode/v1/json"
24
+ api_key = "03c48dae07364cabb7f121d8c1519492"
25
+
26
+ # URL encode the address
27
+ encoded_address = quote(address)
28
+
29
+ # Construct the full request URL
30
+ url = f"{base_url}?q={encoded_address}&key={api_key}&no_annotations=1&language=en"
31
+
32
+ headers = {
33
+ 'accept': 'application/json, text/javascript, */*; q=0.01',
34
+ 'accept-language': 'pt-BR,pt;q=0.9,en-US;q=0.8,en;q=0.7',
35
+ 'origin': 'https://www.gps-coordinates.net',
36
+ 'referer': 'https://www.gps-coordinates.net/',
37
+ 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36'
38
+ }
39
+
40
+ try:
41
+ response = requests.get(url, headers=headers)
42
+ data = response.json()
43
+
44
+ if data['total_results'] > 0:
45
+ location = data['results'][0]['geometry']
46
+ return {
47
+ 'lat': location['lat'],
48
+ 'lng': location['lng'],
49
+ 'formatted_address': data['results'][0]['formatted']
50
+ }
51
+ else:
52
+ return None
53
+ except Exception as e:
54
+ return f"Error geocoding address: {str(e)}"
55
+
56
+ def get_meteor_data():
57
+ """Fetch meteor landing site data from NASA API."""
58
+ url = "https://data.nasa.gov/resource/gh4g-9sfh.json"
59
+ try:
60
+ response = requests.get(url)
61
+ return response.json()
62
+ except Exception as e:
63
+ return f"Error fetching meteor data: {str(e)}"
64
+
65
+ def calculate_distance(lat1, lon1, lat2, lon2):
66
+ """Calculate the great circle distance between two points on earth."""
67
+ # Radius of earth in kilometers
68
+ R = 6371.0
69
+
70
+ # Convert latitude and longitude from degrees to radians
71
+ lat1_rad = math.radians(lat1)
72
+ lon1_rad = math.radians(lon1)
73
+ lat2_rad = math.radians(lat2)
74
+ lon2_rad = math.radians(lon2)
75
+
76
+ # Difference in coordinates
77
+ dlon = lon2_rad - lon1_rad
78
+ dlat = lat2_rad - lat1_rad
79
+
80
+ # Haversine formula
81
+ a = math.sin(dlat / 2)**2 + math.cos(lat1_rad) * math.cos(lat2_rad) * math.sin(dlon / 2)**2
82
+ c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
83
+ distance = R * c
84
+
85
+ return distance
86
+
87
+ # Geocode the address
88
+ location_data = geocode_address(address)
89
+ if not location_data or isinstance(location_data, str):
90
+ return f"Could not geocode address: {address}. {location_data if isinstance(location_data, str) else ''}"
91
+
92
+ # Get meteor data
93
+ meteor_data = get_meteor_data()
94
+ if isinstance(meteor_data, str):
95
+ return meteor_data
96
+
97
+ # Calculate distance to each meteor
98
+ nearest_meteor = None
99
+ min_distance = float('inf')
100
+
101
+ for meteor in meteor_data:
102
+ # Check if meteor has geolocation data
103
+ if 'geolocation' in meteor and meteor['geolocation'] and 'latitude' in meteor['geolocation'] and 'longitude' in meteor['geolocation']:
104
+ try:
105
+ meteor_lat = float(meteor['geolocation']['latitude'])
106
+ meteor_lng = float(meteor['geolocation']['longitude'])
107
+
108
+ distance = calculate_distance(
109
+ location_data['lat'],
110
+ location_data['lng'],
111
+ meteor_lat,
112
+ meteor_lng
113
+ )
114
+
115
+ if distance < min_distance:
116
+ min_distance = distance
117
+ nearest_meteor = meteor
118
+ except (ValueError, TypeError):
119
+ # Skip entries with invalid coordinates
120
+ continue
121
+
122
+ if nearest_meteor:
123
+ # Format the response
124
+ name = nearest_meteor.get('name', 'Unknown')
125
+ year = nearest_meteor.get('year', 'Unknown year')
126
+ mass = nearest_meteor.get('mass', 'Unknown mass')
127
+ recclass = nearest_meteor.get('recclass', 'Unknown classification')
128
+
129
+ return (
130
+ f"Nearest meteor to {location_data['formatted_address']}:\n"
131
+ f"Name: {name}\n"
132
+ f"Year: {year}\n"
133
+ f"Classification: {recclass}\n"
134
+ f"Mass (grams): {mass}\n"
135
+ f"Distance: {min_distance:.2f} km\n"
136
+ f"Coordinates: {nearest_meteor['geolocation']['latitude']}, {nearest_meteor['geolocation']['longitude']}"
137
+ )
138
+ else:
139
+ return "No meteor landing sites found with valid coordinates."
140
  @tool
141
  def get_current_time_in_timezone(timezone: str) -> str:
142
  """A tool that fetches the current local time in a specified timezone.