Spaces:
Sleeping

File size: 4,172 Bytes
6444a02
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const $ = (id) => document.getElementById(id);
const form = $("search-form");
const query = $("query");

function showError(message) {
  $("error").textContent = message;
  $("error").hidden = !message;
}

async function post(path, body) {
  const res = await fetch(path, {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify(body),
  });
  const data = await res.json().catch(() => ({}));
  if (!res.ok) {
    const detail = Array.isArray(data.detail)
      ? data.detail.map((d) => `${d.loc.at(-1)}: ${d.msg}`).join("; ")
      : data.detail || res.statusText;
    throw new Error(detail);
  }
  return data;
}

function sources(occurrences) {
  const shown = occurrences.slice(0, 3).map((o) => `${o.contract} #${o.position + 1}`);
  if (occurrences.length > 3) shown.push(`+${occurrences.length - 3} more`);
  return shown.join(", ");
}

function renderResults(data) {
  const box = $("results");
  box.replaceChildren();
  const summary = document.createElement("p");
  summary.className = "summary";
  summary.textContent = `Top ${data.results.length} by ${data.method}`;
  box.append(summary);
  if (!data.results.length) {
    const p = document.createElement("p");
    p.className = "empty";
    p.textContent = "No matching sentences.";
    box.append(p);
    return;
  }
  for (const hit of data.results) {
    const card = document.createElement("article");
    card.className = "card";
    card.innerHTML = `
      <span class="rank">${hit.rank}</span>
      <p class="text"></p>
      <div class="meta">
        <span class="source"></span>
        <span class="score">score ${hit.score.toFixed(3)}</span>
      </div>`;
    card.querySelector(".text").textContent = hit.text;
    card.querySelector(".source").textContent = `Source: ${sources(hit.occurrences)}`;
    card.title = hit.occurrences.map((o) => `${o.contract} #${o.position + 1}: ${o.original}`).join("\n");
    box.append(card);
  }
}

async function runSearch() {
  const text = query.value.trim();
  if (!text) { showError("Enter a sentence to search."); return; }
  showError("");
  $("search-btn").disabled = true;
  try {
    renderResults(await post("/search", { query: text, k: Number($("k").value) }));
  } catch (err) {
    showError(`Search failed: ${err.message}`);
  } finally {
    $("search-btn").disabled = false;
  }
}

async function runClassify() {
  const text = query.value.trim();
  if (!text) { showError("Enter a sentence to classify."); return; }
  showError("");
  $("classify-btn").disabled = true;
  try {
    const r = await post("/classify", { text });
    const box = $("classification");
    box.className = `classification ${r.potentially_unfair ? "unfair" : "fair"}`;
    box.replaceChildren();
    const strong = document.createElement("strong");
    strong.textContent = r.potentially_unfair ? "Potentially unfair clause" : "Not flagged as potentially unfair";
    const detail = document.createElement("span");
    detail.textContent = ` (model score ${r.probability.toFixed(2)}, threshold ${r.threshold.toFixed(2)})`;
    const note = document.createElement("small");
    note.textContent = r.note;
    box.append(strong, detail, note);
    box.hidden = false;
  } catch (err) {
    showError(`Classification failed: ${err.message}`);
  } finally {
    $("classify-btn").disabled = false;
  }
}

async function init() {
  try {
    const res = await fetch("/ready");
    const info = await res.json();
    if (!info.ready) throw new Error("not ready");
    $("method").textContent = info.method;
    $("status").textContent = `${info.corpus_groups.toLocaleString()} unique sentences indexed`;
    $("classify-btn").hidden = !info.classifier;
  } catch {
    $("status").textContent = "Service is starting or unavailable — try again shortly.";
  }
}

form.addEventListener("submit", (e) => { e.preventDefault(); runSearch(); });
$("classify-btn").addEventListener("click", runClassify);
query.addEventListener("keydown", (e) => {
  if (e.key === "Enter" && (e.ctrlKey || e.metaKey)) { e.preventDefault(); runSearch(); }
});
query.addEventListener("input", () => { $("classification").hidden = true; });
init();