Image-Detection / app.py
abdulrafishaik's picture
Update app.py
2c8822a verified
import pickle
import gradio as gr
import numpy as np
# Load the trained model
with open(r"final_pickle_test.pkl","rb") as f:
model = pickle.load(f)
# Define encoding mappings for categorical variables
sales_channel_mapping = {"Online": 0, "Offline": 1}
trip_type_mapping = {"Single Trip": 0, "Round Trip": 1}
route_mapping = {
"AKLDEL": 0, "AKLHGH": 1, "AKLHND": 2, "AKLICN": 3, "AKLKIX": 4, "AKLKTM": 5
}
booking_origin_mapping = {"India": 0, "USA": 1, "UK": 2} # Add actual values
def predict_booking(num_passengers, sales_channel, trip_type, purchase_lead, length_of_stay,
flight_hour, flight_day, route, booking_origin, wants_extra_baggage,
wants_preferred_seat, wants_in_flight_meals, flight_duration):
# Convert categorical values to numerical
sales_channel_encoded = sales_channel_mapping.get(sales_channel, -1)
trip_type_encoded = trip_type_mapping.get(trip_type, -1)
route_encoded = route_mapping.get(route, -1)
booking_origin_encoded = booking_origin_mapping.get(booking_origin, -1)
wants_extra_baggage = 1 if wants_extra_baggage == "Yes" else 0
wants_preferred_seat = 1 if wants_preferred_seat == "Yes" else 0
wants_in_flight_meals = 1 if wants_in_flight_meals == "Yes" else 0
# Ensure correct feature shape
input_data = np.array([[num_passengers, sales_channel_encoded, trip_type_encoded, purchase_lead,
length_of_stay, flight_hour, flight_day, route_encoded, booking_origin_encoded,
wants_extra_baggage, wants_preferred_seat, wants_in_flight_meals, flight_duration]])
print("Input Data Shape:", input_data.shape) # Debugging
# Make prediction
prediction = model.predict(input_data)[0]
return "Booking Completed βœ…" if prediction == 1 else "Booking Not Completed ❌"
# Define available options for dropdowns
routes = ["AKLDEL", "AKLHGH", "AKLHND", "AKLICN", "AKLKIX", "AKLKTM"]
booking_origins = ["India", "USA", "UK"] # Add actual locations if needed
# Create Gradio interface
iface = gr.Interface(
fn=predict_booking,
inputs=[
gr.Number(label="Number of Passengers"),
gr.Dropdown(choices=["Online", "Offline"], label="Sales Channel"),
gr.Dropdown(choices=["Single Trip", "Round Trip"], label="Trip Type"),
gr.Number(label="Purchase Lead"),
gr.Number(label="Length of Stay"),
gr.Number(label="Flight Hours"),
gr.Number(label="Flight Day"),
gr.Dropdown(choices=routes, label="Route"),
gr.Dropdown(choices=booking_origins, label="Booking Origin"),
gr.Dropdown(choices=["Yes", "No"], label="Want Extra Baggage"),
gr.Dropdown(choices=["Yes", "No"], label="Want Preferred Seat"),
gr.Dropdown(choices=["Yes", "No"], label="Want In-Flight Meals"),
gr.Number(label="Flight Duration")
],
outputs=gr.Textbox(label="Booking Prediction"),
title="British Airways Booking Predictions",
description="Enter Flight Details to Predict Booking Completion"
)
iface.launch()