Spaces:
Sleeping
Sleeping
Download app.py from MaxFPonce/FlightPrice_Prediction: direct link, hf CLI and curl.
- Browser
- Download file 2.86 kB
-
https://huggingface.co/spaces/MaxFPonce/FlightPrice_Prediction/resolve/main/app.py
- Command line
-
hf download hf://spaces/MaxFPonce/FlightPrice_Prediction/app.py
-
curl -L -o app.py https://huggingface.co/spaces/MaxFPonce/FlightPrice_Prediction/resolve/main/app.py
2.86 kB
| import numpy as np | |
| import gradio as gr | |
| import joblib | |
| import pandas as pd | |
| df = pd.read_csv('./price_data_transformed.csv') | |
| predict_model = joblib.load('./mejor_modelo.pkl') | |
| scaler = joblib.load('./scaler_x.pkl') | |
| origen_mapping = { | |
| 'bangalore': 'origen_bangalore', | |
| 'chennai': 'origen_chennai', | |
| 'delhi': 'origen_delhi', | |
| 'hyderabad': 'origen_hyderabad', | |
| 'kolkata': 'origen_kolkata', | |
| 'mumbai': 'origen_mumbai' | |
| } | |
| destino_mapping = { | |
| 'bangalore': 'destino_bangalore', | |
| 'chennai': 'destino_chennai', | |
| 'delhi': 'destino_delhi', | |
| 'hyderabad': 'destino_hyderabad', | |
| 'kolkata': 'destino_kolkata', | |
| 'mumbai': 'destino_mumbai' | |
| } | |
| stops_mapping = { | |
| 'zero': 'stop_zero', | |
| 'one': 'stop_one', | |
| 'two_or_more': 'stop_two_or_more' | |
| } | |
| airline_mapping = { | |
| 'air_india': 'airline_air_india', | |
| 'vistara': 'airline_vistara' | |
| } | |
| def predict_flight_price_gradio(airline, origin, destination, stops, time_taken_hours): | |
| # Vector inicial con ceros | |
| feature_vector = np.zeros(df.drop('price', axis=1).shape[1]) | |
| # Nombres de columnas (sin 'price') | |
| feature_names = df.drop('price', axis=1).columns | |
| # Mapear categorías | |
| if origin.lower() in origen_mapping: | |
| feature_vector[feature_names.get_loc(origen_mapping[origin.lower()])] = 1.0 | |
| if destination.lower() in destino_mapping: | |
| feature_vector[feature_names.get_loc(destino_mapping[destination.lower()])] = 1.0 | |
| if stops.lower().replace(' ', '_') in stops_mapping: | |
| feature_vector[feature_names.get_loc(stops_mapping[stops.lower().replace(' ', '_')])] = 1.0 | |
| if airline.lower().replace(' ', '_') in airline_mapping: | |
| feature_vector[feature_names.get_loc(airline_mapping[airline.lower().replace(' ', '_')])] = 1.0 | |
| # Tiempo de vuelo | |
| feature_vector[feature_names.get_loc('time_taken_hours')] = time_taken_hours | |
| # Redimensionar para scaler y modelo | |
| feature_vector_reshaped = feature_vector.reshape(1, -1) | |
| # Escalar datos | |
| scaled_features = scaler.transform(feature_vector_reshaped) | |
| # Predecir precio | |
| predicted_price = predict_model.predict(scaled_features) | |
| return predicted_price[0] | |
| # Input | |
| input_airline = gr.Dropdown(list(airline_mapping.keys()), label="Airline") | |
| input_origin = gr.Dropdown(list(origen_mapping.keys()), label="Origin City") | |
| input_destination = gr.Dropdown(list(destino_mapping.keys()), label="Destination City") | |
| input_stops = gr.Dropdown(list(stops_mapping.keys()), label="Number of Stops") | |
| input_time_taken = gr.Number(label="Time Taken (Hours)") | |
| # Salida | |
| output_price = gr.Number(label="Predicted Price") | |
| iface = gr.Interface( | |
| fn=predict_flight_price_gradio, | |
| inputs=[input_airline, input_origin, input_destination, input_stops, input_time_taken], | |
| outputs=output_price, | |
| title="Flight Price Prediction" | |
| ) | |
| # Launch the interface | |
| iface.launch() |