marcch1234 commited on
Commit
cca0dda
Β·
verified Β·
1 Parent(s): f7b526a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +96 -87
app.py CHANGED
@@ -5,10 +5,14 @@ import os
5
  import requests
6
  from sklearn.ensemble import RandomForestClassifier
7
  from sklearn.preprocessing import LabelEncoder
8
- from nltk.sentiment import SentimentIntensityAnalyzer
9
  import nltk
 
10
 
11
- nltk.download("vader_lexicon")
 
 
 
 
12
 
13
  # =========================
14
  # LOAD DATA
@@ -18,7 +22,7 @@ bookings = pd.read_csv("bookings_small.csv")
18
  X = bookings.drop(columns=["is_canceled"])
19
  y = bookings["is_canceled"]
20
 
21
- # Encode categoricals
22
  encoders = {}
23
  for col in X.select_dtypes(include="object").columns:
24
  le = LabelEncoder()
@@ -29,79 +33,96 @@ for col in X.select_dtypes(include="object").columns:
29
  model = RandomForestClassifier(n_estimators=100, max_depth=10)
30
  model.fit(X, y)
31
 
32
- # Sentiment model
33
- sia = SentimentIntensityAnalyzer()
34
-
35
- # Secret webhook
36
  WEBHOOK_URL = os.getenv("N8N_WEBHOOK_URL")
37
 
38
  # =========================
39
- # BOOKING PREDICTION
40
  # =========================
41
  def predict_booking(hotel, lead_time, adr, total_nights, total_guests):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
 
43
- input_dict = {
44
- "hotel": hotel,
45
- "lead_time": lead_time,
46
- "adr": adr,
47
- "total_nights": total_nights,
48
- "total_guests": total_guests,
49
-
50
- # Auto-fill features
51
- "market_segment": "Online",
52
- "deposit_type": "No Deposit",
53
- "is_repeated_guest": 0,
54
- "previous_cancellations": 0,
55
- "total_of_special_requests": 1,
56
-
57
- "seasonality_index": 1.0,
58
- "competitor_price_index": 1.0,
59
- "service_quality_proxy": 50,
60
- "booking_value_score": adr * total_nights * max(total_guests,1)
61
- }
62
-
63
- df_input = pd.DataFrame([input_dict])
64
-
65
- for col, le in encoders.items():
66
- if col in df_input:
67
- df_input[col] = le.transform(df_input[col].astype(str))
68
-
69
- for col in X.columns:
70
- if col not in df_input:
71
- df_input[col] = 0
72
-
73
- df_input = df_input[X.columns]
74
 
75
- prob = model.predict_proba(df_input)[0][1]
 
76
 
77
- if prob > 0.6:
78
- risk = "πŸ”΄ HIGH RISK"
79
- rec = "Reduce pricing and investigate booking risk"
80
- elif prob > 0.3:
81
- risk = "🟠 MEDIUM RISK"
82
- rec = "Monitor demand and pricing"
83
- else:
84
- risk = "🟒 LOW RISK"
85
- rec = "Safe to increase pricing"
86
 
87
- return prob, risk, rec, input_dict
 
 
 
 
 
88
 
 
 
89
 
90
- # =========================
91
- # SENTIMENT ANALYSIS
92
- # =========================
93
- def analyze_review(text):
94
- score = sia.polarity_scores(text)["compound"]
95
 
96
- if score > 0.2:
97
- label = "🟒 Positive"
98
- elif score < -0.2:
99
- label = "πŸ”΄ Negative"
100
- else:
101
- label = "🟑 Neutral"
102
 
103
- return score, label
 
 
104
 
 
 
105
 
106
  # =========================
107
  # SEND TO N8N
@@ -140,7 +161,6 @@ def send_to_n8n(source_tab, payload):
140
  except Exception as e:
141
  return f"❌ Request failed: {str(e)}"
142
 
143
-
144
  # =========================
145
  # UI
146
  # =========================
@@ -168,7 +188,7 @@ with gr.Blocks() as demo:
168
  hotel, lead_time, adr, total_nights, total_guests
169
  )
170
 
171
- text = f"""
172
  ### πŸ“Š Booking Analysis
173
 
174
  **Cancellation Probability:** {prob:.2%}
@@ -177,13 +197,14 @@ with gr.Blocks() as demo:
177
 
178
  **Recommendation:**
179
  {rec}
180
- """
181
- return text, payload
182
 
183
- gr.Button("πŸ” Analyze Booking").click(
184
- run_booking,
185
- [hotel, lead_time, adr, total_nights, total_guests],
186
- [output, state_payload]
 
 
187
  )
188
 
189
  send_output = gr.Markdown()
@@ -204,24 +225,12 @@ with gr.Blocks() as demo:
204
  sentiment_output = gr.Markdown()
205
  state_review = gr.State()
206
 
207
- def run_sentiment(text):
208
- score, label = analyze_review(text)
209
-
210
- return f"""
211
- ### πŸ’¬ Sentiment Analysis
212
-
213
- **Score:** {score:.2f}
214
-
215
- **Label:** {label}
216
-
217
- **Insight:**
218
- {"Improve service before increasing prices" if "Negative" in label else "Customer perception supports pricing"}
219
- """, {"sentiment": score, "label": label}
220
 
221
- gr.Button("πŸ” Analyze Sentiment").click(
222
- run_sentiment,
223
- review,
224
- [sentiment_output, state_review]
225
  )
226
 
227
  send_output2 = gr.Markdown()
 
5
  import requests
6
  from sklearn.ensemble import RandomForestClassifier
7
  from sklearn.preprocessing import LabelEncoder
 
8
  import nltk
9
+ from nltk.sentiment import SentimentIntensityAnalyzer
10
 
11
+ # =========================
12
+ # FIX NLTK (VERY IMPORTANT)
13
+ # =========================
14
+ nltk.download('vader_lexicon')
15
+ sia = SentimentIntensityAnalyzer()
16
 
17
  # =========================
18
  # LOAD DATA
 
22
  X = bookings.drop(columns=["is_canceled"])
23
  y = bookings["is_canceled"]
24
 
25
+ # Encode categorical variables
26
  encoders = {}
27
  for col in X.select_dtypes(include="object").columns:
28
  le = LabelEncoder()
 
33
  model = RandomForestClassifier(n_estimators=100, max_depth=10)
34
  model.fit(X, y)
35
 
36
+ # =========================
37
+ # SECRET WEBHOOK
38
+ # =========================
 
39
  WEBHOOK_URL = os.getenv("N8N_WEBHOOK_URL")
40
 
41
  # =========================
42
+ # BOOKING FUNCTION
43
  # =========================
44
  def predict_booking(hotel, lead_time, adr, total_nights, total_guests):
45
+ try:
46
+ lead_time = int(lead_time)
47
+ total_nights = int(total_nights)
48
+ total_guests = int(total_guests)
49
+ adr = float(adr)
50
+
51
+ input_dict = {
52
+ "hotel": hotel,
53
+ "lead_time": lead_time,
54
+ "adr": adr,
55
+ "total_nights": total_nights,
56
+ "total_guests": total_guests,
57
+
58
+ "market_segment": "Online",
59
+ "deposit_type": "No Deposit",
60
+ "is_repeated_guest": 0,
61
+ "previous_cancellations": 0,
62
+ "total_of_special_requests": 1,
63
+
64
+ "seasonality_index": 1.0,
65
+ "competitor_price_index": 1.0,
66
+ "service_quality_proxy": 50,
67
+ "booking_value_score": adr * total_nights * max(total_guests,1)
68
+ }
69
+
70
+ df_input = pd.DataFrame([input_dict])
71
+
72
+ for col, le in encoders.items():
73
+ if col in df_input:
74
+ df_input[col] = le.transform(df_input[col].astype(str))
75
+
76
+ for col in X.columns:
77
+ if col not in df_input:
78
+ df_input[col] = 0
79
+
80
+ df_input = df_input[X.columns]
81
+
82
+ prob = model.predict_proba(df_input)[0][1]
83
+
84
+ if prob > 0.6:
85
+ risk = "πŸ”΄ HIGH RISK"
86
+ rec = "Reduce pricing and investigate risk"
87
+ elif prob > 0.3:
88
+ risk = "🟠 MEDIUM RISK"
89
+ rec = "Monitor demand"
90
+ else:
91
+ risk = "🟒 LOW RISK"
92
+ rec = "Safe to increase pricing"
93
 
94
+ return prob, risk, rec, input_dict
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95
 
96
+ except Exception as e:
97
+ return 0, "ERROR", str(e), {}
98
 
99
+ # =========================
100
+ # SENTIMENT FUNCTION (FIXED)
101
+ # =========================
102
+ def run_sentiment(text):
103
+ try:
104
+ score = sia.polarity_scores(text)["compound"]
 
 
 
105
 
106
+ if score > 0.2:
107
+ label = "🟒 Positive"
108
+ elif score < -0.2:
109
+ label = "πŸ”΄ Negative"
110
+ else:
111
+ label = "🟑 Neutral"
112
 
113
+ return f"""
114
+ ### πŸ’¬ Sentiment Analysis
115
 
116
+ **Score:** {score:.2f}
 
 
 
 
117
 
118
+ **Label:** {label}
 
 
 
 
 
119
 
120
+ **Insight:**
121
+ {"🚨 Negative feedback β†’ improve service" if "Negative" in label else "βœ… Customer perception supports pricing"}
122
+ """, {"sentiment": score, "label": label}
123
 
124
+ except Exception as e:
125
+ return f"❌ ERROR: {str(e)}", {}
126
 
127
  # =========================
128
  # SEND TO N8N
 
161
  except Exception as e:
162
  return f"❌ Request failed: {str(e)}"
163
 
 
164
  # =========================
165
  # UI
166
  # =========================
 
188
  hotel, lead_time, adr, total_nights, total_guests
189
  )
190
 
191
+ return f"""
192
  ### πŸ“Š Booking Analysis
193
 
194
  **Cancellation Probability:** {prob:.2%}
 
197
 
198
  **Recommendation:**
199
  {rec}
200
+ """, payload
 
201
 
202
+ analyze_btn = gr.Button("πŸ” Analyze Booking")
203
+
204
+ analyze_btn.click(
205
+ fn=run_booking,
206
+ inputs=[hotel, lead_time, adr, total_nights, total_guests],
207
+ outputs=[output, state_payload],
208
  )
209
 
210
  send_output = gr.Markdown()
 
225
  sentiment_output = gr.Markdown()
226
  state_review = gr.State()
227
 
228
+ analyze_btn2 = gr.Button("πŸ” Analyze Sentiment")
 
 
 
 
 
 
 
 
 
 
 
 
229
 
230
+ analyze_btn2.click(
231
+ fn=run_sentiment,
232
+ inputs=review,
233
+ outputs=[sentiment_output, state_review]
234
  )
235
 
236
  send_output2 = gr.Markdown()