|
import pickle, joblib |
|
import gradio as gr |
|
from datetime import datetime, timedelta, timezone |
|
|
|
model = joblib.load('model.pkl') |
|
|
|
def preprocess_city(selected_city): |
|
|
|
city_mapping = { |
|
'Hyderabad' : [1, 0, 0, 0, 0, 0, 0], |
|
'Indore': [1, 0, 0, 0, 0, 0, 0], |
|
'Jaipur': [0, 1, 0, 0, 0, 0, 0], |
|
'Mahabaleshwar': [0, 0, 1, 0, 0, 0, 0], |
|
'Mussoorie': [0, 0, 0, 1, 0, 0, 0], |
|
'Raipur': [0, 0, 0, 0, 1, 0, 0], |
|
'Udaipur': [0, 0, 0, 0, 0, 1, 0], |
|
'Varanasi': [0, 0, 0, 0, 0, 0, 1] |
|
} |
|
return city_mapping[selected_city] |
|
|
|
def preprocess_date(date_string): |
|
|
|
date_obj = datetime.strptime(date_string, '%Y-%m-%d') |
|
year = date_obj.year |
|
month = date_obj.month |
|
day = date_obj.day |
|
return year, month, day |
|
|
|
def calculate_lead_time(checkin_date): |
|
|
|
input_date = datetime.strptime(checkin_date, '%Y-%m-%d') |
|
|
|
|
|
current_date = datetime.now(timezone(timedelta(hours=5, minutes=30))) |
|
|
|
|
|
current_date = current_date.replace(tzinfo=input_date.tzinfo) |
|
|
|
|
|
lead_time = (input_date - current_date).days |
|
|
|
return lead_time |
|
|
|
def is_weekend(checkin_date): |
|
|
|
input_date = datetime.strptime(checkin_date, '%Y-%m-%d') |
|
|
|
|
|
day_of_week = input_date.weekday() |
|
|
|
|
|
return 1 if day_of_week == 4 or day_of_week == 5 else 0 |
|
|
|
def predict(selected_city, checkin_date, star_rating, text_rating, season, additional_views, room_category): |
|
|
|
|
|
|
|
|
|
|
|
|
|
season_binary = 1 if season == 'On Season' else 0 |
|
|
|
additional_views_binary = 1 if additional_views == 'Yes' else 0 |
|
|
|
room_categories = ["Dorm", "Standard", "Deluxe", "Executive", "Suite"] |
|
room_category_number = room_categories.index(room_category) |
|
|
|
|
|
year, month, day = preprocess_date(checkin_date) |
|
|
|
|
|
city_encoded = preprocess_city(selected_city) |
|
|
|
|
|
lead_time = calculate_lead_time(checkin_date) |
|
|
|
|
|
is_weekend_value = is_weekend(checkin_date) |
|
|
|
|
|
input_data = [star_rating, text_rating, season_binary, day, month, year, is_weekend_value, lead_time,room_category_number, additional_views_binary]+city_encoded |
|
|
|
|
|
prediction = model.predict([input_data]) |
|
return "{:.2f}".format(prediction[0]) |
|
|
|
|
|
city_dropdown = gr.components.Dropdown(choices=['Hyderabad', 'Indore', 'Jaipur', 'Mahabaleshwar', 'Mussoorie', 'Raipur', 'Udaipur', 'Varanasi'], label='Select a City') |
|
date_input = gr.components.Textbox(label='Check-in Date (YYYY-MM-DD)') |
|
star_rating_dropdown = gr.components.Dropdown(choices=[1, 2, 3, 4, 5], label='Select Star Rating') |
|
text_rating_input = gr.components.Number(label='Enter Numeric Rating (1-5)') |
|
season_radio = gr.components.Radio(['On Season', 'Off Season'], label='Season') |
|
room_category_dropdown = gr.components.Dropdown(choices=["Dorm", "Standard", "Deluxe", "Executive", "Suite"], label='Select Room Category') |
|
additional_views_radio = gr.components.Radio(['Yes', 'No'], label='Additional Views') |
|
|
|
|
|
output = gr.components.Textbox(label='Predicted Output') |
|
|
|
interface = gr.Interface(fn=predict, inputs=[city_dropdown, date_input, star_rating_dropdown, text_rating_input, season_radio, additional_views_radio, room_category_dropdown], outputs=output, title='Model Prediction Interface') |
|
|
|
|
|
interface.launch() |
|
|
|
|