Spaces:
Sleeping
Sleeping
File size: 6,890 Bytes
9b5b26a c19d193 6aae614 8fe992b 9b5b26a 966694f 9b5b26a 966694f 9b5b26a 966694f 9b5b26a 8c01ffb 6aae614 ae7a494 e121372 bf6d34c 29ec968 fe328e0 13d500a 8c01ffb 9b5b26a 8c01ffb 861422e 9b5b26a 8c01ffb 8fe992b 3451846 8c01ffb 861422e 8fe992b 9b5b26a 8c01ffb |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
import datetime
import requests
import pytz
import yaml
from tools.final_answer import FinalAnswerTool
from Gradio_UI import GradioUI
@tool
def find_nearest_meteor(address: str) -> str:
"""A tool that finds the nearest meteor landing site based on a given address.
Args:
address: A string representing an address (e.g., '123 Main St, New York, NY')
"""
import requests
import math
from urllib.parse import quote
def geocode_address(address):
"""Convert address to latitude and longitude using OpenCage API."""
base_url = "https://api.opencagedata.com/geocode/v1/json"
api_key = "03c48dae07364cabb7f121d8c1519492"
# URL encode the address
encoded_address = quote(address)
# Construct the full request URL
url = f"{base_url}?q={encoded_address}&key={api_key}&no_annotations=1&language=en"
headers = {
'accept': 'application/json, text/javascript, */*; q=0.01',
'accept-language': 'pt-BR,pt;q=0.9,en-US;q=0.8,en;q=0.7',
'origin': 'https://www.gps-coordinates.net',
'referer': 'https://www.gps-coordinates.net/',
'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'
}
try:
response = requests.get(url, headers=headers)
data = response.json()
if data['total_results'] > 0:
location = data['results'][0]['geometry']
return {
'lat': location['lat'],
'lng': location['lng'],
'formatted_address': data['results'][0]['formatted']
}
else:
return None
except Exception as e:
return f"Error geocoding address: {str(e)}"
def get_meteor_data():
"""Fetch meteor landing site data from NASA API."""
url = "https://data.nasa.gov/resource/gh4g-9sfh.json"
try:
response = requests.get(url)
return response.json()
except Exception as e:
return f"Error fetching meteor data: {str(e)}"
def calculate_distance(lat1, lon1, lat2, lon2):
"""Calculate the great circle distance between two points on earth."""
# Radius of earth in kilometers
R = 6371.0
# Convert latitude and longitude from degrees to radians
lat1_rad = math.radians(lat1)
lon1_rad = math.radians(lon1)
lat2_rad = math.radians(lat2)
lon2_rad = math.radians(lon2)
# Difference in coordinates
dlon = lon2_rad - lon1_rad
dlat = lat2_rad - lat1_rad
# Haversine formula
a = math.sin(dlat / 2)**2 + math.cos(lat1_rad) * math.cos(lat2_rad) * math.sin(dlon / 2)**2
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
distance = R * c
return distance
# Geocode the address
location_data = geocode_address(address)
if not location_data or isinstance(location_data, str):
return f"Could not geocode address: {address}. {location_data if isinstance(location_data, str) else ''}"
# Get meteor data
meteor_data = get_meteor_data()
if isinstance(meteor_data, str):
return meteor_data
# Calculate distance to each meteor
nearest_meteor = None
min_distance = float('inf')
for meteor in meteor_data:
# Check if meteor has geolocation data
if 'geolocation' in meteor and meteor['geolocation'] and 'latitude' in meteor['geolocation'] and 'longitude' in meteor['geolocation']:
try:
meteor_lat = float(meteor['geolocation']['latitude'])
meteor_lng = float(meteor['geolocation']['longitude'])
distance = calculate_distance(
location_data['lat'],
location_data['lng'],
meteor_lat,
meteor_lng
)
if distance < min_distance:
min_distance = distance
nearest_meteor = meteor
except (ValueError, TypeError):
# Skip entries with invalid coordinates
continue
if nearest_meteor:
# Format the response
name = nearest_meteor.get('name', 'Unknown')
year = nearest_meteor.get('year', 'Unknown year')
mass = nearest_meteor.get('mass', 'Unknown mass')
recclass = nearest_meteor.get('recclass', 'Unknown classification')
return (
f"Nearest meteor to {location_data['formatted_address']}:\n"
f"Name: {name}\n"
f"Year: {year}\n"
f"Classification: {recclass}\n"
f"Mass (grams): {mass}\n"
f"Distance: {min_distance:.2f} km\n"
f"Coordinates: {nearest_meteor['geolocation']['latitude']}, {nearest_meteor['geolocation']['longitude']}"
)
else:
return "No meteor landing sites found with valid coordinates."
@tool
def get_current_time_in_timezone(timezone: str) -> str:
"""A tool that fetches the current local time in a specified timezone.
Args:
timezone: A string representing a valid timezone (e.g., 'America/New_York').
"""
try:
# Create timezone object
tz = pytz.timezone(timezone)
# Get current time in that timezone
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
return f"The current local time in {timezone} is: {local_time}"
except Exception as e:
return f"Error fetching time for timezone '{timezone}': {str(e)}"
final_answer = FinalAnswerTool()
# 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:
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
model = HfApiModel(
max_tokens=2096,
temperature=0.5,
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
custom_role_conversions=None,
)
# Import tool from Hub
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
with open("prompts.yaml", 'r') as stream:
prompt_templates = yaml.safe_load(stream)
agent = CodeAgent(
model=model,
tools=[find_nearest_meteor, final_answer], ## add your tools here (don't remove final answer)
max_steps=6,
verbosity_level=1,
grammar=None,
planning_interval=None,
name=None,
description=None,
prompt_templates=prompt_templates
)
GradioUI(agent).launch() |