File size: 10,631 Bytes
c8c1166
 
 
 
 
 
 
 
 
 
 
 
97aa0c1
 
 
 
 
 
d516f3e
 
 
 
1c7ab87
 
 
d516f3e
 
c8c1166
d516f3e
c8c1166
 
b1539c9
1c7ab87
c8c1166
 
0751fa8
97aa0c1
09edc34
 
b1539c9
 
97aa0c1
 
621c0b1
 
 
 
 
97aa0c1
621c0b1
 
c8c1166
d516f3e
1c7ab87
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97aa0c1
 
c8c1166
97aa0c1
 
 
c8c1166
d516f3e
 
c8c1166
 
d516f3e
2c99a96
 
 
 
 
 
 
 
c8c1166
2c99a96
d516f3e
c8c1166
 
2c99a96
58b15f4
 
 
 
2c99a96
58b15f4
 
 
 
 
2c99a96
 
 
58b15f4
d516f3e
 
58b15f4
d516f3e
 
c8c1166
 
d516f3e
c8c1166
d516f3e
b1539c9
 
1c7ab87
 
 
 
b1539c9
1c7ab87
 
 
 
 
b1539c9
1c7ab87
 
 
 
 
 
621c0b1
1c7ab87
 
 
 
 
621c0b1
b1539c9
c8c1166
 
 
d516f3e
c8c1166
 
d516f3e
 
c8c1166
 
d516f3e
 
 
c8c1166
d516f3e
 
97aa0c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1c7ab87
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d516f3e
 
b1539c9
 
621c0b1
b1539c9
 
 
621c0b1
b1539c9
621c0b1
 
 
 
 
 
 
 
 
 
b1539c9
 
621c0b1
 
 
 
 
 
b1539c9
 
621c0b1
 
 
 
 
 
b1539c9
 
 
621c0b1
 
 
b1539c9
 
 
 
 
 
 
 
d516f3e
c8c1166
 
 
 
 
 
 
 
 
 
 
 
d516f3e
 
 
c8c1166
d516f3e
c8c1166
d516f3e
b1539c9
1c7ab87
621c0b1
 
 
 
 
 
 
 
 
 
 
 
 
 
b1539c9
 
 
 
621c0b1
b1539c9
 
 
 
 
 
 
 
1c7ab87
b1539c9
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
"""NeuroBait UI — hybrid: a standard AI-chat experience (``gr.ChatInterface``,
DeepSeek/Perplexity-ish) carrying NeuroBait's identity.

gradio-only (no torch/spaces) so the UI can be built and smoke-tested without the
model stack. Theme + CSS are applied at ``launch()`` because Gradio 6 moved
``theme``/``css``/``js`` off the Blocks/ChatInterface constructor — see the
deprecation warning we hit in the Space logs.

Dark-only, earthy, calm sage-teal accent (gentler / more sensory-safe for ADHD
than vivid green). The palette is set straight on ``:root`` and mapped onto
Gradio's core CSS vars unconditionally, so the app is dark with no dependence on
JS or HF's ``?__theme``.

UX notes for ADHD users:
- Fewer decisions: mood is 4 choices, examples are single-click starting points.
- No shame framing: copy is warm, safety scope is visible, no streaks/deadlines.
- Consistent language: the Gradio footer is forced to English so the whole app
  feels like one voice, regardless of browser locale.
"""

from __future__ import annotations

import base64
from pathlib import Path

import gradio as gr

MOODS = ["Calm", "Tired", "Anxious", "Focused"]

THEME = gr.themes.Soft(primary_hue="teal", neutral_hue="slate")

TITLE = ""
MARK_PATH = Path(__file__).with_name("neurobait-mark.png")

DESCRIPTION = (
    "ADHD-friendly companion. "
    "A warm space and a gentle boost for your everyday. "
    "No red pen · no pressure · no bullet points paralysis. "
    "Not diagnostic"
)

# Force the Gradio shell footer to English so it does not clash with the app's
# English copy when the viewer's browser locale is Indonesian.
I18N_FOOTER = {
    "use_via_api": "Use via API",
    "built_with_gradio": "Built with Gradio",
    "settings": "Settings",
}
I18N = gr.I18n(
    en=I18N_FOOTER,
    id=I18N_FOOTER,
)

MARK_SRC = f"data:image/png;base64,{base64.b64encode(MARK_PATH.read_bytes()).decode('ascii')}"
HEADER_HTML = f"""
<header class="nb-header">
  <img src="{MARK_SRC}" alt="" class="nb-mark" />
  <div>
    <div class="nb-wordmark">NeuroBait</div>
    <div class="nb-kicker">task initiation companion</div>
  </div>
</header>
"""

FOOTER_HTML = """
<div class="nb-footer-links">
  <a href="/?view=api">Use via API</a>
  <span>·</span>
  <a href="https://gradio.app" target="_blank" rel="noreferrer">Built with Gradio</a>
  <span>·</span>
  <span>Settings</span>
</div>
"""

PLACEHOLDER = "What's on your mind? No need to be tidy."

EXAMPLES = [
    ["There's something I've been meaning to get to, and it keeps slipping by."],
    ["My space feels a bit cluttered today."],
    ["My mind feels a little foggy right now."],
]

CSS = """
/* Earthy, always-dark palette on :root (no .dark dependency), with a calm muted
   sage-teal accent instead of vivid green. */
:root {
  --forest: #6fb6a2;
  --sage: #5da894;
  --mint: #20362f;
  --cream: #1d2128;
  --linen: #15181d;
  --sand: #2e333b;
  --stone: #aeb6bf;
  --charcoal: #eef1f4;
  --radius: 10px;
  --brand-grad: linear-gradient(135deg, #5da894, #4f9582);
}
/* map Gradio core theme vars onto our dark palette so built-in components render
   dark even when Gradio's own .dark class is off */
body, .gradio-container, gradio-app, .dark {
  --body-background-fill: var(--linen);
  --background-fill-primary: var(--cream);
  --background-fill-secondary: var(--linen);
  --block-background-fill: var(--cream);
  --block-label-background-fill: var(--cream);
  --block-border-color: var(--sand);
  --border-color-primary: var(--sand);
  --body-text-color: var(--charcoal);
  --body-text-color-subdued: var(--stone);
  --input-background-fill: var(--cream);
  --input-border-color: var(--sand);
  --button-secondary-background-fill: var(--cream);
  --button-secondary-text-color: var(--charcoal);
}
body, .gradio-container {
  background: var(--linen) !important;
  color: var(--charcoal) !important;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji" !important;
}
/* keep the conversation in a comfortable centered column like a modern chat app */
.gradio-container { max-width: 960px !important; margin: 0 auto !important; }

h1 { letter-spacing: -0.5px; font-weight: 800; }

.nb-header {
  display: flex;
  align-items: center;
  justify-content: flex-start;
  gap: 12px;
  padding: 1rem 0 0.4rem;
}
.nb-mark {
  width: 42px;
  height: 42px;
  object-fit: contain;
  flex: 0 0 auto;
}
.nb-wordmark {
  font-size: 1.24rem;
  line-height: 1.05;
  font-weight: 780;
  letter-spacing: 0;
  color: var(--charcoal);
}
.nb-kicker {
  margin-top: 3px;
  color: var(--stone);
  font-size: 0.78rem;
  line-height: 1.2;
}

/* user bubble gets the brand accent, assistant stays flat/quiet */
.message.user, [data-testid="user"] {
  background: var(--brand-grad) !important; color: #fff !important; border: none !important;
}
.message.bot, [data-testid="bot"] {
  background: var(--cream) !important; border: 1px solid var(--sand) !important; color: var(--charcoal) !important;
}

/* rounded, calm input + primary button */
textarea, input[type="text"] {
  border-radius: var(--radius) !important; background: var(--cream) !important;
  border: 1px solid var(--sand) !important; color: var(--charcoal) !important;
}
button.primary, .submit-button, [variant="primary"] {
  background: var(--brand-grad) !important; border: none !important; color: #fff !important;
}

/* ADHD-friendly examples: render as calm chips instead of a data table */
.examples table,
[data-testid="examples"] table,
.examples-wrap table {
  display: block !important;
  width: 100% !important;
}
.examples table thead,
[data-testid="examples"] table thead,
.examples-wrap table thead {
  display: none !important;
}
.examples table tbody,
[data-testid="examples"] table tbody,
.examples-wrap table tbody {
  display: flex !important;
  flex-wrap: wrap !important;
  gap: 8px !important;
}
.examples table tr,
[data-testid="examples"] table tr,
.examples-wrap table tr {
  display: inline-flex !important;
}
.examples table td,
[data-testid="examples"] table td,
.examples-wrap table td {
  background: var(--cream) !important;
  border: 1px solid var(--sand) !important;
  border-radius: 999px !important;
  padding: 8px 14px !important;
  color: var(--charcoal) !important;
  cursor: pointer !important;
  font-size: 0.9rem !important;
  line-height: 1.4 !important;
  transition: background 0.15s ease, border-color 0.15s ease !important;
}
.examples table td:hover,
[data-testid="examples"] table td:hover,
.examples-wrap table td:hover {
  background: var(--mint) !important;
  border-color: var(--forest) !important;
}

/* Make the empty-state placeholder feel centered and readable */
.chatbot .placeholder,
[data-testid="chatbot"] .placeholder {
  text-align: center !important;
  display: flex !important;
  align-items: center !important;
  justify-content: center !important;
  height: 100% !important;
  opacity: 0.9 !important;
}

/* Safety scope footer at the bottom of the app */
.gradio-container::after {
  content: "Not a medical device · not a therapist · just a gentle nudge";
  display: block;
  text-align: center;
  color: var(--stone);
  font-size: 0.75rem;
  padding: 1.5rem 0 1rem;
  margin-top: 1rem;
  border-top: 1px solid var(--sand);
}
.nb-footer-links {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 9px;
  color: var(--stone);
  font-size: 0.8rem;
  margin: 1.75rem 0 0.25rem;
}
.nb-footer-links a,
.nb-footer-links span {
  color: var(--stone) !important;
  text-decoration: none !important;
}
.nb-footer-links a:hover {
  color: var(--charcoal) !important;
}
"""

JS = """
() => {
  const replacements = [
    ["Gunakan melalui API", "Use via API"],
    ["Dibuat dengan Gradio", "Built with Gradio"],
    ["Pengaturan", "Settings"],
  ];
  const normalizeFooter = () => {
    const walker = document.createTreeWalker(document.body, NodeFilter.SHOW_TEXT);
    const textNodes = [];
    while (walker.nextNode()) textNodes.push(walker.currentNode);
    textNodes.forEach((node) => {
      let text = node.nodeValue;
      replacements.forEach(([from, to]) => {
        text = text.replaceAll(from, to);
      });
      node.nodeValue = text;
    });
    document.querySelectorAll("a, button, span").forEach((node) => {
      const label = node.getAttribute?.("aria-label");
      if (label) {
        let next = label;
        replacements.forEach(([from, to]) => {
          next = next.replaceAll(from, to);
        });
        node.setAttribute("aria-label", next);
      }
      const title = node.getAttribute?.("title");
      if (title) {
        let next = title;
        replacements.forEach(([from, to]) => {
          next = next.replaceAll(from, to);
        });
        node.setAttribute("title", next);
      }
    });
  };
  for (let i = 0; i < 20; i += 1) {
    setTimeout(normalizeFooter, i * 250);
  }
  new MutationObserver(normalizeFooter).observe(document.body, {
    childList: true,
    subtree: true,
    characterData: true,
  });
}
"""


def message_text(content) -> str:
    if isinstance(content, str):
        return content.strip()
    if isinstance(content, list):
        parts = []
        for item in content:
            if isinstance(item, dict):
                text = item.get("text")
                if isinstance(text, str):
                    parts.append(text)
        return " ".join(part.strip() for part in parts if part.strip()).strip()
    return ""


def build_demo(respond_fn):
    """Build the NeuroBait chat. ``respond_fn`` signature (streaming generator ok):

    respond_fn(message: str, history: list[dict], mood: str) -> str | generator[str]
    """
    with gr.Blocks(fill_height=True) as demo:
        gr.HTML(HEADER_HTML)
        mood = gr.Radio(
            MOODS,
            value="Calm",
            label="🌳 How are you feeling?",
            info="Pick the one that feels closest right now.",
            render=False,
        )
        chatbot = gr.Chatbot(
            height=560,
            show_label=False,
            placeholder=PLACEHOLDER,
            avatar_images=(None, None),
            render=False,
        )
        gr.ChatInterface(
            fn=respond_fn,
            chatbot=chatbot,
            additional_inputs=[mood],
            additional_inputs_accordion="🌳 How are you feeling?",
            title=TITLE,
            description=DESCRIPTION,
            examples=EXAMPLES,
            cache_examples=False,
            save_history=True,
            fill_height=True,
            autoscroll=True,
        )
        gr.HTML(FOOTER_HTML)
    return demo