Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,69 +1,88 @@
|
|
| 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 |
-
|
| 8 |
from Gradio_UI import GradioUI
|
| 9 |
|
| 10 |
-
#
|
| 11 |
@tool
|
| 12 |
-
def
|
| 13 |
-
|
| 14 |
-
"""A tool that does nothing yet
|
| 15 |
Args:
|
| 16 |
-
|
| 17 |
-
arg2: the second argument
|
| 18 |
"""
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
|
|
|
| 21 |
@tool
|
| 22 |
-
def
|
| 23 |
-
"""
|
| 24 |
Args:
|
| 25 |
-
|
|
|
|
| 26 |
"""
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 32 |
-
return f"The current local time in {timezone} is: {local_time}"
|
| 33 |
-
except Exception as e:
|
| 34 |
-
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
|
|
|
|
|
|
| 42 |
model = HfApiModel(
|
| 43 |
-
max_tokens=2096,
|
| 44 |
-
temperature=0.5,
|
| 45 |
-
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
|
| 46 |
-
custom_role_conversions=None,
|
| 47 |
)
|
| 48 |
|
| 49 |
-
|
| 50 |
-
# Import tool from Hub
|
| 51 |
-
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 52 |
-
|
| 53 |
-
with open("prompts.yaml", 'r') as stream:
|
| 54 |
-
prompt_templates = yaml.safe_load(stream)
|
| 55 |
-
|
| 56 |
agent = CodeAgent(
|
| 57 |
model=model,
|
| 58 |
-
tools=[final_answer
|
| 59 |
max_steps=6,
|
| 60 |
verbosity_level=1,
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
name=None,
|
| 64 |
-
description=None,
|
| 65 |
-
prompt_templates=prompt_templates
|
| 66 |
)
|
| 67 |
|
| 68 |
-
|
| 69 |
-
GradioUI(agent).launch()
|
|
|
|
| 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 Gradio_UI import GradioUI
|
| 8 |
|
| 9 |
+
# Fetch METAR/TAF aviation weather data
|
| 10 |
@tool
|
| 11 |
+
def get_aviation_weather(icao_code: str) -> str:
|
| 12 |
+
"""Fetches the METAR and TAF weather reports for a given airport ICAO code.
|
|
|
|
| 13 |
Args:
|
| 14 |
+
icao_code: The ICAO airport code (e.g., 'JFK' for John F. Kennedy International).
|
|
|
|
| 15 |
"""
|
| 16 |
+
try:
|
| 17 |
+
url = f"https://aviationweather.gov/metar/data?ids={icao_code}&format=raw&taf=on"
|
| 18 |
+
response = requests.get(url)
|
| 19 |
+
if response.status_code == 200:
|
| 20 |
+
return response.text
|
| 21 |
+
else:
|
| 22 |
+
return f"Error fetching aviation weather for {icao_code}"
|
| 23 |
+
except Exception as e:
|
| 24 |
+
return f"Failed to retrieve data: {str(e)}"
|
| 25 |
|
| 26 |
+
# Flight time estimation tool
|
| 27 |
@tool
|
| 28 |
+
def estimate_flight_time(distance_nm: float, speed_kts: float) -> str:
|
| 29 |
+
"""Estimates flight time based on distance (nautical miles) and speed (knots).
|
| 30 |
Args:
|
| 31 |
+
distance_nm: Distance in nautical miles.
|
| 32 |
+
speed_kts: Speed in knots.
|
| 33 |
"""
|
| 34 |
+
if speed_kts <= 0:
|
| 35 |
+
return "Invalid speed. Must be greater than zero."
|
| 36 |
+
time_hours = distance_nm / speed_kts
|
| 37 |
+
return f"Estimated flight time: {time_hours:.2f} hours."
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
+
# Fuel consumption estimator
|
| 40 |
+
@tool
|
| 41 |
+
def estimate_fuel_burn(rate_gph: float, time_hours: float) -> str:
|
| 42 |
+
"""Estimates total fuel consumption for a flight.
|
| 43 |
+
Args:
|
| 44 |
+
rate_gph: Fuel burn rate in gallons per hour.
|
| 45 |
+
time_hours: Estimated flight duration in hours.
|
| 46 |
+
"""
|
| 47 |
+
fuel_needed = rate_gph * time_hours
|
| 48 |
+
return f"Estimated fuel needed: {fuel_needed:.2f} gallons."
|
| 49 |
|
| 50 |
+
# Current time in major aviation hubs
|
| 51 |
+
@tool
|
| 52 |
+
def get_airport_local_time(icao_code: str) -> str:
|
| 53 |
+
"""Gets the local time of an airport using its ICAO code.
|
| 54 |
+
Args:
|
| 55 |
+
icao_code: ICAO airport code (e.g., 'LHR' for London Heathrow).
|
| 56 |
+
"""
|
| 57 |
+
airport_timezones = {
|
| 58 |
+
"JFK": "America/New_York",
|
| 59 |
+
"LAX": "America/Los_Angeles",
|
| 60 |
+
"LHR": "Europe/London",
|
| 61 |
+
"DXB": "Asia/Dubai",
|
| 62 |
+
"HND": "Asia/Tokyo"
|
| 63 |
+
}
|
| 64 |
+
if icao_code not in airport_timezones:
|
| 65 |
+
return "Airport timezone not available."
|
| 66 |
+
|
| 67 |
+
tz = pytz.timezone(airport_timezones[icao_code])
|
| 68 |
+
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 69 |
+
return f"Local time at {icao_code}: {local_time}"
|
| 70 |
|
| 71 |
+
# Finalizing agent setup
|
| 72 |
+
final_answer = FinalAnswerTool()
|
| 73 |
model = HfApiModel(
|
| 74 |
+
max_tokens=2096,
|
| 75 |
+
temperature=0.5,
|
| 76 |
+
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
|
|
|
|
| 77 |
)
|
| 78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
agent = CodeAgent(
|
| 80 |
model=model,
|
| 81 |
+
tools=[final_answer, get_aviation_weather, estimate_flight_time, estimate_fuel_burn, get_airport_local_time],
|
| 82 |
max_steps=6,
|
| 83 |
verbosity_level=1,
|
| 84 |
+
name="AeroNavBot",
|
| 85 |
+
description="An AI assistant for aviation navigation, weather, and flight planning.",
|
|
|
|
|
|
|
|
|
|
| 86 |
)
|
| 87 |
|
| 88 |
+
GradioUI(agent).launch()
|
|
|