File size: 6,988 Bytes
78ee177
 
6930408
 
78ee177
 
 
 
cca0dda
30ea6fe
cca0dda
6515e91
 
 
 
 
30ea6fe
6515e91
 
cca0dda
78ee177
6930408
 
 
 
78ee177
6930408
 
78ee177
6515e91
30ea6fe
6515e91
6930408
 
 
 
 
78ee177
6515e91
 
 
30ea6fe
6930408
78ee177
6930408
78ee177
6930408
30ea6fe
6515e91
 
 
 
 
 
 
 
 
 
 
6930408
f7b526a
cca0dda
 
 
 
 
 
 
 
 
 
 
 
 
30ea6fe
 
 
 
cca0dda
 
 
 
 
 
 
30ea6fe
cca0dda
 
 
6515e91
cca0dda
 
 
 
 
 
 
 
 
30ea6fe
 
cca0dda
30ea6fe
 
cca0dda
30ea6fe
cca0dda
 
30ea6fe
6930408
cca0dda
78ee177
cca0dda
 
78ee177
cca0dda
30ea6fe
cca0dda
 
 
30ea6fe
 
cca0dda
6930408
30ea6fe
 
 
 
cca0dda
 
 
 
 
 
6930408
cca0dda
 
f7b526a
cca0dda
f7b526a
cca0dda
f7b526a
cca0dda
30ea6fe
 
78ee177
cca0dda
 
f7b526a
6930408
30ea6fe
6930408
 
 
 
78ee177
 
f7b526a
 
30ea6fe
f7b526a
 
78ee177
6930408
 
 
f7b526a
78ee177
f7b526a
78ee177
f7b526a
78ee177
f7b526a
78ee177
f7b526a
 
78ee177
6930408
 
78ee177
6930408
f7b526a
 
6930408
 
 
 
f7b526a
 
 
 
 
 
 
6515e91
 
 
f7b526a
 
 
6930408
 
f7b526a
 
 
 
6930408
cca0dda
f7b526a
6930408
f7b526a
6930408
f7b526a
6930408
f7b526a
 
30ea6fe
 
 
 
f7b526a
6515e91
cca0dda
 
6515e91
f7b526a
 
 
 
 
 
 
 
 
 
 
 
 
6515e91
f7b526a
 
6930408
 
6515e91
cca0dda
 
 
f7b526a
6930408
f7b526a
6930408
f7b526a
 
 
 
 
6930408
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import gradio as gr
import pandas as pd
import numpy as np
import os
import requests
from sklearn.ensemble import RandomForestClassifier
from sklearn.preprocessing import LabelEncoder

# =========================
# NLTK FIX (HF SAFE)
# =========================
import nltk
from nltk.sentiment import SentimentIntensityAnalyzer

try:
    nltk.data.find('sentiment/vader_lexicon.zip')
except:
    nltk.download('vader_lexicon')

sia = SentimentIntensityAnalyzer()

# =========================
# LOAD DATA
# =========================
bookings = pd.read_csv("bookings_small.csv")

X = bookings.drop(columns=["is_canceled"])
y = bookings["is_canceled"]

# =========================
# ENCODERS
# =========================
encoders = {}
for col in X.select_dtypes(include="object").columns:
    le = LabelEncoder()
    X[col] = le.fit_transform(X[col].astype(str))
    encoders[col] = le

# =========================
# MODEL
# =========================
model = RandomForestClassifier(n_estimators=150, max_depth=12)
model.fit(X, y)

WEBHOOK_URL = os.getenv("N8N_WEBHOOK_URL")

# =========================
# SAFE ENCODE
# =========================
def safe_encode(df):
    for col, le in encoders.items():
        if col in df:
            df[col] = df[col].apply(
                lambda x: le.transform([x])[0] if x in le.classes_ else 0
            )
    return df

# =========================
# BOOKING PREDICTION
# =========================
def predict_booking(hotel, lead_time, adr, total_nights, total_guests):
    try:
        lead_time = int(lead_time)
        total_nights = int(total_nights)
        total_guests = int(total_guests)
        adr = float(adr)

        input_dict = {
            "hotel": hotel,
            "lead_time": lead_time,
            "adr": adr,
            "total_nights": total_nights,
            "total_guests": total_guests,

            # SAFE VALUES FROM DATA
            "market_segment": bookings["market_segment"].mode()[0],
            "deposit_type": bookings["deposit_type"].mode()[0],

            "is_repeated_guest": 0,
            "previous_cancellations": 0,
            "total_of_special_requests": 1,

            "seasonality_index": 1.0,
            "competitor_price_index": 1.0,
            "service_quality_proxy": 50,
            "booking_value_score": adr * total_nights * max(total_guests, 1)
        }

        df_input = pd.DataFrame([input_dict])
        df_input = safe_encode(df_input)

        for col in X.columns:
            if col not in df_input:
                df_input[col] = 0

        df_input = df_input[X.columns]

        prob = model.predict_proba(df_input)[0][1]

        # πŸ”₯ FIXED THRESHOLDS (WIDER RANGE)
        if prob > 0.5:
            risk = "πŸ”΄ HIGH RISK"
            rec = "High cancellation probability β†’ reduce price or verify booking"
        elif prob > 0.25:
            risk = "🟠 MEDIUM RISK"
            rec = "Moderate uncertainty β†’ monitor demand"
        else:
            risk = "🟒 LOW RISK"
            rec = "Stable demand β†’ pricing power available"

        return prob, risk, rec, input_dict

    except Exception as e:
        return 0, "ERROR", str(e), {}

# =========================
# SENTIMENT (BOOSTED)
# =========================
def run_sentiment(text):
    try:
        text = text.lower()

        score = sia.polarity_scores(text)["compound"]

        # πŸ”₯ BOOST FOR SHORT NEGATIVE TEXT
        if any(word in text for word in ["trash", "bad", "terrible", "awful"]):
            score -= 0.4

        if score > 0.2:
            label = "🟒 Positive"
        elif score < -0.2:
            label = "πŸ”΄ Negative"
        else:
            label = "🟑 Neutral"

        return f"""
### πŸ’¬ Sentiment Analysis

**Score:** {score:.2f}

**Label:** {label}

**Insight:**  
{"🚨 Negative feedback β†’ improve service before pricing increases" if "Negative" in label else "βœ… Customer perception supports pricing strategy"}
""", {"sentiment_label": label, "sentiment_score": score}

    except Exception as e:
        return f"❌ ERROR: {str(e)}", {}

# =========================
# SEND TO N8N (FIXED PAYLOAD)
# =========================
def send_to_n8n(source_tab, payload):
    if not WEBHOOK_URL:
        return "❌ Missing webhook secret"

    try:
        response = requests.post(
            WEBHOOK_URL,
            json=payload,   # πŸ”₯ IMPORTANT: send FLAT payload
            timeout=10
        )

        if response.status_code == 200:
            data = response.json()
            return f"""
### πŸ”— n8n Response

**Message:** {data.get("message")}

**Decision:** {data.get("decision")}

**Severity:** {data.get("severity")}

**Recommendation:**  
{data.get("recommendation")}
"""
        else:
            return f"❌ Error {response.status_code}: {response.text}"

    except Exception as e:
        return f"❌ Request failed: {str(e)}"

# =========================
# UI
# =========================
with gr.Blocks() as demo:
    gr.Markdown("# 🏨 LuxeRate AI")
    gr.Markdown("Smart hotel pricing & risk decision system")

    # BOOKING TAB
    with gr.Tab("πŸ“Š Booking Risk"):

        hotel = gr.Dropdown(["City Hotel", "Resort Hotel"], label="Hotel Type")
        lead_time = gr.Slider(0, 200, value=30, label="Lead Time")
        adr = gr.Slider(50, 300, value=120, label="Price (€)")
        total_nights = gr.Slider(1, 10, value=2, label="Nights")
        total_guests = gr.Slider(1, 5, value=2, label="Guests")

        output = gr.Markdown()
        state_payload = gr.State()

        def run_booking(hotel, lead_time, adr, total_nights, total_guests):
            prob, risk, rec, payload = predict_booking(
                hotel, lead_time, adr, total_nights, total_guests
            )

            return f"""
### πŸ“Š Booking Analysis

**Cancellation Probability:** {prob:.2%}

**Risk Level:** {risk}

**Recommendation:**  
{rec}
""", {
                "risk_label": risk,
                "cancellation_probability": prob
            }

        gr.Button("πŸ” Analyze Booking").click(
            fn=run_booking,
            inputs=[hotel, lead_time, adr, total_nights, total_guests],
            outputs=[output, state_payload]
        )

        send_output = gr.Markdown()

        gr.Button("πŸš€ Send to n8n").click(
            lambda p: send_to_n8n("booking", p),
            state_payload,
            send_output
        )

    # SENTIMENT TAB
    with gr.Tab("πŸ’¬ Review Sentiment"):

        review = gr.Textbox(label="Paste Review", lines=4)

        sentiment_output = gr.Markdown()
        state_review = gr.State()

        gr.Button("πŸ” Analyze Sentiment").click(
            fn=run_sentiment,
            inputs=review,
            outputs=[sentiment_output, state_review]
        )

        send_output2 = gr.Markdown()

        gr.Button("πŸš€ Send to n8n").click(
            lambda p: send_to_n8n("review", p),
            state_review,
            send_output2
        )

demo.launch()