Spaces:
Running
Running
File size: 3,039 Bytes
9728e7c 1d9f73b c4da4c0 9728e7c 1d9f73b c4da4c0 9728e7c c4da4c0 1d9f73b 9728e7c c4da4c0 9728e7c 1d9f73b 9728e7c c4da4c0 9728e7c c4da4c0 9858485 c4da4c0 2bd700a 9728e7c 2bd700a 9728e7c 2bd700a 9728e7c 2bd700a 9728e7c 2bd700a 9728e7c 4585cd9 2bd700a 4585cd9 9728e7c 1d9f73b | 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 | async function handleResponse(response) {
const raw = await response.text();
// Try to parse JSON safely
try {
return JSON.parse(raw);
} catch (e) {
throw new Error("Invalid JSON response:\n" + raw);
}
}
// ======================
// NER
// ======================
async function runNER() {
try {
const text = document.getElementById("text").value.trim();
if (!text) {
document.getElementById("output").textContent = "Please enter text.";
return;
}
const response = await fetch("/predict", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ text: text, mode: "1" })
});
const data = await handleResponse(response);
if (data.error) {
document.getElementById("output").textContent =
"NER Backend Error:\n" + data.error;
return;
}
if (!data.resp || data.resp.length === 0) {
document.getElementById("output").textContent =
"No NER results.";
return;
}
// pretty print
document.getElementById("output").textContent =
JSON.stringify(data.resp, null, 2);
} catch (err) {
document.getElementById("output").textContent =
"NER Error: " + err.message;
}
}
// ======================
// Relation Extraction
// ======================
async function runRE() {
try {
const text = document.getElementById("text").value.trim();
if (!text) {
document.getElementById("output").textContent = "Please enter text.";
return;
}
const response = await fetch("/predict_re", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ text: text })
});
const data = await handleResponse(response);
if (data.error) {
document.getElementById("output").textContent =
"RE Backend Error:\n" + data.error;
return;
}
if (!data.resp || data.resp.length === 0) {
document.getElementById("output").textContent =
"No relations found.";
return;
}
// =========================
// CLEAN JSON OUTPUT FORMAT
// =========================
const formattedList = data.resp.map(r => ({
Subject: {
Type: r.Subject.Type,
Label: r.Subject.Label
},
Relation: r.Relation,
Object: {
Type: r.Object.Type,
Label: r.Object.Label
},
Confidence: r.Confidence
}));
document.getElementById("output").textContent =
JSON.stringify(formattedList, null, 2);
} catch (err) {
document.getElementById("output").textContent =
"RE Error: " + err.message;
}
} |