import gradio as gr import pandas as pd import joblib # Load the trained model model = joblib.load('random_forest_model.pkl') # replace with your model path # Define the function to make predictions def predict_price(host_id, neighbourhood_group, room_type, number_of_reviews, calculated_host_listings_count, latitude, longitude): # Create a dictionary with default values data = { 'host_id': host_id, 'number_of_reviews': number_of_reviews, 'calculated_host_listings_count': calculated_host_listings_count, 'latitude': latitude, 'longitude': longitude, # Set dummy values for categorical features 'neighbourhood_group_Brooklyn': 0, 'neighbourhood_group_Manhattan': 0, 'neighbourhood_group_Queens': 0, 'neighbourhood_group_Bronx': 0, 'neighbourhood_group_Staten Island': 0, 'room_type_Shared room': 0, 'room_type_Private room': 0, 'room_type_Entire home/apt': 0, } # Set appropriate values based on user input if neighbourhood_group == 'Brooklyn': data['neighbourhood_group_Brooklyn'] = 1 elif neighbourhood_group == 'Manhattan': data['neighbourhood_group_Manhattan'] = 1 elif neighbourhood_group == 'Queens': data['neighbourhood_group_Queens'] = 1 elif neighbourhood_group == 'Bronx': data['neighbourhood_group_Bronx'] = 1 elif neighbourhood_group == 'Staten Island': data['neighbourhood_group_Staten Island'] = 1 if room_type == 'Shared room': data['room_type_Shared room'] = 1 elif room_type == 'Private room': data['room_type_Private room'] = 1 elif room_type == 'Entire home/apt': data['room_type_Entire home/apt'] = 1 # Create DataFrame from the dictionary custom_data = pd.DataFrame([data]) # Make prediction predicted_price = model.predict(custom_data) return predicted_price[0] # Create the Gradio interface iface = gr.Interface( fn=predict_price, inputs=[ gr.Number(label="Host ID"), gr.Dropdown(label="Neighbourhood Group", choices=["Brooklyn", "Manhattan", "Queens", "Bronx", "Staten Island"]), gr.Dropdown(label="Room Type", choices=["Shared room", "Private room", "Entire home/apt"]), gr.Number(label="Number of Reviews"), gr.Number(label="Calculated Host Listings Count"), gr.Number(label="Latitude"), gr.Number(label="Longitude") ], outputs=gr.Number(label="Predicted Price"), title="Airbnb Price Prediction", description="Enter the details to predict the price of an Airbnb listing." ) # Launch the interface iface.launch(share=True) # Set share=True to create a public link