Spaces:
Running
Running
Update static/script.js
Browse files- static/script.js +78 -21
static/script.js
CHANGED
|
@@ -1,20 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
async function runNER() {
|
| 2 |
try {
|
| 3 |
-
const text = document.getElementById("text").value;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
const response = await fetch("/predict", {
|
| 6 |
method: "POST",
|
| 7 |
headers: { "Content-Type": "application/json" },
|
| 8 |
-
body: JSON.stringify({ text, mode: "1" })
|
| 9 |
});
|
| 10 |
|
| 11 |
-
const
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
|
|
|
| 16 |
document.getElementById("output").textContent =
|
| 17 |
-
JSON.stringify(data, null, 2);
|
| 18 |
|
| 19 |
} catch (err) {
|
| 20 |
document.getElementById("output").textContent =
|
|
@@ -23,27 +53,54 @@ async function runNER() {
|
|
| 23 |
}
|
| 24 |
|
| 25 |
|
|
|
|
|
|
|
|
|
|
| 26 |
async function runRE() {
|
| 27 |
-
|
|
|
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
});
|
| 34 |
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
-
if (data.resp.length === 0) {
|
| 41 |
-
formatted = "No relations found.";
|
| 42 |
-
} else {
|
| 43 |
data.resp.forEach(r => {
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
});
|
| 46 |
-
}
|
| 47 |
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
}
|
|
|
|
| 1 |
+
async function handleResponse(response) {
|
| 2 |
+
const raw = await response.text();
|
| 3 |
+
|
| 4 |
+
// Try to parse JSON safely
|
| 5 |
+
try {
|
| 6 |
+
return JSON.parse(raw);
|
| 7 |
+
} catch (e) {
|
| 8 |
+
throw new Error("Invalid JSON response:\n" + raw);
|
| 9 |
+
}
|
| 10 |
+
}
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
// ======================
|
| 14 |
+
// NER
|
| 15 |
+
// ======================
|
| 16 |
async function runNER() {
|
| 17 |
try {
|
| 18 |
+
const text = document.getElementById("text").value.trim();
|
| 19 |
+
|
| 20 |
+
if (!text) {
|
| 21 |
+
document.getElementById("output").textContent = "Please enter text.";
|
| 22 |
+
return;
|
| 23 |
+
}
|
| 24 |
|
| 25 |
const response = await fetch("/predict", {
|
| 26 |
method: "POST",
|
| 27 |
headers: { "Content-Type": "application/json" },
|
| 28 |
+
body: JSON.stringify({ text: text, mode: "1" })
|
| 29 |
});
|
| 30 |
|
| 31 |
+
const data = await handleResponse(response);
|
| 32 |
+
|
| 33 |
+
if (data.error) {
|
| 34 |
+
document.getElementById("output").textContent =
|
| 35 |
+
"NER Backend Error:\n" + data.error;
|
| 36 |
+
return;
|
| 37 |
+
}
|
| 38 |
|
| 39 |
+
if (!data.resp || data.resp.length === 0) {
|
| 40 |
+
document.getElementById("output").textContent =
|
| 41 |
+
"No NER results.";
|
| 42 |
+
return;
|
| 43 |
+
}
|
| 44 |
|
| 45 |
+
// pretty print
|
| 46 |
document.getElementById("output").textContent =
|
| 47 |
+
JSON.stringify(data.resp, null, 2);
|
| 48 |
|
| 49 |
} catch (err) {
|
| 50 |
document.getElementById("output").textContent =
|
|
|
|
| 53 |
}
|
| 54 |
|
| 55 |
|
| 56 |
+
// ======================
|
| 57 |
+
// Relation Extraction
|
| 58 |
+
// ======================
|
| 59 |
async function runRE() {
|
| 60 |
+
try {
|
| 61 |
+
const text = document.getElementById("text").value.trim();
|
| 62 |
|
| 63 |
+
if (!text) {
|
| 64 |
+
document.getElementById("output").textContent = "Please enter text.";
|
| 65 |
+
return;
|
| 66 |
+
}
|
|
|
|
| 67 |
|
| 68 |
+
const response = await fetch("/predict_re", {
|
| 69 |
+
method: "POST",
|
| 70 |
+
headers: { "Content-Type": "application/json" },
|
| 71 |
+
body: JSON.stringify({ text: text })
|
| 72 |
+
});
|
| 73 |
+
|
| 74 |
+
const data = await handleResponse(response);
|
| 75 |
+
|
| 76 |
+
if (data.error) {
|
| 77 |
+
document.getElementById("output").textContent =
|
| 78 |
+
"RE Backend Error:\n" + data.error;
|
| 79 |
+
return;
|
| 80 |
+
}
|
| 81 |
|
| 82 |
+
if (!data.resp || data.resp.length === 0) {
|
| 83 |
+
document.getElementById("output").textContent =
|
| 84 |
+
"No relations found.";
|
| 85 |
+
return;
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
// formatted output
|
| 89 |
+
let formatted = "";
|
| 90 |
|
|
|
|
|
|
|
|
|
|
| 91 |
data.resp.forEach(r => {
|
| 92 |
+
const subj = r[0];
|
| 93 |
+
const rel = r[1];
|
| 94 |
+
const obj = r[2];
|
| 95 |
+
const conf = r[3];
|
| 96 |
+
|
| 97 |
+
formatted += `${subj} → ${rel} → ${obj} (conf: ${conf.toFixed(2)})\n`;
|
| 98 |
});
|
|
|
|
| 99 |
|
| 100 |
+
document.getElementById("output").textContent = formatted;
|
| 101 |
+
|
| 102 |
+
} catch (err) {
|
| 103 |
+
document.getElementById("output").textContent =
|
| 104 |
+
"RE Error: " + err.message;
|
| 105 |
+
}
|
| 106 |
}
|