Spaces:
Sleeping
Sleeping
File size: 5,490 Bytes
9728e7c 1d9f73b c4da4c0 9728e7c 1d9f73b c4da4c0 9728e7c c4da4c0 1d9f73b 9728e7c c4da4c0 9728e7c 1d9f73b 9728e7c c4da4c0 9728e7c c4da4c0 9858485 c4da4c0 2bd700a e891925 2bd700a f1fc708 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 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 | 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;
}
}
async function runEAE() {
const text = document.getElementById("text").value;
const output = document.getElementById("output");
if (!text.trim()) {
output.textContent = "Please enter Arabic text.";
return;
}
output.textContent = "Processing event arguments...";
try {
const response = await fetch("/predict_eae", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({ text: text })
});
const data = await response.json();
output.textContent = JSON.stringify(data, null, 2);
} catch (error) {
output.textContent = "Error extracting event arguments:\n" + error;
}
}
async function runAllRelations() {
const text = document.getElementById("text").value;
const output = document.getElementById("output");
if (!text.trim()) {
output.textContent = "Please enter Arabic text.";
return;
}
output.textContent = "Extracting relations and event arguments...";
try {
const [reResponse, eaeResponse] = await Promise.all([
fetch("/predict_re", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({ text: text })
}),
fetch("/predict_eae", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({ text: text })
})
]);
const reData = await reResponse.json();
const eaeData = await eaeResponse.json();
const relationResults = reData.resp || [];
const eventResults = eaeData.resp || [];
const combinedResults = {
resp: [
...relationResults.map(item => ({
...item,
Task: "Relation Extraction"
})),
...eventResults.map(item => ({
...item,
Task: "Event Argument Extraction"
}))
],
statusText: "OK",
statusCode: 0
};
output.textContent = JSON.stringify(combinedResults, null, 2);
} catch (error) {
output.textContent = "Error extracting all relations:\n" + error;
}
}
// ======================
// 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;
}
} |