aaljabari commited on
Commit
7e01f61
·
verified ·
1 Parent(s): 8292d89

Create script.js

Browse files
Files changed (1) hide show
  1. static/script.js +111 -0
static/script.js ADDED
@@ -0,0 +1,111 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 =
51
+ "NER Error: " + err.message;
52
+ }
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
+ // =========================
89
+ // CLEAN JSON OUTPUT FORMAT
90
+ // =========================
91
+ const formattedList = data.resp.map(r => ({
92
+ Subject: {
93
+ Type: r.Subject.Type,
94
+ Label: r.Subject.Label
95
+ },
96
+ Relation: r.Relation,
97
+ Object: {
98
+ Type: r.Object.Type,
99
+ Label: r.Object.Label
100
+ },
101
+ Confidence: r.Confidence
102
+ }));
103
+
104
+ document.getElementById("output").textContent =
105
+ JSON.stringify(formattedList, null, 2);
106
+
107
+ } catch (err) {
108
+ document.getElementById("output").textContent =
109
+ "RE Error: " + err.message;
110
+ }
111
+ }