Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import joblib
|
| 4 |
+
|
| 5 |
+
predict_model = joblib.load('/content/mejor_modelo.pkl')
|
| 6 |
+
scaler = joblib.load('/content/scaler_x.pkl')
|
| 7 |
+
|
| 8 |
+
origen_mapping = {
|
| 9 |
+
'bangalore': 'origen_bangalore',
|
| 10 |
+
'chennai': 'origen_chennai',
|
| 11 |
+
'delhi': 'origen_delhi',
|
| 12 |
+
'hyderabad': 'origen_hyderabad',
|
| 13 |
+
'kolkata': 'origen_kolkata',
|
| 14 |
+
'mumbai': 'origen_mumbai'
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
destino_mapping = {
|
| 18 |
+
'bangalore': 'destino_bangalore',
|
| 19 |
+
'chennai': 'destino_chennai',
|
| 20 |
+
'delhi': 'destino_delhi',
|
| 21 |
+
'hyderabad': 'destino_hyderabad',
|
| 22 |
+
'kolkata': 'destino_kolkata',
|
| 23 |
+
'mumbai': 'destino_mumbai'
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
stops_mapping = {
|
| 27 |
+
'zero': 'stop_zero',
|
| 28 |
+
'one': 'stop_one',
|
| 29 |
+
'two_or_more': 'stop_two_or_more'
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
airline_mapping = {
|
| 33 |
+
'air_india': 'airline_air_india',
|
| 34 |
+
'vistara': 'airline_vistara'
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def predict_flight_price_gradio(airline, origin, destination, stops, time_taken_hours):
|
| 39 |
+
# Vector inicial con ceros
|
| 40 |
+
feature_vector = np.zeros(df.drop('price', axis=1).shape[1])
|
| 41 |
+
|
| 42 |
+
# Nombres de columnas (sin 'price')
|
| 43 |
+
feature_names = df.drop('price', axis=1).columns
|
| 44 |
+
|
| 45 |
+
# Mapear categorías
|
| 46 |
+
if origin.lower() in origen_mapping:
|
| 47 |
+
feature_vector[feature_names.get_loc(origen_mapping[origin.lower()])] = 1.0
|
| 48 |
+
if destination.lower() in destino_mapping:
|
| 49 |
+
feature_vector[feature_names.get_loc(destino_mapping[destination.lower()])] = 1.0
|
| 50 |
+
if stops.lower().replace(' ', '_') in stops_mapping:
|
| 51 |
+
feature_vector[feature_names.get_loc(stops_mapping[stops.lower().replace(' ', '_')])] = 1.0
|
| 52 |
+
if airline.lower().replace(' ', '_') in airline_mapping:
|
| 53 |
+
feature_vector[feature_names.get_loc(airline_mapping[airline.lower().replace(' ', '_')])] = 1.0
|
| 54 |
+
|
| 55 |
+
# Tiempo de vuelo
|
| 56 |
+
feature_vector[feature_names.get_loc('time_taken_hours')] = time_taken_hours
|
| 57 |
+
|
| 58 |
+
# Redimensionar para scaler y modelo
|
| 59 |
+
feature_vector_reshaped = feature_vector.reshape(1, -1)
|
| 60 |
+
|
| 61 |
+
# Escalar datos
|
| 62 |
+
scaled_features = scaler.transform(feature_vector_reshaped)
|
| 63 |
+
|
| 64 |
+
# Predecir precio
|
| 65 |
+
predicted_price = predict_model.predict(scaled_features)
|
| 66 |
+
|
| 67 |
+
return predicted_price[0]
|
| 68 |
+
|
| 69 |
+
# Input
|
| 70 |
+
input_airline = gr.Dropdown(list(airline_mapping.keys()), label="Airline")
|
| 71 |
+
input_origin = gr.Dropdown(list(origen_mapping.keys()), label="Origin City")
|
| 72 |
+
input_destination = gr.Dropdown(list(destino_mapping.keys()), label="Destination City")
|
| 73 |
+
input_stops = gr.Dropdown(list(stops_mapping.keys()), label="Number of Stops")
|
| 74 |
+
input_time_taken = gr.Number(label="Time Taken (Hours)")
|
| 75 |
+
|
| 76 |
+
# Salida
|
| 77 |
+
output_price = gr.Number(label="Predicted Price")
|
| 78 |
+
|
| 79 |
+
iface = gr.Interface(
|
| 80 |
+
fn=predict_flight_price_gradio,
|
| 81 |
+
inputs=[input_airline, input_origin, input_destination, input_stops, input_time_taken],
|
| 82 |
+
outputs=output_price,
|
| 83 |
+
title="Flight Price Prediction"
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
# Launch the interface
|
| 87 |
+
iface.launch()
|