Spaces:
Build error
Build error
| import gradio as gr | |
| import requests | |
| import joblib | |
| import pandas as pd | |
| from datetime import datetime | |
| from datetime import datetime, timedelta | |
| import os | |
| #Airport Names to ICAO codes | |
| airport_icao_map = { | |
| "Mumbai (BOM)": "VABB", | |
| "Delhi (DEL)": "VIDP", | |
| "Dubai (DXB)": "OMDB", | |
| "London Heathrow (LHR)": "EGLL", | |
| "New York JFK (JFK)": "KJFK", | |
| "Singapore Changi (SIN)": "WSSS", | |
| "Frankfurt (FRA)": "EDDF" | |
| } | |
| # Get Live Flight Status | |
| def get_flight_status(flight_iata): | |
| API_KEY = os.getenv("AVIATIONSTACK_API_KEY") | |
| url = f"http://api.aviationstack.com/v1/flights?access_key={API_KEY}&flight_iata={flight_iata}" | |
| try: | |
| response = requests.get(url) | |
| data = response.json() | |
| if "data" in data and data["data"]: | |
| flight = data["data"][0] | |
| airline = flight["airline"]["name"] | |
| departure = flight["departure"]["airport"] | |
| arrival = flight["arrival"]["airport"] | |
| dep_time = flight["departure"]["scheduled"] | |
| arr_time = flight["arrival"]["scheduled"] | |
| status = flight["flight_status"] | |
| return ( | |
| f"βοΈ **{airline}**\n" | |
| f"From **{departure}** to **{arrival}**\n" | |
| f"π Departure: `{dep_time}`\n" | |
| f"π Arrival: `{arr_time}`\n" | |
| f"π‘ Status: **{status.upper()}**" | |
| ) | |
| else: | |
| return "β Flight not found or no status available." | |
| except Exception as e: | |
| return f"β οΈ Error fetching flight data: {e}" | |
| #airport insights | |
| def get_airport_insights(icao_code): | |
| end = int(datetime.utcnow().timestamp()) | |
| begin = int((datetime.utcnow() - timedelta(hours=1)).timestamp()) | |
| url = f"https://opensky-network.org/api/flights/airport?airport={icao_code}&begin={begin}&end={end}" | |
| try: | |
| response = requests.get(url) | |
| data = response.json() | |
| if isinstance(data, list) and data: | |
| arrivals = sum(1 for f in data if f["estArrivalAirport"] == icao_code) | |
| departures = sum(1 for f in data if f["estDepartureAirport"] == icao_code) | |
| return ( | |
| f"π Airport ICAO: `{icao_code}`\n" | |
| f"π¬ Arrivals in past hour: **{arrivals}**\n" | |
| f"π« Departures in past hour: **{departures}**" | |
| ) | |
| else: | |
| return "β οΈ No flight activity found or invalid ICAO code." | |
| except Exception as e: | |
| return f"β Error fetching airport data: {e}" | |
| # Chatbot | |
| def chatbot_interface(message, history): | |
| message = message.lower() | |
| if "status of" in message: | |
| flight_code = message.split()[-1].upper() | |
| return {"role": "assistant", "content": get_flight_status(flight_code)} | |
| elif "predict delay" in message: | |
| return {"role": "assistant", "content": "βοΈ Please fill out the form below to predict delay."} | |
| else: | |
| return {"role": "assistant", "content": "π€ Ask me: `Status of SQ421`."} | |
| # β¨ Modern Gradio UI | |
| chatbot = gr.ChatInterface( | |
| fn=chatbot_interface, | |
| title="βοΈ FlightBot", | |
| description="Track Real-Time Flight Status and Airport Insights.", | |
| theme=gr.themes.Soft(), # πΈ Modern look | |
| type="messages" | |
| ) | |
| with gr.Blocks(theme=gr.themes.Soft()) as demo: | |
| gr.Markdown("## π« FlightBot β Real-time Flight Status & Provides Airport Insights") | |
| gr.Markdown("> Built with AviationStack API ") | |
| with gr.Row(): | |
| chatbot.render() | |
| # Optional Popular Flight Dropdown | |
| with gr.Accordion("π Popular Flights", open=False): | |
| flight_selector = gr.Dropdown( | |
| label="Choose a Flight", | |
| choices=[ | |
| "SQ421 β Mumbai β Singapore", | |
| "EK501 β Mumbai β Dubai", | |
| "BA143 β London β Delhi", | |
| "AA50 β Dallas β London", | |
| "LH760 β Frankfurt β Delhi", | |
| "QR579 β Delhi β Doha" | |
| ], | |
| value="AI202 β Delhi β London" | |
| ) | |
| get_status_btn = gr.Button("π Get Status") | |
| status_output = gr.Textbox(label="Flight Status") | |
| def extract_code_and_lookup(selection): | |
| code = selection.split("β")[0].strip() | |
| return get_flight_status(code) | |
| get_status_btn.click( | |
| fn=extract_code_and_lookup, | |
| inputs=flight_selector, | |
| outputs=status_output | |
| ) | |
| #Airport insights | |
| with gr.Accordion("π Airport Insights", open=False): | |
| airport_input = gr.Textbox(label="Enter Airport ICAO Code (e.g., VABB)") | |
| insights_btn = gr.Button("π Get Insights") | |
| insights_output = gr.Textbox(label="Airport Activity Insights") | |
| insights_btn.click( | |
| fn=get_airport_insights, | |
| inputs=airport_input, | |
| outputs=insights_output | |
| ) | |
| demo.launch() | |