Spaces:
Sleeping
Sleeping
Create flight_status.py
Browse files- tools/flight_status.py +78 -0
tools/flight_status.py
ADDED
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import Any, Optional
|
2 |
+
from smolagents.tools import Tool
|
3 |
+
import requests
|
4 |
+
|
5 |
+
class FlightStatus(Tool):
|
6 |
+
name = "flight_status"
|
7 |
+
description = "Fetches the status of a given flight based on the flight number using AviationStack API."
|
8 |
+
inputs = {
|
9 |
+
'flight': {'type': 'string', 'description': 'Flight Code to fetch the information.'},
|
10 |
+
'api_key': {'type': 'string', 'description': 'The API key for accessing the AviationStack data'}
|
11 |
+
}
|
12 |
+
output_type = {'flight_info': {'type': 'dict', 'description': 'A dictionary containing the flight status information or an error message'}}
|
13 |
+
|
14 |
+
def forward(self) -> dict:
|
15 |
+
"""
|
16 |
+
Fetches the status of a flight by querying the AviationStack API.
|
17 |
+
Returns a dictionary containing flight status or error details.
|
18 |
+
"""
|
19 |
+
flight_number = self.inputs.get('flight')
|
20 |
+
#api_key = self.inputs.get('api_key')
|
21 |
+
api_key = ""
|
22 |
+
|
23 |
+
# Ensure flight number and API key are provided
|
24 |
+
if not flight_number or not api_key:
|
25 |
+
return {'error': 'Missing flight number or API key'}
|
26 |
+
|
27 |
+
base_url = "http://api.aviationstack.com/v1/flights"
|
28 |
+
|
29 |
+
# Parameters for the API request
|
30 |
+
params = {
|
31 |
+
'access_key': api_key,
|
32 |
+
'flight_iata': flight_number # Flight number in IATA format (e.g., "AA100")
|
33 |
+
}
|
34 |
+
|
35 |
+
try:
|
36 |
+
# Send a GET request to the AviationStack API
|
37 |
+
#response = requests.get(base_url, params=params)
|
38 |
+
response = 200
|
39 |
+
|
40 |
+
# Check if the request was successful
|
41 |
+
# if response.status_code == 200:
|
42 |
+
# data = response.json()
|
43 |
+
|
44 |
+
# # If no flight data is found
|
45 |
+
# if not data.get("data"):
|
46 |
+
# return {'error': f"No information available for flight {flight_number}."}
|
47 |
+
|
48 |
+
# flight_data = data['data'][0] # Get the first flight result
|
49 |
+
if response == 200:
|
50 |
+
flight_data = {'flight_date': '2025-02-16', 'flight_status': 'scheduled', 'departure': {'airport': 'John F Kennedy International', 'timezone': 'America/New_York', 'iata': 'JFK', 'icao': 'KJFK', 'terminal': '8', 'gate': '4', 'delay': None, 'scheduled': '2025-02-16T19:00:00+00:00', 'estimated': '2025-02-16T19:00:00+00:00', 'actual': None, 'estimated_runway': None, 'actual_runway': None}, 'arrival': {'airport': 'Heathrow', 'timezone': 'Europe/London', 'iata': 'LHR', 'icao': 'EGLL', 'terminal': '3', 'gate': None, 'baggage': None, 'delay': None, 'scheduled': '2025-02-17T06:50:00+00:00', 'estimated': '2025-02-17T06:50:00+00:00', 'actual': None, 'estimated_runway': None, 'actual_runway': None}, 'airline': {'name': 'American Airlines', 'iata': 'AA', 'icao': 'AAL'}, 'flight': {'number': '100', 'iata': 'AA100', 'icao': 'AAL100', 'codeshared': None}, 'aircraft': None, 'live': None}
|
51 |
+
|
52 |
+
# Extract relevant flight information
|
53 |
+
flight_info = {
|
54 |
+
'flight_number': flight_data['flight']['iata'],
|
55 |
+
'airline': flight_data['airline']['name'],
|
56 |
+
'status': flight_data['flight_status'],
|
57 |
+
'departure': {
|
58 |
+
'airport': flight_data['departure']['airport'],
|
59 |
+
'estimated': flight_data['departure'].get('estimated', 'N/A'),
|
60 |
+
'actual': flight_data['departure'].get('estimated_runway', 'N/A'),
|
61 |
+
},
|
62 |
+
'arrival': {
|
63 |
+
'airport': flight_data['arrival']['airport'],
|
64 |
+
'estimated': flight_data['arrival'].get('estimated', 'N/A'),
|
65 |
+
'actual': flight_data['arrival'].get('estimated_runway', 'N/A'),
|
66 |
+
},
|
67 |
+
}
|
68 |
+
|
69 |
+
return flight_info
|
70 |
+
|
71 |
+
else:
|
72 |
+
return {'error': f"Error fetching data for {flight_number}. Status code: {response.status_code}"}
|
73 |
+
|
74 |
+
except Exception as e:
|
75 |
+
return {'error': f"An error occurred: {str(e)}"}
|
76 |
+
|
77 |
+
def __init__(self, *args, **kwargs):
|
78 |
+
self.is_initialized = True # Marking the tool as initialized
|