Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files
App.py
ADDED
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import joblib
|
2 |
+
import gradio as gr
|
3 |
+
from datetime import datetime, timedelta, timezone
|
4 |
+
|
5 |
+
model = joblib.load('model.pkl')
|
6 |
+
|
7 |
+
def preprocess_city(selected_city):
|
8 |
+
# Map the selected city to its one-hot encoded representation
|
9 |
+
city_mapping = {
|
10 |
+
'Indore': [1, 0, 0, 0, 0, 0, 0],
|
11 |
+
'Jaipur': [0, 1, 0, 0, 0, 0, 0],
|
12 |
+
'Mahabaleshwar': [0, 0, 1, 0, 0, 0, 0],
|
13 |
+
'Mussoorie': [0, 0, 0, 1, 0, 0, 0],
|
14 |
+
'Raipur': [0, 0, 0, 0, 1, 0, 0],
|
15 |
+
'Udaipur': [0, 0, 0, 0, 0, 1, 0],
|
16 |
+
'Varanasi': [0, 0, 0, 0, 0, 0, 1]
|
17 |
+
}
|
18 |
+
return city_mapping[selected_city]
|
19 |
+
|
20 |
+
def preprocess_date(date_string):
|
21 |
+
# Parse the date string into a datetime object
|
22 |
+
date_obj = datetime.strptime(date_string, '%Y-%m-%d')
|
23 |
+
year = date_obj.year
|
24 |
+
month = date_obj.month
|
25 |
+
day = date_obj.day
|
26 |
+
return year, month, day
|
27 |
+
|
28 |
+
def calculate_lead_time(checkin_date):
|
29 |
+
# Convert input date to datetime object
|
30 |
+
input_date = datetime.strptime(checkin_date, '%Y-%m-%d')
|
31 |
+
|
32 |
+
# Get current date and time in GMT+5:30 timezone
|
33 |
+
current_date = datetime.now(timezone(timedelta(hours=5, minutes=30)))
|
34 |
+
|
35 |
+
# Make current_date an aware datetime with the same timezone
|
36 |
+
current_date = current_date.replace(tzinfo=input_date.tzinfo)
|
37 |
+
|
38 |
+
# Calculate lead time as difference in days
|
39 |
+
lead_time = (input_date - current_date).days
|
40 |
+
|
41 |
+
return lead_time
|
42 |
+
|
43 |
+
def is_weekend(checkin_date):
|
44 |
+
# Convert input date to datetime object
|
45 |
+
input_date = datetime.strptime(checkin_date, '%Y-%m-%d')
|
46 |
+
|
47 |
+
# Calculate the day of the week (0=Monday, 6=Sunday)
|
48 |
+
day_of_week = input_date.weekday()
|
49 |
+
|
50 |
+
# Check if the day is Friday (4) or Saturday (5)
|
51 |
+
return 1 if day_of_week == 4 or day_of_week == 5 else 0
|
52 |
+
|
53 |
+
def predict(selected_city, checkin_date, star_rating, text_rating, season, additional_views, room_category):
|
54 |
+
# Preprocess user input
|
55 |
+
# Here, selected_city is the name of the city selected from the dropdown
|
56 |
+
# checkin_date is the date selected using the text input
|
57 |
+
# star_rating is the selected star rating from the dropdown
|
58 |
+
# text_rating is the numeric rating from the text box
|
59 |
+
# season is the selected option from the radio button (On Season or Off Season)
|
60 |
+
season_binary = 1 if season == 'On Season' else 0
|
61 |
+
# additional_views is the selected option from the radio button (Yes or No)
|
62 |
+
additional_views_binary = 1 if additional_views == 'Yes' else 0
|
63 |
+
|
64 |
+
room_categories = ["Dorm", "Standard", "Deluxe", "Executive", "Suite"]
|
65 |
+
room_category_number = room_categories.index(room_category)
|
66 |
+
|
67 |
+
# Preprocess the date
|
68 |
+
year, month, day = preprocess_date(checkin_date)
|
69 |
+
|
70 |
+
# Preprocess the selected city
|
71 |
+
city_encoded = preprocess_city(selected_city)
|
72 |
+
|
73 |
+
# Calculate lead time
|
74 |
+
lead_time = calculate_lead_time(checkin_date)
|
75 |
+
|
76 |
+
# Calculate if the input date is a weekend (1) or weekday (0)
|
77 |
+
is_weekend_value = is_weekend(checkin_date)
|
78 |
+
|
79 |
+
# Combine all the input features
|
80 |
+
input_data = [star_rating, text_rating, season_binary, day, month, year, is_weekend_value, lead_time,room_category_number, additional_views_binary]+city_encoded
|
81 |
+
|
82 |
+
# Make predictions using the model
|
83 |
+
prediction = model.predict([input_data])
|
84 |
+
return "{:.2f}".format(prediction[0])
|
85 |
+
|
86 |
+
# Define input components
|
87 |
+
city_dropdown = gr.components.Dropdown(choices=['Indore', 'Jaipur', 'Mahabaleshwar', 'Mussoorie', 'Raipur', 'Udaipur', 'Varanasi'], label='Select a City')
|
88 |
+
date_input = gr.components.Textbox(label='Check-in Date (YYYY-MM-DD)')
|
89 |
+
star_rating_dropdown = gr.components.Dropdown(choices=[1, 2, 3, 4, 5], label='Select Star Rating')
|
90 |
+
text_rating_input = gr.components.Number(label='Enter Numeric Rating (1-5)')
|
91 |
+
season_radio = gr.components.Radio(['On Season', 'Off Season'], label='Season')
|
92 |
+
room_category_dropdown = gr.components.Dropdown(choices=["Dorm", "Standard", "Deluxe", "Executive", "Suite"], label='Select Room Category')
|
93 |
+
additional_views_radio = gr.components.Radio(['Yes', 'No'], label='Additional Views')
|
94 |
+
|
95 |
+
# Define output component
|
96 |
+
output = gr.components.Textbox(label='Predicted Output')
|
97 |
+
|
98 |
+
# Create the interface
|
99 |
+
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')
|
100 |
+
|
101 |
+
# Launch the interface
|
102 |
+
interface.launch()
|
103 |
+
|
model.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:01e42fa7ca9d6fd55b9eb98d8da9c9d2d24a958fa112cce01ce77e7ee2438290
|
3 |
+
size 381227006
|