File size: 13,729 Bytes
49c79a1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
"""
๐Ÿ”” Notification Bad-Timing Detector โ€” Interactive Demo
Predicts the probability that NOW is a bad time to send a push notification.
Uses a LightGBM model trained on 100K samples with 21 contextual features.
"""

import math
import pickle
import numpy as np
import pandas as pd
import gradio as gr
from huggingface_hub import hf_hub_download

# โ”€โ”€ Load model from HF Hub โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
MODEL_REPO = "alianassmaaa/notification-bad-timing-detector"
model_path = hf_hub_download(repo_id=MODEL_REPO, filename="calibrated_model.pkl")
with open(model_path, "rb") as f:
    model = pickle.load(f)

# โ”€โ”€ Feature order (must match training) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
FEATURES = [
    "hour_of_day", "day_of_week", "hour_sin", "hour_cos",
    "is_weekend", "is_night",
    "battery_level", "is_charging", "battery_change_rate",
    "screen_on", "screen_on_duration_30min", "app_opens_last_hour",
    "session_length_current", "time_since_last_interaction",
    "notif_shown_last_30min", "notif_clicked_last_30min",
    "notif_dismissed_last_30min", "notif_ignored_last_30min",
    "notif_shown_last_24h", "notif_ctr_last_7d",
    "recent_notification_density",
]

DAY_NAMES = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]

# โ”€โ”€ Prediction function โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
def predict(
    hour_of_day, day_of_week,
    battery_level, is_charging, battery_change_rate,
    screen_on, screen_on_duration_30min, app_opens_last_hour,
    session_length_current, time_since_last_interaction,
    notif_shown_last_30min, notif_clicked_last_30min,
    notif_dismissed_last_30min, notif_ignored_last_30min,
    notif_shown_last_24h, notif_ctr_last_7d,
    recent_notification_density,
):
    # Derived features
    day_idx = DAY_NAMES.index(day_of_week)
    hour_sin = math.sin(2 * math.pi * hour_of_day / 24)
    hour_cos = math.cos(2 * math.pi * hour_of_day / 24)
    is_weekend = 1 if day_idx >= 5 else 0
    is_night = 1 if (hour_of_day >= 22 or hour_of_day < 7) else 0

    features = pd.DataFrame([[
        hour_of_day, day_idx, hour_sin, hour_cos,
        is_weekend, is_night,
        battery_level, int(is_charging), battery_change_rate,
        int(screen_on), screen_on_duration_30min, app_opens_last_hour,
        session_length_current, time_since_last_interaction,
        notif_shown_last_30min, notif_clicked_last_30min,
        notif_dismissed_last_30min, notif_ignored_last_30min,
        notif_shown_last_24h, notif_ctr_last_7d,
        recent_notification_density,
    ]], columns=FEATURES)

    prob = model.predict_proba(features)[:, 1][0]

    # Build result
    if prob < 0.3:
        verdict = "โœ… Good time to send!"
        advice = "The user is likely receptive. Send the notification now."
    elif prob < 0.5:
        verdict = "โš ๏ธ Consider priority"
        advice = "Send only if the notification is important or time-sensitive."
    elif prob < 0.8:
        verdict = "๐Ÿšซ Bad timing โ€” delay"
        advice = "The user is likely busy or disengaged. Schedule for later."
    else:
        verdict = "๐Ÿ”ด Definitely delay!"
        advice = "Very bad timing. The notification will almost certainly be ignored or dismissed."

    # Gauge-like HTML output
    bar_width = int(prob * 100)
    bar_color = f"hsl({int((1 - prob) * 120)}, 80%, 45%)"

    html = f"""
    <div style="font-family: sans-serif; max-width: 500px; margin: auto;">
        <div style="text-align: center; margin-bottom: 16px;">
            <span style="font-size: 3em; font-weight: bold; color: {bar_color};">{prob:.1%}</span>
            <br/>
            <span style="font-size: 1.3em; font-weight: 600;">{verdict}</span>
        </div>
        <div style="background: #e0e0e0; border-radius: 12px; height: 28px; overflow: hidden; margin-bottom: 12px;">
            <div style="background: {bar_color}; width: {bar_width}%; height: 100%; border-radius: 12px; transition: width 0.4s;"></div>
        </div>
        <div style="display: flex; justify-content: space-between; font-size: 0.85em; color: #666; margin-bottom: 16px;">
            <span>0% โ€” Great time</span>
            <span>100% โ€” Terrible time</span>
        </div>
        <div style="background: #f8f9fa; border-left: 4px solid {bar_color}; padding: 12px 16px; border-radius: 4px;">
            <strong>Recommendation:</strong> {advice}
        </div>
    </div>
    """
    return html


# โ”€โ”€ Preset scenarios โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
PRESETS = {
    "๐ŸŒ… Morning commute (good time)": {
        "hour_of_day": 8, "day_of_week": "Tuesday",
        "battery_level": 90, "is_charging": False, "battery_change_rate": -0.5,
        "screen_on": True, "screen_on_duration_30min": 600, "app_opens_last_hour": 5,
        "session_length_current": 120, "time_since_last_interaction": 10,
        "notif_shown_last_30min": 1, "notif_clicked_last_30min": 1,
        "notif_dismissed_last_30min": 0, "notif_ignored_last_30min": 0,
        "notif_shown_last_24h": 15, "notif_ctr_last_7d": 0.45,
        "recent_notification_density": 0.5,
    },
    "๐Ÿ˜ด Late night sleeping (bad time)": {
        "hour_of_day": 3, "day_of_week": "Wednesday",
        "battery_level": 65, "is_charging": True, "battery_change_rate": 1.5,
        "screen_on": False, "screen_on_duration_30min": 0, "app_opens_last_hour": 0,
        "session_length_current": 0, "time_since_last_interaction": 7200,
        "notif_shown_last_30min": 0, "notif_clicked_last_30min": 0,
        "notif_dismissed_last_30min": 0, "notif_ignored_last_30min": 0,
        "notif_shown_last_24h": 20, "notif_ctr_last_7d": 0.15,
        "recent_notification_density": 0.0,
    },
    "๐Ÿ“ฑ Active browsing (good time)": {
        "hour_of_day": 14, "day_of_week": "Saturday",
        "battery_level": 72, "is_charging": False, "battery_change_rate": -1.2,
        "screen_on": True, "screen_on_duration_30min": 1200, "app_opens_last_hour": 8,
        "session_length_current": 300, "time_since_last_interaction": 5,
        "notif_shown_last_30min": 2, "notif_clicked_last_30min": 2,
        "notif_dismissed_last_30min": 0, "notif_ignored_last_30min": 0,
        "notif_shown_last_24h": 18, "notif_ctr_last_7d": 0.52,
        "recent_notification_density": 1.0,
    },
    "๐Ÿ”‹ Low battery, ignoring notifs (bad time)": {
        "hour_of_day": 19, "day_of_week": "Friday",
        "battery_level": 8, "is_charging": False, "battery_change_rate": -3.0,
        "screen_on": False, "screen_on_duration_30min": 60, "app_opens_last_hour": 1,
        "session_length_current": 0, "time_since_last_interaction": 1800,
        "notif_shown_last_30min": 5, "notif_clicked_last_30min": 0,
        "notif_dismissed_last_30min": 3, "notif_ignored_last_30min": 2,
        "notif_shown_last_24h": 45, "notif_ctr_last_7d": 0.08,
        "recent_notification_density": 4.0,
    },
    "๐Ÿข Work meeting (bad time)": {
        "hour_of_day": 10, "day_of_week": "Monday",
        "battery_level": 55, "is_charging": False, "battery_change_rate": -0.8,
        "screen_on": False, "screen_on_duration_30min": 30, "app_opens_last_hour": 0,
        "session_length_current": 0, "time_since_last_interaction": 3600,
        "notif_shown_last_30min": 3, "notif_clicked_last_30min": 0,
        "notif_dismissed_last_30min": 1, "notif_ignored_last_30min": 2,
        "notif_shown_last_24h": 30, "notif_ctr_last_7d": 0.12,
        "recent_notification_density": 2.5,
    },
}

def load_preset(preset_name):
    if not preset_name or preset_name not in PRESETS:
        return [gr.update()] * 17
    p = PRESETS[preset_name]
    return [
        p["hour_of_day"], p["day_of_week"],
        p["battery_level"], p["is_charging"], p["battery_change_rate"],
        p["screen_on"], p["screen_on_duration_30min"], p["app_opens_last_hour"],
        p["session_length_current"], p["time_since_last_interaction"],
        p["notif_shown_last_30min"], p["notif_clicked_last_30min"],
        p["notif_dismissed_last_30min"], p["notif_ignored_last_30min"],
        p["notif_shown_last_24h"], p["notif_ctr_last_7d"],
        p["recent_notification_density"],
    ]


# โ”€โ”€ Gradio UI โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
with gr.Blocks(
    title="๐Ÿ”” Notification Timing Detector",
    theme=gr.themes.Soft(),
) as demo:
    gr.Markdown("""
    # ๐Ÿ”” Notification Bad-Timing Detector
    **Should you send that push notification right now?** This model predicts the probability that
    the current moment is a **bad time** to interrupt the user, based on their activity patterns,
    battery status, and notification interaction history.

    Built with LightGBM + isotonic calibration โ€ข Trained on 100K samples โ€ข
    [Model](https://huggingface.co/alianassmaaa/notification-bad-timing-detector) โ€ข
    [Dataset](https://huggingface.co/datasets/alianassmaaa/notification-timing-dataset)
    """)

    with gr.Row():
        preset = gr.Dropdown(
            choices=list(PRESETS.keys()),
            label="โšก Quick presets โ€” try a scenario",
            value=None,
            interactive=True,
        )

    with gr.Row():
        # Left column: inputs
        with gr.Column(scale=3):
            gr.Markdown("### โฐ Time Context")
            with gr.Row():
                hour_of_day = gr.Slider(0, 23, value=14, step=1, label="Hour of day (0โ€“23)")
                day_of_week = gr.Dropdown(DAY_NAMES, value="Wednesday", label="Day of week")

            gr.Markdown("### ๐Ÿ”‹ Battery Status")
            with gr.Row():
                battery_level = gr.Slider(0, 100, value=75, step=1, label="Battery level (%)")
                is_charging = gr.Checkbox(value=False, label="Charging?")
                battery_change_rate = gr.Slider(-5, 5, value=-1.0, step=0.1, label="Battery drain rate (%/hr)")

            gr.Markdown("### ๐Ÿ“ฑ User Activity")
            with gr.Row():
                screen_on = gr.Checkbox(value=True, label="Screen on?")
                screen_on_duration_30min = gr.Slider(0, 1800, value=600, step=10, label="Screen-on time last 30 min (sec)")
            with gr.Row():
                app_opens_last_hour = gr.Slider(0, 30, value=5, step=1, label="App opens last hour")
                session_length_current = gr.Slider(0, 3600, value=120, step=10, label="Current session length (sec)")
            time_since_last_interaction = gr.Slider(0, 14400, value=30, step=10, label="Time since last interaction (sec)")

            gr.Markdown("### ๐Ÿ”” Notification History")
            with gr.Row():
                notif_shown_last_30min = gr.Slider(0, 20, value=2, step=1, label="Shown (30 min)")
                notif_clicked_last_30min = gr.Slider(0, 20, value=1, step=1, label="Clicked (30 min)")
            with gr.Row():
                notif_dismissed_last_30min = gr.Slider(0, 20, value=0, step=1, label="Dismissed (30 min)")
                notif_ignored_last_30min = gr.Slider(0, 20, value=1, step=1, label="Ignored (30 min)")
            with gr.Row():
                notif_shown_last_24h = gr.Slider(0, 100, value=20, step=1, label="Shown (24h)")
                notif_ctr_last_7d = gr.Slider(0, 1, value=0.35, step=0.01, label="7-day CTR")
            recent_notification_density = gr.Slider(0, 10, value=1.0, step=0.1, label="Recent notification density")

        # Right column: output
        with gr.Column(scale=2):
            predict_btn = gr.Button("๐Ÿ”ฎ Predict Bad-Timing Probability", variant="primary", size="lg")
            output = gr.HTML(label="Prediction")

            gr.Markdown("""
            ### ๐Ÿ“Š Decision Thresholds
            | Probability | Action |
            |:-----------:|--------|
            | **< 30%** | โœ… Send notification |
            | **30โ€“50%** | โš ๏ธ Only if important |
            | **50โ€“80%** | ๐Ÿšซ Delay notification |
            | **> 80%** | ๐Ÿ”ด Definitely delay |

            ---
            ### ๐Ÿง  How it works
            The model uses **21 signals** across 4 categories:
            - **Time**: hour, day, weekend/night flags
            - **Battery**: level, charging, drain rate
            - **Activity**: screen, app opens, session length
            - **Notifications**: recent shown/clicked/dismissed/ignored, 7-day CTR

            Built on research from the [C-3PO paper](https://arxiv.org/abs/1803.00458)
            (Cheetah Mobile, 600M monthly active users).
            """)

    # All input components in order
    all_inputs = [
        hour_of_day, day_of_week,
        battery_level, is_charging, battery_change_rate,
        screen_on, screen_on_duration_30min, app_opens_last_hour,
        session_length_current, time_since_last_interaction,
        notif_shown_last_30min, notif_clicked_last_30min,
        notif_dismissed_last_30min, notif_ignored_last_30min,
        notif_shown_last_24h, notif_ctr_last_7d,
        recent_notification_density,
    ]

    # Wire up events
    predict_btn.click(fn=predict, inputs=all_inputs, outputs=output)
    preset.change(fn=load_preset, inputs=preset, outputs=all_inputs)


if __name__ == "__main__":
    demo.launch()