File size: 15,706 Bytes
ea3fcbe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aa16b4b
ea3fcbe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aa16b4b
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
<!doctype html>
<html lang="id">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>NER Test — Prompt Builder</title>
  <link rel="preconnect" href="https://fonts.googleapis.com">
  <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
  <link href="https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@400;500;600;700;800&display=swap"
    rel="stylesheet">
  <script src="https://cdn.tailwindcss.com"></script>
  <script>
    tailwind.config = {
      theme: {
        fontFamily: { sans: ['"Plus Jakarta Sans"', 'Arial', 'sans-serif'] },
        extend: {
          colors: {
            line: '#dbe3ea',
            input: '#cbd5df',
            teal: { DEFAULT: '#167c80', dark: '#0f5f63', light: '#dff4f5', lighter: '#e7f7f8' },
          },
          screens: { mobile: { max: '860px' } },
        },
      },
    }
  </script>

  <style>
    /* Warna highlight per tipe entitas (dipakai via inline style oleh JS) */
    :root {
      --c-orang: #dcfce7;
      --ct-orang: #14532d;
      --cb-orang: #86efac;
      --c-organisasi: #dbeafe;
      --ct-organisasi: #1e3a8a;
      --cb-organisasi: #93c5fd;
      --c-lokasi: #ede9fe;
      --ct-lokasi: #4c1d95;
      --cb-lokasi: #c4b5fd;
      --c-fasilitas: #ffedd5;
      --ct-fasilitas: #7c2d12;
      --cb-fasilitas: #fdba74;
      --c-kejadian: #fef9c3;
      --ct-kejadian: #713f12;
      --cb-kejadian: #fde047;
      --c-produk: #ccfbf1;
      --ct-produk: #134e4a;
      --cb-produk: #5eead4;
      --c-karya: #fce7f3;
      --ct-karya: #831843;
      --cb-karya: #f9a8d4;
      --c-peraturan: #fee2e2;
      --ct-peraturan: #7f1d1d;
      --cb-peraturan: #fca5a5;
      --c-tanggal: #f1f5f9;
      --ct-tanggal: #1e293b;
      --cb-tanggal: #cbd5e1;
      --c-waktu: #e0e7ff;
      --ct-waktu: #1e1b4b;
      --cb-waktu: #a5b4fc;
      --c-uang: #fef3c7;
      --ct-uang: #78350f;
      --cb-uang: #fcd34d;
      --c-agama: #d1fae5;
      --ct-agama: #064e3b;
      --cb-agama: #6ee7b7;
      --c-bahasa: #cffafe;
      --ct-bahasa: #164e63;
      --cb-bahasa: #67e8f9;
    }
  </style>
</head>

<body class="font-sans bg-[#f6f8fb] text-[#1f2933] min-h-screen
             py-[clamp(16px,3vh,24px)] px-[clamp(16px,2.5vw,24px)] box-border">

  <main class="w-full max-w-[1280px] mx-auto flex flex-col gap-4">

    <!-- Header -->
    <div class="flex items-baseline justify-between flex-wrap gap-3">
      <div>
        <h1 class="m-0 text-[28px] font-extrabold leading-tight">NER Test</h1>
        <p class="mt-1 text-sm text-[#52606d]">
          Uji model <span id="modelName" class="font-semibold text-teal">memuat…</span>
        </p>
      </div>
      <!-- Status badge -->
      <span id="statusBadge" class="inline-flex items-center gap-2 px-3 py-1 rounded-full text-sm font-semibold
               bg-slate-100 text-slate-500 border border-slate-200">
        <span id="statusDot" class="w-2 h-2 rounded-full bg-slate-400"></span>
        <span id="statusText">Menghubungi server…</span>
      </span>
    </div>

    <!-- Grid dua kolom -->
    <div class="grid grid-cols-2 gap-6 mobile:grid-cols-1">

      <!-- ── Panel Kiri: Input ── -->
      <section class="bg-white border border-line rounded-lg p-5 flex flex-col gap-4">
        <label class="text-sm font-bold m-0" for="nerInput">Teks Input</label>
        <textarea id="nerInput" class="w-full box-border px-[10px] py-[10px] border border-input rounded-[6px]
                 font-[inherit] text-base resize-none leading-[1.55]
                 focus:outline-none focus:border-teal focus:ring-[3px] focus:ring-teal/[.14]" rows="10"
          placeholder="Tempel teks untuk mengenali nama orang, organisasi, lokasi, tanggal, atau dokumen.&#10;&#10;Contoh:&#10;Ahmad Santoso bekerja di PT Teknologi Maju Indonesia di Jakarta sejak 15 Maret 2020. Kementerian Keuangan menerbitkan PP No. 23/2024."></textarea>

        <div class="flex gap-2 flex-wrap">
          <button id="detectBtn" class="px-4 py-[10px] bg-teal text-white rounded-[6px] font-bold border-0
                   cursor-pointer hover:bg-teal-dark transition-colors duration-150 disabled:opacity-50" type="button">
            Deteksi Entitas
          </button>
          <button id="sampleBtn" class="px-4 py-[10px] bg-teal-light text-teal rounded-[6px] font-bold border-0
                   cursor-pointer hover:bg-teal-lighter transition-colors duration-150" type="button">
            Isi Contoh
          </button>
          <button id="clearBtn" class="px-4 py-[10px] bg-white text-[#52606d] rounded-[6px] font-bold
                   border border-line cursor-pointer hover:bg-[#f6f8fb] transition-colors duration-150" type="button">
            Hapus
          </button>
        </div>

        <!-- Legend warna -->
        <div>
          <p class="text-xs font-bold text-[#52606d] uppercase tracking-[0.5px] mb-2">Legend</p>
          <div id="legend" class="flex flex-wrap gap-[6px] text-xs font-semibold"></div>
        </div>
      </section>

      <!-- Panel Kanan: Output -->
      <section class="bg-white border border-line rounded-lg p-5 flex flex-col gap-4">

        <!-- Teks dengan highlight -->
        <div>
          <p class="text-sm font-bold mb-2 m-0">Teks Ternotasi</p>
          <div id="highlightBox" class="min-h-[120px] p-4 bg-[#fbfdff] border border-line rounded-[6px]
                   leading-[2] text-base [overflow-wrap:anywhere] text-[#1f2933]">
            <span class="text-[#52606d] text-sm italic">Hasil highlight akan muncul di sini.</span>
          </div>
        </div>

        <!-- Daftar entitas -->
        <div id="entityListSection" hidden>
          <p class="text-sm font-bold mb-2 m-0">
            Entitas Terdeteksi
            <span id="entityCount" class="ml-2 text-xs font-semibold text-[#52606d]"></span>
          </p>
          <div id="entityList" class="flex flex-col gap-3"></div>
        </div>

        <!-- Pesan error -->
        <div id="errorBox" hidden
          class="p-4 bg-red-50 border border-red-200 rounded-[6px] text-sm text-red-700 font-medium">
        </div>

      </section>
    </div>
  </main>

  <script>
    "use strict";

    // Konfigurasi 
    const API_BASE = "http://127.0.0.1:8001";

    const SAMPLE_TEXTS = [
      "Ahmad Santoso bekerja di PT Teknologi Maju Indonesia di Jakarta sejak 15 Maret 2020.",
      "Kementerian Keuangan menerbitkan Peraturan Pemerintah No. 23/2024 tentang pajak UMKM.",
      "Prof. Budi Raharjo dari Universitas Gadjah Mada memenangkan penghargaan Rp 500 juta.",
      "KPK memeriksa pejabat OJK terkait kasus di Provinsi Jawa Barat pada bulan Oktober.",
      "Partai Golkar dan PDIP bersepakat dalam sidang MPR membahas Undang-Undang No. 12/2023.",
    ];

    // Warna per tipe entitas (bg, text, border)
    const ENTITY_COLORS = {
      ORANG: { bg: "#dcfce7", color: "#14532d", border: "#86efac", label: "Orang" },
      ORGANISASI: { bg: "#dbeafe", color: "#1e3a8a", border: "#93c5fd", label: "Organisasi" },
      LOKASI: { bg: "#ede9fe", color: "#4c1d95", border: "#c4b5fd", label: "Lokasi" },
      FASILITAS: { bg: "#ffedd5", color: "#7c2d12", border: "#fdba74", label: "Fasilitas" },
      KEJADIAN: { bg: "#fef9c3", color: "#713f12", border: "#fde047", label: "Kejadian" },
      PRODUK: { bg: "#ccfbf1", color: "#134e4a", border: "#5eead4", label: "Produk" },
      KARYA: { bg: "#fce7f3", color: "#831843", border: "#f9a8d4", label: "Karya" },
      PERATURAN: { bg: "#fee2e2", color: "#7f1d1d", border: "#fca5a5", label: "Peraturan" },
      TANGGAL: { bg: "#f1f5f9", color: "#1e293b", border: "#cbd5e1", label: "Tanggal" },
      WAKTU: { bg: "#e0e7ff", color: "#1e1b4b", border: "#a5b4fc", label: "Waktu" },
      UANG: { bg: "#fef3c7", color: "#78350f", border: "#fcd34d", label: "Uang" },
      AGAMA: { bg: "#d1fae5", color: "#064e3b", border: "#6ee7b7", label: "Agama" },
      BAHASA: { bg: "#cffafe", color: "#164e63", border: "#67e8f9", label: "Bahasa" },
    };

    // Elemen DOM
    const $ = (id) => document.getElementById(id);
    const nerInput = $("nerInput");
    const detectBtn = $("detectBtn");
    const sampleBtn = $("sampleBtn");
    const clearBtn = $("clearBtn");
    const highlightBox = $("highlightBox");
    const entityList = $("entityList");
    const entityListSec = $("entityListSection");
    const entityCount = $("entityCount");
    const errorBox = $("errorBox");
    const statusBadge = $("statusBadge");
    const statusDot = $("statusDot");
    const statusText = $("statusText");
    const modelName = $("modelName");
    const legend = $("legend");

    // Legend
    function buildLegend() {
      for (const [key, c] of Object.entries(ENTITY_COLORS)) {
        const el = document.createElement("span");
        el.style.cssText = `background:${c.bg};color:${c.color};border:1px solid ${c.border};
                            border-radius:4px;padding:2px 8px;`;
        el.textContent = c.label;
        legend.appendChild(el);
      }
    }

    // Status server
    async function checkStatus() {
      try {
        const res = await fetch(API_BASE + "/api/status", { signal: AbortSignal.timeout(4000) });
        const data = await res.json();
        const ready = data.model_loaded;

        statusDot.className = `w-2 h-2 rounded-full ${ready ? "bg-emerald-500" : "bg-amber-400"}`;
        statusText.textContent = ready ? "Server siap" : "Model sedang dimuat…";
        statusBadge.className = statusBadge.className
          .replace(/bg-\S+ text-\S+ border-\S+/g, "")
          .trim() + (ready
            ? " bg-emerald-50 text-emerald-700 border-emerald-200"
            : " bg-amber-50 text-amber-700 border-amber-200");
        modelName.textContent = data.model || "—";
        detectBtn.disabled = !ready;

        if (!ready) setTimeout(checkStatus, 3000);
      } catch {
        statusDot.className = "w-2 h-2 rounded-full bg-red-500";
        statusText.textContent = "Server tidak terjangkau";
        statusBadge.className = statusBadge.className
          .replace(/bg-\S+ text-\S+ border-\S+/g, "")
          .trim() + " bg-red-50 text-red-700 border-red-200";
        modelName.textContent = "—";
        detectBtn.disabled = true;
        setTimeout(checkStatus, 5000);
      }
    }

    // Teks helper
    function escapeHtml(str) {
      return str
        .replace(/&/g, "&amp;")
        .replace(/</g, "&lt;")
        .replace(/>/g, "&gt;");
    }

    // Highlight teks
    function buildHighlight(text, entities) {
      const sorted = [...entities].sort((a, b) => a.start - b.start);
      let html = "";
      let cursor = 0;

      for (const e of sorted) {
        if (e.start < cursor) continue; // entitas tumpang-tindih, skip
        html += escapeHtml(text.slice(cursor, e.start));

        const c = ENTITY_COLORS[e.label] || { bg: "#f3f4f6", color: "#374151", border: "#d1d5db" };
        const pct = Math.round(e.score * 100);
        const srcIcon = e.source === "rule" ? " &#9670;" : "";
        html += `<mark
          style="background:${c.bg};color:${c.color};border:1px solid ${c.border};
                 border-radius:4px;padding:1px 6px;cursor:default;white-space:nowrap;"
          title="${e.label} — ${pct}% kepercayaan (${e.source})">` +
          escapeHtml(e.word) +
          `<sup style="font-size:9px;margin-left:3px;opacity:0.75;font-weight:700;">${e.label}${srcIcon}</sup></mark>`;

        cursor = e.end;
      }

      html += escapeHtml(text.slice(cursor));
      return html || '<span class="text-[#52606d] text-sm italic">Tidak ada entitas terdeteksi.</span>';
    }

    // Daftar entitas per grup
    function buildEntityList(entities) {
      const grouped = {};
      for (const e of entities) {
        (grouped[e.label] ??= []).push(e);
      }

      entityList.innerHTML = "";
      for (const [label, items] of Object.entries(grouped).sort()) {
        const c = ENTITY_COLORS[label] || { bg: "#f3f4f6", color: "#374151", border: "#d1d5db", label };

        const section = document.createElement("div");

        // Header grup
        const header = document.createElement("div");
        header.className = "flex items-center justify-between mb-1";
        header.innerHTML = `
          <span class="text-xs font-bold text-[#1f2933]">${c.label || label}</span>
          <span class="text-xs text-[#52606d] font-semibold">${items.length} entitas</span>`;
        section.appendChild(header);

        // Badge entitas
        const badges = document.createElement("div");
        badges.style.lineHeight = "2.4";
        for (const e of items) {
          const badge = document.createElement("span");
          badge.style.cssText = `display:inline-block;margin:2px 4px;padding:3px 9px;
            background:${c.bg};color:${c.color};border:1px solid ${c.border};
            border-radius:5px;font-size:13px;font-weight:600;cursor:default;`;
          const srcIcon = e.source === "rule" ? " &#9670;" : "";
          badge.title = `Skor: ${Math.round(e.score * 100)}% | Sumber: ${e.source}`;
          badge.innerHTML = escapeHtml(e.word) + srcIcon;
          badges.appendChild(badge);
        }
        section.appendChild(badges);

        // Pemisah antar grup (kecuali yang terakhir)
        entityList.appendChild(section);
        const divider = document.createElement("hr");
        divider.className = "border-line";
        entityList.appendChild(divider);
      }
      // Hapus divider terakhir
      entityList.lastElementChild?.remove();
    }

    // Deteksi entitas
    async function detect() {
      const text = nerInput.value.trim();
      if (!text) return;

      detectBtn.disabled = true;
      detectBtn.textContent = "Mendeteksi…";
      errorBox.hidden = true;
      highlightBox.innerHTML = '<span class="text-[#52606d] text-sm">Memproses…</span>';
      entityListSec.hidden = true;

      try {
        const res = await fetch(API_BASE + "/api/ner", {
          method: "POST",
          headers: { "Content-Type": "application/json" },
          body: JSON.stringify({ text }),
          signal: AbortSignal.timeout(30000),
        });

        if (!res.ok) throw new Error(`HTTP ${res.status}`);
        const data = await res.json();
        const entities = data.entities || [];

        highlightBox.innerHTML = buildHighlight(text, entities);

        if (entities.length > 0) {
          buildEntityList(entities);
          entityCount.textContent = `(${entities.length} total)`;
          entityListSec.hidden = false;
        } else {
          entityListSec.hidden = true;
        }
      } catch (err) {
        errorBox.textContent = `Gagal menghubungi server NER: ${err.message}`;
        errorBox.hidden = false;
        highlightBox.innerHTML = '<span class="text-[#52606d] text-sm italic">Hasil highlight akan muncul di sini.</span>';
      } finally {
        detectBtn.disabled = false;
        detectBtn.textContent = "Deteksi Entitas";
        checkStatus(); // perbarui status setelah request
      }
    }

    // Event binding
    detectBtn.addEventListener("click", detect);

    nerInput.addEventListener("keydown", (e) => {
      if (e.key === "Enter" && (e.ctrlKey || e.metaKey)) detect();
    });

    sampleBtn.addEventListener("click", () => {
      nerInput.value = SAMPLE_TEXTS.join("\n");
    });

    clearBtn.addEventListener("click", () => {
      nerInput.value = "";
      highlightBox.innerHTML = '<span class="text-[#52606d] text-sm italic">Hasil highlight akan muncul di sini.</span>';
      entityListSec.hidden = true;
      errorBox.hidden = true;
    });

    // Init
    buildLegend();
    checkStatus();
  </script>
</body>

</html>